From 57a47d2f3ab8b4123ce7cb6867b280979a77640c Mon Sep 17 00:00:00 2001 From: Arthur Del Esposte Date: Mon, 30 Mar 2015 12:38:21 +0000 Subject: [PATCH] Create support for plugins' hotspots --- lib/noosfero/plugin.rb | 22 ++++++++++++++++++++++ plugins/foo/lib/foo_plugin.rb | 20 +++++++++++++++++++- plugins/foo/test/unit/foo_plugin_test.rb | 18 ++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/lib/noosfero/plugin.rb b/lib/noosfero/plugin.rb index cf65a63..b6d51a9 100644 --- a/lib/noosfero/plugin.rb +++ b/lib/noosfero/plugin.rb @@ -8,6 +8,10 @@ class Noosfero::Plugin self.context = context end + def environment + context.environment if self.context + end + class << self attr_writer :should_load @@ -35,6 +39,7 @@ class Noosfero::Plugin # filters must be loaded after all extensions klasses.each do |plugin| load_plugin_filters plugin + load_plugin_hotspots plugin end end @@ -108,6 +113,23 @@ class Noosfero::Plugin end end + # This is a generic method to extend the hotspots list with plugins + # hotspots. This allows plugins to extend other plugins. + # To use this, the plugin must define its hotspots inside a module Hotspots. + # Its also needed to include Noosfero::Plugin::HotSpot module + # in order to dispatch plugins methods. + # + # Checkout FooPlugin for usage example. + def load_plugin_hotspots(plugin) + ActionDispatch::Reloader.to_prepare do + begin + module_name = "#{plugin.name}::Hotspots" + Noosfero::Plugin.send(:include, module_name.constantize) + rescue NameError + end + end + end + def add_controller_filters(controller_class, plugin, filters) unless filters.is_a?(Array) filters = [filters] diff --git a/plugins/foo/lib/foo_plugin.rb b/plugins/foo/lib/foo_plugin.rb index ed1340c..50f3b76 100644 --- a/plugins/foo/lib/foo_plugin.rb +++ b/plugins/foo/lib/foo_plugin.rb @@ -1,4 +1,5 @@ class FooPlugin < Noosfero::Plugin + include Noosfero::Plugin::HotSpot def self.plugin_name "Foo" @@ -8,12 +9,29 @@ class FooPlugin < Noosfero::Plugin _("A sample plugin to test autoload craziness.") end + module Hotspots + # -> Custom foo plugin hotspot + # do something to extend the FooPlugin behaviour + # receive params a, b and c + # returns = boolean or something else + def foo_plugin_my_hotspot(a, b, c) + end + + # -> Custom title for foo profiles tab + # returns = a string with a custom title + def foo_plugin_tab_title + end + end + def control_panel_buttons {:title => 'Foo plugin button', :icon => '', :url => ''} end def profile_tabs - {:title => 'Foo plugin tab', :id => 'foo_plugin', :content => lambda {'Foo plugin random content'} } + title = plugins.dispatch_first(:foo_plugin_tab_title) + title = 'Foo plugin tab' unless title + + {:title => title, :id => 'foo_plugin', :content => lambda {'Foo plugin random content'} } end end diff --git a/plugins/foo/test/unit/foo_plugin_test.rb b/plugins/foo/test/unit/foo_plugin_test.rb index 71d51e0..191d390 100644 --- a/plugins/foo/test/unit/foo_plugin_test.rb +++ b/plugins/foo/test/unit/foo_plugin_test.rb @@ -4,7 +4,25 @@ class FooPluginTest < ActiveSupport::TestCase def test_foo FooPlugin::Bar.create! end + def test_monkey_patch Profile.new.bar end + + should "respond to new hotspots" do + plugin = FooPlugin.new + + assert plugin.respond_to?(:foo_plugin_my_hotspot) + assert plugin.respond_to?(:foo_plugin_tab_title) + end + + should "other plugin respond to new hotspots" do + class TestPlugin < Noosfero::Plugin + end + + plugin = TestPlugin.new + + assert plugin.respond_to?(:foo_plugin_my_hotspot) + assert plugin.respond_to?(:foo_plugin_tab_title) + end end -- libgit2 0.21.2