diff --git a/app/assets/javascripts/forums.js.coffee b/app/assets/javascripts/forums.js.coffee new file mode 100644 index 000000000..761567942 --- /dev/null +++ b/app/assets/javascripts/forums.js.coffee @@ -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/ diff --git a/app/controllers/forums_controller.rb b/app/controllers/forums_controller.rb new file mode 100644 index 000000000..806ba2574 --- /dev/null +++ b/app/controllers/forums_controller.rb @@ -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 diff --git a/app/helpers/forums_helper.rb b/app/helpers/forums_helper.rb new file mode 100644 index 000000000..2e531fd46 --- /dev/null +++ b/app/helpers/forums_helper.rb @@ -0,0 +1,2 @@ +module ForumsHelper +end diff --git a/app/models/forum.rb b/app/models/forum.rb new file mode 100644 index 000000000..822098801 --- /dev/null +++ b/app/models/forum.rb @@ -0,0 +1,3 @@ +class Forum < ActiveRecord::Base + attr_accessible :description, :name, :owner_id +end diff --git a/app/views/forums/_form.html.haml b/app/views/forums/_form.html.haml new file mode 100644 index 000000000..05c4d5538 --- /dev/null +++ b/app/views/forums/_form.html.haml @@ -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' diff --git a/app/views/forums/edit.html.haml b/app/views/forums/edit.html.haml new file mode 100644 index 000000000..88c265f45 --- /dev/null +++ b/app/views/forums/edit.html.haml @@ -0,0 +1,7 @@ +%h1 Editing forum + += render 'form' + += link_to 'Show', @forum +\| += link_to 'Back', forums_path diff --git a/app/views/forums/index.html.haml b/app/views/forums/index.html.haml new file mode 100644 index 000000000..aac289aae --- /dev/null +++ b/app/views/forums/index.html.haml @@ -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 diff --git a/app/views/forums/new.html.haml b/app/views/forums/new.html.haml new file mode 100644 index 000000000..45a9dc26a --- /dev/null +++ b/app/views/forums/new.html.haml @@ -0,0 +1,5 @@ +%h1 New forum + += render 'form' + += link_to 'Back', forums_path diff --git a/app/views/forums/show.html.haml b/app/views/forums/show.html.haml new file mode 100644 index 000000000..8f2f1eacb --- /dev/null +++ b/app/views/forums/show.html.haml @@ -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 diff --git a/config/routes.rb b/config/routes.rb index c3c818b43..4577680c5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,7 @@ Growstuff::Application.routes.draw do + resources :forums + + devise_for :members, :controllers => { :registrations => "registrations" } resources :plantings diff --git a/db/migrate/20130213014511_create_forums.rb b/db/migrate/20130213014511_create_forums.rb new file mode 100644 index 000000000..6388876e6 --- /dev/null +++ b/db/migrate/20130213014511_create_forums.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 9b65a025a..4b8ae4410 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" diff --git a/spec/controllers/forums_controller_spec.rb b/spec/controllers/forums_controller_spec.rb new file mode 100644 index 000000000..37c484523 --- /dev/null +++ b/spec/controllers/forums_controller_spec.rb @@ -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 diff --git a/spec/factories/forums.rb b/spec/factories/forums.rb new file mode 100644 index 000000000..5bc68e5bb --- /dev/null +++ b/spec/factories/forums.rb @@ -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 diff --git a/spec/models/forum_spec.rb b/spec/models/forum_spec.rb new file mode 100644 index 000000000..a20746c9b --- /dev/null +++ b/spec/models/forum_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe Forum do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/requests/forums_spec.rb b/spec/requests/forums_spec.rb new file mode 100644 index 000000000..ee85dacfd --- /dev/null +++ b/spec/requests/forums_spec.rb @@ -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 diff --git a/spec/routing/forums_routing_spec.rb b/spec/routing/forums_routing_spec.rb new file mode 100644 index 000000000..c783d5199 --- /dev/null +++ b/spec/routing/forums_routing_spec.rb @@ -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 diff --git a/spec/views/forums/edit.html.haml_spec.rb b/spec/views/forums/edit.html.haml_spec.rb new file mode 100644 index 000000000..d957b9adc --- /dev/null +++ b/spec/views/forums/edit.html.haml_spec.rb @@ -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 diff --git a/spec/views/forums/index.html.haml_spec.rb b/spec/views/forums/index.html.haml_spec.rb new file mode 100644 index 000000000..f45bdedd4 --- /dev/null +++ b/spec/views/forums/index.html.haml_spec.rb @@ -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 diff --git a/spec/views/forums/new.html.haml_spec.rb b/spec/views/forums/new.html.haml_spec.rb new file mode 100644 index 000000000..63b4bac34 --- /dev/null +++ b/spec/views/forums/new.html.haml_spec.rb @@ -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 diff --git a/spec/views/forums/show.html.haml_spec.rb b/spec/views/forums/show.html.haml_spec.rb new file mode 100644 index 000000000..96ac62921 --- /dev/null +++ b/spec/views/forums/show.html.haml_spec.rb @@ -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
" 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