diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index e4a2a11..ab3c9cf 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -135,9 +135,15 @@ class ApplicationController < ActionController::Base plugins.each do |plugin| filters = plugin.send(self.class.name.underscore + '_filters') filters = [filters] if !filters.kind_of?(Array) + controller_filters = self.class.filter_chain.map {|c| c.method } 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]) + filter_method = plugin.class.name.underscore.gsub('/','_') + '_' + plugin_filter[:method_name] + unless controller_filters.include?(filter_method) + self.class.send(plugin_filter[:type], filter_method, (plugin_filter[:options] || {})) + self.class.send(:define_method, filter_method) do + instance_eval(&plugin_filter[:block]) if environment.plugin_enabled?(plugin.class) + end + end end end end diff --git a/test/functional/application_controller_test.rb b/test/functional/application_controller_test.rb index 49d09a2..a277dbb 100644 --- a/test/functional/application_controller_test.rb +++ b/test/functional/application_controller_test.rb @@ -500,4 +500,46 @@ class ApplicationControllerTest < ActionController::TestCase end end + + should 'do not duplicate plugin filters' do + + class FilterPlugin < Noosfero::Plugin + def test_controller_filters + { :type => 'before_filter', + :method_name => 'filter_plugin', + :options => {:only => 'some_method'}, + :block => lambda {} } + end + end + + Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([FilterPlugin.new]) + + get :index + get :index + assert_equal 1, @controller.class.filter_chain.select{|c| c.method == 'application_controller_test_filter_plugin_filter_plugin'}.count + end + + should 'do not call plugin filter block on a environment that this plugin is not enabled' do + + class OtherFilterPlugin < Noosfero::Plugin + def test_controller_filters + { :type => 'before_filter', + :method_name => 'filter_plugin', + :options => {:only => 'some_method'}, + :block => lambda {'plugin block called'} } + end + end + + environment1 = fast_create(Environment, :name => 'test environment') + environment1.enable_plugin(OtherFilterPlugin.name) + environment2 = fast_create(Environment, :name => 'other test environment') + + @controller.stubs(:environment).returns(environment1) + get :index + assert_equal 'plugin block called', @controller.application_controller_test_other_filter_plugin_filter_plugin + + @controller.stubs(:environment).returns(environment2) + assert_equal nil, @controller.application_controller_test_other_filter_plugin_filter_plugin + end + end -- libgit2 0.21.2