Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add Model.lock and relation#lock now that arel has locking

  • Loading branch information...
commit a3c1db4e444baa8ae4f8d968fab786e03c93f413 1 parent 1c47d04
@lifo lifo authored
View
7 activerecord/CHANGELOG
@@ -1,5 +1,12 @@
*Edge*
+* Add .lock finder method [Pratik Naik]
+
+ User.lock.where(:name => 'lifo').to_a
+
+ old_items = Item.where("age > 100")
+ old_items.locked.each {|i| .. }
+
* Add Model.from and association_collection#from finder methods [Pratik Naik]
user = User.scoped
View
2  activerecord/lib/active_record/associations/association_collection.rb
@@ -21,7 +21,7 @@ def initialize(owner, reflection)
construct_sql
end
- delegate :group, :order, :limit, :joins, :where, :preload, :eager_load, :from, :to => :scoped
+ delegate :group, :order, :limit, :joins, :where, :preload, :eager_load, :from, :lock, :to => :scoped
def select(select = nil, &block)
if block_given?
View
5 activerecord/lib/active_record/base.rb
@@ -652,7 +652,7 @@ def find(*args)
end
end
- delegate :select, :group, :order, :limit, :joins, :where, :preload, :eager_load, :from, :to => :scoped
+ delegate :select, :group, :order, :limit, :joins, :where, :preload, :eager_load, :from, :lock, :to => :scoped
# A convenience wrapper for <tt>find(:first, *args)</tt>. You can pass in all the
# same arguments to this method as you can to <tt>find(:first)</tt>.
@@ -1573,6 +1573,9 @@ def construct_finder_arel(options = {}, scope = scope(:find))
offset(construct_offset(options[:offset], scope)).
from(options[:from])
+ lock = (scope && scope[:lock]) || options[:lock]
+ relation = relation.lock if lock.present?
+
relation = relation.readonly if options[:readonly]
relation
View
11 activerecord/lib/active_record/relation.rb
@@ -55,6 +55,17 @@ def order(orders)
orders.present? ? create_new_relation(@relation.order(orders)) : create_new_relation
end
+ def lock(locks = true)
+ case locks
+ when String
+ create_new_relation(@relation.lock(locks))
+ when TrueClass, NilClass
+ create_new_relation(@relation.lock)
+ else
+ create_new_relation
+ end
+ end
+
def reverse_order
relation = create_new_relation
relation.instance_variable_set(:@orders, nil)
Please sign in to comment.
Something went wrong with that request. Please try again.