Skip to content

Commit

Permalink
Extend specs and enhance adding&invalidation of memberships
Browse files Browse the repository at this point in the history
  • Loading branch information
jbx26 committed Jan 18, 2015
1 parent 1fc9aae commit 5473944
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def corporations
# Find corporation groups of a certain user.
#
def find_corporation_groups_of( user )
ancestor_groups_of_user = user.ancestor_groups
ancestor_groups_of_user = user.groups
corporation_groups = Group.find_corporation_groups if Group.find_corporations_parent_group
return ancestor_groups_of_user & corporation_groups if ancestor_groups_of_user and corporation_groups
end
Expand Down Expand Up @@ -112,7 +112,7 @@ def find_corporations_branch_groups
# are displayed separately.
#
def find_corporations_branch_groups_of( user )
ancestor_groups = user.ancestor_groups
ancestor_groups = user.groups
corporations_branch = self.find_corporations_branch_groups
return ancestor_groups & corporations_branch if ancestor_groups and corporations_branch
end
Expand All @@ -127,7 +127,7 @@ def find_corporations_branch_groups_of( user )
# in the groups list, e.g. attendee groups of corporation events.
#
def find_non_corporations_branch_groups_of( user )
ancestor_groups = user.ancestor_groups
ancestor_groups = user.groups
corporations_branch = self.find_corporations_branch_groups
corporations_branch = [] unless corporations_branch
return ancestor_groups -
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ def assign_user( user, options = {} )
membership = UserGroupMembership.create(user: user, group: self)
time_of_joining = options[:joined_at] || options[:at] || options[:time] || Time.zone.now
membership.update_attribute(:valid_from, time_of_joining)
# force evaluation of User#groups to fill cache
user.groups(true)
return membership
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ def set_valid_from_to_now(force = false)
def make_invalid(time = Time.zone.now)
time = time[:at] if time.kind_of?(Hash) && time[:at]
self.update_attribute(:valid_to, time)
# force evaluation of User#groups to fill cache
self.user.groups(true)
return self
end

Expand Down Expand Up @@ -191,4 +193,4 @@ class Array
def started_after(time)
self.select { |membership| membership.valid_from && membership.valid_from >= time }
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@corporation_group_of_user.parent_groups << @corporations_parent_group
@subgroup = create( :group ); @subgroup.parent_groups << @corporation_group_of_user
@user = create( :user ); @user.parent_groups << @subgroup
@non_corporations_branch_group = create( :group ); @non_corporations_branch_group.child_users << @user
@non_corporations_branch_group = create( :group ); @non_corporations_branch_group.assign_user(@user)
end

describe ".create_corporations_parent_group" do
Expand Down Expand Up @@ -65,7 +65,33 @@

describe ".find_corporation_groups_of( user )" do
subject { Group.find_corporation_groups_of( @user ) }
it { should == [ @corporation_group_of_user ] }
describe "at the begin" do
it "should contain all groups of user which are at the same time corporations" do
subject.should == [ @corporation_group_of_user ]
end
end
describe "with a second corporation" do
before do
# force evaluation of User#groups to fill cache
@user.groups.collect{|x|x.title}
@second_corporation_group_of_user = create( :group )
@second_corporation_group_of_user.parent_groups << @corporations_parent_group
@subgroup2 = create( :group ); @subgroup2.parent_groups << @second_corporation_group_of_user
@subgroup2.assign_user(@user)
end
it "should also contain the second corporation" do
subject.should include @second_corporation_group_of_user
end
end
describe "with ended membership" do
before do
@ugm = UserGroupMembership.find_by_user_and_group(@user, @subgroup)
@ugm.invalidate(Time.zone.now - 10.seconds)
end
it "should no longer contain the corporation" do
subject.should_not include @corporation_group_of_user
end
end
end

describe ".find_corporations_branch_groups_of( user )" do
Expand All @@ -77,6 +103,27 @@
it "should include the corporations_parent_group" do
subject.should include( @corporations_parent_group )
end
describe "with a second corporation" do
before do
@user.groups.collect{|x|x.title}
@second_corporation_group_of_user = create( :group )
@second_corporation_group_of_user.parent_groups << @corporations_parent_group
@subgroup2 = create( :group ); @subgroup2.parent_groups << @second_corporation_group_of_user
@subgroup2.assign_user(@user)
end
it "should also contain the second corporation" do
subject.should include @second_corporation_group_of_user
end
end
describe "with ended membership" do
before do
@ugm = UserGroupMembership.find_by_user_and_group(@user, @subgroup)
@ugm.invalidate(Time.zone.now - 10.seconds)
end
it "should no longer contain the corporation" do
subject.should_not include @corporation_group_of_user
end
end
end

describe ".find_non_corporations_branch_groups_of( user )" do
Expand All @@ -88,6 +135,27 @@
it "should not include the corporations_parent_group" do
subject.should_not include( @corporations_parent_group )
end
describe "with a second corporation" do
before do
@user.groups.collect{|x|x.title}
@second_non_corporations_branch_group = create( :group );
@second_non_corporations_branch_group.assign_user(@user)
end
it "should also contain the second non corporation group" do
subject.should include @second_non_corporations_branch_group
end
end
describe "with ended membership" do
before do
@ugm = UserGroupMembership.find_by_user_and_group(@user, @non_corporations_branch_group)
@ugm.invalidate(Time.zone.now - 10.seconds)
p @ugm.valid_to
p @user.groups
end
it "should no longer contain the non corporation group" do
subject.should_not include @non_corporations_branch_group
end
end
end

end
Expand Down

0 comments on commit 5473944

Please sign in to comment.