diff --git a/app/assets/javascripts/comments.js.coffee b/app/assets/javascripts/comments.js.coffee new file mode 100644 index 000000000..761567942 --- /dev/null +++ b/app/assets/javascripts/comments.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/comments_controller.rb b/app/controllers/comments_controller.rb new file mode 100644 index 000000000..dfb5f63c6 --- /dev/null +++ b/app/controllers/comments_controller.rb @@ -0,0 +1,83 @@ +class CommentsController < ApplicationController + # GET /comments + # GET /comments.json + def index + @comments = Comment.all + + respond_to do |format| + format.html # index.html.erb + format.json { render json: @comments } + end + end + + # GET /comments/1 + # GET /comments/1.json + def show + @comment = Comment.find(params[:id]) + + respond_to do |format| + format.html # show.html.erb + format.json { render json: @comment } + end + end + + # GET /comments/new + # GET /comments/new.json + def new + @comment = Comment.new + + respond_to do |format| + format.html # new.html.erb + format.json { render json: @comment } + end + end + + # GET /comments/1/edit + def edit + @comment = Comment.find(params[:id]) + end + + # POST /comments + # POST /comments.json + def create + @comment = Comment.new(params[:comment]) + + respond_to do |format| + if @comment.save + format.html { redirect_to @comment, notice: 'Comment was successfully created.' } + format.json { render json: @comment, status: :created, location: @comment } + else + format.html { render action: "new" } + format.json { render json: @comment.errors, status: :unprocessable_entity } + end + end + end + + # PUT /comments/1 + # PUT /comments/1.json + def update + @comment = Comment.find(params[:id]) + + respond_to do |format| + if @comment.update_attributes(params[:comment]) + format.html { redirect_to @comment, notice: 'Comment was successfully updated.' } + format.json { head :no_content } + else + format.html { render action: "edit" } + format.json { render json: @comment.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /comments/1 + # DELETE /comments/1.json + def destroy + @comment = Comment.find(params[:id]) + @comment.destroy + + respond_to do |format| + format.html { redirect_to comments_url } + format.json { head :no_content } + end + end +end diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb new file mode 100644 index 000000000..0ec9ca5f2 --- /dev/null +++ b/app/helpers/comments_helper.rb @@ -0,0 +1,2 @@ +module CommentsHelper +end diff --git a/app/models/comment.rb b/app/models/comment.rb new file mode 100644 index 000000000..7dfdd6c41 --- /dev/null +++ b/app/models/comment.rb @@ -0,0 +1,3 @@ +class Comment < ActiveRecord::Base + attr_accessible :author_id, :body, :post_id +end diff --git a/app/views/comments/_form.html.haml b/app/views/comments/_form.html.haml new file mode 100644 index 000000000..4a7db6193 --- /dev/null +++ b/app/views/comments/_form.html.haml @@ -0,0 +1,19 @@ += form_for @comment do |f| + - if @comment.errors.any? + #error_explanation + %h2= "#{pluralize(@comment.errors.count, "error")} prohibited this comment from being saved:" + %ul + - @comment.errors.full_messages.each do |msg| + %li= msg + + .field + = f.label :post_id + = f.number_field :post_id + .field + = f.label :author_id + = f.number_field :author_id + .field + = f.label :body + = f.text_area :body + .actions + = f.submit 'Save' diff --git a/app/views/comments/edit.html.haml b/app/views/comments/edit.html.haml new file mode 100644 index 000000000..da858a956 --- /dev/null +++ b/app/views/comments/edit.html.haml @@ -0,0 +1,7 @@ +%h1 Editing comment + += render 'form' + += link_to 'Show', @comment +\| += link_to 'Back', comments_path diff --git a/app/views/comments/index.html.haml b/app/views/comments/index.html.haml new file mode 100644 index 000000000..14dffe74e --- /dev/null +++ b/app/views/comments/index.html.haml @@ -0,0 +1,23 @@ +%h1 Listing comments + +%table + %tr + %th Post + %th Author + %th Body + %th + %th + %th + + - @comments.each do |comment| + %tr + %td= comment.post_id + %td= comment.author_id + %td= comment.body + %td= link_to 'Show', comment + %td= link_to 'Edit', edit_comment_path(comment) + %td= link_to 'Destroy', comment, method: :delete, data: { confirm: 'Are you sure?' } + +%br + += link_to 'New Comment', new_comment_path diff --git a/app/views/comments/new.html.haml b/app/views/comments/new.html.haml new file mode 100644 index 000000000..874b7895b --- /dev/null +++ b/app/views/comments/new.html.haml @@ -0,0 +1,5 @@ +%h1 New comment + += render 'form' + += link_to 'Back', comments_path diff --git a/app/views/comments/show.html.haml b/app/views/comments/show.html.haml new file mode 100644 index 000000000..79b7ff31e --- /dev/null +++ b/app/views/comments/show.html.haml @@ -0,0 +1,15 @@ +%p#notice= notice + +%p + %b Post: + = @comment.post_id +%p + %b Author: + = @comment.author_id +%p + %b Body: + = @comment.body + += link_to 'Edit', edit_comment_path(@comment) +\| += link_to 'Back', comments_path diff --git a/config/routes.rb b/config/routes.rb index fe39180dd..4e74cbac4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,7 @@ Growstuff::Application.routes.draw do + resources :comments + + devise_for :members resources :plantings diff --git a/db/migrate/20130206033956_create_comments.rb b/db/migrate/20130206033956_create_comments.rb new file mode 100644 index 000000000..6fc9d9279 --- /dev/null +++ b/db/migrate/20130206033956_create_comments.rb @@ -0,0 +1,11 @@ +class CreateComments < ActiveRecord::Migration + def change + create_table :comments do |t| + t.integer :post_id + t.integer :author_id + t.text :body + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 9ee8655cc..ceb9be432 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,15 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20130118043431) do +ActiveRecord::Schema.define(:version => 20130206033956) do + + create_table "comments", :force => true do |t| + t.integer "post_id" + t.integer "author_id" + t.text "body" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end create_table "crops", :force => true do |t| t.string "system_name", :null => false diff --git a/spec/controllers/comments_controller_spec.rb b/spec/controllers/comments_controller_spec.rb new file mode 100644 index 000000000..fdde2fc7d --- /dev/null +++ b/spec/controllers/comments_controller_spec.rb @@ -0,0 +1,164 @@ +require 'spec_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. + +describe CommentsController do + + # This should return the minimal set of attributes required to create a valid + # Comment. As you add validations to Comment, be sure to + # update the return value of this method accordingly. + def valid_attributes + { "post_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 + # CommentsController. Be sure to keep this updated too. + def valid_session + {} + end + + describe "GET index" do + it "assigns all comments as @comments" do + comment = Comment.create! valid_attributes + get :index, {}, valid_session + assigns(:comments).should eq([comment]) + end + end + + describe "GET show" do + it "assigns the requested comment as @comment" do + comment = Comment.create! valid_attributes + get :show, {:id => comment.to_param}, valid_session + assigns(:comment).should eq(comment) + end + end + + describe "GET new" do + it "assigns a new comment as @comment" do + get :new, {}, valid_session + assigns(:comment).should be_a_new(Comment) + end + end + + describe "GET edit" do + it "assigns the requested comment as @comment" do + comment = Comment.create! valid_attributes + get :edit, {:id => comment.to_param}, valid_session + assigns(:comment).should eq(comment) + end + end + + describe "POST create" do + describe "with valid params" do + it "creates a new Comment" do + expect { + post :create, {:comment => valid_attributes}, valid_session + }.to change(Comment, :count).by(1) + end + + it "assigns a newly created comment as @comment" do + post :create, {:comment => valid_attributes}, valid_session + assigns(:comment).should be_a(Comment) + assigns(:comment).should be_persisted + end + + it "redirects to the created comment" do + post :create, {:comment => valid_attributes}, valid_session + response.should redirect_to(Comment.last) + end + end + + describe "with invalid params" do + it "assigns a newly created but unsaved comment as @comment" do + # Trigger the behavior that occurs when invalid params are submitted + Comment.any_instance.stub(:save).and_return(false) + post :create, {:comment => { "post_id" => "invalid value" }}, valid_session + assigns(:comment).should be_a_new(Comment) + end + + it "re-renders the 'new' template" do + # Trigger the behavior that occurs when invalid params are submitted + Comment.any_instance.stub(:save).and_return(false) + post :create, {:comment => { "post_id" => "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 comment" do + comment = Comment.create! valid_attributes + # Assuming there are no other comments in the database, this + # specifies that the Comment created on the previous line + # receives the :update_attributes message with whatever params are + # submitted in the request. + Comment.any_instance.should_receive(:update_attributes).with({ "post_id" => "1" }) + put :update, {:id => comment.to_param, :comment => { "post_id" => "1" }}, valid_session + end + + it "assigns the requested comment as @comment" do + comment = Comment.create! valid_attributes + put :update, {:id => comment.to_param, :comment => valid_attributes}, valid_session + assigns(:comment).should eq(comment) + end + + it "redirects to the comment" do + comment = Comment.create! valid_attributes + put :update, {:id => comment.to_param, :comment => valid_attributes}, valid_session + response.should redirect_to(comment) + end + end + + describe "with invalid params" do + it "assigns the comment as @comment" do + comment = Comment.create! valid_attributes + # Trigger the behavior that occurs when invalid params are submitted + Comment.any_instance.stub(:save).and_return(false) + put :update, {:id => comment.to_param, :comment => { "post_id" => "invalid value" }}, valid_session + assigns(:comment).should eq(comment) + end + + it "re-renders the 'edit' template" do + comment = Comment.create! valid_attributes + # Trigger the behavior that occurs when invalid params are submitted + Comment.any_instance.stub(:save).and_return(false) + put :update, {:id => comment.to_param, :comment => { "post_id" => "invalid value" }}, valid_session + response.should render_template("edit") + end + end + end + + describe "DELETE destroy" do + it "destroys the requested comment" do + comment = Comment.create! valid_attributes + expect { + delete :destroy, {:id => comment.to_param}, valid_session + }.to change(Comment, :count).by(-1) + end + + it "redirects to the comments list" do + comment = Comment.create! valid_attributes + delete :destroy, {:id => comment.to_param}, valid_session + response.should redirect_to(comments_url) + end + end + +end diff --git a/spec/factories/comments.rb b/spec/factories/comments.rb new file mode 100644 index 000000000..9a067805f --- /dev/null +++ b/spec/factories/comments.rb @@ -0,0 +1,9 @@ +# Read about factories at https://github.com/thoughtbot/factory_girl + +FactoryGirl.define do + factory :comment do + post_id 1 + author_id 1 + body "MyText" + end +end diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb new file mode 100644 index 000000000..51f5efc44 --- /dev/null +++ b/spec/models/comment_spec.rb @@ -0,0 +1,2 @@ +require 'spec_helper' + diff --git a/spec/requests/comments_spec.rb b/spec/requests/comments_spec.rb new file mode 100644 index 000000000..7a38a46e8 --- /dev/null +++ b/spec/requests/comments_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' + +describe "Comments" do + describe "GET /comments" 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 comments_path + response.status.should be(200) + end + end +end diff --git a/spec/routing/comments_routing_spec.rb b/spec/routing/comments_routing_spec.rb new file mode 100644 index 000000000..73c80563e --- /dev/null +++ b/spec/routing/comments_routing_spec.rb @@ -0,0 +1,35 @@ +require "spec_helper" + +describe CommentsController do + describe "routing" do + + it "routes to #index" do + get("/comments").should route_to("comments#index") + end + + it "routes to #new" do + get("/comments/new").should route_to("comments#new") + end + + it "routes to #show" do + get("/comments/1").should route_to("comments#show", :id => "1") + end + + it "routes to #edit" do + get("/comments/1/edit").should route_to("comments#edit", :id => "1") + end + + it "routes to #create" do + post("/comments").should route_to("comments#create") + end + + it "routes to #update" do + put("/comments/1").should route_to("comments#update", :id => "1") + end + + it "routes to #destroy" do + delete("/comments/1").should route_to("comments#destroy", :id => "1") + end + + end +end diff --git a/spec/views/comments/edit.html.haml_spec.rb b/spec/views/comments/edit.html.haml_spec.rb new file mode 100644 index 000000000..b3d496b9e --- /dev/null +++ b/spec/views/comments/edit.html.haml_spec.rb @@ -0,0 +1,22 @@ +require 'spec_helper' + +describe "comments/edit" do + before(:each) do + @comment = assign(:comment, stub_model(Comment, + :post_id => 1, + :author_id => 1, + :body => "MyText" + )) + end + + it "renders the edit comment form" do + render + + # Run the generator again with the --webrat flag if you want to use webrat matchers + assert_select "form", :action => comments_path(@comment), :method => "post" do + assert_select "input#comment_post_id", :name => "comment[post_id]" + assert_select "input#comment_author_id", :name => "comment[author_id]" + assert_select "textarea#comment_body", :name => "comment[body]" + end + end +end diff --git a/spec/views/comments/index.html.haml_spec.rb b/spec/views/comments/index.html.haml_spec.rb new file mode 100644 index 000000000..fe8aa0338 --- /dev/null +++ b/spec/views/comments/index.html.haml_spec.rb @@ -0,0 +1,26 @@ +require 'spec_helper' + +describe "comments/index" do + before(:each) do + assign(:comments, [ + stub_model(Comment, + :post_id => 1, + :author_id => 2, + :body => "MyText" + ), + stub_model(Comment, + :post_id => 1, + :author_id => 2, + :body => "MyText" + ) + ]) + end + + it "renders a list of comments" do + render + # Run the generator again with the --webrat flag if you want to use webrat matchers + assert_select "tr>td", :text => 1.to_s, :count => 2 + assert_select "tr>td", :text => 2.to_s, :count => 2 + assert_select "tr>td", :text => "MyText".to_s, :count => 2 + end +end diff --git a/spec/views/comments/new.html.haml_spec.rb b/spec/views/comments/new.html.haml_spec.rb new file mode 100644 index 000000000..cdae1772f --- /dev/null +++ b/spec/views/comments/new.html.haml_spec.rb @@ -0,0 +1,22 @@ +require 'spec_helper' + +describe "comments/new" do + before(:each) do + assign(:comment, stub_model(Comment, + :post_id => 1, + :author_id => 1, + :body => "MyText" + ).as_new_record) + end + + it "renders new comment form" do + render + + # Run the generator again with the --webrat flag if you want to use webrat matchers + assert_select "form", :action => comments_path, :method => "post" do + assert_select "input#comment_post_id", :name => "comment[post_id]" + assert_select "input#comment_author_id", :name => "comment[author_id]" + assert_select "textarea#comment_body", :name => "comment[body]" + end + end +end diff --git a/spec/views/comments/show.html.haml_spec.rb b/spec/views/comments/show.html.haml_spec.rb new file mode 100644 index 000000000..3760f18ff --- /dev/null +++ b/spec/views/comments/show.html.haml_spec.rb @@ -0,0 +1,19 @@ +require 'spec_helper' + +describe "comments/show" do + before(:each) do + @comment = assign(:comment, stub_model(Comment, + :post_id => 1, + :author_id => 2, + :body => "MyText" + )) + 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(/1/) + rendered.should match(/2/) + rendered.should match(/MyText/) + end +end