From df46012ff4d74d92a2ba5470e607e3b2724cae7f Mon Sep 17 00:00:00 2001 From: Antonio Terceiro Date: Thu, 23 Aug 2012 11:20:06 -0300 Subject: [PATCH] modularize hot spot behaviour --- app/controllers/application_controller.rb | 7 ++++--- lib/noosfero/plugin/hot_spot.rb | 18 ++++++++++++++++++ test/unit/plugin_hot_spot_test.rb | 18 ++++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 lib/noosfero/plugin/hot_spot.rb create mode 100644 test/unit/plugin_hot_spot_test.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index e2a3c19..0888f6b 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -101,9 +101,10 @@ class ApplicationController < ActionController::Base end end + include Noosfero::Plugin::HotSpot + def init_noosfero_plugins - @plugins = Noosfero::Plugin::Manager.new(environment, self) - @plugins.each do |plugin| + plugins.each do |plugin| prepend_view_path(plugin.class.view_path) end init_noosfero_plugins_controller_filters @@ -112,7 +113,7 @@ class ApplicationController < ActionController::Base # This is a generic method that initialize any possible filter defined by a # plugin to the current controller being initialized. def init_noosfero_plugins_controller_filters - @plugins.each do |plugin| + plugins.each do |plugin| plugin.send(self.class.name.underscore + '_filters').each do |plugin_filter| self.class.send(plugin_filter[:type], plugin.class.name.underscore + '_' + plugin_filter[:method_name], (plugin_filter[:options] || {})) self.class.send(:define_method, plugin.class.name.underscore + '_' + plugin_filter[:method_name], plugin_filter[:block]) diff --git a/lib/noosfero/plugin/hot_spot.rb b/lib/noosfero/plugin/hot_spot.rb new file mode 100644 index 0000000..fd683f9 --- /dev/null +++ b/lib/noosfero/plugin/hot_spot.rb @@ -0,0 +1,18 @@ +# This module must be included by classes that contain Noosfero plugin +# hotspots. +# +# Classes that include this module *must* provide a method called +# environment which returns an intance of Environment. This +# Environment will be used to determine which plugins are enabled and therefore +# which plugins should be instantiated. +module Noosfero::Plugin::HotSpot + + # Returns an instance of Noosfero::Plugin::Manager. + # + # This which is intantiated on the first call and just returned in subsequent + # calls. + def plugins + @plugins ||= Noosfero::Plugin::Manager.new(environment, self) + end + +end diff --git a/test/unit/plugin_hot_spot_test.rb b/test/unit/plugin_hot_spot_test.rb new file mode 100644 index 0000000..62a8e8d --- /dev/null +++ b/test/unit/plugin_hot_spot_test.rb @@ -0,0 +1,18 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class PluginHotSpotTest < ActiveSupport::TestCase + + class Client + include Noosfero::Plugin::HotSpot + end + + def setup + @client = Client.new + @client.stubs(:environment).returns(Environment.new) + end + + should 'instantiate only once' do + assert_same @client.plugins, @client.plugins + end + +end -- libgit2 0.21.2