aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/named_scope.rb
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2010-04-02 18:57:01 +0100
committerPratik Naik <pratiknaik@gmail.com>2010-04-02 18:57:46 +0100
commitcfa283201e079b4f700eb915490bcfa18451b11e (patch)
tree9a9a4d9e165663d6a4a3b4310bc7cc1d8563e5e9 /activerecord/lib/active_record/named_scope.rb
parent62fe16932c9b7c3044017900114193e06814fd0c (diff)
downloadrails-cfa283201e079b4f700eb915490bcfa18451b11e.tar.gz
rails-cfa283201e079b4f700eb915490bcfa18451b11e.tar.bz2
rails-cfa283201e079b4f700eb915490bcfa18451b11e.zip
Goodbye ActiveRecord::NamedScope::Scope
Diffstat (limited to 'activerecord/lib/active_record/named_scope.rb')
-rw-r--r--activerecord/lib/active_record/named_scope.rb52
1 files changed, 17 insertions, 35 deletions
diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb
index be26b1f47f..d56f7afb74 100644
--- a/activerecord/lib/active_record/named_scope.rb
+++ b/activerecord/lib/active_record/named_scope.rb
@@ -25,7 +25,8 @@ module ActiveRecord
# You can define a scope that applies to all finders using ActiveRecord::Base.default_scope.
def scoped(options = {}, &block)
if options.present?
- Scope.init(self, options, &block)
+ relation = scoped.apply_finder_options(options)
+ block_given? ? relation.extending(Module.new(&block)) : relation
else
current_scoped_methods ? unscoped.merge(current_scoped_methods) : unscoped.clone
end
@@ -107,13 +108,22 @@ module ActiveRecord
end
scopes[name] = lambda do |parent_scope, *args|
- Scope.init(parent_scope, case options
- when Hash, Relation
- options
- when Proc
- options.call(*args)
- end, &block)
+ scope_options = case options
+ when Hash, Relation
+ options
+ when Proc
+ options.call(*args)
+ end
+
+ relation = if scope_options.is_a?(Hash)
+ parent_scope.scoped.apply_finder_options(scope_options)
+ else
+ scope_options ? parent_scope.scoped.merge(scope_options) : parent_scope.scoped
+ end
+
+ block_given? ? relation.extending(Module.new(&block)) : relation
end
+
singleton_class.instance_eval do
define_method name do |*args|
scopes[name].call(self, *args)
@@ -127,33 +137,5 @@ module ActiveRecord
end
end
- class Scope < Relation
- delegate :scopes, :with_scope, :with_exclusive_scope, :scoped_methods, :scoped, :to => :klass
-
- def self.init(klass, options, &block)
- relation = new(klass, klass.arel_table, &block)
-
- scope = if options.is_a?(Hash)
- klass.scoped.apply_finder_options(options)
- else
- options ? klass.scoped.merge(options) : klass.scoped
- end
-
- relation.merge(scope)
- end
-
- def ==(other)
- case other
- when Scope
- to_sql == other.to_sql
- when Relation
- other == self
- when Array
- to_a == other.to_a
- end
- end
-
- end
-
end
end