Commit d07031fe9e22764559f563d67be1b3fae300d14e

Authored by Leandro Santos
Committed by Victor Costa
1 parent a79d01ab

Refactoring article extra toolbar buttons

app/helpers/plugins_helper.rb
@@ -6,4 +6,11 @@ module PluginsHelper @@ -6,4 +6,11 @@ module PluginsHelper
6 end 6 end
7 end 7 end
8 8
  9 + def plugins_toolbar_actions_for_article(article)
  10 + toolbar_actions = Array.wrap(@plugins.dispatch(:article_extra_toolbar_buttons, article))
  11 + toolbar_actions.each do |action|
  12 + [:title, :url, :icon].each { |param| raise "No #{param} was passed as parameter for #{action}" unless action.has_key?(param) }
  13 + end
  14 + end
  15 +
9 end 16 end
app/views/content_viewer/_article_toolbar.html.erb
@@ -46,11 +46,10 @@ @@ -46,11 +46,10 @@
46 <%= button(:clock, _('All versions'), {:controller => 'content_viewer', :profile => profile.identifier, :action => 'article_versions'}, :id => 'article-versions-link') %> 46 <%= button(:clock, _('All versions'), {:controller => 'content_viewer', :profile => profile.identifier, :action => 'article_versions'}, :id => 'article-versions-link') %>
47 <% end %> 47 <% end %>
48 48
49 - <% @plugins.dispatch(:article_extra_toolbar_buttons, @page).each do |plugin_button| %> 49 + <% plugins_toolbar_actions_for_article(@page).each do |plugin_button| %>
50 <%= button plugin_button[:icon], plugin_button[:title], plugin_button[:url], plugin_button[:html_options] %> 50 <%= button plugin_button[:icon], plugin_button[:title], plugin_button[:url], plugin_button[:html_options] %>
51 <% end %> 51 <% end %>
52 52
53 -  
54 <%= report_abuse(profile, :link, @page) %> 53 <%= report_abuse(profile, :link, @page) %>
55 54
56 </div> 55 </div>
lib/noosfero/plugin.rb
@@ -687,6 +687,8 @@ class Noosfero::Plugin @@ -687,6 +687,8 @@ class Noosfero::Plugin
687 # returns = string with reason of expiration 687 # returns = string with reason of expiration
688 elsif method.to_s =~ /^content_expire_(#{content_actions.join('|')})$/ 688 elsif method.to_s =~ /^content_expire_(#{content_actions.join('|')})$/
689 nil 689 nil
  690 + elsif context.respond_to?(method)
  691 + context.send(method)
690 else 692 else
691 super 693 super
692 end 694 end
test/functional/content_viewer_controller_test.rb
@@ -1479,7 +1479,7 @@ class ContentViewerControllerTest &lt; ActionController::TestCase @@ -1479,7 +1479,7 @@ class ContentViewerControllerTest &lt; ActionController::TestCase
1479 should 'add icon attribute in extra toolbar actions on article from plugins' do 1479 should 'add icon attribute in extra toolbar actions on article from plugins' do
1480 class Plugin1 < Noosfero::Plugin 1480 class Plugin1 < Noosfero::Plugin
1481 def article_extra_toolbar_buttons(article) 1481 def article_extra_toolbar_buttons(article)
1482 - {:title => 'some_title1', :icon => 'some_icon1', :url => {}} 1482 + {:title => 'some_title', :icon => 'some_icon', :url => {}}
1483 end 1483 end
1484 end 1484 end
1485 Noosfero::Plugin.stubs(:all).returns([Plugin1.name]) 1485 Noosfero::Plugin.stubs(:all).returns([Plugin1.name])
@@ -1489,13 +1489,33 @@ class ContentViewerControllerTest &lt; ActionController::TestCase @@ -1489,13 +1489,33 @@ class ContentViewerControllerTest &lt; ActionController::TestCase
1489 page = profile.articles.create!(:name => 'myarticle', :body => 'the body of the text') 1489 page = profile.articles.create!(:name => 'myarticle', :body => 'the body of the text')
1490 1490
1491 get :view_page, :profile => profile.identifier, :page => [ 'myarticle' ] 1491 get :view_page, :profile => profile.identifier, :page => [ 'myarticle' ]
1492 - assert_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :class => /some_icon1/ }} 1492 + assert_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :class => /some_icon/ }}
1493 end 1493 end
1494 1494
1495 should 'add url attribute in extra toolbar actions on article from plugins' do 1495 should 'add url attribute in extra toolbar actions on article from plugins' do
1496 class Plugin1 < Noosfero::Plugin 1496 class Plugin1 < Noosfero::Plugin
1497 def article_extra_toolbar_buttons(article) 1497 def article_extra_toolbar_buttons(article)
1498 - {:title => 'some_title1', :icon => 'some_icon1', :url => '/bli'} 1498 + {:title => 'some_title', :icon => 'some_icon', :url => '/someurl'}
  1499 + end
  1500 + end
  1501 + Noosfero::Plugin.stubs(:all).returns([Plugin1.name])
  1502 +
  1503 + Environment.default.enable_plugin(Plugin1.name)
  1504 +
  1505 + page = profile.articles.create!(:name => 'myarticle', :body => 'the body of the text')
  1506 +
  1507 + get :view_page, :profile => profile.identifier, :page => [ 'myarticle' ]
  1508 + assert_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :href => "/someurl" }}
  1509 + end
  1510 +
  1511 + should 'use context method in extra toolbar actions on article from plugins' do
  1512 + class Plugin1 < Noosfero::Plugin
  1513 + def article_extra_toolbar_buttons(article)
  1514 + if current_person.public?
  1515 + {:title => 'some_title', :icon => 'some_icon', :url => '/someurl'}
  1516 + else
  1517 + {:title => 'another_title', :icon => 'another_icon', :url => '/anotherurl'}
  1518 + end
1499 end 1519 end
1500 end 1520 end
1501 Noosfero::Plugin.stubs(:all).returns([Plugin1.name]) 1521 Noosfero::Plugin.stubs(:all).returns([Plugin1.name])
@@ -1504,8 +1524,11 @@ class ContentViewerControllerTest &lt; ActionController::TestCase @@ -1504,8 +1524,11 @@ class ContentViewerControllerTest &lt; ActionController::TestCase
1504 1524
1505 page = profile.articles.create!(:name => 'myarticle', :body => 'the body of the text') 1525 page = profile.articles.create!(:name => 'myarticle', :body => 'the body of the text')
1506 1526
  1527 + profile.public_profile = false
  1528 + profile.save
  1529 + login_as(profile.identifier)
1507 get :view_page, :profile => profile.identifier, :page => [ 'myarticle' ] 1530 get :view_page, :profile => profile.identifier, :page => [ 'myarticle' ]
1508 - assert_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :href => "/bli" }} 1531 + assert_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :href => "/anotherurl" }}
1509 end 1532 end
1510 1533
1511 end 1534 end
test/unit/plugin_test.rb
@@ -561,8 +561,6 @@ class PluginTest &lt; ActiveSupport::TestCase @@ -561,8 +561,6 @@ class PluginTest &lt; ActiveSupport::TestCase
561 end 561 end
562 562
563 should 'article_extra_toolbar_buttons return an empty array by default' do 563 should 'article_extra_toolbar_buttons return an empty array by default' do
564 - class CustomBlock1 < Block; end;  
565 -  
566 class Plugin1 < Noosfero::Plugin 564 class Plugin1 < Noosfero::Plugin
567 end 565 end
568 p = Plugin1.new 566 p = Plugin1.new
test/unit/plugins_helper_test.rb 0 → 100644
@@ -0,0 +1,47 @@ @@ -0,0 +1,47 @@
  1 +require_relative "../test_helper"
  2 +
  3 +class PluginsHelperTest < ActionView::TestCase
  4 +
  5 + def setup
  6 + @environment = Environment.default
  7 + @plugins = mock
  8 + end
  9 +
  10 + attr_accessor :environment, :plugins
  11 +
  12 + should 'plugins_toolbar_actions_for_article return an array if the plugin return a single hash' do
  13 + hash = {:title => 'some title', :url => 'some_url', :icon => 'some icon'}
  14 + plugins.expects(:dispatch).with(:article_extra_toolbar_buttons, nil).returns(hash)
  15 + assert_equal [hash], plugins_toolbar_actions_for_article(nil)
  16 + end
  17 +
  18 + should 'plugins_toolbar_actions_for_article return an empty array if an array is passed as parameter' do
  19 + plugins.expects(:dispatch).with(:article_extra_toolbar_buttons, nil).returns([])
  20 + assert_equal [], plugins_toolbar_actions_for_article(nil)
  21 + end
  22 +
  23 + should 'plugins_toolbar_actions_for_article throw raise if no title is passed as parameter' do
  24 + plugins.expects(:dispatch).with(:article_extra_toolbar_buttons, nil).returns({:url => 'some_url', :icon => 'some icon'})
  25 +
  26 + assert_raise(RuntimeError) do
  27 + plugins_toolbar_actions_for_article(nil)
  28 + end
  29 + end
  30 +
  31 + should 'plugins_toolbar_actions_for_article throw raise if no icon is passed as parameter' do
  32 + plugins.expects(:dispatch).with(:article_extra_toolbar_buttons, nil).returns({:title => 'some title', :url => 'some_url'})
  33 +
  34 + assert_raise(RuntimeError) do
  35 + plugins_toolbar_actions_for_article(nil)
  36 + end
  37 + end
  38 +
  39 + should 'plugins_toolbar_actions_for_article throw raise if no url is passed as parameter' do
  40 + plugins.expects(:dispatch).with(:article_extra_toolbar_buttons, nil).returns({:title => 'some title', :icon => 'some icon'})
  41 +
  42 + assert_raise(RuntimeError) do
  43 + plugins_toolbar_actions_for_article(nil)
  44 + end
  45 + end
  46 +
  47 +end