Commit c7af3905f1be7f4ce2104f0110417c9a633708ea

Authored by Victor Costa
1 parent 6a3b50fc

Refactor SuggestArticle to accept other article types

app/models/article.rb
... ... @@ -5,7 +5,7 @@ class Article < ActiveRecord::Base
5 5 :allow_members_to_edit, :translation_of_id, :language,
6 6 :license_id, :parent_id, :display_posts_in_current_language,
7 7 :category_ids, :posts_per_page, :moderate_comments,
8   - :accept_comments, :feed, :published, :source,
  8 + :accept_comments, :feed, :published, :source, :source_name,
9 9 :highlighted, :notify_comments, :display_hits, :slug,
10 10 :external_feed_builder, :display_versions, :external_link,
11 11 :image_builder, :show_to_followers
... ...
app/models/suggest_article.rb
1 1 class SuggestArticle < Task
2 2  
3   - validates_presence_of :target_id, :article_name, :article_body
  3 + validates_presence_of :target_id
4 4 validates_presence_of :email, :name, :if => Proc.new { |task| task.requestor.blank? }
  5 + validates_associated :article_object
5 6  
6 7 settings_items :email, :type => String
7 8 settings_items :name, :type => String
8   - settings_items :article_name, :type => String
9   - settings_items :article_body, :type => String
10   - settings_items :article_abstract, :type => String
11   - settings_items :article_parent_id, :type => String
12   - settings_items :source, :type => String
13   - settings_items :source_name, :type => String
14   - settings_items :highlighted, :type => :boolean, :default => false
15 9 settings_items :ip_address, :type => String
16 10 settings_items :user_agent, :type => String
17 11 settings_items :referrer, :type => String
  12 + settings_items :article, :type => Hash, :default => {}
18 13  
19 14 after_create :schedule_spam_checking
20 15  
... ... @@ -28,24 +23,34 @@ class SuggestArticle &lt; Task
28 23 requestor ? "#{requestor.name}" : "#{name} (#{email})"
29 24 end
30 25  
  26 + def article_object
  27 + if @article_object.nil?
  28 + @article_object = article_type.new(article.merge({:profile => target}).except(:type))
  29 + if requestor.present?
  30 + @article_object.author = requestor
  31 + else
  32 + @article_object.author_name = name
  33 + end
  34 + end
  35 + @article_object
  36 + end
  37 +
  38 + def article_type
  39 + (article[:type] || 'TinyMceArticle').constantize
  40 + end
  41 +
31 42 def perform
32   - task = TinyMceArticle.new
33   - task.profile = target
34   - task.name = article_name
35   - task.author_name = name
36   - task.body = article_body
37   - task.abstract = article_abstract
38   - task.parent_id = article_parent_id
39   - task.source = source
40   - task.source_name = source_name
41   - task.highlighted = highlighted
42   - task.save!
  43 + article_object.save!
43 44 end
44 45  
45 46 def title
46 47 _("Article suggestion")
47 48 end
48 49  
  50 + def article_name
  51 + article[:name]
  52 + end
  53 +
49 54 def subject
50 55 article_name
51 56 end
... ...
app/views/cms/suggest_an_article.html.erb
... ... @@ -6,18 +6,18 @@
6 6  
7 7 <%= labelled_form_for 'task' do |f| %>
8 8  
9   - <%= required labelled_form_field(_('Title'), text_field(:task, 'article_name', :size => 50)) %>
  9 + <%= required labelled_form_field(_('Title'), text_field('task[article]', 'name', :size => 50)) %>
10 10  
11   - <%= labelled_form_field(_('Source'), text_field(:task, 'source_name')) %>
  11 + <%= labelled_form_field(_('Source'), text_field('task[article]', 'source_name')) %>
12 12  
13   - <%= labelled_form_field(_('Source URL'), text_field(:task, 'source')) %>
  13 + <%= labelled_form_field(_('Source URL'), text_field('task[article]', 'source')) %>
14 14  
15 15 <% unless logged_in? %>
16 16 <%= required labelled_form_field(_('Your name'), text_field(:task, 'name')) %>
17 17 <%= required labelled_form_field(_('Email'), text_field(:task, 'email')) %>
18 18 <% end %>
19 19  
20   - <%= render :partial => 'shared/lead_and_body', :locals => {:tiny_mce => true, :object => :task, :abstract_method => 'article_abstract', :body_method => 'article_body'} %>
  20 + <%= render :partial => 'shared/lead_and_body', :locals => {:tiny_mce => true, :object => 'task[article]'} %>
21 21  
22 22 <%= hidden_field_tag('back_to', @back_to) %>
23 23  
... ...
app/views/spam/_suggest_article.html.erb
... ... @@ -7,13 +7,13 @@
7 7 <ul class="suggest-article-details" style="display: none">
8 8 <li><strong><%=_('Sent by')%></strong>: <%=task.name%> </li>
9 9 <li><strong><%=_('Email')%></strong>: <%=task.email%> </li>
10   - <li><strong><%=_('Source')%></strong>: <%=task.source_name%> </li>
11   - <li><strong><%=_('Source URL')%></strong>: <%=task.source%> </li>
12   - <li><strong><%=_('Folder')%></strong>: <%=(a = Article.find_by_id(task.article_parent_id))?a.name : '<em>' + s_('Folder|none') + '</em>'%> </li>
13   - <li><strong><%=_('Lead')%></strong>: <%=task.article_abstract.blank? ? '<em>' + s_('Abstract|empty') + '</em>' : task.article_abstract%> </li>
  10 + <li><strong><%=_('Source')%></strong>: <%=task.article_object.source_name%> </li>
  11 + <li><strong><%=_('Source URL')%></strong>: <%=task.article_object.source%> </li>
  12 + <li><strong><%=_('Folder')%></strong>: <%=(a = Article.find_by_id(task.article_object.parent_id))?a.name : '<em>' + s_('Folder|none') + '</em>'%> </li>
  13 + <li><strong><%=_('Lead')%></strong>: <%=task.article_object.abstract.blank? ? '<em>' + s_('Abstract|empty') + '</em>' : task.article_object.abstract%> </li>
14 14 <li><strong><%=_('Body')%></strong>:
15 15 <div class='suggest-article-body'>
16   - <%= task.article_body %>
  16 + <%= task.article_object.body %>
17 17 </div>
18 18 </li>
19 19 </ul>
... ...
app/views/tasks/_suggest_article_accept_details.html.erb
... ... @@ -4,11 +4,14 @@
4 4 <%= labelled_form_field(_("Sent by: "), f.text_field(:name)) %>
5 5 <p><%= label_tag(_("Email: %s") % task.email) %> </p>
6 6 <% end %>
7   -<%= required labelled_form_field(_('Title'), f.text_field(:article_name, :size => 50)) %>
8   -<%= labelled_form_field(_('Source'), f.text_field(:source_name)) %>
9   -<%= labelled_form_field(_("Source URL"), f.text_field(:source)) %>
10 7  
11   -<%= select_profile_folder(_('Select the folder where the article must be published'), "tasks[#{task.id}][task][article_parent_id]", task.target) %>
12   -<%= labelled_form_field(_('Highlight this article'), f.check_box(:highlighted)) %>
  8 +<%= f.fields_for 'article', OpenStruct.new(task.article) do |a| %>
  9 + <%= required labelled_form_field(_('Title'), a.text_field(:name, :size => 50)) %>
  10 + <%= labelled_form_field(_('Source'), a.text_field(:source_name)) %>
  11 + <%= labelled_form_field(_("Source URL"), a.text_field(:source)) %>
13 12  
14   -<%= render :partial => 'shared/lead_and_body', :locals => {:tiny_mce => true, :f => f, :abstract_method => 'article_abstract', :body_method => 'article_body', :lead_id => task.id} %>
  13 + <%= select_profile_folder(_('Select the folder where the article must be published'), "tasks[#{task.id}][task][article][parent_id]", task.target) %>
  14 + <%= labelled_form_field(_('Highlight this article'), a.check_box(:highlighted)) %>
  15 +
  16 + <%= render :partial => 'shared/lead_and_body', :locals => {:tiny_mce => true, :f => a, :lead_id => task.id} %>
  17 +<% end %>
... ...
test/factories.rb
... ... @@ -454,7 +454,7 @@ module Noosfero::Factory
454 454 end
455 455  
456 456 def defaults_for_suggest_article
457   - { :name => 'Sender', :email => 'sender@example.com', :article_name => 'Some title', :article_body => 'some body text', :article_abstract => 'some abstract text'}
  457 + { :name => 'Sender', :email => 'sender@example.com', :article => {:name => 'Some title', :body => 'some body text', :abstract => 'some abstract text'}}
458 458 end
459 459  
460 460 def defaults_for_comment(params = {})
... ...
test/functional/cms_controller_test.rb
... ... @@ -1439,22 +1439,22 @@ class CmsControllerTest &lt; ActionController::TestCase
1439 1439 logout
1440 1440 get :suggest_an_article, :profile => profile.identifier
1441 1441  
1442   - assert_tag :tag => 'textarea', :attributes => { :name => /article_abstract/, :class => 'mceEditor' }
1443   - assert_tag :tag => 'textarea', :attributes => { :name => /article_body/, :class => 'mceEditor' }
  1442 + assert_tag :tag => 'textarea', :attributes => { :name => /task\[article\]\[abstract\]/, :class => 'mceEditor' }
  1443 + assert_tag :tag => 'textarea', :attributes => { :name => /task\[article\]\[body\]/, :class => 'mceEditor' }
1444 1444 end
1445 1445  
1446 1446 should 'create a task suggest task to a profile' do
1447 1447 c = Community.create!(:name => 'test comm', :identifier => 'test_comm', :moderated_articles => true)
1448 1448  
1449 1449 assert_difference 'SuggestArticle.count' do
1450   - post :suggest_an_article, :profile => c.identifier, :back_to => 'action_view', :task => {:article_name => 'some name', :article_body => 'some body', :email => 'some@localhost.com', :name => 'some name'}
  1450 + post :suggest_an_article, :profile => c.identifier, :back_to => 'action_view', :task => {:article => {:name => 'some name', :body => 'some body'}, :email => 'some@localhost.com', :name => 'some name'}
1451 1451 end
1452 1452 end
1453 1453  
1454 1454 should 'create suggest task with logged in user as the article author' do
1455 1455 c = Community.create!(:name => 'test comm', :identifier => 'test_comm', :moderated_articles => true)
1456 1456  
1457   - post :suggest_an_article, :profile => c.identifier, :back_to => 'action_view', :task => {:article_name => 'some name', :article_body => 'some body'}
  1457 + post :suggest_an_article, :profile => c.identifier, :back_to => 'action_view', :task => {:article => {:name => 'some name', :body => 'some body'}}
1458 1458 assert_equal profile, SuggestArticle.last.requestor
1459 1459 end
1460 1460  
... ...
test/functional/spam_controller_test.rb
... ... @@ -10,7 +10,7 @@ class SpamControllerTest &lt; ActionController::TestCase
10 10 @article = fast_create(TextileArticle, :profile_id => @community.id)
11 11 @spam_comment = fast_create(Comment, :source_id => @article.id, :spam => true, :name => 'foo', :email => 'foo@example.com')
12 12  
13   - @spam_suggest_article = SuggestArticle.create!(:name => 'spammer', :article_name => 'Spam article', :email => 'spammer@shady.place', :article_body => "Something you don't need", :target => @community, :spam => true)
  13 + @spam_suggest_article = SuggestArticle.create!(:name => 'spammer', :article => {:name => 'Spam article', :body => "Something you don't need"}, :email => 'spammer@shady.place', :target => @community, :spam => true)
14 14 login_as @profile.identifier
15 15 end
16 16  
... ...
test/functional/tasks_controller_test.rb
... ... @@ -264,11 +264,11 @@ class TasksControllerTest &lt; ActionController::TestCase
264 264 c = fast_create(Community)
265 265 c.add_admin profile
266 266 @controller.stubs(:profile).returns(c)
267   - t = SuggestArticle.create!(:article_name => 'test name', :article_abstract => 'test abstract', :article_body => 'test body', :name => 'some name', :email => 'test@localhost.com', :target => c)
  267 + t = SuggestArticle.create!(:article => {:name => 'test name', :abstract => 'test abstract', :body => 'test body'}, :name => 'some name', :email => 'test@localhost.com', :target => c)
268 268  
269 269 get :index
270   - assert_tag :tag => 'textarea', :content => /test abstract/, :attributes => { :name => /article_abstract/, :class => 'mceEditor' }
271   - assert_tag :tag => 'textarea', :content => /test body/, :attributes => { :name => /article_body/, :class => 'mceEditor' }
  270 + assert_tag :tag => 'textarea', :content => /test abstract/, :attributes => { :name => /tasks\[#{t.id}\]\[task\]\[article\]\[abstract\]/, :class => 'mceEditor' }
  271 + assert_tag :tag => 'textarea', :content => /test body/, :attributes => { :name => /tasks\[#{t.id}\]\[task\]\[article\]\[body\]/, :class => 'mceEditor' }
272 272 end
273 273  
274 274 should 'create TinyMceArticle article after finish approve suggested article task' do
... ... @@ -276,7 +276,7 @@ class TasksControllerTest &lt; ActionController::TestCase
276 276 c = fast_create(Community)
277 277 c.affiliate(profile, Profile::Roles.all_roles(profile.environment.id))
278 278 @controller.stubs(:profile).returns(c)
279   - t = SuggestArticle.create!(:article_name => 'test name', :article_body => 'test body', :name => 'some name', :email => 'test@localhost.com', :target => c)
  279 + t = SuggestArticle.create!(:article => {:name => 'test name', :body => 'test body'}, :name => 'some name', :email => 'test@localhost.com', :target => c)
280 280  
281 281 post :close, :tasks => {t.id => { :task => {}, :decision => "finish"}}
282 282 assert_not_nil TinyMceArticle.find(:first)
... ... @@ -288,16 +288,13 @@ class TasksControllerTest &lt; ActionController::TestCase
288 288 c.affiliate(profile, Profile::Roles.all_roles(profile.environment.id))
289 289 @controller.stubs(:profile).returns(c)
290 290 t = SuggestArticle.new
291   - t.article_name = 'test name'
292   - t.article_body = 'test body'
  291 + t.article = {:name => 'test name', :body => 'test body', :source => 'http://test.com', :source_name => 'some source name'}
293 292 t.name = 'some name'
294   - t.source = 'http://test.com'
295   - t.source_name = 'some source name'
296 293 t.email = 'test@localhost.com'
297 294 t.target = c
298 295 t.save!
299 296  
300   - post :close, :tasks => {t.id => { :task => {:article_name => 'new article name', :article_body => 'new body', :source => 'http://www.noosfero.com', :source_name => 'new source', :name => 'new name'}, :decision => "finish"}}
  297 + post :close, :tasks => {t.id => { :task => {:article => {:name => 'new article name', :body => 'new body', :source => 'http://www.noosfero.com', :source_name => 'new source'}, :name => 'new name'}, :decision => "finish"}}
301 298 assert_equal 'new article name', TinyMceArticle.find(:first).name
302 299 assert_equal 'new name', TinyMceArticle.find(:first).author_name
303 300 assert_equal 'new body', TinyMceArticle.find(:first).body
... ... @@ -310,7 +307,7 @@ class TasksControllerTest &lt; ActionController::TestCase
310 307 c = fast_create(Community)
311 308 c.add_admin profile
312 309 @controller.stubs(:profile).returns(c)
313   - t = SuggestArticle.create!(:article_name => 'test name', :article_abstract => 'test abstract', :article_body => 'test body', :name => 'some name', :email => 'test@localhost.com', :target => c)
  310 + t = SuggestArticle.create!(:article => {:name => 'test name', :abstract => 'test abstract', :body => 'test body'}, :name => 'some name', :email => 'test@localhost.com', :target => c)
314 311  
315 312 get :index
316 313 assert_select "#tasks_#{t.id}_task_name"
... ... @@ -321,7 +318,7 @@ class TasksControllerTest &lt; ActionController::TestCase
321 318 c = fast_create(Community)
322 319 c.add_admin profile
323 320 @controller.stubs(:profile).returns(c)
324   - t = SuggestArticle.create!(:article_name => 'test name', :article_abstract => 'test abstract', :article_body => 'test body', :requestor => fast_create(Person), :target => c)
  321 + t = SuggestArticle.create!(:article => {:name => 'test name', :abstract => 'test abstract', :body => 'test body'}, :requestor => fast_create(Person), :target => c)
325 322  
326 323 get :index
327 324 assert_select "#tasks_#{t.id}_task_name", 0
... ...
test/unit/suggest_article_test.rb
... ... @@ -13,16 +13,9 @@ class SuggestArticleTest &lt; ActiveSupport::TestCase
13 13  
14 14 should 'have the article_name' do
15 15 t = SuggestArticle.new
16   - assert !t.errors[:article_name.to_s].present?
17   - t.valid?
18   - assert t.errors[:article_name.to_s].present?
19   - end
20   -
21   - should 'have the article_body' do
22   - t = SuggestArticle.new
23   - assert !t.errors[:article_body.to_s].present?
24   - t.valid?
25   - assert t.errors[:article_body.to_s].present?
  16 + assert !t.article_object.errors[:name].present?
  17 + t.article_object.valid?
  18 + assert t.article_object.errors[:name].present?
26 19 end
27 20  
28 21 should 'have the email' do
... ... @@ -46,19 +39,12 @@ class SuggestArticleTest &lt; ActiveSupport::TestCase
46 39 assert t.errors[:target_id.to_s].present?
47 40 end
48 41  
49   - should 'have the article_abstract' do
  42 + should 'have the article' do
50 43 t = SuggestArticle.new
51   - assert t.respond_to?(:article_abstract)
52   - end
53   -
54   - should 'have the article_parent_id' do
55   - t = SuggestArticle.new
56   - assert t.respond_to?(:article_parent_id)
57   - end
58   -
59   - should 'source be defined' do
60   - t = SuggestArticle.new
61   - assert t.respond_to?(:source)
  44 + assert t.respond_to?(:article_object)
  45 + assert !t.errors[:article_object].present?
  46 + t.valid?
  47 + assert t.errors[:article_object].present?
62 48 end
63 49  
64 50 should 'create an article on with perfom method' do
... ... @@ -66,9 +52,7 @@ class SuggestArticleTest &lt; ActiveSupport::TestCase
66 52 name = 'some name'
67 53 body = 'some body'
68 54 abstract = 'some abstract'
69   - t.article_name = name
70   - t.article_body = body
71   - t.article_abstract = abstract
  55 + t.article = {:name => name, :body => body, :abstract => abstract}
72 56 t.target = @profile
73 57 count = TinyMceArticle.count
74 58 t.perform
... ... @@ -77,8 +61,7 @@ class SuggestArticleTest &lt; ActiveSupport::TestCase
77 61  
78 62 should 'fill source name and URL into created article' do
79 63 t = build(SuggestArticle, :target => @profile)
80   - t.source_name = 'GNU project'
81   - t.source = 'http://www.gnu.org/'
  64 + t.article.merge!({:source_name => 'GNU project', :source => 'http://www.gnu.org/'})
82 65 t.perform
83 66  
84 67 article = TinyMceArticle.last
... ... @@ -95,7 +78,7 @@ class SuggestArticleTest &lt; ActiveSupport::TestCase
95 78  
96 79 should 'highlight created article' do
97 80 t = build(SuggestArticle, :target => @profile)
98   - t.highlighted = true
  81 + t.article[:highlighted] = true
99 82 t.perform
100 83  
101 84 article = TinyMceArticle.last(:conditions => { :name => t.article_name}) # just to be sure
... ... @@ -132,19 +115,19 @@ class SuggestArticleTest &lt; ActiveSupport::TestCase
132 115 end
133 116  
134 117 should 'have target notification message' do
135   - task = build(SuggestArticle, :target => @profile, :article_name => 'suggested article', :name => 'johndoe')
  118 + task = build(SuggestArticle, :target => @profile, :article => {:name => 'suggested article'}, :name => 'johndoe')
136 119  
137 120 assert_match(/#{task.name}.*suggested the publication of the article: #{task.subject}.*[\n]*.*to approve or reject/, task.target_notification_message)
138 121 end
139 122  
140 123 should 'have target notification description' do
141   - task = build(SuggestArticle,:target => @profile, :article_name => 'suggested article', :name => 'johndoe')
  124 + task = build(SuggestArticle,:target => @profile, :article => {:name => 'suggested article'}, :name => 'johndoe')
142 125  
143 126 assert_match(/#{task.name}.*suggested the publication of the article: #{task.subject}/, task.target_notification_description)
144 127 end
145 128  
146 129 should 'deliver target notification message' do
147   - task = build(SuggestArticle, :target => @profile, :article_name => 'suggested article', :name => 'johndoe', :email => 'johndoe@example.com')
  130 + task = build(SuggestArticle, :target => @profile, :article => {:name => 'suggested article'}, :name => 'johndoe', :email => 'johndoe@example.com')
148 131  
149 132 email = TaskMailer.target_notification(task, task.target_notification_message).deliver
150 133  
... ... @@ -160,7 +143,7 @@ class SuggestArticleTest &lt; ActiveSupport::TestCase
160 143 should 'delegate spam detection to plugins' do
161 144 Environment.default.enable_plugin(EverythingIsSpam)
162 145  
163   - t1 = build(SuggestArticle, :target => @profile, :article_name => 'suggested article', :name => 'johndoe', :email => 'johndoe@example.com')
  146 + t1 = build(SuggestArticle, :target => @profile, :article => {:name => 'suggested article'}, :name => 'johndoe', :email => 'johndoe@example.com')
164 147  
165 148 EverythingIsSpam.any_instance.expects(:check_for_spam)
166 149  
... ... @@ -189,7 +172,7 @@ class SuggestArticleTest &lt; ActiveSupport::TestCase
189 172 should 'notify plugins of suggest_articles being marked as spam' do
190 173 Environment.default.enable_plugin(SpamNotification)
191 174  
192   - t = SuggestArticle.create!(:target => @profile, :article_name => 'suggested article', :name => 'johndoe', :article_body => 'wanna feel my body? my body baaaby', :email => 'johndoe@example.com')
  175 + t = SuggestArticle.create!(:target => @profile, :article => {:name => 'suggested article', :body => 'wanna feel my body? my body baaaby'}, :name => 'johndoe', :email => 'johndoe@example.com')
193 176  
194 177 t.spam!
195 178 process_delayed_job_queue
... ... @@ -200,7 +183,7 @@ class SuggestArticleTest &lt; ActiveSupport::TestCase
200 183 should 'notify plugins of suggest_articles being marked as ham' do
201 184 Environment.default.enable_plugin(SpamNotification)
202 185  
203   - t = SuggestArticle.create!(:target => @profile, :article_name => 'suggested article', :name => 'johndoe', :article_body => 'wanna feel my body? my body baaaby', :email => 'johndoe@example.com')
  186 + t = SuggestArticle.create!(:target => @profile, :article => {:name => 'suggested article', :body => 'wanna feel my body? my body baaaby'}, :name => 'johndoe', :email => 'johndoe@example.com')
204 187  
205 188 t.ham!
206 189 process_delayed_job_queue
... ... @@ -219,7 +202,7 @@ class SuggestArticleTest &lt; ActiveSupport::TestCase
219 202 end
220 203  
221 204 should 'log spammer ip after marking comment as spam' do
222   - t = SuggestArticle.create!(:target => @profile, :article_name => 'suggested article', :name => 'johndoe', :article_body => 'wanna feel my body? my body baaaby', :email => 'johndoe@example.com', :ip_address => '192.168.0.1')
  205 + t = SuggestArticle.create!(:target => @profile, :article => {:name => 'suggested article', :body => 'wanna feel my body? my body baaaby'}, :name => 'johndoe', :email => 'johndoe@example.com', :ip_address => '192.168.0.1')
223 206 t.spam!
224 207 log = File.open('log/test_spammers.log')
225 208 assert_match "SuggestArticle-id: #{t.id} IP: 192.168.0.1", log.read
... ... @@ -238,4 +221,13 @@ class SuggestArticleTest &lt; ActiveSupport::TestCase
238 221 assert_equal person.name, t.sender
239 222 end
240 223  
  224 + should 'create an event on perfom method' do
  225 + t = SuggestArticle.new
  226 + t.article = {:name => 'name', :body => 'body', :type => 'Event'}
  227 + t.target = @profile
  228 + assert_difference "Event.count" do
  229 + t.perform
  230 + end
  231 + end
  232 +
241 233 end
... ...