From 202c83361c22e385e3fbe3154380c6cc1a50e564 Mon Sep 17 00:00:00 2001 From: GIT_AUTHOR_NAME Date: Wed, 10 Dec 2008 16:56:04 -0500 Subject: [PATCH] allow commenting on Pitches and Tips --- app/controllers/comments_controller.rb | 32 ++++++++++++++++++++ app/helpers/comments_helper.rb | 2 ++ app/models/comment.rb | 7 +++++ app/models/pitch.rb | 2 +- app/models/tip.rb | 3 +- app/views/comments/_form.html.haml | 8 +++++ app/views/pitches/show.html.haml | 9 ++++++ app/views/shared/_comment.html.haml | 11 +++++++ app/views/tips/show.html.haml | 15 +++++++-- config/routes.rb | 4 +-- db/migrate/20081210191320_create_comments.rb | 15 +++++++++ public/stylesheets/screen_spotus.css | 32 +++++++++++++++++++- spec/controllers/comments_controller_spec.rb | 10 ++++++ spec/factories.rb | 8 +++++ spec/models/comment_spec.rb | 21 +++++++++++++ spec/models/pitch_spec.rb | 1 + spec/models/tip_spec.rb | 1 + 17 files changed, 173 insertions(+), 8 deletions(-) create mode 100644 app/controllers/comments_controller.rb create mode 100644 app/helpers/comments_helper.rb create mode 100644 app/models/comment.rb create mode 100644 app/views/comments/_form.html.haml create mode 100644 app/views/shared/_comment.html.haml create mode 100644 db/migrate/20081210191320_create_comments.rb create mode 100644 spec/controllers/comments_controller_spec.rb create mode 100644 spec/models/comment_spec.rb diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb new file mode 100644 index 000000000..ef0fa6159 --- /dev/null +++ b/app/controllers/comments_controller.rb @@ -0,0 +1,32 @@ +class CommentsController < ApplicationController + before_filter :login_required + + resources_controller_for :comments, :only => [:create] + + response_for :create do |format| + format.html do + if resource_saved? + flash[:notice] = "Successfully created comment" + redirect_to :back + else + render :action => :new + end + end + end + + protected + def new_resource + returning resource_service.new(params[resource_name]) do |resource| + resource.user = current_user + end + end + + def resource_service + if enclosing_resource + enclosing_resource.comments + else + Comment + 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..b11ad4a09 --- /dev/null +++ b/app/models/comment.rb @@ -0,0 +1,7 @@ +class Comment < ActiveRecord::Base + belongs_to :commentable, :polymorphic => true + belongs_to :user + + validates_presence_of :title, :body + validates_length_of :body, :maximum => 2000 +end diff --git a/app/models/pitch.rb b/app/models/pitch.rb index a2410a1c1..fec9f6c48 100644 --- a/app/models/pitch.rb +++ b/app/models/pitch.rb @@ -80,7 +80,7 @@ def total_amount_in_cents_for_user(user) end end has_many :supporters, :through => :donations, :source => :user, :order => "donations.created_at", :uniq => true - + has_many :comments, :as => :commentable has_one :story, :foreign_key => 'news_item_id' before_save :dispatch_fact_checker after_save :check_if_funded_state diff --git a/app/models/tip.rb b/app/models/tip.rb index 56282a8f2..03c2c29a0 100644 --- a/app/models/tip.rb +++ b/app/models/tip.rb @@ -41,9 +41,10 @@ class Tip < NewsItem has_many :supporters, :through => :pledges, :source => :user, :order => "pledges.created_at", :uniq => true has_many :affiliations, :dependent => :destroy has_many :pitches, :through => :affiliations + has_many :comments, :as => :commentable before_create :build_initial_pledge - + validates_presence_of :short_description validates_presence_of :user validates_presence_of :pledge_amount, :on => :create diff --git a/app/views/comments/_form.html.haml b/app/views/comments/_form.html.haml new file mode 100644 index 000000000..47d5cf885 --- /dev/null +++ b/app/views/comments/_form.html.haml @@ -0,0 +1,8 @@ +.row + = f.label :title + = f.text_field :title +.row + = f.label :body + = fckeditor_textarea('comment', 'body', :toolbarSet => 'Spotus', :height => '200px') +.row + = f.submit 'Post Comment' diff --git a/app/views/pitches/show.html.haml b/app/views/pitches/show.html.haml index a9a94c5b8..3b07f462f 100644 --- a/app/views/pitches/show.html.haml +++ b/app/views/pitches/show.html.haml @@ -38,6 +38,15 @@ .double_content_border .alt-spotus= h @pitch.skills .block-spacer-negative + %h3 Comments + .double_content_border + .comments + %p= "There are no comments yet, be the first!" if @pitch.comments.empty? + = render :partial => 'shared/comment', :collection => @pitch.comments + %h3 Post A Comment + .double_content_border + - form_for [@pitch, Comment.new] do |f| + = render :partial => 'comments/form', :locals => {:f => f} .span-220.column_v.box_white = render :partial => "donations/button", :locals => { :news_item => @pitch } diff --git a/app/views/shared/_comment.html.haml b/app/views/shared/_comment.html.haml new file mode 100644 index 000000000..6456e8c84 --- /dev/null +++ b/app/views/shared/_comment.html.haml @@ -0,0 +1,11 @@ +.comment + %h5= comment.title + .avatar + = image_tag comment.user.photo.url(:thumb) + .body + = comment.body + %p.attribution + by + %span= comment.user.full_name + on + %span= comment.created_at.to_s(:long) diff --git a/app/views/tips/show.html.haml b/app/views/tips/show.html.haml index 1fd2995e5..68effa7e6 100644 --- a/app/views/tips/show.html.haml +++ b/app/views/tips/show.html.haml @@ -11,12 +11,21 @@ %p = render :partial => 'news_items/featured_image', :locals => {:news_item => @tip} .alt-spotus= @tip.short_description - .block-spacer-negative - %p - - if @tip.video_embed + - unless @tip.video_embed.blank? + .block-spacer-negative %h3 Video .double_content_border = sanitize @tip.video_embed, :tags => %w(object param embed a), :attributes => %w(width height name src value allowfullscreen type href allowscriptaccess) + .block-spacer-negative + %h3 Comments + .double_content_border + .comments + %p= "There are no comments yet, be the first!" if @tip.comments.empty? + = render :partial => 'shared/comment', :collection => @tip.comments + %h3 Post A Comment + .double_content_border + - form_for [@tip, Comment.new] do |f| + = render :partial => 'comments/form', :locals => {:f => f} .span-220.column_v.box_white .centered= render :partial => "pledges/button", :locals => { :news_item => @tip } .block-spacer-negative diff --git a/config/routes.rb b/config/routes.rb index 4c87549f3..477a846df 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,8 +5,8 @@ map.resources :news_items, :collection => {:search => :any, :sort_options => :get} map.resources :donations, :affiliations, :pledges, :profiles, :pages map.resources :stories, :member => {:accept => :put, :reject => :put, :fact_check => :put, :publish => :put} - map.resources :tips, :has_many => :affiliations - map.resources :pitches, :member => {:feature => :post} + map.resources :tips, :has_many => [:affiliations, :comments] + map.resources :pitches, :member => {:feature => :post}, :has_many => :comments # TODO: remove when done map.resources :ui diff --git a/db/migrate/20081210191320_create_comments.rb b/db/migrate/20081210191320_create_comments.rb new file mode 100644 index 000000000..0d64af38a --- /dev/null +++ b/db/migrate/20081210191320_create_comments.rb @@ -0,0 +1,15 @@ +class CreateComments < ActiveRecord::Migration + def self.up + create_table :comments do |t| + t.string :title, :commentable_type + t.integer :commentable_id + t.belongs_to :user + t.text :body + t.timestamps + end + end + + def self.down + drop_table :comments + end +end diff --git a/public/stylesheets/screen_spotus.css b/public/stylesheets/screen_spotus.css index 01b8f4f78..997235604 100644 --- a/public/stylesheets/screen_spotus.css +++ b/public/stylesheets/screen_spotus.css @@ -1016,4 +1016,34 @@ textarea#headline { } ul.search_navigation a:hover {color:#666;} .search_field_button {margin: 10px 0 0 25px;} -.search-check-box label { font-weight: normal;} \ No newline at end of file +.search-check-box label { font-weight: normal;} + +.comments{margin:0 0 1em 0;} +.comment{ + overflow: auto; +} +.comment h5{ + margin-bottom: .2em; +} +.comment .avatar{ + float: left; + width: 50px; +} +.comment .body{ + float: left; + width: 390px; + margin: 0 0.5em; +} +.comment .body>p{ + margin-top:0; +} +.comment .body p{ + margin: 0.5em 0; +} +.comment .body p.attribution{ + margin: 0; + font-size: 0.9em; +} +.comment .body p.attribution span{ + color: gray; +} diff --git a/spec/controllers/comments_controller_spec.rb b/spec/controllers/comments_controller_spec.rb new file mode 100644 index 000000000..ba9688e18 --- /dev/null +++ b/spec/controllers/comments_controller_spec.rb @@ -0,0 +1,10 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe CommentsController do + describe "POST to create" do + before do + post :create, :comment => {:foo => 'bar'}, :pitch_id => 1 + end + it_denies_access + end +end diff --git a/spec/factories.rb b/spec/factories.rb index 84ba3a638..77db9279d 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -37,6 +37,14 @@ user.add_attribute(:type, 'Admin') end +Factory.define :comment do |comment| + comment.add_attribute(:commentable_type, 'Pitch') + comment.add_attribute(:commentable_id, '1') + comment.association(:user) + comment.title 'zOMg!1 PWNiEZ1!!' + comment.body 'r Teh AwSUm' +end + Factory.define :reporter do |user| user.email { random_email_address } user.first_name 'Reporter' diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb new file mode 100644 index 000000000..420e17716 --- /dev/null +++ b/spec/models/comment_spec.rb @@ -0,0 +1,21 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe Comment do + table_has_columns(Comment, :string, "commentable_type") + table_has_columns(Comment, :string, "title") + table_has_columns(Comment, :integer, "commentable_id") + table_has_columns(Comment, :integer, "user_id") + table_has_columns(Comment, :text, "body") + + requires_presence_of Comment, :body + requires_presence_of Comment, :title + + it { Factory(:comment).should belong_to(:commentable) } + + it "should not be valid if body length is greater than 2000" do + comment = Factory(:comment) + comment.body = 'f' * 2001 + comment.save + comment.errors_on(:body).should_not be_nil + end +end diff --git a/spec/models/pitch_spec.rb b/spec/models/pitch_spec.rb index 897ba14fa..70dc0e22d 100644 --- a/spec/models/pitch_spec.rb +++ b/spec/models/pitch_spec.rb @@ -26,6 +26,7 @@ it { Factory(:pitch).should have_many(:donations) } it { Factory(:pitch).should have_many(:supporters)} it { Factory(:pitch).should have_many(:topics)} + it { Factory(:pitch).should have_many(:comments)} describe "requested amount" do it "normalizes before validation" do diff --git a/spec/models/tip_spec.rb b/spec/models/tip_spec.rb index 64595dec5..fed441777 100644 --- a/spec/models/tip_spec.rb +++ b/spec/models/tip_spec.rb @@ -12,6 +12,7 @@ it {Factory(:tip).should have_many(:pledges)} it {Factory(:tip).should have_many(:affiliations)} it {Factory(:tip).should have_many(:pitches)} + it {Factory(:tip).should have_many(:comments)} describe "editable_by?" do it "allows editing of unpledged tip by owner" do