[noosfero/noosfero][master] 4 commits: Revert "plugins: don't crash on method_missing methods"

Bráulio Bhavamitra gitlab at gitlab.com
Tue Mar 31 21:23:16 BRT 2015


Bráulio Bhavamitra pushed to master at Noosfero / noosfero


Commits:
81478cba by Braulio Bhavamitra at 2015-03-31T21:23:12Z
Revert "plugins: don't crash on method_missing methods"

This reverts commit 51d3ac08b10f2f063a6915b83e95329dd14613dd.

- - - - -
3e99a558 by Braulio Bhavamitra at 2015-03-31T21:23:12Z
Revert "plugins: revert change on pipeline"

This reverts commit 3c94729bc1231c1a23209fc6ca3aeaed5a29534d.

- - - - -
d32bf3a4 by Braulio Bhavamitra at 2015-03-31T21:23:12Z
Revert "plugins: only call event on plugin if it is defined"

This reverts commit da84575ee29154fa4f24c9acdfce9ec16a7acb5b.

- - - - -
037e6360 by Braulio Bhavamitra at 2015-03-31T21:23:12Z
Revert "plugins: only call default value if result is blank"

This reverts commit 8a3fb6f312e27c4225b95dff84ea365432e361ed.

- - - - -


3 changed files:

- lib/noosfero/plugin/manager.rb
- test/unit/plugin_manager_test.rb
- test/unit/plugin_test.rb


Changes:

=====================================
lib/noosfero/plugin/manager.rb
=====================================
--- a/lib/noosfero/plugin/manager.rb
+++ b/lib/noosfero/plugin/manager.rb
@@ -20,50 +20,44 @@ class Noosfero::Plugin::Manager
   # return [1,0,1,2,3]
   #
   def dispatch(event, *args)
-    flat_map{ |plugin| result_for plugin, event, *args }.compact
+    dispatch_without_flatten(event, *args).flatten
   end
 
   def fetch_plugins(event, *args)
-    flat_map{ |plugin| plugin.class if result_for plugin, event, *args }.compact
+    map { |plugin| plugin.class if plugin.send(event, *args) }.compact.flatten
   end
 
   def dispatch_without_flatten(event, *args)
-    map { |plugin| result_for plugin, event, *args }.compact
+    map { |plugin| plugin.send(event, *args) }.compact
   end
 
   alias :dispatch_scopes :dispatch_without_flatten
 
-  def default_for event, *args
-    Noosfero::Plugin.new.send event, *args
-  end
-
-  def result_for plugin, event, *args
-    # check if defined to avoid crash, as there is hotspots using method_missing
-    return unless plugin.respond_to? event
-    method = plugin.method event
-    method.call *args if method.owner != Noosfero::Plugin
-  end
-
   def dispatch_first(event, *args)
+    default = Noosfero::Plugin.new.send(event, *args)
+    result = default
     each do |plugin|
-      result = result_for plugin, event, *args
-      return result if result.present?
+      result = plugin.send(event, *args)
+      break if result != default
     end
-    default_for event, *args
+    result
   end
 
   def fetch_first_plugin(event, *args)
+    default = Noosfero::Plugin.new.send(event, *args)
+    result = nil
     each do |plugin|
-      result = result_for plugin, event, *args
-      return plugin.class if result.present?
+      if plugin.send(event, *args) != default
+        result = plugin.class
+        break
+      end
     end
-    nil
+    result
   end
 
   def pipeline(event, *args)
     each do |plugin|
-      # result_for can't be used here and default must be returned to keep args
-      result = plugin.send event, *args
+      result = plugin.send(event, *args)
       result = result.kind_of?(Array) ? result : [result]
       raise ArgumentError, "Pipeline broken by #{plugin.class.name} on #{event} with #{result.length} arguments instead of #{args.length}." if result.length != args.length
       args = result
@@ -72,7 +66,7 @@ class Noosfero::Plugin::Manager
   end
 
   def filter(property, data)
-    inject(data){ |data, plugin| data = plugin.send(property, data) }
+    inject(data) {|data, plugin| data = plugin.send(property, data)}
   end
 
   def parse_macro(macro_name, macro, source = nil)

=====================================
test/unit/plugin_manager_test.rb
=====================================
--- a/test/unit/plugin_manager_test.rb
+++ b/test/unit/plugin_manager_test.rb
@@ -178,15 +178,6 @@ class PluginManagerTest < ActiveSupport::TestCase
     assert_equal Plugin2, manager.fetch_first_plugin(:random_event)
   end
 
-  should 'return nil if missing method is called' do
-    class Plugin1 < Noosfero::Plugin
-    end
-    Noosfero::Plugin.stubs(:all).returns(['PluginManagerTest::Plugin1'])
-    environment.enable_plugin(Plugin1)
-
-    assert_equal nil, @manager.result_for(Plugin1.new, :content_remove_new)
-  end
-
   should 'parse macro' do
     class Plugin1 < Noosfero::Plugin
       def macros
@@ -300,44 +291,4 @@ class PluginManagerTest < ActiveSupport::TestCase
     assert_equal [7,9], manager.filter(:invalid_numbers, [1,2,3,4,5,6,7,8,9,10])
   end
 
-  should 'only call default if value is blank' do
-    class Plugin1 < Noosfero::Plugin
-      def find_by_contents asset, scope, query, paginate_options={}, options={}
-        {results: [1,2,3]}
-      end
-    end
-    Noosfero::Plugin.stubs(:all).returns(['PluginManagerTest::Plugin1'])
-    environment.enable_plugin(Plugin1)
-
-    Noosfero::Plugin.any_instance.expects(:find_by_contents).never
-    @manager.dispatch_first :find_by_contents, :products, environment.products, 'product'
-  end
-
-  should 'not event if it is not defined by plugin' do
-    class Noosfero::Plugin
-      def never_call
-        nil
-      end
-    end
-    class Plugin1 < Noosfero::Plugin
-      def never_call
-        'defined'
-      end
-    end
-    class Plugin2 < Noosfero::Plugin
-    end
-    Noosfero::Plugin.stubs(:all).returns(['PluginManagerTest::Plugin1', 'PluginManagerTest::Plugin2'])
-    environment.enable_plugin(Plugin1)
-    environment.enable_plugin(Plugin2)
-    plugin1 = @manager.enabled_plugins.detect{ |p| p.is_a? Plugin1 }
-    plugin2 = @manager.enabled_plugins.detect{ |p| p.is_a? Plugin2 }
-
-    assert_equal Plugin1, Plugin1.new.method(:never_call).owner
-    assert_equal Noosfero::Plugin, Plugin2.new.method(:never_call).owner
-    # expects never can't be used as it defines the method
-    @manager.expects(:result_for).with(plugin1, :never_call).returns(Plugin1.new.never_call)
-    @manager.expects(:result_for).with(plugin2, :never_call).returns(nil)
-    @manager.dispatch :never_call
-  end
-
 end

=====================================
test/unit/plugin_test.rb
=====================================
--- a/test/unit/plugin_test.rb
+++ b/test/unit/plugin_test.rb
@@ -23,7 +23,7 @@ class PluginTest < ActiveSupport::TestCase
   end
 
   should 'returns empty hash for class method extra_blocks by default if no blocks are defined on plugin' do
-
+    
     class SomePlugin1 < Noosfero::Plugin
     end
 


View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/81b33ca3a1f312617cf37a9af036b7c9bceb7bed...037e6360f53129c37e4e065cbe6185920dbff7fd
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20150401/98f1e058/attachment.html>


More information about the Noosfero-dev mailing list