Skip to content

Commit

Permalink
Raise RecordNotSaved if aggregated model fails to save
Browse files Browse the repository at this point in the history
In Rails 4.2, ActiveRecord::RecordNotSaved requires a message to be passed in.
  • Loading branch information
albertosaurus committed Mar 30, 2017
1 parent 8f64665 commit 8b7465d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion HornsAndHooves-moribus.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ["[email protected]", "[email protected]"]
s.email = ["[email protected]", "[email protected]", "[email protected]"]
s.homepage = "https://github.com/HornsAndHooves/moribus"
s.licenses = ["MIT"]
s.summary = %q{Introduces Aggregated and Tracked behavior to ActiveRecord::Base models}
Expand Down
9 changes: 8 additions & 1 deletion lib/moribus/aggregated_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions spec/moribus_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 8b7465d

Please sign in to comment.