Skip to content

Commit

Permalink
allow commenting on Pitches and Tips
Browse files Browse the repository at this point in the history
  • Loading branch information
GIT_AUTHOR_NAME committed Dec 10, 2008
1 parent f0bc3ab commit 202c833
Show file tree
Hide file tree
Showing 17 changed files with 173 additions and 8 deletions.
32 changes: 32 additions & 0 deletions 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
2 changes: 2 additions & 0 deletions app/helpers/comments_helper.rb
@@ -0,0 +1,2 @@
module CommentsHelper
end
7 changes: 7 additions & 0 deletions 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
2 changes: 1 addition & 1 deletion app/models/pitch.rb
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion app/models/tip.rb
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions 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'
9 changes: 9 additions & 0 deletions app/views/pitches/show.html.haml
Expand Up @@ -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 }
Expand Down
11 changes: 11 additions & 0 deletions 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)
15 changes: 12 additions & 3 deletions app/views/tips/show.html.haml
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions config/routes.rb
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions 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
32 changes: 31 additions & 1 deletion public/stylesheets/screen_spotus.css
Expand Up @@ -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;}
.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;
}
10 changes: 10 additions & 0 deletions 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
8 changes: 8 additions & 0 deletions spec/factories.rb
Expand Up @@ -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'
Expand Down
21 changes: 21 additions & 0 deletions 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
1 change: 1 addition & 0 deletions spec/models/pitch_spec.rb
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/models/tip_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit 202c833

Please sign in to comment.