From 54739441fce2698831be181414430ffc78f89a3a Mon Sep 17 00:00:00 2001 From: Joachim Back Date: Mon, 19 Jan 2015 00:15:19 +0100 Subject: [PATCH] Extend specs and enhance adding&invalidation of memberships --- .../app/models/group_mixins/corporations.rb | 6 +- .../app/models/group_mixins/memberships.rb | 2 + .../validity_range.rb | 4 +- .../models/group_mixins/corporations_spec.rb | 72 ++++++++++++++++++- 4 files changed, 78 insertions(+), 6 deletions(-) diff --git a/vendor/engines/your_platform/app/models/group_mixins/corporations.rb b/vendor/engines/your_platform/app/models/group_mixins/corporations.rb index 0d9771c16..995c1972e 100644 --- a/vendor/engines/your_platform/app/models/group_mixins/corporations.rb +++ b/vendor/engines/your_platform/app/models/group_mixins/corporations.rb @@ -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 @@ -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 @@ -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 - diff --git a/vendor/engines/your_platform/app/models/group_mixins/memberships.rb b/vendor/engines/your_platform/app/models/group_mixins/memberships.rb index 97c17699e..7ec984ae2 100644 --- a/vendor/engines/your_platform/app/models/group_mixins/memberships.rb +++ b/vendor/engines/your_platform/app/models/group_mixins/memberships.rb @@ -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 diff --git a/vendor/engines/your_platform/app/models/user_group_membership_mixins/validity_range.rb b/vendor/engines/your_platform/app/models/user_group_membership_mixins/validity_range.rb index e9d0475c8..48b3966e7 100644 --- a/vendor/engines/your_platform/app/models/user_group_membership_mixins/validity_range.rb +++ b/vendor/engines/your_platform/app/models/user_group_membership_mixins/validity_range.rb @@ -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 @@ -191,4 +193,4 @@ class Array def started_after(time) self.select { |membership| membership.valid_from && membership.valid_from >= time } end -end \ No newline at end of file +end diff --git a/vendor/engines/your_platform/spec/models/group_mixins/corporations_spec.rb b/vendor/engines/your_platform/spec/models/group_mixins/corporations_spec.rb index cc63b7443..96f7aa52f 100644 --- a/vendor/engines/your_platform/spec/models/group_mixins/corporations_spec.rb +++ b/vendor/engines/your_platform/spec/models/group_mixins/corporations_spec.rb @@ -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 @@ -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 @@ -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 @@ -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