aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2009-12-28 12:33:35 +0530
committerPratik Naik <pratiknaik@gmail.com>2009-12-28 12:42:06 +0530
commita3c1db4e444baa8ae4f8d968fab786e03c93f413 (patch)
tree0a4aaf8ff52648ff9e9593c867300765a66d3445
parent1c47d04ea5ac19601b316daf8fdc6f38c50eec73 (diff)
downloadrails-a3c1db4e444baa8ae4f8d968fab786e03c93f413.tar.gz
rails-a3c1db4e444baa8ae4f8d968fab786e03c93f413.tar.bz2
rails-a3c1db4e444baa8ae4f8d968fab786e03c93f413.zip
Add Model.lock and relation#lock now that arel has locking
-rw-r--r--activerecord/CHANGELOG7
-rw-r--r--activerecord/lib/active_record/associations/association_collection.rb2
-rwxr-xr-xactiverecord/lib/active_record/base.rb5
-rw-r--r--activerecord/lib/active_record/relation.rb11
4 files changed, 23 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 3f4d77979b..2f0bdb6582 100644
--- a/activerecord/CHANGELOG
+++ b/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
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb
index d2c61cdc78..b3a69ab0c0 100644
--- a/activerecord/lib/active_record/associations/association_collection.rb
+++ b/activerecord/lib/active_record/associations/association_collection.rb
@@ -21,7 +21,7 @@ module ActiveRecord
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?
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 3b880ce17f..a887b0c571 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -652,7 +652,7 @@ module ActiveRecord #:nodoc:
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 @@ module ActiveRecord #:nodoc:
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
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
index 530402bf5d..a7f62abe1d 100644
--- a/activerecord/lib/active_record/relation.rb
+++ b/activerecord/lib/active_record/relation.rb
@@ -55,6 +55,17 @@ module ActiveRecord
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)