Class: Filterameter::Helpers::DeclarationWithModel

Inherits:
Object
  • Object
show all
Defined in:
lib/filterameter/helpers/declaration_with_model.rb

Overview

# Declaration with Model

Class DeclarationWithModel inspects the declaration under the context of the model. This enables predicate methods as well as drilling through associations.

Instance Method Summary collapse

Constructor Details

#initialize(model, declaration) ⇒ DeclarationWithModel

Returns a new instance of DeclarationWithModel.



10
11
12
13
# File 'lib/filterameter/helpers/declaration_with_model.rb', line 10

def initialize(model, declaration)
  @model = model
  @declaration = declaration
end

Instance Method Details

#any_collections?Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
32
# File 'lib/filterameter/helpers/declaration_with_model.rb', line 23

def any_collections?
  @declaration.association.reduce(@model) do |related_model, name|
    association = related_model.reflect_on_association(name)
    return true if association.collection?

    association.klass
  end

  false
end

#model_from_associationObject



34
35
36
37
38
39
40
41
# File 'lib/filterameter/helpers/declaration_with_model.rb', line 34

def model_from_association
  @declaration.association.flatten.reduce(@model) do |memo, name|
    association = memo.reflect_on_association(name)
    raise_invalid_association if association.nil?

    association.klass
  end
end

#scope?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
# File 'lib/filterameter/helpers/declaration_with_model.rb', line 15

def scope?
  model = @declaration.nested? ? model_from_association : @model

  model.respond_to?(@declaration.name) &&
    # checking dangerous_class_method? excludes any names that cannot be scope names, such as "name"
    !model.dangerous_class_method?(@declaration.name)
end