diff --git a/app/controllers/my_profile/profile_members_controller.rb b/app/controllers/my_profile/profile_members_controller.rb index 227848c..2a2bfb2 100644 --- a/app/controllers/my_profile/profile_members_controller.rb +++ b/app/controllers/my_profile/profile_members_controller.rb @@ -10,7 +10,11 @@ class ProfileMembersController < MyProfileController def update_roles @roles = params[:roles] ? environment.roles.find(params[:roles].select{|r|!r.to_i.zero?}) : [] @roles = @roles.select{|r| r.has_kind?('Profile') } - @person = profile.members.find { |m| m.id == params[:person].to_i } + begin + @person = profile.members.find(params[:person]) + rescue ActiveRecord::RecordNotFound + @person = nil + end if @person && @person.define_roles(@roles, profile) session[:notice] = _('Roles successfuly updated') else @@ -21,7 +25,11 @@ class ProfileMembersController < MyProfileController def change_role @roles = Profile::Roles.organization_member_roles(environment.id) - @member = profile.members.find { |m| m.id == params[:id].to_i } + begin + @member = profile.members.find(params[:id]) + rescue ActiveRecord::RecordNotFound + @member = nil + end if @member @associations = @member.find_roles(@profile) else diff --git a/app/models/communities_block.rb b/app/models/communities_block.rb index 086fdb7..0e9ef2e 100644 --- a/app/models/communities_block.rb +++ b/app/models/communities_block.rb @@ -32,28 +32,8 @@ class CommunitiesBlock < ProfileListBlock end end - def profile_count - if owner.kind_of?(Environment) - owner.communities.count(:conditions => { :visible => true }) - else - owner.communities(:visible => true).count - end - end - - def profile_finder - @profile_finder ||= CommunitiesBlock::Finder.new(self) - end - - class Finder < ProfileListBlock::Finder - def ids - # FIXME when owner is an environment (i.e. listing communities globally - # this can become SLOW) - if block.owner.kind_of?(Environment) - block.owner.communities.all(:conditions => {:visible => true}, :limit => block.limit, :order => Noosfero::SQL.random_function).map(&:id) - else - block.owner.communities(:visible => true).map(&:id) - end - end + def profiles + owner.communities end end diff --git a/app/models/enterprises_block.rb b/app/models/enterprises_block.rb index 3c30499..309a4d2 100644 --- a/app/models/enterprises_block.rb +++ b/app/models/enterprises_block.rb @@ -28,29 +28,8 @@ class EnterprisesBlock < ProfileListBlock end end - def profile_count - if owner.kind_of?(Environment) - owner.enterprises.count(:conditions => { :visible => true }) - else - owner.enterprises(:visible => true).count - end - - end - - def profile_finder - @profile_finder ||= EnterprisesBlock::Finder.new(self) - end - - class Finder < ProfileListBlock::Finder - def ids - # FIXME when owner is an environment (i.e. listing enterprises globally - # this can become SLOW) - if block.owner.kind_of?(Environment) - block.owner.enterprises.all(:conditions => {:visible => true}, :limit => block.limit, :order => Noosfero::SQL.random_function).map(&:id) - else - block.owner.enterprises.select(&:visible).map(&:id) - end - end + def profiles + owner.enterprises end end diff --git a/app/models/environment.rb b/app/models/environment.rb index 0d49f3e..1664168 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -70,7 +70,7 @@ class Environment < ActiveRecord::Base end def admins - self.members_by_role(Environment::Roles.admin(self.id)) + Person.members_of(self).all(:conditions => ['role_assignments.role_id = ?', Environment::Roles.admin(self).id]) end # returns the available features for a Environment, in the form of a diff --git a/app/models/favorite_enterprises_block.rb b/app/models/favorite_enterprises_block.rb index 2cb14d3..e4b407b 100644 --- a/app/models/favorite_enterprises_block.rb +++ b/app/models/favorite_enterprises_block.rb @@ -20,18 +20,8 @@ class FavoriteEnterprisesBlock < ProfileListBlock end end - def profile_count - owner.favorite_enterprises.count - end - - def profile_finder - @profile_finder ||= FavoriteEnterprisesBlock::Finder.new(self) - end - - class Finder < ProfileListBlock::Finder - def ids - block.owner.favorite_enterprises.map(&:id) - end + def profiles + owner.favorite_enterprises end end diff --git a/app/models/friends_block.rb b/app/models/friends_block.rb index 11b1204..4fbde10 100644 --- a/app/models/friends_block.rb +++ b/app/models/friends_block.rb @@ -19,18 +19,8 @@ class FriendsBlock < ProfileListBlock end end - class FriendsBlock::Finder < ProfileListBlock::Finder - def ids - self.block.owner.friend_ids - end - end - - def profile_finder - @profile_finder ||= FriendsBlock::Finder.new(self) - end - - def profile_count - owner.friends.visible.count + def profiles + owner.friends end end diff --git a/app/models/members_block.rb b/app/models/members_block.rb index 5cd590d..f13dfc0 100644 --- a/app/models/members_block.rb +++ b/app/models/members_block.rb @@ -19,20 +19,8 @@ class MembersBlock < ProfileListBlock end end - def profile_count - owner.members.select {|member| member.visible? }.count + def profiles + owner.members end - def profile_finder - @profile_finder ||= MembersBlock::Finder.new(self) - end - - # Finds random members, up to the limit. - class Finder < ProfileListBlock::Finder - def ids - block.owner.members.select {|member| member.visible? }.map(&:id) - end - end - - end diff --git a/app/models/people_block.rb b/app/models/people_block.rb index 2714502..913144b 100644 --- a/app/models/people_block.rb +++ b/app/models/people_block.rb @@ -12,14 +12,8 @@ class PeopleBlock < ProfileListBlock _('Random people') end - def profile_finder - @profile_finder ||= PeopleBlock::Finder.new(self) - end - - class Finder < ProfileListBlock::Finder - def ids - block.owner.people.visible.all(:limit => block.limit, :order => Noosfero::SQL.random_function).map(&:id) - end + def profiles + owner.people end def footer @@ -28,8 +22,4 @@ class PeopleBlock < ProfileListBlock end end - def profile_count - owner.people.visible.count - end - end diff --git a/app/models/person.rb b/app/models/person.rb index ec0571a..9fce90a 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -3,6 +3,12 @@ class Person < Profile acts_as_accessor + named_scope :members_of, lambda { |resource| { :select => 'DISTINCT profiles.*', :joins => :role_assignments, :conditions => ['role_assignments.resource_type = ? AND role_assignments.resource_id = ?', resource.class.base_class.name, resource.id ] } } + + def memberships + Profile.memberships_of(self) + end + has_many :friendships, :dependent => :destroy has_many :friends, :class_name => 'Person', :through => :friendships @@ -132,27 +138,14 @@ class Person < Profile new_conditions end - def memberships(conditions = {}) - # FIXME this should be a proper ActiveRecord relationship! - Profile.find( - :all, - :conditions => self.class.conditions_for_profiles(conditions, self), - :joins => "LEFT JOIN role_assignments ON profiles.id = role_assignments.resource_id AND role_assignments.resource_type = \'#{Profile.base_class.name}\'", - :select => 'profiles.*').uniq + def enterprises + memberships.enterprises end - def enterprise_memberships(conditions = {}) - memberships({:type => Enterprise.name}.merge(conditions)) + def communities + memberships.communities end - alias :enterprises :enterprise_memberships - - def community_memberships(conditions = {}) - memberships({:type => Community.name}.merge(conditions)) - end - - alias :communities :community_memberships - validates_presence_of :user_id validates_uniqueness_of :user_id diff --git a/app/models/profile.rb b/app/models/profile.rb index 425614a..8cec437 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -51,6 +51,18 @@ class Profile < ActiveRecord::Base acts_as_accessible + 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 ] } } + named_scope :enterprises, :conditions => "profiles.type = 'Enterprise'" + named_scope :communities, :conditions => "profiles.type = 'Community'" + + def members + Person.members_of(self) + end + + def members_by_role(role) + Person.members_of(self).all(:conditions => ['role_assignments.role_id = ?', role.id]) + end + acts_as_having_boxes acts_as_searchable :additional_fields => [ :extra_data_for_index ] diff --git a/app/models/profile_list_block.rb b/app/models/profile_list_block.rb index 13e6ba7..c85bf9b 100644 --- a/app/models/profile_list_block.rb +++ b/app/models/profile_list_block.rb @@ -6,48 +6,18 @@ class ProfileListBlock < Block _('Random profiles') end - # Override this method to make the block list specific types of profiles - # instead of anyone. - # - # In this class this method just returns Profile (the class). In - # subclasses you could return Person, for instance, if you only want - # to list people, or Organization, if you want organizations only. - # - # You don't need to return only classes. You can for instance return an - # association array from a has_many ActiveRecord association, for example. - # Actually the only requirement for the object returned by this method is to - # have a find method that accepts the same interface as the - # ActiveRecord::Base's find method . - def profile_finder - @profile_finder ||= ProfileListBlock::Finder.new(self) + # override in subclasses! + def profiles + owner.profiles end - # Default finder. Finds the most recently added profiles. - class Finder - def initialize(block) - @block = block - end - attr_reader :block - def find - id_list = self.ids - result = [] - [block.limit, id_list.size].min.times do - i = pick_random(id_list.size) - result << Profile.find(id_list[i]) - id_list.delete_at(i) - end - result - end - def pick_random(top) - rand(top) - end - def ids - block.owner.profiles.visible.all(:limit => block.limit, :order => Noosfero::SQL.random_function).map(&:id) - end + def profile_list + random = Noosfero::SQL.random_function + profiles.visible.all(:limit => limit, :select => 'DISTINCT profiles.*, ' + random, :order => random) end - def profiles - profile_finder.find + def profile_count + profiles.visible.count end # the title of the block. Probably will be overriden in subclasses. @@ -60,7 +30,7 @@ class ProfileListBlock < Block end def content - profiles = self.profiles + profiles = self.profile_list title = self.view_title nl = "\n" link_method = profile_image_link_method @@ -89,8 +59,4 @@ class ProfileListBlock < Block title.gsub('{#}', profile_count.to_s) end - def profile_count - owner.profiles.visible.count - end - end diff --git a/app/views/blocks/my_network/person.rhtml b/app/views/blocks/my_network/person.rhtml index 9099aa0..2aa9f85 100644 --- a/app/views/blocks/my_network/person.rhtml +++ b/app/views/blocks/my_network/person.rhtml @@ -3,8 +3,8 @@ content_tag('b', owner.articles.count), owner.public_profile_url.merge(:action => 'sitemap') ) %>
  • <%= link_to(n__('One friend', '%s friends', owner.friends.count) % content_tag('b', owner.friends.count), owner.public_profile_url.merge(:action => 'friends')) %>
  • -
  • <%= link_to(n__('One community', '%{num} communities', owner.communities(:visible => true).size) % - {:num => content_tag('b', owner.communities.size)}, owner.public_profile_url.merge(:action => 'communities')) %>
  • +
  • <%= link_to(n__('One community', '%{num} communities', owner.communities.visible.size) % + {:num => content_tag('b', owner.communities.visible.size)}, owner.public_profile_url.merge(:action => 'communities')) %>
  • <%= link_to(n_('One tag', '%s tags', owner.article_tags.size) % content_tag('b', owner.article_tags.size), owner.public_profile_url.merge(:action => 'tags')) %>
  • diff --git a/test/unit/add_member_test.rb b/test/unit/add_member_test.rb index fa363e9..c6d76a2 100644 --- a/test/unit/add_member_test.rb +++ b/test/unit/add_member_test.rb @@ -12,10 +12,9 @@ class AddMemberTest < ActiveSupport::TestCase c.update_attribute(:closed, true) TaskMailer.stubs(:deliver_target_notification) task = fast_create(AddMember, :requestor_id => p.id, :target_id => c.id, :target_type => 'Community') - assert_difference c, :members, [p] do - task.finish - c.reload - end + task.finish + + assert_equal [p], c.members end should 'require requestor' do diff --git a/test/unit/communities_block_test.rb b/test/unit/communities_block_test.rb index a4cc7db..9358764 100644 --- a/test/unit/communities_block_test.rb +++ b/test/unit/communities_block_test.rb @@ -15,32 +15,16 @@ class CommunitiesBlockTest < Test::Unit::TestCase assert_not_equal ProfileListBlock.description, CommunitiesBlock.description end - should 'use its own finder' do - assert_not_equal CommunitiesBlock::Finder, ProfileListBlock::Finder - assert_kind_of CommunitiesBlock::Finder, CommunitiesBlock.new.profile_finder - end - should 'list owner communities' do - block = CommunitiesBlock.new - block.limit = 2 owner = mock - block.expects(:owner).at_least_once.returns(owner) - - community1 = mock; community1.stubs(:id).returns(1); community1.stubs(:visible).returns(true) - community2 = mock; community2.stubs(:id).returns(2); community2.stubs(:visible).returns(true) - community3 = mock; community3.stubs(:id).returns(3); community3.stubs(:visible).returns(true) + block.stubs(:owner).returns(owner) - owner.expects(:communities).returns([community1, community2, community3]) - - block.profile_finder.expects(:pick_random).with(3).returns(2) - block.profile_finder.expects(:pick_random).with(2).returns(0) + list = [] + owner.stubs(:communities).returns(list) - Profile.expects(:find).with(3).returns(community3) - Profile.expects(:find).with(1).returns(community1) - - assert_equal [community3, community1], block.profiles + assert_same list, block.profiles end should 'link to all communities of profile' do @@ -85,86 +69,4 @@ class CommunitiesBlockTest < Test::Unit::TestCase assert_equivalent [public_community, private_community], block.profiles end - should 'not list non-visible communities' do - user = create_user('testuser').person - - visible_community = fast_create(Community, :environment_id => Environment.default.id) - visible_community.add_member(user) - - not_visible_community = fast_create(Community, :environment_id => Environment.default.id, :visible => false) - not_visible_community.add_member(user) - - block = CommunitiesBlock.new - block.expects(:owner).at_least_once.returns(user) - - assert_equal [visible_community], block.profiles - end - - should 'count number of owner communities' do - user = create_user('testuser').person - - community1 = fast_create(Community, :environment_id => Environment.default.id, :visible => true) - community1.add_member(user) - - community2 = fast_create(Community, :environment_id => Environment.default.id, :visible => true) - community2.add_member(user) - - block = CommunitiesBlock.new - block.expects(:owner).at_least_once.returns(user) - - assert_equal 2, block.profile_count - end - - should 'count non-public profile communities' do - user = create_user('testuser').person - - community_public = fast_create(Community, :environment_id => Environment.default.id, :public_profile => true) - community_public.add_member(user) - - community_private = fast_create(Community, :public_profile => false) - community_private.add_member(user) - - block = CommunitiesBlock.new - block.expects(:owner).at_least_once.returns(user) - - assert_equal 2, block.profile_count - end - - should 'not count non-visible profile communities' do - user = create_user('testuser').person - - visible_community = fast_create(Community, :name => 'tcommunity 1', :identifier => 'comm1', :visible => true) - visible_community.add_member(user) - - not_visible_community = fast_create(Community, :name => ' community 2', :identifier => 'comm2', :visible => false) - not_visible_community.add_member(user) - - block = CommunitiesBlock.new - block.expects(:owner).at_least_once.returns(user) - - assert_equal 1, block.profile_count - end - - should 'count non-public environment communities' do - community_public = fast_create(Community, :name => 'tcommunity 1', :identifier => 'comm1', :public_profile => true) - - community_private = fast_create(Community, :name => ' community 2', :identifier => 'comm2', :public_profile => false) - - block = CommunitiesBlock.new - block.expects(:owner).at_least_once.returns(Environment.default) - - assert_equal 2, block.profile_count - end - - should 'not count non-visible environment communities' do - visible_community = fast_create(Community, :name => 'tcommunity 1', :identifier => 'comm1', :visible => true) - - not_visible_community = fast_create(Community, :name => ' community 2', :identifier => 'comm2', :visible => false) - - block = CommunitiesBlock.new - block.expects(:owner).at_least_once.returns(Environment.default) - - assert_equal 1, block.profile_count - end - end diff --git a/test/unit/create_enterprise_test.rb b/test/unit/create_enterprise_test.rb index 67e2e4c..1375875 100644 --- a/test/unit/create_enterprise_test.rb +++ b/test/unit/create_enterprise_test.rb @@ -99,6 +99,7 @@ class CreateEnterpriseTest < Test::Unit::TestCase should 'actually create an enterprise when finishing the task and associate the task requestor as its owner through the "user" association' do environment = fast_create(Environment) + environment.create_roles region = fast_create(Region, :name => 'My region', :environment_id => environment.id) validator = fast_create(Organization, :name => "My organization", :identifier => 'myorg', :environment_id => environment.id) region.validators << validator @@ -135,6 +136,7 @@ class CreateEnterpriseTest < Test::Unit::TestCase should 'actually create an enterprise when finishing the task and associate the task requestor as its owner through the "user" association even when environment is not default' do environment = fast_create(Environment) + environment.create_roles region = fast_create(Region, :name => 'My region', :environment_id => environment.id) validator = fast_create(Organization, :name => "My organization", :identifier => 'myorg', :environment_id => environment.id) region.validators << validator diff --git a/test/unit/enterprises_block_test.rb b/test/unit/enterprises_block_test.rb index 06e297f..af63b5f 100644 --- a/test/unit/enterprises_block_test.rb +++ b/test/unit/enterprises_block_test.rb @@ -15,85 +15,16 @@ class EnterprisesBlockTest < Test::Unit::TestCase assert_not_equal ProfileListBlock.description, EnterprisesBlock.description end - should 'use its own finder' do - assert_not_equal EnterprisesBlock::Finder, ProfileListBlock::Finder - assert_kind_of EnterprisesBlock::Finder, EnterprisesBlock.new.profile_finder - end - should 'list owner enterprises' do block = EnterprisesBlock.new - block.limit = 2 owner = mock block.expects(:owner).at_least_once.returns(owner) - member1 = stub(:id => 1, :visible => true ) - member2 = stub(:id => 2, :visible => true ) - member3 = stub(:id => 3, :visible => true ) - - owner.expects(:enterprises).returns([member1, member2, member3]) - - block.profile_finder.expects(:pick_random).with(3).returns(2) - block.profile_finder.expects(:pick_random).with(2).returns(0) - - Profile.expects(:find).with(3).returns(member3) - Profile.expects(:find).with(1).returns(member1) - - assert_equal [member3, member1], block.profiles - end - - should 'list private enterprises in environment' do - env = Environment.create!(:name => 'test_env') - enterprise1 = fast_create(Enterprise, :environment_id => env.id, :public_profile => true) - enterprise2 = fast_create(Enterprise, :environment_id => env.id, :public_profile => false) #private profile - block = EnterprisesBlock.new - env.boxes.first.blocks << block - block.save! - ids = block.profile_finder.ids - assert_includes ids, enterprise1.id - assert_includes ids, enterprise2.id - end + list = [] + owner.expects(:enterprises).returns(list) - should 'not list invisible enterprises in environment' do - env = Environment.create!(:name => 'test_env') - enterprise1 = fast_create(Enterprise, :environment_id => env.id, :visible => true) - enterprise2 = fast_create(Enterprise, :environment_id => env.id, :visible => false) #invisible profile - block = EnterprisesBlock.new - env.boxes.first.blocks << block - block.save! - ids = block.profile_finder.ids - assert_includes ids, enterprise1.id - assert_not_includes ids, enterprise2.id - end - - should 'list private enterprises in profile' do - person = create_user('testuser').person - enterprise1 = fast_create(Enterprise, :public_profile => true) - role = Profile::Roles.member(enterprise1.environment.id) - enterprise1.affiliate(person, role) - enterprise2 = fast_create(Enterprise, :public_profile => false) - enterprise2.affiliate(person, role) - block = EnterprisesBlock.new - person.boxes.first.blocks << block - block.save! - ids = block.profile_finder.ids - assert_includes ids, enterprise1.id - assert_includes ids, enterprise2.id - end - - should 'not list invisible enterprises in profile' do - person = create_user('testuser').person - enterprise1 = fast_create(Enterprise, :visible => true) - role = Profile::Roles.member(enterprise1.environment.id) - enterprise1.affiliate(person, role) - enterprise2 = fast_create(Enterprise, :visible => false) - enterprise2.affiliate(person, role) - block = EnterprisesBlock.new - person.boxes.first.blocks << block - block.save! - ids = block.profile_finder.ids - assert_includes ids, enterprise1.id - assert_not_includes ids, enterprise2.id + assert_same list, block.profiles end should 'link to all enterprises for profile' do @@ -139,61 +70,4 @@ class EnterprisesBlockTest < Test::Unit::TestCase assert_equal 2, block.profile_count end - should 'count non-public person enterprises' do - user = fast_create(Person) - - ent1 = fast_create(Enterprise, :public_profile => true) - ent1.expects(:closed?).returns(false) - ent1.add_member(user) - - ent2 = fast_create(Enterprise, :public_profile => false) - ent2.expects(:closed?).returns(false) - ent2.add_member(user) - - block = EnterprisesBlock.new - block.expects(:owner).at_least_once.returns(user) - - assert_equal 2, block.profile_count - end - - should 'not count non-visible person enterprises' do - user = fast_create(Person) - - ent1 = fast_create(Enterprise, :visible => true) - ent1.expects(:closed?).returns(false) - ent1.add_member(user) - - ent2 = fast_create(Enterprise, :visible => false) - ent2.expects(:closed?).returns(false) - ent2.add_member(user) - - block = EnterprisesBlock.new - block.expects(:owner).at_least_once.returns(user) - - assert_equal 1, block.profile_count - end - - - should 'count non-public environment enterprises' do - env = fast_create(Environment) - ent1 = fast_create(Enterprise, :environment_id => env.id, :public_profile => true) - ent2 = fast_create(Enterprise, :environment_id => env.id, :public_profile => false) - - block = EnterprisesBlock.new - block.expects(:owner).at_least_once.returns(env) - - assert_equal 2, block.profile_count - end - - should 'not count non-visible environment enterprises' do - env = fast_create(Environment) - ent1 = fast_create(Enterprise, :name => 'test enterprise 1', :identifier => 'ent1', :environment_id => env.id, :visible => true) - ent2 = fast_create(Enterprise, :name => 'test enterprise 2', :identifier => 'ent2', :environment_id => env.id, :visible => false) - - block = EnterprisesBlock.new - block.expects(:owner).at_least_once.returns(env) - - assert_equal 1, block.profile_count - end - end diff --git a/test/unit/favorite_enterprises_block_test.rb b/test/unit/favorite_enterprises_block_test.rb index e673b0c..a8f92c1 100644 --- a/test/unit/favorite_enterprises_block_test.rb +++ b/test/unit/favorite_enterprises_block_test.rb @@ -14,65 +14,17 @@ class FavoriteEnterprisesBlockTest < ActiveSupport::TestCase assert_not_equal ProfileListBlock.description, FavoriteEnterprisesBlock.description end - should 'use its own finder' do - assert_not_equal FavoriteEnterprisesBlock::Finder, ProfileListBlock::Finder - assert_kind_of FavoriteEnterprisesBlock::Finder, FavoriteEnterprisesBlock.new.profile_finder - end - should 'list owner favorite enterprises' do block = FavoriteEnterprisesBlock.new - block.limit = 2 owner = mock block.expects(:owner).returns(owner) - member1 = mock; member1.stubs(:id).returns(1) - member2 = mock; member2.stubs(:id).returns(2) - member3 = mock; member3.stubs(:id).returns(3) - - owner.expects(:favorite_enterprises).returns([member1, member2, member3]) + list = [] + owner.expects(:favorite_enterprises).returns(list) - block.profile_finder.expects(:pick_random).with(3).returns(2) - block.profile_finder.expects(:pick_random).with(2).returns(0) - - Profile.expects(:find).with(3).returns(member3) - Profile.expects(:find).with(1).returns(member1) - - assert_equal [member3, member1], block.profiles - end - - should 'link to all enterprises for person' do - person = Person.new - person.expects(:identifier).returns('theprofile') - block = FavoriteEnterprisesBlock.new - block.expects(:owner).returns(person) - - expects(:__).with('View all').returns('View all enterprises') - expects(:link_to).with('View all enterprises', :controller => 'profile', :profile => 'theprofile', :action => 'favorite_enterprises') - - instance_eval(&block.footer) - end - - should 'give empty footer for unsupported owner type' do - block = FavoriteEnterprisesBlock.new - block.expects(:owner).returns(1) - assert_equal '', block.footer - end - - should 'count number of owner favorite enterprises' do - user = create_user('testuser').person - - ent1 = fast_create(Enterprise, :name => 'test enterprise 1', :identifier => 'ent1') - - ent2 = fast_create(Enterprise, :name => 'test enterprise 2', :identifier => 'ent2') - - user.favorite_enterprises << [ent1, ent2] - - block = FavoriteEnterprisesBlock.new - block.expects(:owner).at_least_once.returns(user) - - assert_equal 2, block.profile_count + assert_same list, block.profiles end end diff --git a/test/unit/friends_block_test.rb b/test/unit/friends_block_test.rb index 9004ff1..106920a 100644 --- a/test/unit/friends_block_test.rb +++ b/test/unit/friends_block_test.rb @@ -11,11 +11,6 @@ class FriendsBlockTest < ActiveSupport::TestCase assert_not_equal ProfileListBlock.new.default_title, FriendsBlock.new.default_title end - should 'use its own finder' do - assert_not_equal ProfileListBlock::Finder, FriendsBlock::Finder - assert_kind_of FriendsBlock::Finder, FriendsBlock.new.profile_finder - end - should 'list owner friends' do p1 = create_user('testuser1').person p2 = create_user('testuser2').person diff --git a/test/unit/members_block_test.rb b/test/unit/members_block_test.rb index 4ae2647..b5e997a 100644 --- a/test/unit/members_block_test.rb +++ b/test/unit/members_block_test.rb @@ -28,74 +28,14 @@ class MembersBlockTest < Test::Unit::TestCase should 'pick random members' do block = MembersBlock.new - block.limit = 2 - block.save! owner = mock block.expects(:owner).returns(owner) - member1 = stub(:id => 1, :visible? => true) - member2 = stub(:id => 2, :visible? => true) - member3 = stub(:id => 3, :visible? => true) - - owner.expects(:members).returns([member1, member2, member3]) - - block.profile_finder.expects(:pick_random).with(3).returns(2) - block.profile_finder.expects(:pick_random).with(2).returns(0) - - Profile.expects(:find).with(3).returns(member3) - Profile.expects(:find).with(1).returns(member1) - - assert_equal [member3, member1], block.profiles - end - - should 'count number of owner members' do - profile = create_user('mytestuser').person - owner = mock - - member1 = mock - member2 = mock - member3 = mock - - member1.stubs(:visible?).returns(true) - member2.stubs(:visible?).returns(true) - member3.stubs(:visible?).returns(true) - - owner.expects(:members).returns([member1, member2, member3]) + list = [] + owner.expects(:members).returns(list) - block = MembersBlock.new - block.expects(:owner).returns(owner) - assert_equal 3, block.profile_count - end - - should 'count non-public community members' do - community = fast_create(Community) - - private_p = fast_create(Person, :public_profile => false) - public_p = fast_create(Person, :public_profile => true) - - community.add_member(private_p) - community.add_member(public_p) - - block = MembersBlock.new - block.expects(:owner).at_least_once.returns(community) - - assert_equal 2, block.profile_count - end - - should 'not count non-visible community members' do - community = fast_create(Community) - - private_p = fast_create(Person, :visible => false) - public_p = fast_create(Person, :visible => true) - - community.add_member(private_p) - community.add_member(public_p) - - block = MembersBlock.new - block.expects(:owner).at_least_once.returns(community) - - assert_equal 1, block.profile_count + assert_same list, block.profiles end end diff --git a/test/unit/people_block_test.rb b/test/unit/people_block_test.rb index 5eb7796..e9c368b 100644 --- a/test/unit/people_block_test.rb +++ b/test/unit/people_block_test.rb @@ -18,11 +18,6 @@ class PeopleBlockTest < ActiveSupport::TestCase assert_not_equal ProfileListBlock.new.help, PeopleBlock.new.help end - should 'use its own finder' do - assert_not_equal ProfileListBlock::Finder, PeopleBlock::Finder - assert_kind_of PeopleBlock::Finder, PeopleBlock.new.profile_finder - end - should 'list people' do owner = fast_create(Environment) block = PeopleBlock.new @@ -49,24 +44,6 @@ class PeopleBlockTest < ActiveSupport::TestCase instance_eval(&block.footer) end - should 'count number of public and private people' do - env = Environment.create!(:name => 'test environment') - private_p = fast_create(Person, :environment_id => env.id, :public_profile => false) - public_p = fast_create(Person, :environment_id => env.id, :public_profile => true) - - env.boxes.first.blocks << block = PeopleBlock.new - assert_equal 2, block.profile_count - end - - should 'count number of visible people' do - env = Environment.create!(:name => 'test environment') - invisible_p = fast_create(Person, :environment_id => env.id, :visible => false) - visible_p = fast_create(Person, :environment_id => env.id, :visible => true) - - env.boxes.first.blocks << block = PeopleBlock.new - assert_equal 1, block.profile_count - end - protected def content_tag(tag, text, options = {}) diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb index ffe29c2..e5c0b0d 100644 --- a/test/unit/person_test.rb +++ b/test/unit/person_test.rb @@ -32,7 +32,7 @@ class PersonTest < Test::Unit::TestCase e.affiliate(p, member_role) assert p.memberships.include?(e) - assert p.enterprise_memberships.include?(e) + assert p.enterprises.include?(e) end should 'belong to communities' do @@ -41,7 +41,7 @@ class PersonTest < Test::Unit::TestCase c.add_member(p) - assert p.community_memberships.include?(c), "Community should add a new member" + assert p.communities.include?(c), "Community should add a new member" end should 'be associated with a user' do diff --git a/test/unit/profile_list_block_test.rb b/test/unit/profile_list_block_test.rb index 50ee43e..81148fe 100644 --- a/test/unit/profile_list_block_test.rb +++ b/test/unit/profile_list_block_test.rb @@ -19,22 +19,17 @@ class ProfileListBlockTest < Test::Unit::TestCase end should 'list people' do - User.destroy_all - person1 = create_user('testperson1').person - person2 = create_user('testperson2').person - person3 = create_user('testperson3').person + env = fast_create(Environment) - owner = fast_create(Environment) - owner.boxes << Box.new - block = ProfileListBlock.new - owner.boxes.first.blocks << block - block.save! + person1 = create_user('testperson1', :environment => env).person + person2 = create_user('testperson2', :environment => env).person + person3 = create_user('testperson3', :environment => env).person - profiles = [person1, person3] - block.expects(:profiles).returns(profiles) + block = ProfileListBlock.new + block.stubs(:owner).returns(env) self.expects(:profile_image_link).with(person1).once - self.expects(:profile_image_link).with(person2).never + self.expects(:profile_image_link).with(person2).once self.expects(:profile_image_link).with(person3).once self.expects(:content_tag).returns('
    ').at_least_once @@ -52,9 +47,9 @@ class ProfileListBlockTest < Test::Unit::TestCase env.boxes.first.blocks << block block.save! - ids = block.profile_finder.ids - assert_includes ids, profile1.id - assert_includes ids, profile2.id + profiles = block.profiles + assert_includes profiles, profile1 + assert_includes profiles, profile2 end should 'not list invisible profiles' do @@ -66,21 +61,9 @@ class ProfileListBlockTest < Test::Unit::TestCase env.boxes.first.blocks << block block.save! - ids = block.profile_finder.ids - assert_includes ids, profile1.id - assert_not_includes ids, profile2.id - end - - should 'use finders to find profiles to be listed' do - block = ProfileListBlock.new - finder = mock - block.expects(:profile_finder).returns(finder).once - finder.expects(:find) - block.profiles - end - - should 'provide random numbers' do - assert_respond_to ProfileListBlock::Finder.new(nil), :pick_random + profiles = block.profile_list + assert_includes profiles, profile1 + assert_not_includes profiles, profile2 end should 'provide view_title' do @@ -139,4 +122,32 @@ class ProfileListBlockTest < Test::Unit::TestCase assert_equal 3, block.profile_count end + should 'respect limit when listing profiles' do + env = fast_create(Environment) + p1 = fast_create(Person, :environment_id => env.id) + p2 = fast_create(Person, :environment_id => env.id) + p3 = fast_create(Person, :environment_id => env.id) + p4 = fast_create(Person, :environment_id => env.id) + + block = ProfileListBlock.new(:limit => 3) + block.stubs(:owner).returns(env) + + assert_equal 3, block.profile_list.size + end + + should 'list random profiles' do + env = fast_create(Environment) + p1 = fast_create(Person, :environment_id => env.id) + p2 = fast_create(Person, :environment_id => env.id) + p3 = fast_create(Person, :environment_id => env.id) + + # force the "random" function to be something we know + Noosfero::SQL.stubs(:random_function).returns('-id') + + block = ProfileListBlock.new + block.stubs(:owner).returns(env) + + assert_equal [p3.id, p2.id, p1.id], block.profile_list.map(&:id) + end + end diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index eb52f9e..4a31241 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -1771,7 +1771,7 @@ class ProfileTest < Test::Unit::TestCase end should "return the number of members on label if the profile has more than one member" do - p1 = fast_create(Profile) + p1 = fast_create(Person) p2 = fast_create(Person) c = fast_create(Community) c.add_member(p1) diff --git a/vendor/plugins/access_control/lib/acts_as_accessible.rb b/vendor/plugins/access_control/lib/acts_as_accessible.rb index 8ba8d67..d4cca61 100644 --- a/vendor/plugins/access_control/lib/acts_as_accessible.rb +++ b/vendor/plugins/access_control/lib/acts_as_accessible.rb @@ -24,13 +24,6 @@ class ActiveRecord::Base role_assignments.map{|ra|ra.destroy if roles.include?(ra.role) && ra.accessor == accessor} end - def members - role_assignments.map(&:accessor).uniq - end - def members_by_role(role) - role_assignments.select{|i| i.role.key == role.key}.map(&:accessor).uniq - end - def roles Role.find_all_by_environment_id(environment.id).select do |r| r.permissions.any?{ |p| PERMISSIONS[self.class.base_class.name].include?(p) } -- libgit2 0.21.2