aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2012-03-30 14:39:55 +0100
committerJon Leighton <j@jonathanleighton.com>2012-03-30 14:39:55 +0100
commit1ce75450a9fba33edf2bffee0bf0ac3d31ea677f (patch)
tree5257cdf7c768b0afbdcdc45026a35c4e5b2a45c0 /activerecord/lib/active_record
parent43f9622c6749d04867534e1a8838df619c2a79f0 (diff)
downloadrails-1ce75450a9fba33edf2bffee0bf0ac3d31ea677f.tar.gz
rails-1ce75450a9fba33edf2bffee0bf0ac3d31ea677f.tar.bz2
rails-1ce75450a9fba33edf2bffee0bf0ac3d31ea677f.zip
clear up duplication between Persistence#destroy and Locking#destroy
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/locking/optimistic.rb31
-rw-r--r--activerecord/lib/active_record/persistence.rb30
2 files changed, 34 insertions, 27 deletions
diff --git a/activerecord/lib/active_record/locking/optimistic.rb b/activerecord/lib/active_record/locking/optimistic.rb
index 64433f580a..a3412582fa 100644
--- a/activerecord/lib/active_record/locking/optimistic.rb
+++ b/activerecord/lib/active_record/locking/optimistic.rb
@@ -101,26 +101,29 @@ module ActiveRecord
end
end
- def destroy #:nodoc:
- return super unless locking_enabled?
+ def destroy_row
+ affected_rows = super
+
+ if locking_enabled? && affected_rows != 1
+ raise ActiveRecord::StaleObjectError.new(self, "destroy")
+ end
- destroy_associations
+ affected_rows
+ end
- if persisted?
- table = self.class.arel_table
- lock_col = self.class.locking_column
- predicate = table[self.class.primary_key].eq(id).
- and(table[lock_col].eq(send(lock_col).to_i))
+ def relation_for_destroy
+ relation = super
- affected_rows = self.class.unscoped.where(predicate).delete_all
+ if locking_enabled?
+ column_name = self.class.locking_column
+ column = self.class.columns_hash[column_name]
+ substitute = connection.substitute_at(column, relation.bind_values.length)
- unless affected_rows == 1
- raise ActiveRecord::StaleObjectError.new(self, "destroy")
- end
+ relation = relation.where(self.class.arel_table[column_name].eq(substitute))
+ relation.bind_values << [column, self[column_name].to_i]
end
- @destroyed = true
- freeze
+ relation
end
module ClassMethods
diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb
index 35c922e979..bb504ae90f 100644
--- a/activerecord/lib/active_record/persistence.rb
+++ b/activerecord/lib/active_record/persistence.rb
@@ -124,19 +124,7 @@ module ActiveRecord
# that no changes should be made (since they can't be persisted).
def destroy
destroy_associations
-
- if persisted?
- pk = self.class.primary_key
- column = self.class.columns_hash[pk]
- substitute = connection.substitute_at(column, 0)
-
- relation = self.class.unscoped.where(
- self.class.arel_table[pk].eq(substitute))
-
- relation.bind_values = [[column, id]]
- relation.delete_all
- end
-
+ destroy_row if persisted?
@destroyed = true
freeze
end
@@ -335,6 +323,22 @@ module ActiveRecord
def destroy_associations
end
+ def destroy_row
+ relation_for_destroy.delete_all
+ end
+
+ def relation_for_destroy
+ pk = self.class.primary_key
+ column = self.class.columns_hash[pk]
+ substitute = connection.substitute_at(column, 0)
+
+ relation = self.class.unscoped.where(
+ self.class.arel_table[pk].eq(substitute))
+
+ relation.bind_values = [[column, id]]
+ relation
+ end
+
def create_or_update
raise ReadOnlyRecord if readonly?
result = new_record? ? create : update