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,26 +33,24 @@ class Noosfero::Plugin::Manager
33 33
34 alias :dispatch_scopes :dispatch_without_flatten 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 def dispatch_first(event, *args) 40 def dispatch_first(event, *args)
37 - default = Noosfero::Plugin.new.send(event, *args)  
38 - result = default  
39 each do |plugin| 41 each do |plugin|
40 result = plugin.send(event, *args) 42 result = plugin.send(event, *args)
41 - break if result != default 43 + return result if result.present?
42 end 44 end
43 - result 45 + default_for event, *args
44 end 46 end
45 47
46 def fetch_first_plugin(event, *args) 48 def fetch_first_plugin(event, *args)
47 - default = Noosfero::Plugin.new.send(event, *args)  
48 - result = nil  
49 each do |plugin| 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 end 52 end
55 - result 53 + nil
56 end 54 end
57 55
58 def pipeline(event, *args) 56 def pipeline(event, *args)
test/unit/plugin_manager_test.rb
@@ -291,4 +291,17 @@ class PluginManagerTest < ActiveSupport::TestCase @@ -291,4 +291,17 @@ class PluginManagerTest < ActiveSupport::TestCase
291 assert_equal [7,9], manager.filter(:invalid_numbers, [1,2,3,4,5,6,7,8,9,10]) 291 assert_equal [7,9], manager.filter(:invalid_numbers, [1,2,3,4,5,6,7,8,9,10])
292 end 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 end 307 end
test/unit/plugin_test.rb
@@ -23,7 +23,7 @@ class PluginTest &lt; ActiveSupport::TestCase @@ -23,7 +23,7 @@ class PluginTest &lt; ActiveSupport::TestCase
23 end 23 end
24 24
25 should 'returns empty hash for class method extra_blocks by default if no blocks are defined on plugin' do 25 should 'returns empty hash for class method extra_blocks by default if no blocks are defined on plugin' do
26 - 26 +
27 class SomePlugin1 < Noosfero::Plugin 27 class SomePlugin1 < Noosfero::Plugin
28 end 28 end
29 29