Commit 86bb3bc1c4f75ebe9e32f96ee0e651a4d003258f

Authored by Antonio Terceiro
1 parent d3836bc2

Finish the new implementation of hotspot behaviour

Undo some things, fix some tests, etc
app/models/person.rb
@@ -22,8 +22,6 @@ class Person < Profile @@ -22,8 +22,6 @@ class Person < Profile
22 super 22 super
23 end 23 end
24 24
25 - acts_as_having_hotspots  
26 -  
27 named_scope :members_of, lambda { |resources| 25 named_scope :members_of, lambda { |resources|
28 resources = [resources] if !resources.kind_of?(Array) 26 resources = [resources] if !resources.kind_of?(Array)
29 conditions = resources.map {|resource| "role_assignments.resource_type = '#{resource.class.base_class.name}' AND role_assignments.resource_id = #{resource.id || -1}"}.join(' OR ') 27 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 @@ -32,7 +30,7 @@ class Person < Profile
32 30
33 def has_permission_with_plugins?(permission, profile) 31 def has_permission_with_plugins?(permission, profile)
34 permissions = [has_permission_without_plugins?(permission, profile)] 32 permissions = [has_permission_without_plugins?(permission, profile)]
35 - permissions += enabled_plugins.map do |plugin| 33 + permissions += plugins.map do |plugin|
36 plugin.has_permission?(self, permission, profile) 34 plugin.has_permission?(self, permission, profile)
37 end 35 end
38 permissions.include?(true) 36 permissions.include?(true)
app/models/profile.rb
@@ -60,7 +60,8 @@ class Profile < ActiveRecord::Base @@ -60,7 +60,8 @@ class Profile < ActiveRecord::Base
60 } 60 }
61 61
62 acts_as_accessible 62 acts_as_accessible
63 - acts_as_having_hotspots 63 +
  64 + include Noosfero::Plugin::HotSpot
64 65
65 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 ] } } 66 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 ] } }
66 #FIXME: these will work only if the subclass is already loaded 67 #FIXME: these will work only if the subclass is already loaded
@@ -69,7 +70,7 @@ class Profile < ActiveRecord::Base @@ -69,7 +70,7 @@ class Profile < ActiveRecord::Base
69 named_scope :templates, :conditions => {:is_template => true} 70 named_scope :templates, :conditions => {:is_template => true}
70 71
71 def members 72 def members
72 - scopes = dispatch_scopes(:organization_members, self) 73 + scopes = plugins.dispatch_scopes(:organization_members, self)
73 scopes << Person.members_of(self) 74 scopes << Person.members_of(self)
74 scopes.size == 1 ? scopes.first : Person.or_scope(scopes) 75 scopes.size == 1 ? scopes.first : Person.or_scope(scopes)
75 end 76 end
config/initializers/plugins.rb
1 require 'noosfero/plugin' 1 require 'noosfero/plugin'
2 -require 'noosfero/plugin/acts_as_having_hotspots' 2 +require 'noosfero/plugin/hot_spot'
3 require 'noosfero/plugin/manager' 3 require 'noosfero/plugin/manager'
4 require 'noosfero/plugin/active_record' 4 require 'noosfero/plugin/active_record'
5 require 'noosfero/plugin/mailer_base' 5 require 'noosfero/plugin/mailer_base'
lib/noosfero/plugin/acts_as_having_hotspots.rb
@@ -1,44 +0,0 @@ @@ -1,44 +0,0 @@
1 -module ActsAsHavingHotspots  
2 - module ClassMethods  
3 - # Adding this feature to a class demands that it defines an instance method  
4 - # 'environment' that returns the environment associated with the instance.  
5 - def acts_as_having_hotspots  
6 - send :include, InstanceMethods  
7 - end  
8 -  
9 - module InstanceMethods  
10 - # Dispatches +event+ to each enabled plugin and collect the results.  
11 - #  
12 - # Returns an Array containing the objects returned by the event method in  
13 - # each plugin. This array is compacted (i.e. nils are removed) and flattened  
14 - # (i.e. elements of arrays are added to the resulting array). For example, if  
15 - # the enabled plugins return 1, 0, nil, and [1,2,3], then this method will  
16 - # return [1,0,1,2,3]  
17 - #  
18 - def dispatch(event, *args)  
19 - enabled_plugins.map { |plugin| plugin.send(event, *args) }.compact.flatten  
20 - end  
21 -  
22 - # Dispatch without flatten since scopes are executed if you run flatten on them  
23 - def dispatch_scopes(event, *args)  
24 - enabled_plugins.map { |plugin| plugin.send(event, *args) }.compact  
25 - end  
26 -  
27 - def enabled_plugins  
28 - Thread.current[:enabled_plugins] ||= (Noosfero::Plugin.all & environment.enabled_plugins).map do |plugin_name|  
29 - plugin = plugin_name.constantize.new  
30 - plugin.context = context  
31 - plugin  
32 - end  
33 - end  
34 -  
35 - if !method_defined?(:context)  
36 - define_method(:context) do  
37 - Noosfero::Plugin::Context.new  
38 - end  
39 - end  
40 - end  
41 - end  
42 -end  
43 -  
44 -ActiveRecord::Base.send(:extend, ActsAsHavingHotspots::ClassMethods)  
lib/noosfero/plugin/manager.rb
@@ -8,7 +8,6 @@ class Noosfero::Plugin::Manager @@ -8,7 +8,6 @@ class Noosfero::Plugin::Manager
8 @constantize = context 8 @constantize = context
9 end 9 end
10 10
11 - delegate :environment, :to => :context  
12 delegate :each, :to => :enabled_plugins 11 delegate :each, :to => :enabled_plugins
13 include Enumerable 12 include Enumerable
14 13
@@ -21,9 +20,15 @@ class Noosfero::Plugin::Manager @@ -21,9 +20,15 @@ class Noosfero::Plugin::Manager
21 # return [1,0,1,2,3] 20 # return [1,0,1,2,3]
22 # 21 #
23 def dispatch(event, *args) 22 def dispatch(event, *args)
24 - map { |plugin| plugin.send(event, *args) }.compact.flatten 23 + dispatch_without_flatten(event, *args).flatten
25 end 24 end
26 25
  26 + def dispatch_without_flatten(event, *args)
  27 + map { |plugin| plugin.send(event, *args) }.compact
  28 + end
  29 +
  30 + alias :dispatch_scopes :dispatch_without_flatten
  31 +
27 def enabled_plugins 32 def enabled_plugins
28 @enabled_plugins ||= (Noosfero::Plugin.all & environment.enabled_plugins).map do |plugin| 33 @enabled_plugins ||= (Noosfero::Plugin.all & environment.enabled_plugins).map do |plugin|
29 p = plugin.constantize.new 34 p = plugin.constantize.new
test/functional/content_viewer_controller_test.rb
@@ -1410,8 +1410,8 @@ end @@ -1410,8 +1410,8 @@ end
1410 1410
1411 should 'not display comments marked as spam' do 1411 should 'not display comments marked as spam' do
1412 article = fast_create(Article, :profile_id => profile.id) 1412 article = fast_create(Article, :profile_id => profile.id)
1413 - ham = fast_create(Comment, :source_id => article.id)  
1414 - spam = fast_create(Comment, :source_id => article.id, :spam => true) 1413 + ham = fast_create(Comment, :source_id => article.id, :source_type => 'Article')
  1414 + spam = fast_create(Comment, :source_id => article.id, :source_type => 'Article', :spam => true)
1415 1415
1416 get 'view_page', :profile => profile.identifier, :page => article.path.split('/') 1416 get 'view_page', :profile => profile.identifier, :page => article.path.split('/')
1417 assert_equal 1, assigns(:comments_count) 1417 assert_equal 1, assigns(:comments_count)
@@ -1420,7 +1420,7 @@ end @@ -1420,7 +1420,7 @@ end
1420 should 'be able to mark comments as spam' do 1420 should 'be able to mark comments as spam' do
1421 login_as profile.identifier 1421 login_as profile.identifier
1422 article = fast_create(Article, :profile_id => profile.id) 1422 article = fast_create(Article, :profile_id => profile.id)
1423 - spam = fast_create(Comment, :name => 'foo', :email => 'foo@example.com', :source_id => article.id) 1423 + spam = fast_create(Comment, :name => 'foo', :email => 'foo@example.com', :source_id => article.id, :source_type => 'Article')
1424 1424
1425 post 'view_page', :profile => profile.identifier, :page => article.path.split('/'), :mark_comment_as_spam => spam.id 1425 post 'view_page', :profile => profile.identifier, :page => article.path.split('/'), :mark_comment_as_spam => spam.id
1426 1426
test/unit/content_viewer_helper_test.rb
@@ -61,9 +61,9 @@ class ContentViewerHelperTest &lt; ActiveSupport::TestCase @@ -61,9 +61,9 @@ class ContentViewerHelperTest &lt; ActiveSupport::TestCase
61 end 61 end
62 62
63 should 'count total of comments from post' do 63 should 'count total of comments from post' do
64 - article = TextileArticle.new(:name => 'first post for test', :body => 'first post for test', :profile => profile) 64 + article = fast_create(TextileArticle)
65 article.stubs(:url).returns({}) 65 article.stubs(:url).returns({})
66 - article.stubs(:comments).returns([Comment.new(:author => profile, :title => 'test', :body => 'test')]) 66 + article.comments.create!(:author => profile, :title => 'test', :body => 'test')
67 result = link_to_comments(article) 67 result = link_to_comments(article)
68 assert_match /One comment/, result 68 assert_match /One comment/, result
69 end 69 end
test/unit/plugin_manager_test.rb
@@ -12,6 +12,7 @@ class PluginManagerTest &lt; ActiveSupport::TestCase @@ -12,6 +12,7 @@ class PluginManagerTest &lt; ActiveSupport::TestCase
12 @manager = Noosfero::Plugin::Manager.new(@environment, @controller) 12 @manager = Noosfero::Plugin::Manager.new(@environment, @controller)
13 end 13 end
14 attr_reader :environment 14 attr_reader :environment
  15 + attr_reader :manager
15 16
16 should 'return the intersection between environment\'s enabled plugins and system available plugins' do 17 should 'return the intersection between environment\'s enabled plugins and system available plugins' do
17 class Plugin1 < Noosfero::Plugin; end; 18 class Plugin1 < Noosfero::Plugin; end;
@@ -20,7 +21,6 @@ class PluginManagerTest &lt; ActiveSupport::TestCase @@ -20,7 +21,6 @@ class PluginManagerTest &lt; ActiveSupport::TestCase
20 class Plugin4 < Noosfero::Plugin; end; 21 class Plugin4 < Noosfero::Plugin; end;
21 environment.stubs(:enabled_plugins).returns([Plugin1.to_s, Plugin2.to_s, Plugin4.to_s]) 22 environment.stubs(:enabled_plugins).returns([Plugin1.to_s, Plugin2.to_s, Plugin4.to_s])
22 Noosfero::Plugin.stubs(:all).returns([Plugin1.to_s, Plugin3.to_s, Plugin4.to_s]) 23 Noosfero::Plugin.stubs(:all).returns([Plugin1.to_s, Plugin3.to_s, Plugin4.to_s])
23 - manager = Noosfero::Plugin::Manager.new(@controller)  
24 plugins = manager.enabled_plugins.map { |instance| instance.class.to_s } 24 plugins = manager.enabled_plugins.map { |instance| instance.class.to_s }
25 assert_equal [Plugin1.to_s, Plugin4.to_s], plugins 25 assert_equal [Plugin1.to_s, Plugin4.to_s], plugins
26 end 26 end
@@ -49,7 +49,6 @@ class PluginManagerTest &lt; ActiveSupport::TestCase @@ -49,7 +49,6 @@ class PluginManagerTest &lt; ActiveSupport::TestCase
49 49
50 p1 = Plugin1.new 50 p1 = Plugin1.new
51 p2 = Plugin2.new 51 p2 = Plugin2.new
52 - manager = Noosfero::Plugin::Manager.new(@controller)  
53 52
54 assert_equal [p1.random_event, p2.random_event], manager.dispatch(:random_event) 53 assert_equal [p1.random_event, p2.random_event], manager.dispatch(:random_event)
55 end 54 end
test/unit/profile_test.rb
@@ -1834,16 +1834,6 @@ class ProfileTest &lt; ActiveSupport::TestCase @@ -1834,16 +1834,6 @@ class ProfileTest &lt; ActiveSupport::TestCase
1834 end 1834 end
1835 1835
1836 should 'merge members of plugins to original members' do 1836 should 'merge members of plugins to original members' do
1837 - original_community = fast_create(Community)  
1838 - community1 = fast_create(Community, :identifier => 'community1')  
1839 - community2 = fast_create(Community, :identifier => 'community2')  
1840 - original_member = fast_create(Person)  
1841 - plugin1_member = fast_create(Person)  
1842 - plugin2_member = fast_create(Person)  
1843 - original_community.add_member(original_member)  
1844 - community1.add_member(plugin1_member)  
1845 - community2.add_member(plugin2_member)  
1846 -  
1847 class Plugin1 < Noosfero::Plugin 1837 class Plugin1 < Noosfero::Plugin
1848 def organization_members(profile) 1838 def organization_members(profile)
1849 Person.members_of(Community.find_by_identifier('community1')) 1839 Person.members_of(Community.find_by_identifier('community1'))
@@ -1855,8 +1845,18 @@ class ProfileTest &lt; ActiveSupport::TestCase @@ -1855,8 +1845,18 @@ class ProfileTest &lt; ActiveSupport::TestCase
1855 Person.members_of(Community.find_by_identifier('community2')) 1845 Person.members_of(Community.find_by_identifier('community2'))
1856 end 1846 end
1857 end 1847 end
  1848 + Environment.default.enable_plugin(Plugin1)
  1849 + Environment.default.enable_plugin(Plugin2)
1858 1850
1859 - original_community.stubs(:enabled_plugins).returns([Plugin1.new, Plugin2.new]) 1851 + original_community = fast_create(Community)
  1852 + community1 = fast_create(Community, :identifier => 'community1')
  1853 + community2 = fast_create(Community, :identifier => 'community2')
  1854 + original_member = fast_create(Person)
  1855 + plugin1_member = fast_create(Person)
  1856 + plugin2_member = fast_create(Person)
  1857 + original_community.add_member(original_member)
  1858 + community1.add_member(plugin1_member)
  1859 + community2.add_member(plugin2_member)
1860 1860
1861 assert_includes original_community.members, original_member 1861 assert_includes original_community.members, original_member
1862 assert_includes original_community.members, plugin1_member 1862 assert_includes original_community.members, plugin1_member