Commit 54df9315eca43f625e95db4c37d81548d3d611e5
Committed by
Rodrigo Souto
1 parent
3e8bfd50
Exists in
master
and in
28 other branches
Do not duplicate filters for plugins
Showing
2 changed files
with
50 additions
and
2 deletions
Show diff stats
app/controllers/application_controller.rb
@@ -135,9 +135,15 @@ class ApplicationController < ActionController::Base | @@ -135,9 +135,15 @@ class ApplicationController < ActionController::Base | ||
135 | plugins.each do |plugin| | 135 | plugins.each do |plugin| |
136 | filters = plugin.send(self.class.name.underscore + '_filters') | 136 | filters = plugin.send(self.class.name.underscore + '_filters') |
137 | filters = [filters] if !filters.kind_of?(Array) | 137 | filters = [filters] if !filters.kind_of?(Array) |
138 | + controller_filters = self.class.filter_chain.map {|c| c.method } | ||
138 | filters.each do |plugin_filter| | 139 | filters.each do |plugin_filter| |
139 | - self.class.send(plugin_filter[:type], plugin.class.name.underscore + '_' + plugin_filter[:method_name], (plugin_filter[:options] || {})) | ||
140 | - self.class.send(:define_method, plugin.class.name.underscore + '_' + plugin_filter[:method_name], plugin_filter[:block]) | 140 | + filter_method = plugin.class.name.underscore.gsub('/','_') + '_' + plugin_filter[:method_name] |
141 | + unless controller_filters.include?(filter_method) | ||
142 | + self.class.send(plugin_filter[:type], filter_method, (plugin_filter[:options] || {})) | ||
143 | + self.class.send(:define_method, filter_method) do | ||
144 | + instance_eval(&plugin_filter[:block]) if environment.plugin_enabled?(plugin.class) | ||
145 | + end | ||
146 | + end | ||
141 | end | 147 | end |
142 | end | 148 | end |
143 | end | 149 | end |
test/functional/application_controller_test.rb
@@ -500,4 +500,46 @@ class ApplicationControllerTest < ActionController::TestCase | @@ -500,4 +500,46 @@ class ApplicationControllerTest < ActionController::TestCase | ||
500 | end | 500 | end |
501 | 501 | ||
502 | end | 502 | end |
503 | + | ||
504 | + should 'do not duplicate plugin filters' do | ||
505 | + | ||
506 | + class FilterPlugin < Noosfero::Plugin | ||
507 | + def test_controller_filters | ||
508 | + { :type => 'before_filter', | ||
509 | + :method_name => 'filter_plugin', | ||
510 | + :options => {:only => 'some_method'}, | ||
511 | + :block => lambda {} } | ||
512 | + end | ||
513 | + end | ||
514 | + | ||
515 | + Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([FilterPlugin.new]) | ||
516 | + | ||
517 | + get :index | ||
518 | + get :index | ||
519 | + assert_equal 1, @controller.class.filter_chain.select{|c| c.method == 'application_controller_test_filter_plugin_filter_plugin'}.count | ||
520 | + end | ||
521 | + | ||
522 | + should 'do not call plugin filter block on a environment that this plugin is not enabled' do | ||
523 | + | ||
524 | + class OtherFilterPlugin < Noosfero::Plugin | ||
525 | + def test_controller_filters | ||
526 | + { :type => 'before_filter', | ||
527 | + :method_name => 'filter_plugin', | ||
528 | + :options => {:only => 'some_method'}, | ||
529 | + :block => lambda {'plugin block called'} } | ||
530 | + end | ||
531 | + end | ||
532 | + | ||
533 | + environment1 = fast_create(Environment, :name => 'test environment') | ||
534 | + environment1.enable_plugin(OtherFilterPlugin.name) | ||
535 | + environment2 = fast_create(Environment, :name => 'other test environment') | ||
536 | + | ||
537 | + @controller.stubs(:environment).returns(environment1) | ||
538 | + get :index | ||
539 | + assert_equal 'plugin block called', @controller.application_controller_test_other_filter_plugin_filter_plugin | ||
540 | + | ||
541 | + @controller.stubs(:environment).returns(environment2) | ||
542 | + assert_equal nil, @controller.application_controller_test_other_filter_plugin_filter_plugin | ||
543 | + end | ||
544 | + | ||
503 | end | 545 | end |