rails g scaffold Forum

This commit is contained in:
gnattery
2013-02-13 12:55:17 +11:00
parent 3052e41278
commit e10ea7f180
21 changed files with 479 additions and 1 deletions

View File

@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

View File

@@ -0,0 +1,83 @@
class ForumsController < ApplicationController
# GET /forums
# GET /forums.json
def index
@forums = Forum.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @forums }
end
end
# GET /forums/1
# GET /forums/1.json
def show
@forum = Forum.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @forum }
end
end
# GET /forums/new
# GET /forums/new.json
def new
@forum = Forum.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @forum }
end
end
# GET /forums/1/edit
def edit
@forum = Forum.find(params[:id])
end
# POST /forums
# POST /forums.json
def create
@forum = Forum.new(params[:forum])
respond_to do |format|
if @forum.save
format.html { redirect_to @forum, notice: 'Forum was successfully created.' }
format.json { render json: @forum, status: :created, location: @forum }
else
format.html { render action: "new" }
format.json { render json: @forum.errors, status: :unprocessable_entity }
end
end
end
# PUT /forums/1
# PUT /forums/1.json
def update
@forum = Forum.find(params[:id])
respond_to do |format|
if @forum.update_attributes(params[:forum])
format.html { redirect_to @forum, notice: 'Forum was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @forum.errors, status: :unprocessable_entity }
end
end
end
# DELETE /forums/1
# DELETE /forums/1.json
def destroy
@forum = Forum.find(params[:id])
@forum.destroy
respond_to do |format|
format.html { redirect_to forums_url }
format.json { head :no_content }
end
end
end

View File

@@ -0,0 +1,2 @@
module ForumsHelper
end

3
app/models/forum.rb Normal file
View File

@@ -0,0 +1,3 @@
class Forum < ActiveRecord::Base
attr_accessible :description, :name, :owner_id
end

View File

@@ -0,0 +1,19 @@
= form_for @forum do |f|
- if @forum.errors.any?
#error_explanation
%h2= "#{pluralize(@forum.errors.count, "error")} prohibited this forum from being saved:"
%ul
- @forum.errors.full_messages.each do |msg|
%li= msg
.field
= f.label :name
= f.text_field :name
.field
= f.label :description
= f.text_area :description
.field
= f.label :owner_id
= f.number_field :owner_id
.actions
= f.submit 'Save'

View File

@@ -0,0 +1,7 @@
%h1 Editing forum
= render 'form'
= link_to 'Show', @forum
\|
= link_to 'Back', forums_path

View File

@@ -0,0 +1,23 @@
%h1 Listing forums
%table
%tr
%th Name
%th Description
%th Owner
%th
%th
%th
- @forums.each do |forum|
%tr
%td= forum.name
%td= forum.description
%td= forum.owner_id
%td= link_to 'Show', forum
%td= link_to 'Edit', edit_forum_path(forum)
%td= link_to 'Destroy', forum, method: :delete, data: { confirm: 'Are you sure?' }
%br
= link_to 'New Forum', new_forum_path

View File

@@ -0,0 +1,5 @@
%h1 New forum
= render 'form'
= link_to 'Back', forums_path

View File

@@ -0,0 +1,15 @@
%p#notice= notice
%p
%b Name:
= @forum.name
%p
%b Description:
= @forum.description
%p
%b Owner:
= @forum.owner_id
= link_to 'Edit', edit_forum_path(@forum)
\|
= link_to 'Back', forums_path

View File

@@ -1,4 +1,7 @@
Growstuff::Application.routes.draw do
resources :forums
devise_for :members, :controllers => { :registrations => "registrations" }
resources :plantings

View File

@@ -0,0 +1,11 @@
class CreateForums < ActiveRecord::Migration
def change
create_table :forums do |t|
t.string :name, :null => false
t.text :description, :null => false
t.integer :owner_id, :null => false
t.timestamps
end
end
end

View File

@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20130212001748) do
ActiveRecord::Schema.define(:version => 20130213014511) do
create_table "comments", :force => true do |t|
t.integer "post_id", :limit => 255, :null => false
@@ -32,6 +32,14 @@ ActiveRecord::Schema.define(:version => 20130212001748) do
add_index "crops", ["slug"], :name => "index_crops_on_slug", :unique => true
add_index "crops", ["system_name"], :name => "index_crops_on_system_name"
create_table "forums", :force => true do |t|
t.string "name", :null => false
t.text "description", :null => false
t.integer "owner_id", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "gardens", :force => true do |t|
t.string "name", :null => false
t.integer "owner_id"

View File

@@ -0,0 +1,147 @@
require 'spec_helper'
describe ForumsController do
def valid_attributes
{
"name" => "MyString",
"description" => "Something",
"owner_id" => 1
}
end
# 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
# ForumsController. Be sure to keep this updated too.
def valid_session
{}
end
describe "GET index" do
it "assigns all forums as @forums" do
forum = Forum.create! valid_attributes
get :index, {}, valid_session
assigns(:forums).should eq([forum])
end
end
describe "GET show" do
it "assigns the requested forum as @forum" do
forum = Forum.create! valid_attributes
get :show, {:id => forum.to_param}, valid_session
assigns(:forum).should eq(forum)
end
end
describe "GET new" do
it "assigns a new forum as @forum" do
get :new, {}, valid_session
assigns(:forum).should be_a_new(Forum)
end
end
describe "GET edit" do
it "assigns the requested forum as @forum" do
forum = Forum.create! valid_attributes
get :edit, {:id => forum.to_param}, valid_session
assigns(:forum).should eq(forum)
end
end
describe "POST create" do
describe "with valid params" do
it "creates a new Forum" do
expect {
post :create, {:forum => valid_attributes}, valid_session
}.to change(Forum, :count).by(1)
end
it "assigns a newly created forum as @forum" do
post :create, {:forum => valid_attributes}, valid_session
assigns(:forum).should be_a(Forum)
assigns(:forum).should be_persisted
end
it "redirects to the created forum" do
post :create, {:forum => valid_attributes}, valid_session
response.should redirect_to(Forum.last)
end
end
describe "with invalid params" do
it "assigns a newly created but unsaved forum as @forum" do
# Trigger the behavior that occurs when invalid params are submitted
Forum.any_instance.stub(:save).and_return(false)
post :create, {:forum => { "name" => "invalid value" }}, valid_session
assigns(:forum).should be_a_new(Forum)
end
it "re-renders the 'new' template" do
# Trigger the behavior that occurs when invalid params are submitted
Forum.any_instance.stub(:save).and_return(false)
post :create, {:forum => { "name" => "invalid value" }}, valid_session
response.should render_template("new")
end
end
end
describe "PUT update" do
describe "with valid params" do
it "updates the requested forum" do
forum = Forum.create! valid_attributes
# Assuming there are no other forums in the database, this
# specifies that the Forum created on the previous line
# receives the :update_attributes message with whatever params are
# submitted in the request.
Forum.any_instance.should_receive(:update_attributes).with({ "name" => "MyString" })
put :update, {:id => forum.to_param, :forum => { "name" => "MyString" }}, valid_session
end
it "assigns the requested forum as @forum" do
forum = Forum.create! valid_attributes
put :update, {:id => forum.to_param, :forum => valid_attributes}, valid_session
assigns(:forum).should eq(forum)
end
it "redirects to the forum" do
forum = Forum.create! valid_attributes
put :update, {:id => forum.to_param, :forum => valid_attributes}, valid_session
response.should redirect_to(forum)
end
end
describe "with invalid params" do
it "assigns the forum as @forum" do
forum = Forum.create! valid_attributes
# Trigger the behavior that occurs when invalid params are submitted
Forum.any_instance.stub(:save).and_return(false)
put :update, {:id => forum.to_param, :forum => { "name" => "invalid value" }}, valid_session
assigns(:forum).should eq(forum)
end
it "re-renders the 'edit' template" do
forum = Forum.create! valid_attributes
# Trigger the behavior that occurs when invalid params are submitted
Forum.any_instance.stub(:save).and_return(false)
put :update, {:id => forum.to_param, :forum => { "name" => "invalid value" }}, valid_session
response.should render_template("edit")
end
end
end
describe "DELETE destroy" do
it "destroys the requested forum" do
forum = Forum.create! valid_attributes
expect {
delete :destroy, {:id => forum.to_param}, valid_session
}.to change(Forum, :count).by(-1)
end
it "redirects to the forums list" do
forum = Forum.create! valid_attributes
delete :destroy, {:id => forum.to_param}, valid_session
response.should redirect_to(forums_url)
end
end
end

9
spec/factories/forums.rb Normal file
View File

@@ -0,0 +1,9 @@
# Read about factories at https://github.com/thoughtbot/factory_girl
FactoryGirl.define do
factory :forum do
name "MyString"
description "MyText"
owner_id 1
end
end

View File

@@ -0,0 +1,5 @@
require 'spec_helper'
describe Forum do
pending "add some examples to (or delete) #{__FILE__}"
end

View File

@@ -0,0 +1,11 @@
require 'spec_helper'
describe "Forums" do
describe "GET /forums" do
it "works! (now write some real specs)" do
# Run the generator again with the --webrat flag if you want to use webrat methods/matchers
get forums_path
response.status.should be(200)
end
end
end

View File

@@ -0,0 +1,35 @@
require "spec_helper"
describe ForumsController do
describe "routing" do
it "routes to #index" do
get("/forums").should route_to("forums#index")
end
it "routes to #new" do
get("/forums/new").should route_to("forums#new")
end
it "routes to #show" do
get("/forums/1").should route_to("forums#show", :id => "1")
end
it "routes to #edit" do
get("/forums/1/edit").should route_to("forums#edit", :id => "1")
end
it "routes to #create" do
post("/forums").should route_to("forums#create")
end
it "routes to #update" do
put("/forums/1").should route_to("forums#update", :id => "1")
end
it "routes to #destroy" do
delete("/forums/1").should route_to("forums#destroy", :id => "1")
end
end
end

View File

@@ -0,0 +1,22 @@
require 'spec_helper'
describe "forums/edit" do
before(:each) do
@forum = assign(:forum, stub_model(Forum,
:name => "MyString",
:description => "MyText",
:owner_id => 1
))
end
it "renders the edit forum form" do
render
# Run the generator again with the --webrat flag if you want to use webrat matchers
assert_select "form", :action => forums_path(@forum), :method => "post" do
assert_select "input#forum_name", :name => "forum[name]"
assert_select "textarea#forum_description", :name => "forum[description]"
assert_select "input#forum_owner_id", :name => "forum[owner_id]"
end
end
end

View File

@@ -0,0 +1,26 @@
require 'spec_helper'
describe "forums/index" do
before(:each) do
assign(:forums, [
stub_model(Forum,
:name => "Name",
:description => "MyText",
:owner_id => 1
),
stub_model(Forum,
:name => "Name",
:description => "MyText",
:owner_id => 1
)
])
end
it "renders a list of forums" do
render
# Run the generator again with the --webrat flag if you want to use webrat matchers
assert_select "tr>td", :text => "Name".to_s, :count => 2
assert_select "tr>td", :text => "MyText".to_s, :count => 2
assert_select "tr>td", :text => 1.to_s, :count => 2
end
end

View File

@@ -0,0 +1,22 @@
require 'spec_helper'
describe "forums/new" do
before(:each) do
assign(:forum, stub_model(Forum,
:name => "MyString",
:description => "MyText",
:owner_id => 1
).as_new_record)
end
it "renders new forum form" do
render
# Run the generator again with the --webrat flag if you want to use webrat matchers
assert_select "form", :action => forums_path, :method => "post" do
assert_select "input#forum_name", :name => "forum[name]"
assert_select "textarea#forum_description", :name => "forum[description]"
assert_select "input#forum_owner_id", :name => "forum[owner_id]"
end
end
end

View File

@@ -0,0 +1,19 @@
require 'spec_helper'
describe "forums/show" do
before(:each) do
@forum = assign(:forum, stub_model(Forum,
:name => "Name",
:description => "MyText",
:owner_id => 1
))
end
it "renders attributes in <p>" do
render
# Run the generator again with the --webrat flag if you want to use webrat matchers
rendered.should match(/Name/)
rendered.should match(/MyText/)
rendered.should match(/1/)
end
end