Commit df46012ff4d74d92a2ba5470e607e3b2724cae7f
1 parent
dfe9d61b
Exists in
master
and in
29 other branches
modularize hot spot behaviour
This way any class can just include Noosfero::Plugin::HotSpot and just invoke the plugins.
Showing
3 changed files
with
40 additions
and
3 deletions
Show diff stats
app/controllers/application_controller.rb
... | ... | @@ -101,9 +101,10 @@ class ApplicationController < ActionController::Base |
101 | 101 | end |
102 | 102 | end |
103 | 103 | |
104 | + include Noosfero::Plugin::HotSpot | |
105 | + | |
104 | 106 | def init_noosfero_plugins |
105 | - @plugins = Noosfero::Plugin::Manager.new(environment, self) | |
106 | - @plugins.each do |plugin| | |
107 | + plugins.each do |plugin| | |
107 | 108 | prepend_view_path(plugin.class.view_path) |
108 | 109 | end |
109 | 110 | init_noosfero_plugins_controller_filters |
... | ... | @@ -112,7 +113,7 @@ class ApplicationController < ActionController::Base |
112 | 113 | # This is a generic method that initialize any possible filter defined by a |
113 | 114 | # plugin to the current controller being initialized. |
114 | 115 | def init_noosfero_plugins_controller_filters |
115 | - @plugins.each do |plugin| | |
116 | + plugins.each do |plugin| | |
116 | 117 | plugin.send(self.class.name.underscore + '_filters').each do |plugin_filter| |
117 | 118 | self.class.send(plugin_filter[:type], plugin.class.name.underscore + '_' + plugin_filter[:method_name], (plugin_filter[:options] || {})) |
118 | 119 | self.class.send(:define_method, plugin.class.name.underscore + '_' + plugin_filter[:method_name], plugin_filter[:block]) | ... | ... |
... | ... | @@ -0,0 +1,18 @@ |
1 | +# This module must be included by classes that contain Noosfero plugin | |
2 | +# hotspots. | |
3 | +# | |
4 | +# Classes that include this module *must* provide a method called | |
5 | +# <tt>environment</tt> which returns an intance of Environment. This | |
6 | +# Environment will be used to determine which plugins are enabled and therefore | |
7 | +# which plugins should be instantiated. | |
8 | +module Noosfero::Plugin::HotSpot | |
9 | + | |
10 | + # Returns an instance of Noosfero::Plugin::Manager. | |
11 | + # | |
12 | + # This which is intantiated on the first call and just returned in subsequent | |
13 | + # calls. | |
14 | + def plugins | |
15 | + @plugins ||= Noosfero::Plugin::Manager.new(environment, self) | |
16 | + end | |
17 | + | |
18 | +end | ... | ... |
... | ... | @@ -0,0 +1,18 @@ |
1 | +require File.dirname(__FILE__) + '/../test_helper' | |
2 | + | |
3 | +class PluginHotSpotTest < ActiveSupport::TestCase | |
4 | + | |
5 | + class Client | |
6 | + include Noosfero::Plugin::HotSpot | |
7 | + end | |
8 | + | |
9 | + def setup | |
10 | + @client = Client.new | |
11 | + @client.stubs(:environment).returns(Environment.new) | |
12 | + end | |
13 | + | |
14 | + should 'instantiate only once' do | |
15 | + assert_same @client.plugins, @client.plugins | |
16 | + end | |
17 | + | |
18 | +end | ... | ... |