diff options
author | José Valim <jose.valim@gmail.com> | 2010-06-29 17:18:55 +0200 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-06-29 17:18:55 +0200 |
commit | bd1666ad1de88598ed6f04ceffb8488a77be4385 (patch) | |
tree | 853cc5a1e6af361ed4fbb4af998099922f1e20c0 /activerecord/lib/active_record/relation.rb | |
parent | 9013227e00895ac95f29077229ec2fb156f450b0 (diff) | |
download | rails-bd1666ad1de88598ed6f04ceffb8488a77be4385.tar.gz rails-bd1666ad1de88598ed6f04ceffb8488a77be4385.tar.bz2 rails-bd1666ad1de88598ed6f04ceffb8488a77be4385.zip |
Add scoping and unscoped as the syntax to replace the old with_scope and with_exclusive_scope. A few examples:
* with_scope now should be scoping:
Before:
Comment.with_scope(:find => { :conditions => { :post_id => 1 } }) do
Comment.first #=> SELECT * FROM comments WHERE post_id = 1
end
After:
Comment.where(:post_id => 1).scoping do
Comment.first #=> SELECT * FROM comments WHERE post_id = 1
end
* with_exclusive_scope now should be unscoped:
class Post < ActiveRecord::Base
default_scope :published => true
end
Post.all #=> SELECT * FROM posts WHERE published = true
Before:
Post.with_exclusive_scope do
Post.all #=> SELECT * FROM posts
end
After:
Post.unscoped do
Post.all #=> SELECT * FROM posts
end
Notice you can also use unscoped without a block and it will return an anonymous scope with default_scope values:
Post.unscoped.all #=> SELECT * FROM posts
Diffstat (limited to 'activerecord/lib/active_record/relation.rb')
-rw-r--r-- | activerecord/lib/active_record/relation.rb | 43 |
1 files changed, 23 insertions, 20 deletions
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index fd0660a138..3b24d4aafd 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -16,7 +16,7 @@ module ActiveRecord attr_reader :table, :klass attr_accessor :extensions - def initialize(klass, table, &block) + def initialize(klass, table) @klass, @table = klass, table @implicit_readonly = nil @@ -25,12 +25,10 @@ module ActiveRecord SINGLE_VALUE_METHODS.each {|v| instance_variable_set(:"@#{v}_value", nil)} (ASSOCIATION_METHODS + MULTI_VALUE_METHODS).each {|v| instance_variable_set(:"@#{v}_values", [])} @extensions = [] - - apply_modules(Module.new(&block)) if block_given? end def new(*args, &block) - with_create_scope { @klass.new(*args, &block) } + scoping { @klass.new(*args, &block) } end def initialize_copy(other) @@ -40,11 +38,11 @@ module ActiveRecord alias build new def create(*args, &block) - with_create_scope { @klass.create(*args, &block) } + scoping { @klass.create(*args, &block) } end def create!(*args, &block) - with_create_scope { @klass.create!(*args, &block) } + scoping { @klass.create!(*args, &block) } end def respond_to?(method, include_private = false) @@ -102,6 +100,25 @@ module ActiveRecord end end + # Scope all queries to the current scope. + # + # ==== Example + # + # Comment.where(:post_id => 1).scoping do + # Comment.first #=> SELECT * FROM comments WHERE post_id = 1 + # end + # + # Please check unscoped if you want to remove all previous scopes (including + # the default_scope) during the execution of a block. + def scoping + @klass.scoped_methods << self + begin + yield + ensure + @klass.scoped_methods.pop + end + end + # Updates all records with details given if they match a set of conditions supplied, limits and order can # also be supplied. This method constructs a single SQL UPDATE statement and sends it straight to the # database. It does not instantiate the involved models and it does not trigger Active Record callbacks @@ -305,7 +322,6 @@ module ActiveRecord if where.is_a?(Arel::Predicates::Equality) hash[where.operand1.name] = where.operand2.respond_to?(:value) ? where.operand2.value : where.operand2 end - hash end end @@ -328,15 +344,6 @@ module ActiveRecord to_a.inspect end - def extend(*args, &block) - if block_given? - apply_modules Module.new(&block) - self - else - super - end - end - protected def method_missing(method, *args, &block) @@ -364,10 +371,6 @@ module ActiveRecord private - def with_create_scope - @klass.send(:with_scope, :create => scope_for_create, :find => {}) { yield } - end - def references_eager_loaded_tables? # always convert table names to downcase as in Oracle quoted table names are in uppercase joined_tables = (tables_in_string(arel.joins(arel)) + [table.name, table.table_alias]).compact.map(&:downcase).uniq |