[noosfero/noosfero][next] 2 commits: Refactoring article extra toolbar buttons

Victor Costa gitlab at gitlab.com
Wed Apr 8 11:28:45 BRT 2015


Victor Costa pushed to next at Noosfero / noosfero


Commits:
d07031fe by Leandro Nunes dos Santos at 2015-04-07T15:54:13Z
Refactoring article extra toolbar buttons

- - - - -
89c5fafb by Victor Costa at 2015-04-08T14:28:37Z
Merge branch 'refactoring_extra_article_toolbar' into 'next'

refactoring article extra toolbar buttons

Refactoring article_extra_toolbar_buttons to make possible to use controllers context in plugins

See merge request !538

- - - - -


6 changed files:

- app/helpers/plugins_helper.rb
- app/views/content_viewer/_article_toolbar.html.erb
- lib/noosfero/plugin.rb
- test/functional/content_viewer_controller_test.rb
- test/unit/plugin_test.rb
- + test/unit/plugins_helper_test.rb


Changes:

=====================================
app/helpers/plugins_helper.rb
=====================================
--- a/app/helpers/plugins_helper.rb
+++ b/app/helpers/plugins_helper.rb
@@ -6,4 +6,11 @@ module PluginsHelper
     end
   end
 
+  def plugins_toolbar_actions_for_article(article)
+    toolbar_actions = Array.wrap(@plugins.dispatch(:article_extra_toolbar_buttons, article))
+    toolbar_actions.each do |action|
+      [:title, :url, :icon].each { |param| raise "No #{param} was passed as parameter for #{action}" unless action.has_key?(param) }
+    end
+  end
+
 end

=====================================
app/views/content_viewer/_article_toolbar.html.erb
=====================================
--- a/app/views/content_viewer/_article_toolbar.html.erb
+++ b/app/views/content_viewer/_article_toolbar.html.erb
@@ -46,11 +46,10 @@
       <%= button(:clock, _('All versions'), {:controller => 'content_viewer', :profile => profile.identifier, :action => 'article_versions'}, :id => 'article-versions-link') %>
     <% end %>
 
-    <% @plugins.dispatch(:article_extra_toolbar_buttons, @page).each do |plugin_button| %>
+    <% plugins_toolbar_actions_for_article(@page).each do |plugin_button| %>
       <%= button plugin_button[:icon], plugin_button[:title], plugin_button[:url], plugin_button[:html_options] %>
     <% end %>
 
-
     <%= report_abuse(profile, :link, @page) %>
 
   </div>

=====================================
lib/noosfero/plugin.rb
=====================================
--- a/lib/noosfero/plugin.rb
+++ b/lib/noosfero/plugin.rb
@@ -687,6 +687,8 @@ class Noosfero::Plugin
     # returns = string with reason of expiration
     elsif method.to_s =~ /^content_expire_(#{content_actions.join('|')})$/
       nil
+    elsif context.respond_to?(method)
+      context.send(method)
     else
       super
     end

=====================================
test/functional/content_viewer_controller_test.rb
=====================================
--- a/test/functional/content_viewer_controller_test.rb
+++ b/test/functional/content_viewer_controller_test.rb
@@ -1479,7 +1479,7 @@ class ContentViewerControllerTest < ActionController::TestCase
   should 'add icon attribute in extra toolbar actions on article from plugins' do
     class Plugin1 < Noosfero::Plugin
       def article_extra_toolbar_buttons(article)
-        {:title => 'some_title1', :icon => 'some_icon1', :url => {}}
+        {:title => 'some_title', :icon => 'some_icon', :url => {}}
       end
     end
     Noosfero::Plugin.stubs(:all).returns([Plugin1.name])
@@ -1489,13 +1489,33 @@ class ContentViewerControllerTest < ActionController::TestCase
     page = profile.articles.create!(:name => 'myarticle', :body => 'the body of the text')
 
     get :view_page, :profile => profile.identifier, :page => [ 'myarticle' ]
-    assert_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :class => /some_icon1/ }}
+    assert_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :class => /some_icon/ }}
   end
 
   should 'add url attribute in extra toolbar actions on article from plugins' do
     class Plugin1 < Noosfero::Plugin
       def article_extra_toolbar_buttons(article)
-        {:title => 'some_title1', :icon => 'some_icon1', :url => '/bli'}
+        {:title => 'some_title', :icon => 'some_icon', :url => '/someurl'}
+      end
+    end
+    Noosfero::Plugin.stubs(:all).returns([Plugin1.name])
+
+    Environment.default.enable_plugin(Plugin1.name)
+
+    page = profile.articles.create!(:name => 'myarticle', :body => 'the body of the text')
+
+    get :view_page, :profile => profile.identifier, :page => [ 'myarticle' ]
+    assert_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :href => "/someurl" }}
+  end
+
+  should 'use context method in extra toolbar actions on article from plugins' do
+    class Plugin1 < Noosfero::Plugin
+      def article_extra_toolbar_buttons(article)
+        if current_person.public?
+          {:title => 'some_title', :icon => 'some_icon', :url => '/someurl'}
+        else
+          {:title => 'another_title', :icon => 'another_icon', :url => '/anotherurl'}
+        end
       end
     end
     Noosfero::Plugin.stubs(:all).returns([Plugin1.name])
@@ -1504,8 +1524,11 @@ class ContentViewerControllerTest < ActionController::TestCase
 
     page = profile.articles.create!(:name => 'myarticle', :body => 'the body of the text')
 
+    profile.public_profile = false
+    profile.save
+    login_as(profile.identifier)
     get :view_page, :profile => profile.identifier, :page => [ 'myarticle' ]
-    assert_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :href => "/bli" }}
+    assert_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :href => "/anotherurl" }}
   end
 
 end

=====================================
test/unit/plugin_test.rb
=====================================
--- a/test/unit/plugin_test.rb
+++ b/test/unit/plugin_test.rb
@@ -561,8 +561,6 @@ class PluginTest < ActiveSupport::TestCase
   end
 
   should 'article_extra_toolbar_buttons return an empty array by default' do
-    class CustomBlock1 < Block; end;
-
     class Plugin1 < Noosfero::Plugin
     end
     p = Plugin1.new

=====================================
test/unit/plugins_helper_test.rb
=====================================
--- /dev/null
+++ b/test/unit/plugins_helper_test.rb
@@ -0,0 +1,47 @@
+require_relative "../test_helper"
+
+class PluginsHelperTest < ActionView::TestCase
+
+  def setup
+    @environment = Environment.default
+    @plugins = mock
+  end
+
+  attr_accessor :environment, :plugins
+
+  should 'plugins_toolbar_actions_for_article return an array if the plugin return a single hash' do
+    hash = {:title => 'some title', :url => 'some_url', :icon => 'some icon'}
+    plugins.expects(:dispatch).with(:article_extra_toolbar_buttons, nil).returns(hash)
+    assert_equal [hash], plugins_toolbar_actions_for_article(nil)
+  end
+
+  should 'plugins_toolbar_actions_for_article return an empty array if an array is passed as parameter' do
+    plugins.expects(:dispatch).with(:article_extra_toolbar_buttons, nil).returns([])
+    assert_equal [], plugins_toolbar_actions_for_article(nil)
+  end
+
+  should 'plugins_toolbar_actions_for_article throw raise if no title is passed as parameter' do
+    plugins.expects(:dispatch).with(:article_extra_toolbar_buttons, nil).returns({:url => 'some_url', :icon => 'some icon'})
+
+    assert_raise(RuntimeError) do
+      plugins_toolbar_actions_for_article(nil)
+    end
+  end
+
+  should 'plugins_toolbar_actions_for_article throw raise if no icon is passed as parameter' do
+    plugins.expects(:dispatch).with(:article_extra_toolbar_buttons, nil).returns({:title => 'some title', :url => 'some_url'})
+
+    assert_raise(RuntimeError) do
+      plugins_toolbar_actions_for_article(nil)
+    end
+  end
+
+  should 'plugins_toolbar_actions_for_article throw raise if no url is passed as parameter' do
+    plugins.expects(:dispatch).with(:article_extra_toolbar_buttons, nil).returns({:title => 'some title', :icon => 'some icon'})
+
+    assert_raise(RuntimeError) do
+      plugins_toolbar_actions_for_article(nil)
+    end
+  end
+
+end


View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/a79d01ab7f327b07409c24efa5e0fba73626d076...89c5fafb842e26296152f8867f995efed4f36d75
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20150408/756e5d28/attachment-0001.html>


More information about the Noosfero-dev mailing list