From 97cf31c4cb36c4d7f259f768f04a8e995da1d278 Mon Sep 17 00:00:00 2001 From: Marin Jankovski Date: Mon, 14 Oct 2013 14:27:27 +0200 Subject: [PATCH] Update user access level when adding to group. --- app/models/group.rb | 3 ++- spec/models/group_spec.rb | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/app/models/group.rb b/app/models/group.rb index d6272ca46f5..0ee058be15d 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -26,7 +26,8 @@ class Group < Namespace def add_users(user_ids, group_access) user_ids.compact.each do |user_id| - self.users_groups.create(user_id: user_id, group_access: group_access) + user = self.users_groups.find_or_initialize_by_user_id(user_id) + user.update_attributes(group_access: group_access) end end diff --git a/spec/models/group_spec.rb b/spec/models/group_spec.rb index 4a08ad3bb15..12b84700eb1 100644 --- a/spec/models/group_spec.rb +++ b/spec/models/group_spec.rb @@ -42,4 +42,16 @@ describe Group do it { group.users_groups.masters.map(&:user).should include(user) } end + + describe :add_users do + let(:user) { create(:user) } + before { group.add_users([user.id], UsersGroup::GUEST) } + + it "should update the group permission" do + group.users_groups.guests.map(&:user).should include(user) + group.add_users([user.id], UsersGroup::DEVELOPER) + group.users_groups.developers.map(&:user).should include(user) + group.users_groups.guests.map(&:user).should_not include(user) + end + end end