Skip to content

Commit

Permalink
some refactoring around comment controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Yannick Francois committed Oct 16, 2011
1 parent a95a4cb commit 6fd964a
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 108 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ db/*.sqlite*
db/schema.rb
.*.swp
.DS_Store
.rspec
installer/rails_installer.yml
/public/files
/public/images/theme
Expand Down
3 changes: 1 addition & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ def blog_base_url
end

def add_to_cookies(name, value, path=nil, expires=nil)
cookies[name] = { :value => value, :path => path || "/#{controller_name}",
:expires => 6.weeks.from_now }
cookies[name] = { :value => value, :path => path || "/#{controller_name}", :expires => 6.weeks.from_now }
end

def this_blog
Expand Down
43 changes: 22 additions & 21 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,16 @@ def create
end
end

set_comment_cookies
use_recaptcha = Blog.default.settings["use_recaptcha"]
set_cookies_for @comment

if ((use_recaptcha && verify_recaptcha(:model => @comment)) || !use_recaptcha) && @comment.save
if request.xhr?
render :partial => '/articles/comment', :object => @comment
else
redirect_to @article.permalink_url
end
partial = '/articles/comment_failed'
if recaptcha_ok_for?(@comment) && @comment.save
partial = '/articles/comment'
end
if request.xhr?
render :partial => partial, :object => @comment
else
if request.xhr?
render :partial => '/articles/comment_failed', :object => @comment
else
redirect_to @article.permalink_url
end
redirect_to @article.permalink_url
end
end

Expand All @@ -36,13 +31,14 @@ def preview
session :session => new
end

if (params[:comment][:body].blank? rescue true)
comment_params = params[:comment]
if (params_comment[:body].blank? rescue true)
render :nothing => true
return
end

set_headers
@comment = Comment.new(params[:comment])
@comment = Comment.new(params_comment)

unless @article.comments_closed?
render 'articles/comment_preview', :locals => { :comment => @comment }
Expand All @@ -53,6 +49,11 @@ def preview

protected

def recaptcha_ok_for? comment
use_recaptcha = Blog.default.settings["use_recaptcha"]
((use_recaptcha && verify_recaptcha(:model => comment)) || !use_recaptcha)
end

def get_feedback
@comments = \
if params[:article_id]
Expand All @@ -63,7 +64,7 @@ def get_feedback
end

def new_comment_defaults
return { :ip => request.remote_ip,
{ :ip => request.remote_ip,
:author => 'Anonymous',
:published => true,
:user => @current_user,
Expand All @@ -76,12 +77,12 @@ def set_headers
headers["Content-Type"] = "text/html; charset=utf-8"
end

def set_comment_cookies
add_to_cookies(:author, @comment.author)
if ! @comment.email.blank?
add_to_cookies(:gravatar_id, Digest::MD5.hexdigest(@comment.email.strip))
def set_cookies_for comment
add_to_cookies(:author, comment.author)
add_to_cookies(:url, comment.url)
if ! comment.email.blank?
add_to_cookies(:gravatar_id, Digest::MD5.hexdigest(comment.email.strip))
end
add_to_cookies(:url, @comment.url)
end

def get_article
Expand Down
168 changes: 83 additions & 85 deletions spec/controllers/comments_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,47 @@
require 'spec_helper'

shared_examples_for "General Comment Creation" do
describe CommentsController do
before do
@article = Factory(:article)
post :create, :comment => {:body => 'content', :author => 'bob',
:email => 'bob@home', :url => 'http://bobs.home/'},
:article_id => @article.id
@blog = Factory(:blog)
end

it "should assign the new comment to @comment" do
assigns[:comment].should == Comment.find_by_author_and_body_and_article_id('bob', 'content', @article.id)
end
describe '#create' do
describe "Basic comment creation" do
before do
@article = Factory(:article)
comment = {:body => 'content', :author => 'bob', :email => 'bob@home', :url => 'http://bobs.home/'}
post :create, :comment => comment, :article_id => @article.id
end

it "should assign the article to @article" do
assigns[:article].should == @article
end
it "should assign the new comment to @comment" do
assigns[:comment].should == Comment.find_by_author_and_body_and_article_id('bob', 'content', @article.id)
end

it "should save the comment" do
@article.comments.size.should == 1
end
it "should assign the article to @article" do
assigns[:article].should == @article
end

it "should set the author" do
@article.comments.last.author.should == 'bob'
end
it "should save the comment" do
@article.comments.size.should == 1
end

it "should set an author cookie" do
cookies["author"].should == 'bob'
end
it "should set the author" do
@article.comments.last.author.should == 'bob'
end

it "should set a gravatar_id cookie" do
cookies["gravatar_id"].should == Digest::MD5.hexdigest('bob@home')
end
it "should set an author cookie" do
cookies["author"].should == 'bob'
end

it "should set a url cookie" do
cookies["url"].should == 'http://bobs.home/'
end
end
it "should set a gravatar_id cookie" do
cookies["gravatar_id"].should == Digest::MD5.hexdigest('bob@home')
end

describe CommentsController do
before do
@blog = Factory(:blog, :sp_global => false)
end
it "should set a url cookie" do
cookies["url"].should == 'http://bobs.home/'
end
end

describe 'create' do
it_should_behave_like "General Comment Creation"

it "should redirect to the article" do
article = Factory(:article, :created_at => '2005-01-01 02:00:00')
Expand All @@ -54,46 +52,24 @@
end

describe 'AJAX creation' do
it_should_behave_like "General Comment Creation"

it "should render the comment partial" do
xhr :post, :create, :comment => {:body => 'content', :author => 'bob'},
:article_id => Factory(:article).id
response.should render_template("/articles/_comment")
end
end

describe 'scoped index' do
it "GET 2007/10/11/slug/comments should redirect to /2007/10/11/slug#comments" do
article = Factory(:article, :created_at => '2005-01-01 02:00:00')
get 'index', :article_id => article.id
response.should redirect_to("#{@blog.base_url}/#{article.created_at.year}/#{sprintf("%.2d", article.created_at.month)}/#{sprintf("%.2d", article.created_at.day)}/#{article.permalink}#comments")
end

it "GET /2007/10/11/slug/comments.atom should return an atom feed" do
get :index, :format => 'atom', :article_id => Factory(:article).id
response.should be_success
response.should render_template("index_atom_feed")
end
describe "#index" do
context 'scoped index' do
it "GET 2007/10/11/slug/comments should redirect to /2007/10/11/slug#comments" do
article = Factory(:article, :created_at => '2005-01-01 02:00:00')
get 'index', :article_id => article.id
response.should redirect_to("#{@blog.base_url}/#{article.created_at.year}/#{sprintf("%.2d", article.created_at.month)}/#{sprintf("%.2d", article.created_at.day)}/#{article.permalink}#comments")
end

it "GET /2007/10/11/slug/comments.rss should return an rss feed" do
get :index, :format => 'rss', :article_id => Factory(:article).id
response.should be_success
response.should render_template("index_rss_feed")
end
end
end

describe CommentsController do
before do
blog = stub_model(Blog, :base_url => "http://myblog.net")
Blog.stub(:default) { blog }
Comment.stub(:find) { [ "some", "items" ] }
end

describe "#index" do

describe "without format" do
context "without format" do
it "should be successful" do
get 'index'
response.should be_success
Expand All @@ -108,39 +84,61 @@
end
end

describe "with :format => 'atom'" do
before do
get 'index', :format => 'atom'
end
context "with :format => 'atom'" do
context "without article" do
before do
Comment.should_receive(:find).and_return(['some','items'])
get 'index', :format => 'atom'
end

it "is succesful" do
response.should be_success
end
it "is succesful" do
response.should be_success
end

it "passes the comments to the template" do
assigns(:comments).should == ["some", "items"]
end

it "passes the comments to the template" do
assigns(:comments).should == ["some", "items"]
it "should return an atom feed" do
response.should render_template("index_atom_feed")
end
end

it "should return an atom feed" do
response.should render_template("index_atom_feed")
context "with an article" do
it "should return an atom feed" do
get :index, :format => 'atom', :article_id => Factory(:article).id
response.should be_success
response.should render_template("index_atom_feed")
end
end
end

describe "with :format => 'rss'" do
before do
get 'index', :format => 'rss'
end
context "with :format => 'rss'" do
context "without article" do
before do
Comment.should_receive(:find).and_return(['some','items'])
get 'index', :format => 'rss'
end

it "is succesful" do
response.should be_success
end
it "is succesful" do
response.should be_success
end

it "passes the comments to the template" do
assigns(:comments).should == ["some", "items"]
end

it "passes the comments to the template" do
assigns(:comments).should == ["some", "items"]
it "should return an rss feed" do
response.should render_template("index_rss_feed")
end
end

it "should return an rss feed" do
response.should render_template("index_rss_feed")
context "with article" do
it "should return an rss feed" do
get :index, :format => 'rss', :article_id => Factory(:article).id
response.should be_success
response.should render_template("index_rss_feed")
end
end
end
end
Expand Down

0 comments on commit 6fd964a

Please sign in to comment.