Commit 8a3fb6f312e27c4225b95dff84ea365432e361ed

Authored by Braulio Bhavamitra
1 parent 01024be2

plugins: only call default value if result is blank

lib/noosfero/plugin/manager.rb
... ... @@ -33,26 +33,24 @@ class Noosfero::Plugin::Manager
33 33  
34 34 alias :dispatch_scopes :dispatch_without_flatten
35 35  
  36 + def default_for event, *args
  37 + Noosfero::Plugin.new.send event, *args
  38 + end
  39 +
36 40 def dispatch_first(event, *args)
37   - default = Noosfero::Plugin.new.send(event, *args)
38   - result = default
39 41 each do |plugin|
40 42 result = plugin.send(event, *args)
41   - break if result != default
  43 + return result if result.present?
42 44 end
43   - result
  45 + default_for event, *args
44 46 end
45 47  
46 48 def fetch_first_plugin(event, *args)
47   - default = Noosfero::Plugin.new.send(event, *args)
48   - result = nil
49 49 each do |plugin|
50   - if plugin.send(event, *args) != default
51   - result = plugin.class
52   - break
53   - end
  50 + result = plugin.send event, *args
  51 + return plugin.class if result.present?
54 52 end
55   - result
  53 + nil
56 54 end
57 55  
58 56 def pipeline(event, *args)
... ...
test/unit/plugin_manager_test.rb
... ... @@ -291,4 +291,17 @@ class PluginManagerTest < ActiveSupport::TestCase
291 291 assert_equal [7,9], manager.filter(:invalid_numbers, [1,2,3,4,5,6,7,8,9,10])
292 292 end
293 293  
  294 + should 'only call default if value is blank' do
  295 + class Plugin1 < Noosfero::Plugin
  296 + def find_by_contents asset, scope, query, paginate_options={}, options={}
  297 + {results: [1,2,3]}
  298 + end
  299 + end
  300 + Noosfero::Plugin.stubs(:all).returns(['PluginManagerTest::Plugin1'])
  301 + environment.enable_plugin(Plugin1)
  302 +
  303 + Noosfero::Plugin.any_instance.expects(:find_by_contents).never
  304 + @manager.dispatch_first :find_by_contents, :products, environment.products, 'product'
  305 + end
  306 +
294 307 end
... ...
test/unit/plugin_test.rb
... ... @@ -23,7 +23,7 @@ class PluginTest &lt; ActiveSupport::TestCase
23 23 end
24 24  
25 25 should 'returns empty hash for class method extra_blocks by default if no blocks are defined on plugin' do
26   -
  26 +
27 27 class SomePlugin1 < Noosfero::Plugin
28 28 end
29 29  
... ...