Commit a1e84d51a34b63ea1a4f846a0b3efec69b68e57d

Authored by Larissa Reis
1 parent 833e0012

suggest_an_article: Fix crash when article type or author are blank

  - Removes an old line that was left behind during merge of support for
  different types of article. It was overriding the correct decision
  made by #article_type.
  - Defines SuggestArticle#author_name the same as the sender so
  noosfero task page doesn't crash in case the processing of the task
  throws an error, since it will try to show the author name in the
  task error page.

  Fixes #166
app/models/suggest_article.rb
@@ -23,6 +23,10 @@ class SuggestArticle < Task @@ -23,6 +23,10 @@ class SuggestArticle < Task
23 requestor ? "#{requestor.name}" : "#{name} (#{email})" 23 requestor ? "#{requestor.name}" : "#{name} (#{email})"
24 end 24 end
25 25
  26 + def author_name
  27 + sender
  28 + end
  29 +
26 def article_object 30 def article_object
27 if @article_object.nil? 31 if @article_object.nil?
28 @article_object = article_type.new(article.merge(target.present? ? {:profile => target} : {}).except(:type)) 32 @article_object = article_type.new(article.merge(target.present? ? {:profile => target} : {}).except(:type))
@@ -41,7 +45,6 @@ class SuggestArticle < Task @@ -41,7 +45,6 @@ class SuggestArticle < Task
41 return type if type < Article 45 return type if type < Article
42 end 46 end
43 TinyMceArticle 47 TinyMceArticle
44 - (article[:type] || 'TinyMceArticle').constantize  
45 end 48 end
46 49
47 def perform 50 def perform
test/functional/tasks_controller_test.rb
@@ -329,6 +329,19 @@ class TasksControllerTest &lt; ActionController::TestCase @@ -329,6 +329,19 @@ class TasksControllerTest &lt; ActionController::TestCase
329 assert_select "#tasks_#{t.id}_task_name" 329 assert_select "#tasks_#{t.id}_task_name"
330 end 330 end
331 331
  332 + should "not crash when article suggestion task fails" do
  333 + TinyMceArticle.destroy_all
  334 + c = fast_create(Community)
  335 + c.affiliate(profile, Profile::Roles.all_roles(profile.environment.id))
  336 + @controller.stubs(:profile).returns(c)
  337 + t = SuggestArticle.create!(:article => {:name => 'test name', :abstract => 'test abstract', :body => 'test body'}, :name => 'some name', :email => 'test@localhost.com', :target => c)
  338 +
  339 + SuggestArticle.any_instance.stubs(:perform).raises('erro')
  340 + assert_nothing_raised do
  341 + post :close, :tasks => {t.id => { :task => {}, :decision => "finish"}}
  342 + end
  343 + end
  344 +
332 should "append hidden tag with type value from article suggestion" do 345 should "append hidden tag with type value from article suggestion" do
333 Task.destroy_all 346 Task.destroy_all
334 c = fast_create(Community) 347 c = fast_create(Community)
test/unit/suggest_article_test.rb
@@ -242,4 +242,9 @@ class SuggestArticleTest &lt; ActiveSupport::TestCase @@ -242,4 +242,9 @@ class SuggestArticleTest &lt; ActiveSupport::TestCase
242 t.article_type == TinyMceArticle 242 t.article_type == TinyMceArticle
243 end 243 end
244 244
  245 + should 'fallback to tinymce when type parameter is blank' do
  246 + t = SuggestArticle.new
  247 + t.article = {:name => 'name', :body => 'body', :type => ''}
  248 + t.article_type == TinyMceArticle
  249 + end
245 end 250 end