Permalink
Browse files

Improve named scope lambda

  • Loading branch information...
1 parent 684e4d3 commit 41a2ba652a6b4b4eb3d12b9a2d55cb5b1eb5581a @lifo lifo committed Apr 3, 2010
Showing with 4 additions and 13 deletions.
  1. +4 −13 activerecord/lib/active_record/named_scope.rb
View
17 activerecord/lib/active_record/named_scope.rb
@@ -99,7 +99,7 @@ def scopes
#
# expected_options = { :conditions => { :colored => 'red' } }
# assert_equal expected_options, Shirt.colored('red').proxy_options
- def scope(name, options = {}, &block)
+ def scope(name, scope_options = {}, &block)
name = name.to_sym
if !scopes[name] && respond_to?(name, true)
@@ -108,19 +108,10 @@ def scope(name, options = {}, &block)
end
scopes[name] = lambda do |*args|
- scope_options = case options
- when Hash, Relation
- options
- when Proc
- options.call(*args)
- end
-
- relation = if scope_options.is_a?(Hash)
- scoped.apply_finder_options(scope_options)
- else
- scope_options ? scoped.merge(scope_options) : scoped
- end
+ 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
end

0 comments on commit 41a2ba6

Please sign in to comment.