Skip to content

Commit

Permalink
move code from controller to model
Browse files Browse the repository at this point in the history
  • Loading branch information
Yannick Francois committed Nov 3, 2012
1 parent 6a45106 commit e8313e6
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 19 deletions.
12 changes: 4 additions & 8 deletions app/controllers/admin/feedback_controller.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
17 changes: 10 additions & 7 deletions app/models/feedback.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/feedback/article.html.erb
@@ -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' } %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/feedback/index.html.erb
Expand Up @@ -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]%>
Expand Down
5 changes: 5 additions & 0 deletions spec/factories.rb
Expand Up @@ -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'
Expand Down
3 changes: 1 addition & 2 deletions spec/models/feedback/states_spec.rb
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions spec/models/feedback_spec.rb
@@ -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.