Commit 52e15eb8 authored by marcheing's avatar marcheing Committed by Diego de Araújo Martinez Camarinha

Add content scaffold

Signed-off-by: Diego de Araújo Martinez Camarinha's avatarDiego Araújo <diegoamc@protonmail.ch>
parent dcfaee50
class ContentsController < ApplicationController
before_action :set_content, only: [:show, :edit, :update, :destroy]
# GET /contents
# GET /contents.json
def index
@contents = Content.all
end
# GET /contents/1
# GET /contents/1.json
def show
end
# GET /contents/new
def new
@content = Content.new
end
# GET /contents/1/edit
def edit
end
# POST /contents
# POST /contents.json
def create
@content = Content.new(content_params)
respond_to do |format|
if @content.save
format.html { redirect_to @content, notice: 'Content was successfully created.' }
format.json { render :show, status: :created, location: @content }
else
format.html { render :new }
format.json { render json: @content.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /contents/1
# PATCH/PUT /contents/1.json
def update
respond_to do |format|
if @content.update(content_params)
format.html { redirect_to @content, notice: 'Content was successfully updated.' }
format.json { render :show, status: :ok, location: @content }
else
format.html { render :edit }
format.json { render json: @content.errors, status: :unprocessable_entity }
end
end
end
# DELETE /contents/1
# DELETE /contents/1.json
def destroy
@content.destroy
respond_to do |format|
format.html { redirect_to contents_url, notice: 'Content was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_content
@content = Content.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def content_params
params.require(:content).permit(:title, :description, :user_id, :adult, :rating, :soundtrack, :view_count, :deleted, :zip_code, :director, :co_director, :team, :allow_comments)
end
end
module ContentsHelper
end
class Content < ApplicationRecord
end
<%= form_for(content) do |f| %>
<% if content.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(content.errors.count, "error") %> prohibited this content from being saved:</h2>
<ul>
<% content.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :description %>
<%= f.text_field :description %>
</div>
<div class="field">
<%= f.label :user_id %>
<%= f.number_field :user_id %>
</div>
<div class="field">
<%= f.label :adult %>
<%= f.check_box :adult %>
</div>
<div class="field">
<%= f.label :rating %>
<%= f.number_field :rating %>
</div>
<div class="field">
<%= f.label :soundtrack %>
<%= f.text_field :soundtrack %>
</div>
<div class="field">
<%= f.label :view_count %>
<%= f.number_field :view_count %>
</div>
<div class="field">
<%= f.label :deleted %>
<%= f.check_box :deleted %>
</div>
<div class="field">
<%= f.label :zip_code %>
<%= f.text_field :zip_code %>
</div>
<div class="field">
<%= f.label :director %>
<%= f.text_field :director %>
</div>
<div class="field">
<%= f.label :co_director %>
<%= f.text_field :co_director %>
</div>
<div class="field">
<%= f.label :team %>
<%= f.text_field :team %>
</div>
<div class="field">
<%= f.label :allow_comments %>
<%= f.check_box :allow_comments %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<h1>Editing Content</h1>
<%= render 'form', content: @content %>
<%= link_to 'Show', @content %> |
<%= link_to 'Back', contents_path %>
<p id="notice"><%= notice %></p>
<h1>Contents</h1>
<table>
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>User</th>
<th>Adult</th>
<th>Rating</th>
<th>Soundtrack</th>
<th>View count</th>
<th>Deleted</th>
<th>Zip code</th>
<th>Director</th>
<th>Co director</th>
<th>Team</th>
<th>Allow comments</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @contents.each do |content| %>
<tr>
<td><%= content.title %></td>
<td><%= content.description %></td>
<td><%= content.user_id %></td>
<td><%= content.adult %></td>
<td><%= content.rating %></td>
<td><%= content.soundtrack %></td>
<td><%= content.view_count %></td>
<td><%= content.deleted %></td>
<td><%= content.zip_code %></td>
<td><%= content.director %></td>
<td><%= content.co_director %></td>
<td><%= content.team %></td>
<td><%= content.allow_comments %></td>
<td><%= link_to 'Show', content %></td>
<td><%= link_to 'Edit', edit_content_path(content) %></td>
<td><%= link_to 'Destroy', content, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Content', new_content_path %>
<h1>New Content</h1>
<%= render 'form', content: @content %>
<%= link_to 'Back', contents_path %>
<p id="notice"><%= notice %></p>
<p>
<strong>Title:</strong>
<%= @content.title %>
</p>
<p>
<strong>Description:</strong>
<%= @content.description %>
</p>
<p>
<strong>User:</strong>
<%= @content.user_id %>
</p>
<p>
<strong>Adult:</strong>
<%= @content.adult %>
</p>
<p>
<strong>Rating:</strong>
<%= @content.rating %>
</p>
<p>
<strong>Soundtrack:</strong>
<%= @content.soundtrack %>
</p>
<p>
<strong>View count:</strong>
<%= @content.view_count %>
</p>
<p>
<strong>Deleted:</strong>
<%= @content.deleted %>
</p>
<p>
<strong>Zip code:</strong>
<%= @content.zip_code %>
</p>
<p>
<strong>Director:</strong>
<%= @content.director %>
</p>
<p>
<strong>Co director:</strong>
<%= @content.co_director %>
</p>
<p>
<strong>Team:</strong>
<%= @content.team %>
</p>
<p>
<strong>Allow comments:</strong>
<%= @content.allow_comments %>
</p>
<%= link_to 'Edit', edit_content_path(@content) %> |
<%= link_to 'Back', contents_path %>
Rails.application.routes.draw do
resources :contents
scope '(:locale)', locale: /en|pt-BR/ do
devise_for :users
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
......
class CreateContents < ActiveRecord::Migration[5.0]
def change
create_table :contents do |t|
t.string :title, null: false
t.string :description
t.integer :user_id, null: false
t.boolean :adult, default: false, null: false
t.integer :rating
t.string :soundtrack, null: false
t.integer :view_count, default: 0
t.boolean :deleted, default: false, null: false
t.string :zip_code
t.string :director, null: false
t.string :co_director
t.string :team
t.boolean :allow_comments, default: true
t.timestamps
end
end
end
......@@ -11,7 +11,25 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160624142640) do
ActiveRecord::Schema.define(version: 20160615165428) do
create_table "contents", force: :cascade do |t|
t.string "title", null: false
t.string "description"
t.integer "user_id", null: false
t.boolean "adult", default: false, null: false
t.integer "rating"
t.string "soundtrack", null: false
t.integer "view_count", default: 0
t.boolean "deleted", default: false, null: false
t.string "zip_code"
t.string "director", null: false
t.string "co_director"
t.string "team"
t.boolean "allow_comments", default: true
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "profiles", force: :cascade do |t|
t.string "first_name"
......
require 'rails_helper'
# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to specify the controller code that
# was generated by Rails when you ran the scaffold generator.
#
# It assumes that the implementation code is generated by the rails scaffold
# generator. If you are using any extension libraries to generate different
# controller code, this generated spec may or may not pass.
#
# It only uses APIs available in rails and/or rspec-rails. There are a number
# of tools you can use to make these specs even more expressive, but we're
# sticking to rails and rspec-rails APIs to keep things simple and stable.
#
# Compared to earlier versions of this generator, there is very limited use of
# stubs and message expectations in this spec. Stubs are only used when there
# is no simpler way to get a handle on the object needed for the example.
# Message expectations are only used when there is no simpler way to specify
# that an instance is receiving a specific message.
RSpec.describe ContentsController, type: :controller do
# This should return the minimal set of attributes required to create a valid
# Content. As you add validations to Content, be sure to
# adjust the attributes here as well.
let(:valid_attributes) {
skip("Add a hash of attributes valid for your model")
}
let(:invalid_attributes) {
skip("Add a hash of attributes invalid for your model")
}
# This should return the minimal set of values that should be in the session
# in order to pass any filters (e.g. authentication) defined in
# ContentsController. Be sure to keep this updated too.
let(:valid_session) { {} }
describe "GET #index" do
it "assigns all contents as @contents" do
content = Content.create! valid_attributes
get :index, params: {}, session: valid_session
expect(assigns(:contents)).to eq([content])
end
end
describe "GET #show" do
it "assigns the requested content as @content" do
content = Content.create! valid_attributes
get :show, params: {id: content.to_param}, session: valid_session
expect(assigns(:content)).to eq(content)
end
end
describe "GET #new" do
it "assigns a new content as @content" do
get :new, params: {}, session: valid_session
expect(assigns(:content)).to be_a_new(Content)
end
end
describe "GET #edit" do
it "assigns the requested content as @content" do
content = Content.create! valid_attributes
get :edit, params: {id: content.to_param}, session: valid_session
expect(assigns(:content)).to eq(content)
end
end
describe "POST #create" do
context "with valid params" do
it "creates a new Content" do
expect {
post :create, params: {content: valid_attributes}, session: valid_session
}.to change(Content, :count).by(1)
end
it "assigns a newly created content as @content" do
post :create, params: {content: valid_attributes}, session: valid_session
expect(assigns(:content)).to be_a(Content)
expect(assigns(:content)).to be_persisted
end
it "redirects to the created content" do
post :create, params: {content: valid_attributes}, session: valid_session
expect(response).to redirect_to(Content.last)
end
end
context "with invalid params" do
it "assigns a newly created but unsaved content as @content" do
post :create, params: {content: invalid_attributes}, session: valid_session
expect(assigns(:content)).to be_a_new(Content)
end
it "re-renders the 'new' template" do
post :create, params: {content: invalid_attributes}, session: valid_session
expect(response).to render_template("new")
end
end
end
describe "PUT #update" do
context "with valid params" do
let(:new_attributes) {
skip("Add a hash of attributes valid for your model")
}
it "updates the requested content" do
content = Content.create! valid_attributes
put :update, params: {id: content.to_param, content: new_attributes}, session: valid_session
content.reload
skip("Add assertions for updated state")
end
it "assigns the requested content as @content" do
content = Content.create! valid_attributes
put :update, params: {id: content.to_param, content: valid_attributes}, session: valid_session
expect(assigns(:content)).to eq(content)
end
it "redirects to the content" do
content = Content.create! valid_attributes
put :update, params: {id: content.to_param, content: valid_attributes}, session: valid_session
expect(response).to redirect_to(content)
end
end
context "with invalid params" do
it "assigns the content as @content" do
content = Content.create! valid_attributes
put :update, params: {id: content.to_param, content: invalid_attributes}, session: valid_session
expect(assigns(:content)).to eq(content)
end
it "re-renders the 'edit' template" do
content = Content.create! valid_attributes
put :update, params: {id: content.to_param, content: invalid_attributes}, session: valid_session
expect(response).to render_template("edit")
end
end
end
describe "DELETE #destroy" do
it "destroys the requested content" do
content = Content.create! valid_attributes
expect {
delete :destroy, params: {id: content.to_param}, session: valid_session
}.to change(Content, :count).by(-1)
end
it "redirects to the contents list" do
content = Content.create! valid_attributes
delete :destroy, params: {id: content.to_param}, session: valid_session
expect(response).to redirect_to(contents_url)
end
end
end
require 'rails_helper'
# Specs in this file have access to a helper object that includes
# the ContentsHelper. For example:
#
# describe ContentsHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe ContentsHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end
require 'rails_helper'
RSpec.describe Content, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
require "rails_helper"
RSpec.describe ContentsController, type: :routing do
describe "routing" do
it "routes to #index" do
expect(:get => "/contents").to route_to("contents#index")
end
it "routes to #new" do
expect(:get => "/contents/new").to route_to("contents#new")
end
it "routes to #show" do
expect(:get => "/contents/1").to route_to("contents#show", :id => "1")
end
it "routes to #edit" do
expect(:get => "/contents/1/edit").to route_to("contents#edit", :id => "1")
end
it "routes to #create" do
expect(:post => "/contents").to route_to("contents#create")
end
it "routes to #update via PUT" do
expect(:put => "/contents/1").to route_to("contents#update", :id => "1")
end
it "routes to #update via PATCH" do
expect(:patch => "/contents/1").to route_to("contents#update", :id => "1")
end
it "routes to #destroy" do
expect(:delete => "/contents/1").to route_to("contents#destroy", :id => "1")
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment