diff --git a/app/models/person.rb b/app/models/person.rb index 3aa4c04..4d564d6 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -22,8 +22,6 @@ class Person < Profile super end - acts_as_having_hotspots - named_scope :members_of, lambda { |resources| resources = [resources] if !resources.kind_of?(Array) conditions = resources.map {|resource| "role_assignments.resource_type = '#{resource.class.base_class.name}' AND role_assignments.resource_id = #{resource.id || -1}"}.join(' OR ') @@ -32,7 +30,7 @@ class Person < Profile def has_permission_with_plugins?(permission, profile) permissions = [has_permission_without_plugins?(permission, profile)] - permissions += enabled_plugins.map do |plugin| + permissions += plugins.map do |plugin| plugin.has_permission?(self, permission, profile) end permissions.include?(true) diff --git a/app/models/profile.rb b/app/models/profile.rb index a4ffcee..b0bd67d 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -60,7 +60,8 @@ class Profile < ActiveRecord::Base } acts_as_accessible - acts_as_having_hotspots + + include Noosfero::Plugin::HotSpot named_scope :memberships_of, lambda { |person| { :select => 'DISTINCT profiles.*', :joins => :role_assignments, :conditions => ['role_assignments.accessor_type = ? AND role_assignments.accessor_id = ?', person.class.base_class.name, person.id ] } } #FIXME: these will work only if the subclass is already loaded @@ -69,7 +70,7 @@ class Profile < ActiveRecord::Base named_scope :templates, :conditions => {:is_template => true} def members - scopes = dispatch_scopes(:organization_members, self) + scopes = plugins.dispatch_scopes(:organization_members, self) scopes << Person.members_of(self) scopes.size == 1 ? scopes.first : Person.or_scope(scopes) end diff --git a/config/initializers/plugins.rb b/config/initializers/plugins.rb index ee8c960..56188fa 100644 --- a/config/initializers/plugins.rb +++ b/config/initializers/plugins.rb @@ -1,5 +1,5 @@ require 'noosfero/plugin' -require 'noosfero/plugin/acts_as_having_hotspots' +require 'noosfero/plugin/hot_spot' require 'noosfero/plugin/manager' require 'noosfero/plugin/active_record' require 'noosfero/plugin/mailer_base' diff --git a/lib/noosfero/plugin/acts_as_having_hotspots.rb b/lib/noosfero/plugin/acts_as_having_hotspots.rb deleted file mode 100644 index c766a81..0000000 --- a/lib/noosfero/plugin/acts_as_having_hotspots.rb +++ /dev/null @@ -1,44 +0,0 @@ -module ActsAsHavingHotspots - module ClassMethods - # Adding this feature to a class demands that it defines an instance method - # 'environment' that returns the environment associated with the instance. - def acts_as_having_hotspots - send :include, InstanceMethods - end - - module InstanceMethods - # Dispatches +event+ to each enabled plugin and collect the results. - # - # Returns an Array containing the objects returned by the event method in - # each plugin. This array is compacted (i.e. nils are removed) and flattened - # (i.e. elements of arrays are added to the resulting array). For example, if - # the enabled plugins return 1, 0, nil, and [1,2,3], then this method will - # return [1,0,1,2,3] - # - def dispatch(event, *args) - enabled_plugins.map { |plugin| plugin.send(event, *args) }.compact.flatten - end - - # Dispatch without flatten since scopes are executed if you run flatten on them - def dispatch_scopes(event, *args) - enabled_plugins.map { |plugin| plugin.send(event, *args) }.compact - end - - def enabled_plugins - Thread.current[:enabled_plugins] ||= (Noosfero::Plugin.all & environment.enabled_plugins).map do |plugin_name| - plugin = plugin_name.constantize.new - plugin.context = context - plugin - end - end - - if !method_defined?(:context) - define_method(:context) do - Noosfero::Plugin::Context.new - end - end - end - end -end - -ActiveRecord::Base.send(:extend, ActsAsHavingHotspots::ClassMethods) diff --git a/lib/noosfero/plugin/manager.rb b/lib/noosfero/plugin/manager.rb index 881f3b3..d1a12ab 100644 --- a/lib/noosfero/plugin/manager.rb +++ b/lib/noosfero/plugin/manager.rb @@ -8,7 +8,6 @@ class Noosfero::Plugin::Manager @constantize = context end - delegate :environment, :to => :context delegate :each, :to => :enabled_plugins include Enumerable @@ -21,9 +20,15 @@ class Noosfero::Plugin::Manager # return [1,0,1,2,3] # def dispatch(event, *args) - map { |plugin| plugin.send(event, *args) }.compact.flatten + dispatch_without_flatten(event, *args).flatten end + def dispatch_without_flatten(event, *args) + map { |plugin| plugin.send(event, *args) }.compact + end + + alias :dispatch_scopes :dispatch_without_flatten + def enabled_plugins @enabled_plugins ||= (Noosfero::Plugin.all & environment.enabled_plugins).map do |plugin| p = plugin.constantize.new diff --git a/test/functional/content_viewer_controller_test.rb b/test/functional/content_viewer_controller_test.rb index ae3fd8b..fe3c080 100644 --- a/test/functional/content_viewer_controller_test.rb +++ b/test/functional/content_viewer_controller_test.rb @@ -1410,8 +1410,8 @@ end should 'not display comments marked as spam' do article = fast_create(Article, :profile_id => profile.id) - ham = fast_create(Comment, :source_id => article.id) - spam = fast_create(Comment, :source_id => article.id, :spam => true) + ham = fast_create(Comment, :source_id => article.id, :source_type => 'Article') + spam = fast_create(Comment, :source_id => article.id, :source_type => 'Article', :spam => true) get 'view_page', :profile => profile.identifier, :page => article.path.split('/') assert_equal 1, assigns(:comments_count) @@ -1420,7 +1420,7 @@ end should 'be able to mark comments as spam' do login_as profile.identifier article = fast_create(Article, :profile_id => profile.id) - spam = fast_create(Comment, :name => 'foo', :email => 'foo@example.com', :source_id => article.id) + spam = fast_create(Comment, :name => 'foo', :email => 'foo@example.com', :source_id => article.id, :source_type => 'Article') post 'view_page', :profile => profile.identifier, :page => article.path.split('/'), :mark_comment_as_spam => spam.id diff --git a/test/unit/content_viewer_helper_test.rb b/test/unit/content_viewer_helper_test.rb index 939914b..fd977d5 100644 --- a/test/unit/content_viewer_helper_test.rb +++ b/test/unit/content_viewer_helper_test.rb @@ -61,9 +61,9 @@ class ContentViewerHelperTest < ActiveSupport::TestCase end should 'count total of comments from post' do - article = TextileArticle.new(:name => 'first post for test', :body => 'first post for test', :profile => profile) + article = fast_create(TextileArticle) article.stubs(:url).returns({}) - article.stubs(:comments).returns([Comment.new(:author => profile, :title => 'test', :body => 'test')]) + article.comments.create!(:author => profile, :title => 'test', :body => 'test') result = link_to_comments(article) assert_match /One comment/, result end diff --git a/test/unit/plugin_manager_test.rb b/test/unit/plugin_manager_test.rb index 7ca8346..70f5be9 100644 --- a/test/unit/plugin_manager_test.rb +++ b/test/unit/plugin_manager_test.rb @@ -12,6 +12,7 @@ class PluginManagerTest < ActiveSupport::TestCase @manager = Noosfero::Plugin::Manager.new(@environment, @controller) end attr_reader :environment + attr_reader :manager should 'return the intersection between environment\'s enabled plugins and system available plugins' do class Plugin1 < Noosfero::Plugin; end; @@ -20,7 +21,6 @@ class PluginManagerTest < ActiveSupport::TestCase class Plugin4 < Noosfero::Plugin; end; environment.stubs(:enabled_plugins).returns([Plugin1.to_s, Plugin2.to_s, Plugin4.to_s]) Noosfero::Plugin.stubs(:all).returns([Plugin1.to_s, Plugin3.to_s, Plugin4.to_s]) - manager = Noosfero::Plugin::Manager.new(@controller) plugins = manager.enabled_plugins.map { |instance| instance.class.to_s } assert_equal [Plugin1.to_s, Plugin4.to_s], plugins end @@ -49,7 +49,6 @@ class PluginManagerTest < ActiveSupport::TestCase p1 = Plugin1.new p2 = Plugin2.new - manager = Noosfero::Plugin::Manager.new(@controller) assert_equal [p1.random_event, p2.random_event], manager.dispatch(:random_event) end diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index 0728359..ec959df 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -1834,16 +1834,6 @@ class ProfileTest < ActiveSupport::TestCase end should 'merge members of plugins to original members' do - original_community = fast_create(Community) - community1 = fast_create(Community, :identifier => 'community1') - community2 = fast_create(Community, :identifier => 'community2') - original_member = fast_create(Person) - plugin1_member = fast_create(Person) - plugin2_member = fast_create(Person) - original_community.add_member(original_member) - community1.add_member(plugin1_member) - community2.add_member(plugin2_member) - class Plugin1 < Noosfero::Plugin def organization_members(profile) Person.members_of(Community.find_by_identifier('community1')) @@ -1855,8 +1845,18 @@ class ProfileTest < ActiveSupport::TestCase Person.members_of(Community.find_by_identifier('community2')) end end + Environment.default.enable_plugin(Plugin1) + Environment.default.enable_plugin(Plugin2) - original_community.stubs(:enabled_plugins).returns([Plugin1.new, Plugin2.new]) + original_community = fast_create(Community) + community1 = fast_create(Community, :identifier => 'community1') + community2 = fast_create(Community, :identifier => 'community2') + original_member = fast_create(Person) + plugin1_member = fast_create(Person) + plugin2_member = fast_create(Person) + original_community.add_member(original_member) + community1.add_member(plugin1_member) + community2.add_member(plugin2_member) assert_includes original_community.members, original_member assert_includes original_community.members, plugin1_member -- libgit2 0.21.2