From 8b7465de81fc859cb76ab3463e1fdd89a31435d7 Mon Sep 17 00:00:00 2001 From: Arthur Shagall Date: Wed, 29 Mar 2017 20:55:35 -0500 Subject: [PATCH] Raise RecordNotSaved if aggregated model fails to save In Rails 4.2, ActiveRecord::RecordNotSaved requires a message to be passed in. --- HornsAndHooves-moribus.gemspec | 2 +- lib/moribus/aggregated_behavior.rb | 9 ++++++++- spec/moribus_spec.rb | 8 ++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/HornsAndHooves-moribus.gemspec b/HornsAndHooves-moribus.gemspec index d38d163..8793fd0 100644 --- a/HornsAndHooves-moribus.gemspec +++ b/HornsAndHooves-moribus.gemspec @@ -7,7 +7,7 @@ Gem::Specification.new do |s| s.name = "HornsAndHooves-moribus" s.version = Moribus::VERSION s.authors = ["HornsAndHooves", "Artem Kuzko", "Sergey Potapov"] - s.email = ["a.kuzko@gmail.com", "blake131313@gmail.com"] + s.email = ["arthur.shagall@gmail.com", "a.kuzko@gmail.com", "blake131313@gmail.com"] s.homepage = "https://github.com/HornsAndHooves/moribus" s.licenses = ["MIT"] s.summary = %q{Introduces Aggregated and Tracked behavior to ActiveRecord::Base models} diff --git a/lib/moribus/aggregated_behavior.rb b/lib/moribus/aggregated_behavior.rb index 935fbf1..e23c800 100644 --- a/lib/moribus/aggregated_behavior.rb +++ b/lib/moribus/aggregated_behavior.rb @@ -48,9 +48,16 @@ def save(*) # Bang version of #save. def save!(*args) - save(*args) or raise ActiveRecord::RecordNotSaved + save(*args) or raise_record_not_saved_error end + # Raise record not saved + def raise_record_not_saved_error + args = Rails::VERSION::MINOR < 2 ? [] : ["Failed to save record"] + raise ActiveRecord::RecordNotSaved, *args + end + private :raise_record_not_saved_error + # Use the +lookup_relation+ to get the very first existing record that # corresponds to +self+. def lookup_self diff --git a/spec/moribus_spec.rb b/spec/moribus_spec.rb index 84c9713..320ec7c 100644 --- a/spec/moribus_spec.rb +++ b/spec/moribus_spec.rb @@ -189,6 +189,14 @@ class SpecCustomerEmail < MoribusSpecModel(:spec_customer_id => :integer, expect(name.id).to eq @existing.id end + it "raises the expected error when 'save!' fails" do + name = SpecPersonName.create :first_name => "Alice", :last_name => "Smith" + name.last_name = nil + expect { + name.save! + }.to raise_error(ActiveRecord::RecordNotSaved) + end + context "with caching" do before do @existing = SpecCustomerFeature.create(:feature_name => "Pays")