From 41a2ba652a6b4b4eb3d12b9a2d55cb5b1eb5581a Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sat, 3 Apr 2010 01:35:10 +0100 Subject: Improve named scope lambda --- activerecord/lib/active_record/named_scope.rb | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb index 86a18f8b6b..50c57783b2 100644 --- a/activerecord/lib/active_record/named_scope.rb +++ b/activerecord/lib/active_record/named_scope.rb @@ -99,7 +99,7 @@ module ActiveRecord # # 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 @@ module ActiveRecord 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 -- cgit v1.2.3