Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/report_lock_version_on_stale_obj…
Browse files Browse the repository at this point in the history
…_error'

* origin/report_lock_version_on_stale_obj_error:
  Report lock version when raising a stale object error
  • Loading branch information
albertosaurus committed Feb 2, 2017
2 parents b638b44 + 8851048 commit 8f64665
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
11 changes: 9 additions & 2 deletions lib/moribus/tracked_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def update_current
affected_rows = self.class.connection.update statement

unless affected_rows == 1
raise ActiveRecord::StaleObjectError.new(self, "update_current")
raise ActiveRecord::StaleObjectError.new(self, "update_current (version #{current_lock_value})")
end
true
end
Expand Down Expand Up @@ -106,7 +106,7 @@ def current_to_false_sql_statement
klass = self.class
is_current_col = klass.columns.detect { |c| c.name == "is_current" }
lock_column_name = klass.locking_column
lock_value = has_attribute?(lock_column_name) && read_attribute(lock_column_name).to_i
lock_value = current_lock_value
lock_column = if lock_value
klass.columns.detect { |c| c.name == lock_column_name }
else
Expand All @@ -127,5 +127,12 @@ def current_to_false_sql_statement
end
end
private :current_to_false_sql_statement

# Returns the current value of the locking column if such exists.
def current_lock_value
lock_column_name = self.class.locking_column
has_attribute?(lock_column_name) && read_attribute(lock_column_name).to_i
end
private :current_lock_value
end
end
2 changes: 1 addition & 1 deletion lib/moribus/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Moribus # :nodoc:
VERSION = "0.4.2" # :nodoc:
VERSION = "0.4.3" # :nodoc:
end
3 changes: 2 additions & 1 deletion spec/moribus_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ class SpecCustomerEmail < MoribusSpecModel(:spec_customer_id => :integer,
@info1.update_attributes(:spec_person_name_id => 3)

expect{ @info2.update_attributes(:spec_person_name_id => 4) }.
to raise_error(ActiveRecord::StaleObjectError)
to raise_error(ActiveRecord::StaleObjectError,
"Attempted to update_current (version #{@info2.lock_version}) a stale object: SpecCustomerInfo")
end

it "updates lock_version incrementally for each new record" do
Expand Down

0 comments on commit 8f64665

Please sign in to comment.