Class: ActiveRecord::DynamicScopeMatch

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/dynamic_scope_match.rb

Overview

Active Record Dynamic Scope Match

Provides dynamic attribute-based scopes such as scoped_by_price(4.99) if, for example, the Product has an attribute with that name. You can chain more scoped_by_* methods after the other. It acts like a named scope except that it’s dynamic.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method) ⇒ DynamicScopeMatch

Returns a new instance of DynamicScopeMatch.



15
16
17
18
19
20
21
22
23
24
# File 'lib/active_record/dynamic_scope_match.rb', line 15

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_')
end

Instance Attribute Details

#attribute_namesObject (readonly)

Returns the value of attribute attribute_names.



26
27
28
# File 'lib/active_record/dynamic_scope_match.rb', line 26

def attribute_names
  @attribute_names
end

#scopeObject (readonly)

Returns the value of attribute scope.



26
27
28
# File 'lib/active_record/dynamic_scope_match.rb', line 26

def scope
  @scope
end

Class Method Details

.match(method) ⇒ Object



10
11
12
13
# File 'lib/active_record/dynamic_scope_match.rb', line 10

def self.match(method)
  ds_match = self.new(method)
  ds_match.scope ? ds_match : nil
end

Instance Method Details

#scope?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/active_record/dynamic_scope_match.rb', line 28

def scope?
  !@scope.nil?
end