diff options
author | Xavier Noria <fxn@hashref.com> | 2010-06-30 20:47:26 +0200 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2010-06-30 20:47:26 +0200 |
commit | c63cf7bf0db708fe46a929cf57649ab5a92034af (patch) | |
tree | 8f0974852b51597652e6ae73da26f3eb80fe878b /activerecord/lib/active_record/named_scope.rb | |
parent | 52c56f9f7ec46ee39f1a6319ff4017e2492683ed (diff) | |
parent | b07e6fdaa0aa07016d1425ada5b7f966142d0212 (diff) | |
download | rails-c63cf7bf0db708fe46a929cf57649ab5a92034af.tar.gz rails-c63cf7bf0db708fe46a929cf57649ab5a92034af.tar.bz2 rails-c63cf7bf0db708fe46a929cf57649ab5a92034af.zip |
Merge remote branch 'rails/master'
Diffstat (limited to 'activerecord/lib/active_record/named_scope.rb')
-rw-r--r-- | activerecord/lib/active_record/named_scope.rb | 33 |
1 files changed, 22 insertions, 11 deletions
diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb index ec0a98c6df..c010dac64e 100644 --- a/activerecord/lib/active_record/named_scope.rb +++ b/activerecord/lib/active_record/named_scope.rb @@ -25,10 +25,9 @@ module ActiveRecord # # You can define a \scope that applies to all finders using # ActiveRecord::Base.default_scope. - def scoped(options = {}, &block) + def scoped(options = nil) if options.present? - relation = scoped.apply_finder_options(options) - block_given? ? relation.extending(Module.new(&block)) : relation + scoped.apply_finder_options(options) else current_scoped_methods ? unscoped.merge(current_scoped_methods) : unscoped.clone end @@ -88,18 +87,22 @@ module ActiveRecord # end def scope(name, scope_options = {}, &block) name = name.to_sym + valid_scope_name?(name) - if !scopes[name] && respond_to?(name, true) - logger.warn "Creating scope :#{name}. " \ - "Overwriting existing method #{self.name}.#{name}." - end + extension = Module.new(&block) if block_given? scopes[name] = lambda do |*args| options = scope_options.is_a?(Proc) ? scope_options.call(*args) : scope_options - relation = scoped - relation = options.is_a?(Hash) ? relation.apply_finder_options(options) : scoped.merge(options) if options - block_given? ? relation.extending(Module.new(&block)) : relation + relation = if options.is_a?(Hash) + scoped.apply_finder_options(options) + elsif options + scoped.merge(options) + else + scoped + end + + extension ? relation.extending(extension) : relation end singleton_class.send :define_method, name, &scopes[name] @@ -109,7 +112,15 @@ module ActiveRecord ActiveSupport::Deprecation.warn("Base.named_scope has been deprecated, please use Base.scope instead", caller) scope(*args, &block) end - end + protected + + def valid_scope_name?(name) + if !scopes[name] && respond_to?(name, true) + logger.warn "Creating scope :#{name}. " \ + "Overwriting existing method #{self.name}.#{name}." + end + end + end end end |