Commit 57a47d2f3ab8b4123ce7cb6867b280979a77640c

Authored by Arthur Esposte
1 parent 43a702b4

Create support for plugins' hotspots

lib/noosfero/plugin.rb
... ... @@ -8,6 +8,10 @@ class Noosfero::Plugin
8 8 self.context = context
9 9 end
10 10  
  11 + def environment
  12 + context.environment if self.context
  13 + end
  14 +
11 15 class << self
12 16  
13 17 attr_writer :should_load
... ... @@ -35,6 +39,7 @@ class Noosfero::Plugin
35 39 # filters must be loaded after all extensions
36 40 klasses.each do |plugin|
37 41 load_plugin_filters plugin
  42 + load_plugin_hotspots plugin
38 43 end
39 44 end
40 45  
... ... @@ -108,6 +113,23 @@ class Noosfero::Plugin
108 113 end
109 114 end
110 115  
  116 + # This is a generic method to extend the hotspots list with plugins
  117 + # hotspots. This allows plugins to extend other plugins.
  118 + # To use this, the plugin must define its hotspots inside a module Hotspots.
  119 + # Its also needed to include Noosfero::Plugin::HotSpot module
  120 + # in order to dispatch plugins methods.
  121 + #
  122 + # Checkout FooPlugin for usage example.
  123 + def load_plugin_hotspots(plugin)
  124 + ActionDispatch::Reloader.to_prepare do
  125 + begin
  126 + module_name = "#{plugin.name}::Hotspots"
  127 + Noosfero::Plugin.send(:include, module_name.constantize)
  128 + rescue NameError
  129 + end
  130 + end
  131 + end
  132 +
111 133 def add_controller_filters(controller_class, plugin, filters)
112 134 unless filters.is_a?(Array)
113 135 filters = [filters]
... ...
plugins/foo/lib/foo_plugin.rb
1 1 class FooPlugin < Noosfero::Plugin
  2 + include Noosfero::Plugin::HotSpot
2 3  
3 4 def self.plugin_name
4 5 "Foo"
... ... @@ -8,12 +9,29 @@ class FooPlugin &lt; Noosfero::Plugin
8 9 _("A sample plugin to test autoload craziness.")
9 10 end
10 11  
  12 + module Hotspots
  13 + # -> Custom foo plugin hotspot
  14 + # do something to extend the FooPlugin behaviour
  15 + # receive params a, b and c
  16 + # returns = boolean or something else
  17 + def foo_plugin_my_hotspot(a, b, c)
  18 + end
  19 +
  20 + # -> Custom title for foo profiles tab
  21 + # returns = a string with a custom title
  22 + def foo_plugin_tab_title
  23 + end
  24 + end
  25 +
11 26 def control_panel_buttons
12 27 {:title => 'Foo plugin button', :icon => '', :url => ''}
13 28 end
14 29  
15 30 def profile_tabs
16   - {:title => 'Foo plugin tab', :id => 'foo_plugin', :content => lambda {'Foo plugin random content'} }
  31 + title = plugins.dispatch_first(:foo_plugin_tab_title)
  32 + title = 'Foo plugin tab' unless title
  33 +
  34 + {:title => title, :id => 'foo_plugin', :content => lambda {'Foo plugin random content'} }
17 35 end
18 36  
19 37 end
... ...
plugins/foo/test/unit/foo_plugin_test.rb
... ... @@ -4,7 +4,25 @@ class FooPluginTest &lt; ActiveSupport::TestCase
4 4 def test_foo
5 5 FooPlugin::Bar.create!
6 6 end
  7 +
7 8 def test_monkey_patch
8 9 Profile.new.bar
9 10 end
  11 +
  12 + should "respond to new hotspots" do
  13 + plugin = FooPlugin.new
  14 +
  15 + assert plugin.respond_to?(:foo_plugin_my_hotspot)
  16 + assert plugin.respond_to?(:foo_plugin_tab_title)
  17 + end
  18 +
  19 + should "other plugin respond to new hotspots" do
  20 + class TestPlugin < Noosfero::Plugin
  21 + end
  22 +
  23 + plugin = TestPlugin.new
  24 +
  25 + assert plugin.respond_to?(:foo_plugin_my_hotspot)
  26 + assert plugin.respond_to?(:foo_plugin_tab_title)
  27 + end
10 28 end
... ...