From 3ca0b15364f3e47e581240061db38484dadf6961 Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Wed, 9 Mar 2016 17:11:28 -0300 Subject: [PATCH] plugin-hotspot: dinamic hotspot for models callbacks --- lib/noosfero/plugin.rb | 4 ++++ lib/noosfero/plugin/hot_spot.rb | 23 +++++++++++++++++++++++ lib/noosfero/plugin/manager.rb | 3 ++- test/unit/plugin_hot_spot_test.rb | 15 +++++++++++++++ vendor/plugins/action_tracker/lib/action_tracker_model.rb | 2 ++ 5 files changed, 46 insertions(+), 1 deletion(-) diff --git a/lib/noosfero/plugin.rb b/lib/noosfero/plugin.rb index fcf8e73..c2099d1 100644 --- a/lib/noosfero/plugin.rb +++ b/lib/noosfero/plugin.rb @@ -707,6 +707,10 @@ class Noosfero::Plugin # returns = string with reason of expiration elsif method.to_s =~ /^content_expire_(#{content_actions.join('|')})$/ nil + # -> Generic hotspots for models callbacks + # Example: article_after_create_callback + elsif method.to_s =~ /^(.+)_#{Noosfero::Plugin::HotSpot::CALLBACK_HOTSPOTS.join('|')}_callback$/ + nil elsif context.respond_to?(method) context.send(method) else diff --git a/lib/noosfero/plugin/hot_spot.rb b/lib/noosfero/plugin/hot_spot.rb index fd683f9..44b75e0 100644 --- a/lib/noosfero/plugin/hot_spot.rb +++ b/lib/noosfero/plugin/hot_spot.rb @@ -6,6 +6,7 @@ # Environment will be used to determine which plugins are enabled and therefore # which plugins should be instantiated. module Noosfero::Plugin::HotSpot + CALLBACK_HOTSPOTS =[:after_save, :after_destroy, :before_save, :before_destroy, :after_create, :before_create] # Returns an instance of Noosfero::Plugin::Manager. # @@ -15,4 +16,26 @@ module Noosfero::Plugin::HotSpot @plugins ||= Noosfero::Plugin::Manager.new(environment, self) end + def self.included(klass) + klass.extend(ClassMethods) + end + + module ClassMethods + def self.extended base + CALLBACK_HOTSPOTS.each do |callback| + if base.respond_to?(callback) + base.class_eval do + self.send callback do |object| + current=self.class + while current.included_modules.include? Noosfero::Plugin::HotSpot do + callback_name = "#{current.name.underscore}_#{callback}_callback" + plugins.dispatch(callback_name, object) + current=current.superclass + end + end + end + end + end + end + end end diff --git a/lib/noosfero/plugin/manager.rb b/lib/noosfero/plugin/manager.rb index b2c7751..e1ac126 100644 --- a/lib/noosfero/plugin/manager.rb +++ b/lib/noosfero/plugin/manager.rb @@ -75,7 +75,8 @@ class Noosfero::Plugin::Manager end def enabled_plugins - @enabled_plugins ||= (Noosfero::Plugin.all & environment.enabled_plugins).map do |plugin| + environment_enabled_plugins = environment.present? ? environment.enabled_plugins : [] + @enabled_plugins ||= (Noosfero::Plugin.all & environment_enabled_plugins).map do |plugin| Noosfero::Plugin.load_plugin_identifier(plugin).new context end end diff --git a/test/unit/plugin_hot_spot_test.rb b/test/unit/plugin_hot_spot_test.rb index 117d2bc..ab1b0de 100644 --- a/test/unit/plugin_hot_spot_test.rb +++ b/test/unit/plugin_hot_spot_test.rb @@ -15,4 +15,19 @@ class PluginHotSpotTest < ActiveSupport::TestCase assert_same @client.plugins, @client.plugins end + Noosfero::Plugin::HotSpot::CALLBACK_HOTSPOTS.each do |callback| + should "call #{callback} hotspot" do + class CoolPlugin < Noosfero::Plugin; end + + Noosfero::Plugin.stubs(:all).returns([CoolPlugin.name]) + Environment.default.enable_plugin(CoolPlugin) + CoolPlugin.any_instance.expects("comment_#{callback}_callback".to_sym) + + person = fast_create(Person) + article = fast_create(Article, :profile_id => person.id) + comment = Comment.create!(:author => person, :title => 'test comment', :body => 'body!', :source => article) + comment.destroy + end + end + end diff --git a/vendor/plugins/action_tracker/lib/action_tracker_model.rb b/vendor/plugins/action_tracker/lib/action_tracker_model.rb index 24dd818..447dfd9 100644 --- a/vendor/plugins/action_tracker/lib/action_tracker_model.rb +++ b/vendor/plugins/action_tracker/lib/action_tracker_model.rb @@ -7,6 +7,8 @@ module ActionTracker belongs_to :user, :polymorphic => true belongs_to :target, :polymorphic => true + alias :profile :user + serialize :params, Hash before_validation :stringify_verb -- libgit2 0.21.2