- <% plugins_tabs = @plugins.map(:profile_tabs).
+ <% plugins_tabs = @plugins.dispatch(:profile_tabs).
map { |tab| {:title => tab[:title], :id => tab[:id], :content => instance_eval(&tab[:content]), :start => tab[:title]} }%>
<% tabs = plugins_tabs.select { |tab| tab[:start] } %>
diff --git a/app/views/profile_editor/edit.rhtml b/app/views/profile_editor/edit.rhtml
index d761f75..d43eb79 100644
--- a/app/views/profile_editor/edit.rhtml
+++ b/app/views/profile_editor/edit.rhtml
@@ -82,7 +82,7 @@
)%>
<%=
- @plugins.map(:profile_editor_extras).each do |content|
+ @plugins.dispatch(:profile_editor_extras).each do |content|
content.respond_to?(:call) ? content.call : content
end.join("\n")
%>
diff --git a/app/views/profile_editor/index.rhtml b/app/views/profile_editor/index.rhtml
index 4ff40f5..96567ec 100644
--- a/app/views/profile_editor/index.rhtml
+++ b/app/views/profile_editor/index.rhtml
@@ -66,7 +66,7 @@
<%= control_panel_button(_('Manage my groups'), 'groups', :controller => 'memberships') if profile.person? %>
- <% @plugins.map(:control_panel_buttons).each do |button| %>
+ <% @plugins.dispatch(:control_panel_buttons).each do |button| %>
<%= control_panel_button(button[:title], button[:icon], button[:url]) %>
<% end %>
diff --git a/app/views/profile_members/_index_buttons.rhtml b/app/views/profile_members/_index_buttons.rhtml
index c81ad88..c94e889 100644
--- a/app/views/profile_members/_index_buttons.rhtml
+++ b/app/views/profile_members/_index_buttons.rhtml
@@ -5,7 +5,7 @@
<%= button :search, _('Invite your friends to join %s') % profile.short_name, :controller => 'invite', :action => 'select_address_book' %>
<% end %>
<%= button :send, _('Send e-mail to members'), :action => 'send_mail' %>
- <% @plugins.map(:manage_members_extra_buttons).each do |plugin_button| %>
+ <% @plugins.dispatch(:manage_members_extra_buttons).each do |plugin_button| %>
<%= button plugin_button[:icon], plugin_button[:title], plugin_button[:url] %>
<% end %>
<% end %>
diff --git a/app/views/search/_product.rhtml b/app/views/search/_product.rhtml
index 265720e..ad6ee72 100644
--- a/app/views/search/_product.rhtml
+++ b/app/views/search/_product.rhtml
@@ -5,8 +5,8 @@ product_item_pos = 0 if ! product_item_pos
product_item_pos += 1
%>
-<% extra_content = @plugins.map(:asset_product_extras, product, product.enterprise).collect { |content| instance_eval(&content) } %>
-<% extra_properties = @plugins.map(:asset_product_properties, product)%>
+<% extra_content = @plugins.dispatch(:asset_product_extras, product, product.enterprise).collect { |content| instance_eval(&content) } %>
+<% extra_properties = @plugins.dispatch(:asset_product_properties, product)%>
<%= link_to_product product, :class => 'product-pic', :style => 'background-image:url(%s)' % product.default_image(:minor) %>
diff --git a/lib/noosfero/plugin/manager.rb b/lib/noosfero/plugin/manager.rb
index 3cbbca9..f49369c 100644
--- a/lib/noosfero/plugin/manager.rb
+++ b/lib/noosfero/plugin/manager.rb
@@ -6,12 +6,19 @@ class Noosfero::Plugin::Manager
@context = Noosfero::Plugin::Context.new(controller)
end
- def map(event, *args)
- enabled_plugins.map { |plugin| plugin.send(event, *args) }.compact.flatten
- end
+ delegate :each, :to => :enabled_plugins
+ include Enumerable
- def collect(&block)
- enabled_plugins.collect(&block)
+ # Dispatches +event+ to each enabled plugin and collect the results.
+ #
+ # Returns an Array containing the objects returned by the event method in
+ # each plugin. This array is compacted (i.e. nils are removed) and flattened
+ # (i.e. elements of arrays are added to the resulting array). For example, if
+ # the enabled plugins return 1, 0, nil, and [1,2,3], then this method will
+ # return [1,0,1,2,3]
+ #
+ def dispatch(event, *args)
+ map { |plugin| plugin.send(event, *args) }.compact.flatten
end
def enabled_plugins
diff --git a/test/functional/admin_panel_controller_test.rb b/test/functional/admin_panel_controller_test.rb
index cf06b2b..d906a9a 100644
--- a/test/functional/admin_panel_controller_test.rb
+++ b/test/functional/admin_panel_controller_test.rb
@@ -332,20 +332,23 @@ class AdminPanelControllerTest < ActionController::TestCase
end
should 'display plugins links' do
- plugin1_link = {:title => 'Plugin1 link', :url => 'plugin1.com'}
- plugin2_link = {:title => 'Plugin2 link', :url => 'plugin2.com'}
- links = [plugin1_link, plugin2_link]
- plugins = mock()
- plugins.stubs(:map).with(:admin_panel_links).returns(links)
- plugins.stubs(:enabled_plugins).returns([])
- plugins.stubs(:map).with(:body_beginning).returns([])
- plugins.stubs(:map).with(:head_ending).returns([])
- Noosfero::Plugin::Manager.stubs(:new).returns(plugins)
+ class TestAdminPanelLinks1 < Noosfero::Plugin
+ def admin_panel_links
+ {:title => 'Plugin1 link', :url => 'plugin1.com'}
+ end
+ end
+ class TestAdminPanelLinks2 < Noosfero::Plugin
+ def admin_panel_links
+ {:title => 'Plugin2 link', :url => 'plugin2.com'}
+ end
+ end
+
+ Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([TestAdminPanelLinks1.new, TestAdminPanelLinks2.new])
get :index
- assert_tag :tag => 'a', :content => /#{plugin1_link[:title]}/, :attributes => {:href => /#{plugin1_link[:url]}/}
- assert_tag :tag => 'a', :content => /#{plugin2_link[:title]}/, :attributes => {:href => /#{plugin2_link[:url]}/}
+ assert_tag :tag => 'a', :content => /Plugin1 link/, :attributes => {:href => /plugin1.com/}
+ assert_tag :tag => 'a', :content => /Plugin2 link/, :attributes => {:href => /plugin2.com/}
end
end
diff --git a/test/functional/application_controller_test.rb b/test/functional/application_controller_test.rb
index 8f9ae1f..508ff8c 100644
--- a/test/functional/application_controller_test.rb
+++ b/test/functional/application_controller_test.rb
@@ -392,38 +392,49 @@ class ApplicationControllerTest < ActionController::TestCase
end
should 'include content in the beginning of body supplied by plugins regardless it is a block or html code' do
- plugin1_local_variable = "Plugin1"
- plugin1_content = lambda {"This is #{plugin1_local_variable} speaking!"}
- plugin2_content = "This is Plugin2 speaking!"
- contents = [plugin1_content, plugin2_content]
+ class TestBodyBeginning1Plugin < Noosfero::Plugin
+ def plugin1_method
+ '[[plugin1]]'
+ end
+ def body_beginning
+ lambda {"This is #{plugin1_method} speaking!"}
+ end
+ end
+ class TestBodyBeginning2Plugin < Noosfero::Plugin
+ def body_beginning
+ "This is Plugin2 speaking!"
+ end
+ end
- plugins = mock()
- plugins.stubs(:enabled_plugins).returns([])
- plugins.stubs(:map).with(:body_beginning).returns(contents)
- plugins.stubs(:map).with(:head_ending).returns([])
- Noosfero::Plugin::Manager.stubs(:new).returns(plugins)
+ Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([TestBodyBeginning1Plugin.new, TestBodyBeginning2Plugin.new])
get :index
- assert_tag :tag => 'span', :content => 'This is ' + plugin1_local_variable + ' speaking!', :attributes => {:id => 'plugin1'}
+ assert_tag :tag => 'span', :content => 'This is [[plugin1]] speaking!', :attributes => {:id => 'plugin1'}
assert_tag :tag => 'span', :content => 'This is Plugin2 speaking!', :attributes => {:id => 'plugin2'}
end
should 'include content in the ending of head supplied by plugins regardless it is a block or html code' do
- plugin1_local_variable = "Plugin1"
- plugin1_content = lambda {""}
- plugin2_content = ""
- contents = [plugin1_content, plugin2_content]
- plugins = mock()
- plugins.stubs(:enabled_plugins).returns([])
- plugins.stubs(:map).with(:head_ending).returns(contents)
- plugins.stubs(:map).with(:body_beginning).returns([])
- Noosfero::Plugin::Manager.stubs(:new).returns(plugins)
+ class TestHeadEnding1Plugin < Noosfero::Plugin
+ def plugin1_method
+ '[[plugin1]]'
+ end
+ def head_ending
+ lambda {""}
+ end
+ end
+ class TestHeadEnding2Plugin < Noosfero::Plugin
+ def head_ending
+ ""
+ end
+ end
+
+ Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([TestHeadEnding1Plugin.new, TestHeadEnding2Plugin.new])
get :index
- assert_tag :tag => 'script', :content => "alert('This is #{plugin1_local_variable} speaking!')"
+ assert_tag :tag => 'script', :content => "alert('This is [[plugin1]] speaking!')"
assert_tag :tag => 'style', :content => 'This is Plugin2 speaking!'
end
diff --git a/test/functional/cms_controller_test.rb b/test/functional/cms_controller_test.rb
index d7e1baa..4eb98da 100644
--- a/test/functional/cms_controller_test.rb
+++ b/test/functional/cms_controller_test.rb
@@ -1509,14 +1509,13 @@ class CmsControllerTest < ActionController::TestCase
end
should 'include new contents special types from plugins' do
- types = [Integer, Float]
- plugins = mock()
-
- Noosfero::Plugin::Manager.expects(:new).with(@controller).returns(plugins).times(2)
- plugins.stubs(:map).with(:content_types).returns(types)
- plugins.stubs(:map).with(:body_beginning).returns([])
- plugins.stubs(:map).with(:head_ending).returns([])
- plugins.stubs(:enabled_plugins).returns([])
+ class TestContentTypesPlugin < Noosfero::Plugin
+ def content_types
+ [Integer, Float]
+ end
+ end
+
+ Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([TestContentTypesPlugin.new])
get :index, :profile => profile.identifier
diff --git a/test/functional/manage_products_controller_test.rb b/test/functional/manage_products_controller_test.rb
index 190109a..1395e4d 100644
--- a/test/functional/manage_products_controller_test.rb
+++ b/test/functional/manage_products_controller_test.rb
@@ -437,24 +437,25 @@ class ManageProductsControllerTest < ActionController::TestCase
end
should 'include extra content supplied by plugins on products info extras' do
+ class TestProductInfoExtras1Plugin < Noosfero::Plugin
+ def product_info_extras(p)
+ lambda {"This is Plugin1 speaking!"}
+ end
+ end
+ class TestProductInfoExtras2Plugin < Noosfero::Plugin
+ def product_info_extras(p)
+ lambda { "This is Plugin2 speaking!" }
+ end
+ end
+
product = fast_create(Product, :enterprise_id => @enterprise.id)
- plugin1_local_variable = "Plugin1"
- plugin1_content = lambda {"This is #{plugin1_local_variable} speaking!"}
- plugin2_local_variable = "Plugin2"
- plugin2_content = lambda {"This is #{plugin2_local_variable} speaking!"}
- contents = [plugin1_content, plugin2_content]
-
- plugins = mock()
- plugins.stubs(:enabled_plugins).returns([])
- plugins.stubs(:map).with(:body_beginning).returns([])
- plugins.stubs(:map).with(:head_ending).returns([])
- plugins.stubs(:map).with(:product_info_extras, product).returns(contents)
- Noosfero::Plugin::Manager.stubs(:new).returns(plugins)
+
+ Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([TestProductInfoExtras1Plugin.new, TestProductInfoExtras2Plugin.new])
get :show, :id => product.id, :profile => @enterprise.identifier
- assert_tag :tag => 'span', :content => 'This is ' + plugin1_local_variable + ' speaking!', :attributes => {:id => 'plugin1'}
- assert_tag :tag => 'span', :content => 'This is ' + plugin2_local_variable + ' speaking!', :attributes => {:id => 'plugin2'}
+ assert_tag :tag => 'span', :content => 'This is Plugin1 speaking!', :attributes => {:id => 'plugin1'}
+ assert_tag :tag => 'span', :content => 'This is Plugin2 speaking!', :attributes => {:id => 'plugin2'}
end
should 'not allow product creation for profiles that can\'t do it' do
diff --git a/test/functional/profile_editor_controller_test.rb b/test/functional/profile_editor_controller_test.rb
index a1caf14..5b6024a 100644
--- a/test/functional/profile_editor_controller_test.rb
+++ b/test/functional/profile_editor_controller_test.rb
@@ -864,30 +864,34 @@ class ProfileEditorControllerTest < ActionController::TestCase
end
should 'display plugins buttons on the control panel' do
- plugin1_button = {:title => "Plugin1 button", :icon => 'plugin1_icon', :url => 'plugin1_url'}
- plugin2_button = {:title => "Plugin2 button", :icon => 'plugin2_icon', :url => 'plugin2_url'}
- buttons = [plugin1_button, plugin2_button]
- plugins = mock()
- plugins.stubs(:map).with(:control_panel_buttons).returns(buttons)
- plugins.stubs(:enabled_plugins).returns([])
- plugins.stubs(:map).with(:body_beginning).returns([])
- plugins.stubs(:map).with(:head_ending).returns([])
- Noosfero::Plugin::Manager.stubs(:new).returns(plugins)
+
+ class TestControlPanelButtons1 < Noosfero::Plugin
+ def control_panel_buttons
+ {:title => "Plugin1 button", :icon => 'plugin1_icon', :url => 'plugin1_url'}
+ end
+ end
+ class TestControlPanelButtons2 < Noosfero::Plugin
+ def control_panel_buttons
+ {:title => "Plugin2 button", :icon => 'plugin2_icon', :url => 'plugin2_url'}
+ end
+ end
+
+ Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([TestControlPanelButtons1.new, TestControlPanelButtons2.new])
get :index, :profile => profile.identifier
- assert_tag :tag => 'a', :content => plugin1_button[:title], :attributes => {:class => /#{plugin1_button[:icon]}/, :href => /#{plugin1_button[:url]}/}
- assert_tag :tag => 'a', :content => plugin2_button[:title], :attributes => {:class => /#{plugin2_button[:icon]}/, :href => /#{plugin2_button[:url]}/}
+ assert_tag :tag => 'a', :content => 'Plugin1 button', :attributes => {:class => /plugin1_icon/, :href => /plugin1_url/}
+ assert_tag :tag => 'a', :content => 'Plugin2 button', :attributes => {:class => /plugin2_icon/, :href => /plugin2_url/}
end
should 'add extra content provided by plugins on edit' do
- plugin1_content = ""
- plugins = mock()
- plugins.stubs(:enabled_plugins).returns([])
- plugins.stubs(:map).with(:profile_editor_extras).returns([plugin1_content])
- plugins.stubs(:map).with(:head_ending).returns([])
- plugins.stubs(:map).with(:body_beginning).returns([])
- Noosfero::Plugin::Manager.stubs(:new).returns(plugins)
+ class TestProfileEditPlugin < Noosfero::Plugin
+ def profile_editor_extras
+ ""
+ end
+ end
+
+ Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([TestProfileEditPlugin.new])
get :edit, :profile => profile.identifier
diff --git a/test/unit/plugin_manager_test.rb b/test/unit/plugin_manager_test.rb
index 5bd40fe..a984dba 100644
--- a/test/unit/plugin_manager_test.rb
+++ b/test/unit/plugin_manager_test.rb
@@ -51,7 +51,7 @@ class PluginManagerTest < ActiveSupport::TestCase
p1 = Plugin1.new
p2 = Plugin2.new
- assert_equal [p1.random_event, p2.random_event], manager.map(:random_event)
+ assert_equal [p1.random_event, p2.random_event], manager.dispatch(:random_event)
end
end
--
libgit2 0.21.2 |