diff --git a/app/models/organization.rb b/app/models/organization.rb index 2d092ef..e319d51 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -21,6 +21,12 @@ class Organization < Profile has_many :mailings, :class_name => 'OrganizationMailing', :foreign_key => :source_id, :as => 'source' + named_scope :more_popular, + :select => "#{Profile.qualified_column_names}, count(resource_id) as total", + :group => Profile.qualified_column_names, + :joins => "LEFT OUTER JOIN role_assignments ON profiles.id = role_assignments.resource_id", + :order => "total DESC" + def validation_methodology self.validation_info ? self.validation_info.validation_methodology : nil end diff --git a/app/models/person.rb b/app/models/person.rb index 3a2f97f..2668fb5 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -22,10 +22,10 @@ class Person < Profile has_many :scraps_sent, :class_name => 'Scrap', :foreign_key => :sender_id, :dependent => :destroy named_scope :more_popular, - :select => "#{Profile.qualified_column_names}, count(friend_id) as total", - :group => Profile.qualified_column_names, - :joins => :friendships, - :order => "total DESC" + :select => "#{Profile.qualified_column_names}, count(friend_id) as total", + :group => Profile.qualified_column_names, + :joins => "LEFT OUTER JOIN friendships on profiles.id = friendships.person_id", + :order => "total DESC" after_destroy do |person| Friendship.find(:all, :conditions => { :friend_id => person.id}).each { |friendship| friendship.destroy } diff --git a/app/models/profile.rb b/app/models/profile.rb index 7f234dd..f166304 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -78,18 +78,19 @@ class Profile < ActiveRecord::Base end named_scope :visible, :conditions => { :visible => true } + # Subclasses must override this method + named_scope :more_popular named_scope :more_recent, :order => "created_at DESC" - named_scope :more_popular, - :select => "#{Profile.qualified_column_names}, count(resource_id) as total", - :group => Profile.qualified_column_names, - :joins => :role_assignments, - :order => "total DESC" - named_scope :more_active, - :select => "#{Profile.qualified_column_names}, count(articles.id) as total, sum(articles.comments_count) as total_comments", - :joins => :articles, - :group => Profile.qualified_column_names, - :order => "total DESC, total_comments DESC", - :conditions => ["articles.created_at BETWEEN ? AND ?", 7.days.ago, DateTime.now] + named_scope :more_active, lambda { + { + :select => "#{Profile.qualified_column_names}, count(action_tracker.id) as total", + :joins => "LEFT OUTER JOIN action_tracker ON profiles.id = action_tracker.user_id" + + (self == Person ? '' : ' OR profiles.id = action_tracker.target_id'), + :group => Profile.qualified_column_names, + :order => 'total DESC', + :conditions => ['action_tracker.created_at >= ? OR action_tracker.id IS NULL', ActionTracker::Record::RECENT_DELAY.days.ago] + } + } acts_as_trackable :dependent => :destroy @@ -779,18 +780,27 @@ private :generate_url, :url_options _("Since: ") end + def recent_actions + tracked_actions.recent + end + + def recent_notifications + tracked_notifications.recent + end + def more_active_label - amount = self.articles.count + amount = recent_actions.count + amount += recent_notifications.count if organization? { - 0 => _('none'), - 1 => _('one article') - }[amount] || _("%s articles") % amount + 0 => _('no actions'), + 1 => _('one action') + }[amount] || _("%s actions") % amount end def more_popular_label amount = self.members_count { - 0 => _('none'), + 0 => _('no members'), 1 => _('one member') }[amount] || _("%s members") % amount end diff --git a/test/functional/browse_controller_test.rb b/test/functional/browse_controller_test.rb index 238a2eb..f796843 100644 --- a/test/functional/browse_controller_test.rb +++ b/test/functional/browse_controller_test.rb @@ -89,11 +89,12 @@ class BrowseControllerTest < Test::Unit::TestCase p1 = create(Person, :name => 'Testing person 1', :user_id => 1) p2 = create(Person, :name => 'Testing person 2', :user_id => 2) p3 = create(Person, :name => 'Testing person 3', :user_id => 3) - Article.delete_all - fast_create(Article, :profile_id => p1, :created_at => 1.day.ago) - fast_create(Article, :profile_id => p2, :created_at => DateTime.now.beginning_of_day) + ActionTracker::Record.delete_all + fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => p1, :created_at => Time.now) + fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => p2, :created_at => Time.now) + fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => p2, :created_at => Time.now) get :people, :filter => 'more_active' - assert_equal [p1,p2] , assigns(:results) + assert_equal [p2,p1,p3] , assigns(:results) end should 'filter more popular people' do @@ -106,7 +107,7 @@ class BrowseControllerTest < Test::Unit::TestCase p2.add_friend(p1) p2.add_friend(p3) get :people, :filter => 'more_popular' - assert_equal [p2,p1] , assigns(:results) + assert_equal [p2,p1,p3] , assigns(:results) end should 'the people filter be only the hardcoded one' do @@ -203,18 +204,19 @@ class BrowseControllerTest < Test::Unit::TestCase c1 = create(Community, :name => 'Testing community 1') c2 = create(Community, :name => 'Testing community 2') c3 = create(Community, :name => 'Testing community 3') - Article.delete_all - fast_create(Article, :profile_id => c1, :created_at => 1.day.ago) - fast_create(Article, :profile_id => c2, :created_at => DateTime.now.beginning_of_day) + ActionTracker::Record.delete_all + fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => c1, :created_at => Time.now) + fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => c2, :created_at => Time.now) + fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => c2, :created_at => Time.now) get :communities, :filter => 'more_active' - assert_equal [c1,c2] , assigns(:results) + assert_equal [c2,c1,c3] , assigns(:results) end should 'filter more popular communities' do Person.delete_all + Community.delete_all c1 = create(Community, :name => 'Testing community 1') c2 = create(Community, :name => 'Testing community 2') - create(Community, :name => 'Testing community 3') p1 = create(Person, :name => 'Testing person 1', :user_id => 1) p2 = create(Person, :name => 'Testing person 2', :user_id => 2) diff --git a/test/unit/organization_test.rb b/test/unit/organization_test.rb index 1c8f81a..f64f5c4 100644 --- a/test/unit/organization_test.rb +++ b/test/unit/organization_test.rb @@ -297,4 +297,51 @@ class OrganizationTest < Test::Unit::TestCase assert_equal "online_community@conference.#{org.environment.default_hostname}/#{org.short_name}", org.full_jid end + should 'find more popular organizations' do + Organization.delete_all + o1 = fast_create(Organization) + o2 = fast_create(Organization) + + p1 = fast_create(Person) + p2 = fast_create(Person) + o1.add_member(p1) + assert_equal [o1,o2] , Organization.more_popular + + o2.add_member(p1) + o2.add_member(p2) + assert_equal [o2,o1] , Organization.more_popular + end + + should 'list organizations that have no members in more popular list' do + organization = fast_create(Organization) + assert_includes Organization.more_popular, organization + end + + should "return no members on label if the organization has no members" do + organization = fast_create(Organization) + assert_equal 0, organization.members_count + assert_equal "no members", organization.more_popular_label + end + + should "return one member on label if the organization has one member" do + person = fast_create(Person) + organization = fast_create(Organization) + organization.add_member(person) + + assert_equal "one member", organization.more_popular_label + end + + should "return the number of members on label if the organization has more than one member" do + person1 = fast_create(Person) + person2 = fast_create(Person) + organization = fast_create(Organization) + + organization.add_member(person1) + organization.add_member(person2) + assert_equal "2 members", organization.more_popular_label + + person3 = fast_create(Person) + organization.add_member(person3) + assert_equal "3 members", organization.more_popular_label + end end diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb index 5853a8d..52e4677 100644 --- a/test/unit/person_test.rb +++ b/test/unit/person_test.rb @@ -625,20 +625,20 @@ class PersonTest < Test::Unit::TestCase should 'find more popular people' do Person.delete_all - env = fast_create(Environment) p1 = fast_create(Person) p2 = fast_create(Person) p3 = fast_create(Person) p1.add_friend(p2) - assert_equal [p1], Person.more_popular - p2.add_friend(p1) p2.add_friend(p3) - assert_equal [p2,p1] , Person.more_popular + assert_equal p2, Person.more_popular[0] + assert_equal p1, Person.more_popular[1] + end - p2.remove_friend(p3) - assert_equal [p1,p2] , Person.more_popular + should 'list people that have no friends in more popular list' do + person = fast_create(Person) + assert_includes Person.more_popular, person end should 'persons has reference to user' do diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index 2625678..a201857 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -1553,151 +1553,53 @@ class ProfileTest < Test::Unit::TestCase assert_match /

Wellformed html code <\/h1>/, profile.custom_footer end - should 'find more recent people' do - Person.delete_all - p1 = fast_create(Person,:created_at => 4.days.ago) - p2 = fast_create(Person, :created_at => DateTime.now) - p3 = fast_create(Person, :created_at => 2.days.ago) - - assert_equal [p2,p3,p1] , Person.more_recent - - p4 = fast_create(Person, :created_at => 3.days.ago) - assert_equal [p2,p3,p4,p1] , Person.more_recent - end - - should 'find more active people' do - Person.delete_all - p1 = fast_create(Person) - p2 = fast_create(Person) - p3 = fast_create(Person) - Article.delete_all - fast_create(Article, :profile_id => p1, :created_at => 7.days.ago) - fast_create(Article, :profile_id => p1, :created_at => DateTime.now.beginning_of_day) - fast_create(Article, :profile_id => p2, :created_at => DateTime.now.beginning_of_day) - assert_equal [p1,p2] , Person.more_active - - fast_create(Article, :profile_id => p2, :created_at => 1.day.ago) - fast_create(Article, :profile_id => p2, :created_at => 5.days.ago) - fast_create(Article, :profile_id => p3, :created_at => 2.days.ago) - assert_equal [p2,p1,p3] , Person.more_active - end - - should 'the ties on more active people be solved by the number of comments' do - Person.delete_all - p1 = fast_create(Person) - p2 = fast_create(Person) - Article.delete_all - a1 = fast_create(Article, :profile_id => p1, :created_at => DateTime.now.beginning_of_day) - a2 = fast_create(Article, :profile_id => p2, :created_at => DateTime.now.beginning_of_day) - assert_equal [], [p1,p2] - Person.more_active - assert_equal [], Person.more_active - [p1, p2] - - a2.comments.build(:title => 'test comment', :body => 'anything', :author => p1).save! - assert_equal [p2,p1] , Person.more_active - - a1.comments.build(:title => 'test comment', :body => 'anything', :author => p2).save! - a1.comments.build(:title => 'test comment', :body => 'anything', :author => p2).save! - assert_equal [p1,p2] , Person.more_active - end - - should 'more active people take in consideration only articles created current the last week' do - Person.delete_all - env = fast_create(Environment) - p1 = fast_create(Person) - p2 = fast_create(Person) - p3 = fast_create(Person) - Article.delete_all - fast_create(Article, :profile_id => p1, :created_at => DateTime.now.beginning_of_day) - fast_create(Article, :profile_id => p2, :created_at => 10.days.ago) - assert_equal [p1] , Person.more_active - - fast_create(Article, :profile_id => p2, :created_at => DateTime.now.beginning_of_day) - fast_create(Article, :profile_id => p2, :created_at => 7.days.ago) - fast_create(Article, :profile_id => p3, :created_at => 8.days.ago) - assert_equal [p2,p1] , Person.more_active - end - - should 'find more recent community' do - c1 = fast_create(Community, :created_at => 3.days.ago) - c2 = fast_create(Community, :created_at => 1.day.ago) - c3 = fast_create(Community, :created_at => DateTime.now) - - assert_equal [c3,c2,c1] , Community.more_recent + should 'find more recent profile' do + Profile.delete_all + p1 = fast_create(Profile, :created_at => 4.days.ago) + p2 = fast_create(Profile, :created_at => Time.now) + p3 = fast_create(Profile, :created_at => 2.days.ago) + assert_equal [p2,p3,p1] , Profile.more_recent - c4 = fast_create(Community, :created_at => 2.days.ago) - assert_equal [c3,c2,c4,c1] , Community.more_recent + p4 = fast_create(Profile, :created_at => 3.days.ago) + assert_equal [p2,p3,p4,p1] , Profile.more_recent end - should 'find more active community' do - c1 = fast_create(Community) - c2 = fast_create(Community) - c3 = fast_create(Community) + should 'find more active profiles' do + Profile.destroy_all + p1 = fast_create(Profile) + p2 = fast_create(Profile) + p3 = fast_create(Profile) - Article.delete_all - fast_create(Article, :profile_id => c1, :created_at => 1.day.ago) - fast_create(Article, :profile_id => c1, :created_at => DateTime.now.beginning_of_day) - fast_create(Article, :profile_id => c2, :created_at => DateTime.now.beginning_of_day) - assert_equal [c1,c2], Community.more_active + ActionTracker::Record.destroy_all + fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => p1, :created_at => Time.now) + fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => p2, :created_at => Time.now) + fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => p2, :created_at => Time.now) + fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => p3, :created_at => Time.now) + fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => p3, :created_at => Time.now) + fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => p3, :created_at => Time.now) - fast_create(Article, :profile_id => c2, :created_at => 2.days.ago) - fast_create(Article, :profile_id => c2, :created_at => 7.days.ago) - fast_create(Article, :profile_id => c3, :created_at => 1.day.ago) - assert_equal [c2,c1,c3] , Community.more_active + assert_equal [p3,p2,p1] , Profile.more_active end - should 'the ties on more active communities be solved by the number of comments' do - env = create(Environment) - Community.delete_all - c1 = fast_create(Community) - c2 = fast_create(Community) - Article.delete_all - a1 = fast_create(Article, :profile_id => c1, :created_at => DateTime.now.beginning_of_day) - a2 = fast_create(Article, :profile_id => c2, :created_at => DateTime.now.beginning_of_day) - assert_equal [c1,c2] , Community.more_active - - p1 = fast_create(Person) - a2.comments.build(:title => 'test comment', :body => 'anything', :author => p1).save! - assert_equal [c2,c1] , Community.more_active + should 'more active profile take in consideration only actions created only in the recent delay interval' do + Profile.delete_all + ActionTracker::Record.destroy_all + recent_delay = ActionTracker::Record::RECENT_DELAY.days.ago - a1.comments.build(:title => 'test comment', :body => 'anything', :author => p1).save! - a1.comments.build(:title => 'test comment', :body => 'anything', :author => p1).save! - assert_equal [c1,c2] , Community.more_active - end + p1 = fast_create(Profile) + p2 = fast_create(Profile) - should 'more active communities take in consideration only articles created current the last week' do - c1 = fast_create(Community) - c2 = fast_create(Community) - c3 = fast_create(Community) - Article.delete_all - fast_create(Article, :profile_id => c1, :created_at => DateTime.now.beginning_of_day) - fast_create(Article, :profile_id => c2, :created_at => 10.days.ago) - assert_equal [c1] , Community.more_active + fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => p1, :created_at => recent_delay) + fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => p1, :created_at => recent_delay) + fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => p2, :created_at => recent_delay) + fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => p2, :created_at => recent_delay - 1.day) - fast_create(Article, :profile_id => c2, :created_at => DateTime.now.beginning_of_day) - fast_create(Article, :profile_id => c2, :created_at => 7.days.ago) - fast_create(Article, :profile_id => c3, :created_at => 8.days.ago) - assert_equal [c2,c1] , Community.more_active + assert_equal [p1,p2], Profile.more_active end - should 'find more popular communities' do - Community.delete_all - - c1 = fast_create(Community) - c2 = fast_create(Community) - fast_create(Community) - - p1 = fast_create(Person) - p2 = fast_create(Person) - c1.add_member(p1) - assert_equal [c1] , Community.more_popular - - c2.add_member(p1) - c2.add_member(p2) - assert_equal [c2,c1] , Community.more_popular - - c2.remove_member(p2) - c2.remove_member(p1) - assert_equal [c1] , Community.more_popular + should 'list profiles that have no actions in more active list' do + profile = fast_create(Profile) + assert_includes Profile.more_active, profile end should "return the more recent label" do @@ -1705,57 +1607,29 @@ class ProfileTest < Test::Unit::TestCase assert_equal "Since: ", p.more_recent_label end - should "return none on label if the profile hasn't articles" do + should "return no actions if profile has 0 actions" do p = fast_create(Profile) - assert_equal 0, p.articles.count - assert_equal "none", p.more_active_label + assert_equal 0, p.recent_actions.count + assert_equal "no actions", p.more_active_label end - should "return one article on label if the profile has one article" do + should "return one action on label if the profile has one action" do p = fast_create(Profile) - fast_create(Article, :profile_id => p.id) - assert_equal 1, p.articles.count - assert_equal "one article", p.more_active_label + fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => p, :created_at => Time.now) + assert_equal 1, p.recent_actions.count + assert_equal "one action", p.more_active_label end - should "return number of artciles on label if the profile has more than one article" do + should "return number of actions on label if the profile has more than one action" do p = fast_create(Profile) - fast_create(Article, :profile_id => p.id) - fast_create(Article, :profile_id => p.id) - assert_equal 2, p.articles.count - assert_equal "2 articles", p.more_active_label - - fast_create(Article, :profile_id => p.id) - assert_equal 3, p.articles.count - assert_equal "3 articles", p.more_active_label - end - - should "return none on label if the profile hasn't members" do - p = fast_create(Profile) - assert_equal 0, p.members_count - assert_equal "none", p.more_popular_label - end - - should "return one member on label if the profile has one member" do - person = fast_create(Person) - community = fast_create(Community) - community.add_member(person) - - assert_equal "one member", community.more_popular_label - end - - should "return the number of members on label if the profile has more than one member" do - person1 = fast_create(Person) - person2 = fast_create(Person) - community = fast_create(Community) - - community.add_member(person1) - community.add_member(person2) - assert_equal "2 members", community.more_popular_label - - person3 = fast_create(Person) - community.add_member(person3) - assert_equal "3 members", community.more_popular_label + fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => p, :created_at => Time.now) + fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => p, :created_at => Time.now) + assert_equal 2, p.recent_actions.count + assert_equal "2 actions", p.more_active_label + + fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => p, :created_at => Time.now) + assert_equal 3, p.recent_actions.count + assert_equal "3 actions", p.more_active_label end should 'provide list of galleries' do diff --git a/vendor/plugins/action_tracker/lib/action_tracker_model.rb b/vendor/plugins/action_tracker/lib/action_tracker_model.rb index 89a5c28..e005d9f 100644 --- a/vendor/plugins/action_tracker/lib/action_tracker_model.rb +++ b/vendor/plugins/action_tracker/lib/action_tracker_model.rb @@ -20,6 +20,11 @@ module ActionTracker alias_method :subject, :user + # In days + RECENT_DELAY = 30 + + named_scope :recent, :conditions => ['created_at >= ?', RECENT_DELAY.days.ago] + def self.current_user_from_model u = new u.valid? diff --git a/vendor/plugins/action_tracker/test/action_tracker_model_test.rb b/vendor/plugins/action_tracker/test/action_tracker_model_test.rb index 01af3d2..7fbd0fe 100644 --- a/vendor/plugins/action_tracker/test/action_tracker_model_test.rb +++ b/vendor/plugins/action_tracker/test/action_tracker_model_test.rb @@ -356,4 +356,10 @@ class ActionTrackerModelTest < ActiveSupport::TestCase assert_equal(["foo 1", "bar 2"], t.collect_group_with_index(:test){|x, i| "#{x} #{i+1}" }) end + def test_recent_filter_actions + ActionTracker::Record.destroy_all + t1 = ActionTracker::Record.create!(:user => SomeModel.create!, :verb => :some_verb, :created_at => Time.now) + t2 = ActionTracker::Record.create!(:user => SomeModel.create!, :verb => :some_verb, :created_at => ActionTracker::Record::RECENT_DELAY.days.ago - 1.day) + assert_equal [t1], ActionTracker::Record.recent.all + end end -- libgit2 0.21.2