Commit 4e7709e72a49cebe84aae9dfc1c0fe00ebc86179

Authored by Leandro Santos
1 parent 17997821
Exists in master

adapting of article_extra_toolbar_buttons refactoring

db/migrate/20140715190749_add_setting_to_comments.rb
@@ -1,9 +0,0 @@ @@ -1,9 +0,0 @@
1 -class AddSettingToComments < ActiveRecord::Migration  
2 - def self.up  
3 - add_column :comments, :setting, :text unless column_exists?(:comments, :setting)  
4 - end  
5 -  
6 - def self.down  
7 - remove_column :comments, :setting  
8 - end  
9 -end  
lib/comment_paragraph_plugin.rb
@@ -44,11 +44,14 @@ class CommentParagraphPlugin &lt; Noosfero::Plugin @@ -44,11 +44,14 @@ class CommentParagraphPlugin &lt; Noosfero::Plugin
44 'manual' 44 'manual'
45 end 45 end
46 46
47 - def article_toolbar_actions(article)  
48 - return unless article.comment_paragraph_plugin_enabled?  
49 - proc do  
50 - button :toggle_comment_paragraph, article.comment_paragraph_plugin_activated? ? _('Deactivate Comments') : _('Activate Comments'), :controller => 'comment_paragraph_plugin_myprofile', :profile => article.profile.identifier, :action => 'toggle_activation', :id => article.id if article.allow_edit?(user)  
51 - end 47 + def article_extra_toolbar_buttons(article)
  48 + return [] if !article.comment_paragraph_plugin_enabled? || !article.allow_edit?(current_person)
  49 + {
  50 + :title => article.comment_paragraph_plugin_activated? ? _('Deactivate Comments') : _('Activate Comments'),
  51 + :url => {:controller => 'comment_paragraph_plugin_myprofile', :profile => article.profile.identifier, :action => 'toggle_activation', :id => article.id},
  52 + :icon => :toggle_comment_paragraph
  53 + }
  54 +
52 end 55 end
53 56
54 end 57 end
test/unit/comment_paragraph_plugin_test.rb
@@ -5,8 +5,10 @@ class CommentParagraphPluginTest &lt; ActiveSupport::TestCase @@ -5,8 +5,10 @@ class CommentParagraphPluginTest &lt; ActiveSupport::TestCase
5 5
6 def setup 6 def setup
7 @environment = Environment.default 7 @environment = Environment.default
8 - @plugin = CommentParagraphPlugin.new  
9 @user = create_user('testuser').person 8 @user = create_user('testuser').person
  9 + context = mock()
  10 + context.stubs(:current_person).returns(@user)
  11 + @plugin = CommentParagraphPlugin.new(context)
10 end 12 end
11 13
12 attr_reader :environment, :plugin, :user 14 attr_reader :environment, :plugin, :user
@@ -42,9 +44,7 @@ class CommentParagraphPluginTest &lt; ActiveSupport::TestCase @@ -42,9 +44,7 @@ class CommentParagraphPluginTest &lt; ActiveSupport::TestCase
42 article.expects(:comment_paragraph_plugin_enabled?).returns(true) 44 article.expects(:comment_paragraph_plugin_enabled?).returns(true)
43 article.expects(:allow_edit?).with(user).returns(true) 45 article.expects(:allow_edit?).with(user).returns(true)
44 46
45 - content = plugin.article_toolbar_actions(article)  
46 - expects(:button).once  
47 - instance_eval(&content) 47 + assert_not_equal [], plugin.article_extra_toolbar_buttons(article)
48 end 48 end
49 49
50 should 'not display button to toggle comment paragraph for users which can not edit the article' do 50 should 'not display button to toggle comment paragraph for users which can not edit the article' do
@@ -53,8 +53,7 @@ class CommentParagraphPluginTest &lt; ActiveSupport::TestCase @@ -53,8 +53,7 @@ class CommentParagraphPluginTest &lt; ActiveSupport::TestCase
53 article.expects(:comment_paragraph_plugin_enabled?).returns(true) 53 article.expects(:comment_paragraph_plugin_enabled?).returns(true)
54 article.expects(:allow_edit?).with(user).returns(false) 54 article.expects(:allow_edit?).with(user).returns(false)
55 55
56 - content = plugin.article_toolbar_actions(article)  
57 - assert_equal nil, instance_eval(&content) 56 + assert_equal [], plugin.article_extra_toolbar_buttons(article)
58 end 57 end
59 58
60 should 'not display button to toggle comment paragraph if plugin is not enabled' do 59 should 'not display button to toggle comment paragraph if plugin is not enabled' do
@@ -62,7 +61,27 @@ class CommentParagraphPluginTest &lt; ActiveSupport::TestCase @@ -62,7 +61,27 @@ class CommentParagraphPluginTest &lt; ActiveSupport::TestCase
62 article = fast_create(Article, :profile_id => profile.id) 61 article = fast_create(Article, :profile_id => profile.id)
63 article.expects(:comment_paragraph_plugin_enabled?).returns(false) 62 article.expects(:comment_paragraph_plugin_enabled?).returns(false)
64 63
65 - assert_equal nil, plugin.article_toolbar_actions(article) 64 + assert_equal [], plugin.article_extra_toolbar_buttons(article)
  65 + end
  66 +
  67 + should 'display Activate Comments title if comment paragraph plugin is activated' do
  68 + profile = fast_create(Profile)
  69 + article = fast_create(Article, :profile_id => profile.id)
  70 + article.expects(:comment_paragraph_plugin_enabled?).returns(true)
  71 + article.expects(:allow_edit?).with(user).returns(true)
  72 + article.expects(:comment_paragraph_plugin_activated?).returns(false)
  73 +
  74 + assert_equal 'Activate Comments', plugin.article_extra_toolbar_buttons(article)[:title]
  75 + end
  76 +
  77 + should 'display Deactivate Comments title if comment paragraph plugin is deactivated' do
  78 + profile = fast_create(Profile)
  79 + article = fast_create(Article, :profile_id => profile.id)
  80 + article.expects(:comment_paragraph_plugin_enabled?).returns(true)
  81 + article.expects(:allow_edit?).with(user).returns(true)
  82 + article.expects(:comment_paragraph_plugin_activated?).returns(true)
  83 +
  84 + assert_equal 'Deactivate Comments', plugin.article_extra_toolbar_buttons(article)[:title]
66 end 85 end
67 86
68 end 87 end