Skip to content

Commit

Permalink
Ensuring that ActiveModel errors result in one retry for Group#defaul…
Browse files Browse the repository at this point in the history
…t_user (#1950)
  • Loading branch information
jrgriffiniii authored Sep 26, 2024
1 parent 20a40d0 commit 57e7172
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
15 changes: 12 additions & 3 deletions app/models/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,18 @@ def default_user(uid)
user = User.find_by(uid:)
return user if user.present?

user = User.new(uid:, default_group_id: id)
user.save!
user
begin
user = User.new(uid:, default_group_id: id)
user.save!
user
rescue ActiveRecord::RecordNotUnique => unique_error
# If adding a submitter to an existing group
Rails.logger.error("Failed to created a new user for #{self}: #{unique_error}")
user = User.new(uid:)
user.default_group_id = id
user.save!
user
end
end
end
# rubocop:enable Metrics/ClassLength
18 changes: 18 additions & 0 deletions spec/models/group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,23 @@
Group.plasma_laboratory.default_user("abc123")
expect(user.reload.default_group).to eq(Group.research_data)
end

context "when an error is encountered while persisting the user model" do
let(:user) { FactoryBot.build(:user) }
let(:uid) { "abc234" }
let(:default_group_id) { Group.plasma_laboratory.id }

before do
user
allow(User).to receive(:new).with(uid:, default_group_id:).and_raise(ActiveRecord::RecordNotUnique)
allow(User).to receive(:new).with(uid:).and_return(user)
end

it "attempts to create a new user mode without initially setting the default group ID" do
persisted = Group.plasma_laboratory.default_user("abc234")

expect(persisted.default_group_id).to eq(Group.plasma_laboratory.id)
end
end
end
end

0 comments on commit 57e7172

Please sign in to comment.