Class: MagicScopes::ScopesBuilder

Inherits:
Object
  • Object
show all
Includes:
StandardScopes
Defined in:
lib/magic_scopes/scopes_builder.rb

Constant Summary collapse

STANDARD_OPTIONS =
[:std, :in, :ex]
MAGIC_SCOPES =
{
  boolean:     [:is, :not, :with, :without],
  integer:     [:with, :without, :eq, :ne, :gt, :gte, :lt, :lte, :by, :by_desc],
  float:       [:with, :without, :lt, :gt, :by, :by_desc],
  string:      [:with, :without, :eq, :ne, :by, :by_desc, :like, :not_like, :ilike, :not_ilike],
  association: [:for, :not_for]
}

Instance Method Summary collapse

Constructor Details

#initialize(model, *attrs) ⇒ ScopesBuilder

Returns a new instance of ScopesBuilder.



21
22
23
24
25
26
27
28
29
# File 'lib/magic_scopes/scopes_builder.rb', line 21

def initialize(model, *attrs)
  @model   = model
  @options = attrs.extract_options!.symbolize_keys
  check_options
  @attributes_with_scopes = extract_attributes_with_scopes
  @attributes             = make_attributes(attrs)
  @is_attributes_passed   = attrs.present? || @attributes_with_scopes.present?
  @needed_scopes          = make_needed_scopes
end

Instance Method Details

#generate_scopesObject



35
36
37
# File 'lib/magic_scopes/scopes_builder.rb', line 35

def generate_scopes
  generate_explicitly_passed_scopes.merge(generate_implicitly_specified_scopes)
end

#generate_standard_scopesObject



31
32
33
# File 'lib/magic_scopes/scopes_builder.rb', line 31

def generate_standard_scopes
  (@options[:std] || STANDARD_SCOPES).each { |scope_type| send("#{scope_type}_scope") }
end