Merged from mistaken branch on G/g

This commit is contained in:
Skud
2013-02-08 16:11:38 +11:00
29 changed files with 554 additions and 3 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,95 @@
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
@post = Post.find_by_id(params[:post_id])
if @post
respond_to do |format|
format.html # new.html.erb
format.json { render json: @comment }
end
else
redirect_to request.referer || root_url,
:alert => "Can't post a comment on a non-existent post"
end
end
# GET /comments/1/edit
def edit
@comment = Comment.find(params[:id])
end
# POST /comments
# POST /comments.json
def create
params[:comment][:author_id] = current_member.id
@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])
# you should never be able to change the author or post when
# updating
params[:comment].delete("post_id")
params[:comment].delete("author_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

View File

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

View File

@@ -20,12 +20,16 @@ class Ability
can :update, ScientificName
can :destroy, ScientificName
# anyone can create a post, but only the author can edit/destroy
# it.
# anyone can create a post, or comment on a post,
# but only the author can edit/destroy it.
can :create, Post
can :update, Post, :author_id => member.id
can :destroy, Post, :author_id => member.id
can :create, Comment
can :update, Comment, :author_id => member.id
can :destroy, Comment, :author_id => member.id
# same deal for gardens and plantings
can :create, Garden
can :update, Garden, :owner_id => member.id

5
app/models/comment.rb Normal file
View File

@@ -0,0 +1,5 @@
class Comment < ActiveRecord::Base
attr_accessible :author_id, :body, :post_id
belongs_to :author, :class_name => 'Member'
belongs_to :post
end

View File

@@ -3,6 +3,7 @@ class Member < ActiveRecord::Base
friendly_id :login_name, use: :slugged
has_many :posts, :foreign_key => 'author_id'
has_many :comments, :foreign_key => 'author_id'
has_many :gardens, :foreign_key => 'owner_id'
# Include default devise modules. Others available are:

View File

@@ -3,6 +3,7 @@ class Post < ActiveRecord::Base
friendly_id :author_date_subject, use: :slugged
attr_accessible :body, :subject, :author_id
belongs_to :author, :class_name => 'Member'
has_many :comments
default_scope order("created_at desc")
def author_date_subject

View File

@@ -0,0 +1,16 @@
= 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 :body
= f.text_area :body, :rows => 6, :class => 'input-block-level'
.actions
= f.submit 'Save'
- if defined?(@post)
.field
= f.hidden_field :post_id, :value => @post.id

View File

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

View File

@@ -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

View File

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

View File

@@ -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

View File

@@ -24,3 +24,7 @@
- if can? :destroy, post
= link_to 'Delete', post, method: :delete, |
data: { confirm: 'Are you sure?' }, :class => 'btn'
- if can? :create, Comment
.post-actions
=link_to 'Comment', new_comment_path(:post_id => post.id), :class => 'btn'

View File

@@ -7,6 +7,7 @@ Growstuff::Application.routes.draw do
resources :scientific_names
resources :crops
resources :members
resources :comments
get "home/index"

View File

@@ -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

View File

@@ -0,0 +1,17 @@
class RequireFieldsForComments < ActiveRecord::Migration
def up
change_table :comments do |t|
t.change :post_id, :string, :null => false
t.change :author_id, :string, :null => false
t.change :body, :string, :null => false
end
end
def down
change_table :comments do |t|
t.change :post_id, :string, :null => true
t.change :author_id, :string, :null => true
t.change :body, :string, :null => true
end
end
end

View File

@@ -11,7 +11,15 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20130206051328) do
ActiveRecord::Schema.define(:version => 20130208034248) do
create_table "comments", :force => true do |t|
t.string "post_id", :null => false
t.string "author_id", :null => false
t.string "body", :null => false
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

View File

@@ -0,0 +1,149 @@
require 'spec_helper'
describe CommentsController do
login_member
def valid_attributes
{ :post_id => 1, :author_id => 1, :body => "some text" }
end
describe "GET index" do
it "assigns all comments as @comments" do
comment = Comment.create! valid_attributes
get :index, {}
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}
assigns(:comment).should eq(comment)
end
end
describe "GET new" do
it "assigns a new comment as @comment" do
get :new, {}
assigns(:comment).should be_a_new(Comment)
end
it "picks up post from params" do
post = FactoryGirl.create(:post)
get :new, {:post_id => post.id}
assigns(:post).should eq(post)
end
it "dies if no post specified" do
get :new
response.should redirect_to(root_url)
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}
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}
}.to change(Comment, :count).by(1)
end
it "assigns a newly created comment as @comment" do
post :create, {:comment => valid_attributes}
assigns(:comment).should be_a(Comment)
assigns(:comment).should be_persisted
end
it "redirects to the created comment" do
post :create, {:comment => valid_attributes}
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" }}
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" }}
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({ "body" => "some text" })
put :update, {:id => comment.to_param, :comment => { "body" => "some text" }}
end
it "assigns the requested comment as @comment" do
comment = Comment.create! valid_attributes
put :update, {:id => comment.to_param, :comment => valid_attributes}
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}
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" }}
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" }}
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}
}.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}
response.should redirect_to(comments_url)
end
end
end

View File

@@ -0,0 +1,7 @@
FactoryGirl.define do
factory :comment do
post
author
body "OMG LOL"
end
end

View File

@@ -0,0 +1,16 @@
require 'spec_helper'
describe Comment do
before(:each) do
@comment = FactoryGirl.create(:comment)
end
it "belongs to a post" do
@comment.post.should be_an_instance_of Post
end
it "belongs to an author" do
@comment.author.should be_an_instance_of Member
end
end

View File

@@ -49,6 +49,12 @@ describe 'member' do
@member.gardens.first.name.should eq "Garden"
end
it "has many comments" do
@member.save
@comment1 = FactoryGirl.create(:comment, :author => @member)
@comment2 = FactoryGirl.create(:comment, :author => @member)
@member.comments.length.should == 2
end
end
context 'no TOS agreement' do

View File

@@ -20,4 +20,11 @@ describe Post do
@datestr.length.should == 4 + @time.year.to_s.size
@post.slug.should == "#{@member.login_name}-#{@datestr}-a-post"
end
it "has many comments" do
@post = FactoryGirl.create(:post, :author => @member)
@comment1 = FactoryGirl.create(:comment, :post => @post)
@comment2 = FactoryGirl.create(:comment, :post => @post)
@post.comments.length.should == 2
end
end

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -0,0 +1,20 @@
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 "textarea#comment_body", :name => "comment[body]"
end
end
end

View File

@@ -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 <p>" 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

View File

@@ -34,4 +34,19 @@ describe "posts/show" do
rendered.should_not match(/a href="http:\/\/evil.com"/)
end
context "signed in" do
before(:each) do
sign_in @author
controller.stub(:current_user) { @author }
@post = assign(:post,
FactoryGirl.create(:post, :author => @author))
render
end
it 'shows a comment button' do
assert_select "a[href=#{new_comment_path(:post_id => @post.id)}]", "Comment"
end
end
end