Permalink
Browse files

move code from controller to model

  • Loading branch information...
1 parent 6a45106 commit e8313e629e4111e91225ac8bee397eac30de62fb Yannick Francois committed Nov 3, 2012
@@ -84,7 +84,8 @@ def create
if request.post? and @comment.save
# We should probably wave a spam filter over this, but for now, just mark it as published.
- @comment.mark_as_ham!
+ @comment.mark_as_ham
+ @comment.save!
flash[:notice] = _('Comment was successfully created.')
end
redirect_to :action => 'article', :id => @article.id
@@ -118,18 +119,13 @@ def change_state
return unless request.xhr?
feedback = Feedback.find(params[:id])
- if (feedback.state.to_s.downcase == 'spam')
- feedback.mark_as_ham!
- else
- feedback.mark_as_spam!
- end
+ template = feedback.change_state!
- template = (feedback.state.to_s.downcase == 'spam') ? 'spam' : 'ham'
render(:update) do |page|
if params[:context] != 'listing'
page.visual_effect :fade, "feedback_#{feedback.id}"
else
- page.replace("feedback_#{feedback.id}", :partial => template, :locals => {:comment => feedback})
+ page.replace("feedback_#{feedback.id}", partial: template, locals: {comment: feedback})
end
end
end
View
@@ -129,14 +129,17 @@ def akismet_is_spam?(options={})
end
end
- def mark_as_ham!
- mark_as_ham
+ def change_state!
+ result = ''
+ if state.spam?
+ mark_as_ham
+ result = 'ham'
+ else
+ mark_as_spam
+ result = 'spam'
+ end
save!
- end
-
- def mark_as_spam!
- mark_as_spam
- save
+ result
end
def report_as_spam
@@ -1,7 +1,7 @@
<% @page_heading = _("Comments for %s", @article.title) %>
<% @link_to_new = link_to(_("Add a comment"), '#comment', {:id => 'dialog-link', :class => 'button'}) %>
-<%= form_tag({:action => 'bulkops'}, :method => :post) do %>
+<%= form_tag({:action => 'bulkops'}) do %>
<%= hidden_field 'article_id', @article.id %>
<%= render 'button', { :position => 'top' } %>
@@ -29,7 +29,7 @@
</div>
<% end %>
-<%= form_tag({:action => 'bulkops'}, :method => :post) do %>
+<%= form_tag({:action => 'bulkops'}) do %>
<%= render 'button', { :position => 'top' } %>
<%= hidden_field_tag "search", params[:search]%>
View
@@ -238,6 +238,11 @@ def some_user
c.published false
end
+ factory :ham_comment, :parent => :comment do |c|
+ c.state 'ham'
+ c.published false
+ end
+
factory :page do |p|
p.name 'page_one'
p.title 'Page One Title'
@@ -3,8 +3,7 @@
describe Feedback::States do
before(:each) do
FactoryGirl.create(:blog)
- @comment = FactoryGirl.create(:article).comments.build(:author => 'Piers',
- :body => 'Body')
+ @comment = FactoryGirl.create(:article).comments.build(author: 'Piers', body: 'Body')
end
it "test_ham_all_the_way" do
@@ -0,0 +1,31 @@
+require 'spec_helper'
+
+describe Feedback do
+ before do
+ FactoryGirl.create(:blog)
+ end
+
+ describe ".change_state!" do
+ it "respond to change_state!" do
+ Feedback.new.should respond_to(:change_state!)
+ end
+
+ context "given a feedback with a spam state" do
+ it "calls mark_as_ham!" do
+ feedback = FactoryGirl.build(:spam_comment)
+ feedback.should_receive(:mark_as_ham)
+ feedback.should_receive(:save!)
+ feedback.change_state!.should eq "ham"
+ end
+ end
+
+ context "given a feedback with a ham state" do
+ it "calls mark_as_spam!" do
+ feedback = FactoryGirl.build(:ham_comment)
+ feedback.should_receive(:mark_as_spam)
+ feedback.should_receive(:save!)
+ feedback.change_state!.should eq "spam"
+ end
+ end
+ end
+end

0 comments on commit e8313e6

Please sign in to comment.