diff --git a/app/controllers/concerns/blacklight/bookmarks.rb b/app/controllers/concerns/blacklight/bookmarks.rb index e36ec6798d..ff8965f604 100644 --- a/app/controllers/concerns/blacklight/bookmarks.rb +++ b/app/controllers/concerns/blacklight/bookmarks.rb @@ -75,12 +75,15 @@ def create current_or_guest_user.save! unless current_or_guest_user.persisted? - success = @bookmarks.all? do |bookmark| - current_or_guest_user.bookmarks.where(bookmark).exists? || current_or_guest_user.bookmarks.create(bookmark) + bookmarks_to_add = @bookmarks.reject { |bookmark| current_or_guest_user.bookmarks.where(bookmark).exists? } + success = ActiveRecord::Base.transaction do + current_or_guest_user.bookmarks.create!(bookmarks_to_add) + rescue ActiveRecord::RecordInvalid + false end if request.xhr? - success ? render(json: { bookmarks: { count: current_or_guest_user.bookmarks.count } }) : render(plain: "", status: "500") + success ? render(json: { bookmarks: { count: current_or_guest_user.bookmarks.count } }) : render(json: current_or_guest_user.errors.full_messages, status: "500") else if @bookmarks.any? && success flash[:notice] = I18n.t('blacklight.bookmarks.add.success', count: @bookmarks.length) diff --git a/spec/controllers/bookmarks_controller_spec.rb b/spec/controllers/bookmarks_controller_spec.rb index 283a6f5362..05df6104ca 100644 --- a/spec/controllers/bookmarks_controller_spec.rb +++ b/spec/controllers/bookmarks_controller_spec.rb @@ -24,7 +24,8 @@ allow(@controller).to receive_message_chain(:current_or_guest_user, :existing_bookmark_for).and_return(false) allow(@controller).to receive_message_chain(:current_or_guest_user, :persisted?).and_return(true) allow(@controller).to receive_message_chain(:current_or_guest_user, :bookmarks, :where, :exists?).and_return(false) - allow(@controller).to receive_message_chain(:current_or_guest_user, :bookmarks, :create).and_return(false) + allow(@controller).to receive_message_chain(:current_or_guest_user, :bookmarks, :create!).and_raise(ActiveRecord::RecordInvalid) + allow(@controller).to receive_message_chain(:current_or_guest_user, :errors, :full_messages).and_return([1]) put :update, xhr: true, params: { id: 'iamabooboo', format: :js } expect(response.code).to eq "500" end