diff options
Diffstat (limited to 'activerecord/lib/active_record/dynamic_scope_match.rb')
-rw-r--r-- | activerecord/lib/active_record/dynamic_scope_match.rb | 23 |
1 files changed, 7 insertions, 16 deletions
diff --git a/activerecord/lib/active_record/dynamic_scope_match.rb b/activerecord/lib/active_record/dynamic_scope_match.rb index 15f65be6bc..c832e927d6 100644 --- a/activerecord/lib/active_record/dynamic_scope_match.rb +++ b/activerecord/lib/active_record/dynamic_scope_match.rb @@ -1,32 +1,23 @@ module ActiveRecord # = Active Record Dynamic Scope Match - # + # # Provides dynamic attribute-based scopes such as <tt>scoped_by_price(4.99)</tt> # if, for example, the <tt>Product</tt> has an attribute with that name. You can # chain more <tt>scoped_by_* </tt> methods after the other. It acts like a named # scope except that it's dynamic. class DynamicScopeMatch def self.match(method) - ds_match = self.new(method) - ds_match.scope ? ds_match : nil + return unless method.to_s =~ /^scoped_by_([_a-zA-Z]\w*)$/ + new(true, $1 && $1.split('_and_')) end - def initialize(method) - @scope = true - case method.to_s - when /^scoped_by_([_a-zA-Z]\w*)$/ - names = $1 - else - @scope = nil - end - @attribute_names = names && names.split('_and_') + def initialize(scope, attribute_names) + @scope = scope + @attribute_names = attribute_names end attr_reader :scope, :attribute_names - - def scope? - !@scope.nil? - end + alias :scope? :scope end end |