Skip to content

Commit

Permalink
Merge pull request #5881 from avalonmediasystem/full_messages
Browse files Browse the repository at this point in the history
Fix NoMethodError when saving parent object fails
  • Loading branch information
cjcolvar authored Jun 25, 2024
2 parents e62be6a + 282df31 commit 8842a38
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/controllers/supplemental_files_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def create
raise Avalon::SaveError, @supplemental_file.errors.full_messages unless @supplemental_file.save

@object.supplemental_files += [@supplemental_file]
raise Avalon::SaveError, @object.errors[:supplemental_files_json].full_messages unless @object.save
raise Avalon::SaveError, @object.errors[:supplemental_files_json] unless @object.save

flash[:success] = "Supplemental file successfully added."

Expand Down Expand Up @@ -131,7 +131,7 @@ def destroy
find_supplemental_file

@object.supplemental_files -= [@supplemental_file]
raise Avalon::SaveError, "An error occurred when deleting the supplemental file: #{@object.errors[:supplemental_files_json].full_messages}" unless @object.save
raise Avalon::SaveError, "An error occurred when deleting the supplemental file: #{@object.errors[:supplemental_files_json]}" unless @object.save
# FIXME: also wrap this in a transaction
raise Avalon::SaveError, "An error occurred when deleting the supplemental file: #{@supplemental_file.errors.full_messages}" unless @supplemental_file.destroy

Expand Down
31 changes: 31 additions & 0 deletions spec/support/supplemental_files_controller_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,23 @@
expect(JSON.parse(response.body)["errors"]).to be_present
end
end

context 'throws error while saving' do
let(:supplemental_file) { FactoryBot.build(:supplemental_file) }
before do
allow(SupplementalFile).to receive(:new).and_return(supplemental_file)
allow(master_file).to receive(:save).and_return(false)
allow(controller).to receive(:fetch_object).and_return(master_file)
end

it 'does not add a SupplementalFile to parent object' do
expect {
post :create, params: { class_id => object.id, supplemental_file: valid_create_attributes, format: :json }, session: valid_session
}.not_to change { master_file.reload.supplemental_files.size }
expect(response).to have_http_status(500)
expect(JSON.parse(response.body)["errors"]).to be_present
end
end
end

describe "PUT #update" do
Expand Down Expand Up @@ -574,5 +591,19 @@
end
end
end

context 'throws error while saving' do
let(:supplemental_file) { FactoryBot.create(:supplemental_file) }
before do
allow(master_file).to receive(:save).and_return(false)
allow(controller).to receive(:fetch_object).and_return(master_file)
end

it 'returns a 500' do
delete :destroy, params: { class_id => object.id, id: supplemental_file.id, format: :json }, session: valid_session
expect(response).to have_http_status(500)
expect(JSON.parse(response.body)["errors"]).to be_present
end
end
end
end

0 comments on commit 8842a38

Please sign in to comment.