Class: Filterameter::FilterDeclaration

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

Overview

# Filter Declaration

Class FilterDeclaration captures the filter declaration within the controller.

When the min_only or max_only range option is specified, in addition to the attribute filter which carries that option, the registry builds a duplicate declaration that also carries the range_type flag (as either :minimum or :maximum).

The predicate methods ‘min_only?` and `max_only?` answer what was declared; the predicate methods `minimum_range?` and `maximum_range?` answer what type of filter should be built.

Constant Summary collapse

VALID_RANGE_OPTIONS =
[true, :min_only, :max_only].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parameter_name, options, range_type: nil) ⇒ FilterDeclaration

Returns a new instance of FilterDeclaration.



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

def initialize(parameter_name, options, range_type: nil)
  @parameter_name = parameter_name.to_s

  validate_options(options)
  @name = options.fetch(:name, parameter_name).to_s
  @association = Array.wrap(options[:association]).presence
  @validations = Array.wrap(options[:validates])
  @raw_partial_options = options.fetch(:partial, false)
  @raw_range = options[:range]
  @range_type = range_type
  @sortable = options.fetch(:sortable, true)
end

Instance Attribute Details

#associationObject (readonly)

Returns the value of attribute association.



19
20
21
# File 'lib/filterameter/filter_declaration.rb', line 19

def association
  @association
end

#nameObject (readonly)

Returns the value of attribute name.



19
20
21
# File 'lib/filterameter/filter_declaration.rb', line 19

def name
  @name
end

#parameter_nameObject (readonly)

Returns the value of attribute parameter_name.



19
20
21
# File 'lib/filterameter/filter_declaration.rb', line 19

def parameter_name
  @parameter_name
end

#validationsObject (readonly)

Returns the value of attribute validations.



19
20
21
# File 'lib/filterameter/filter_declaration.rb', line 19

def validations
  @validations
end

Instance Method Details

#max_only?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/filterameter/filter_declaration.rb', line 62

def max_only?
  @raw_range == :max_only
end

#maximum_range?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/filterameter/filter_declaration.rb', line 70

def maximum_range?
  @range_type == :maximum
end

#min_only?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/filterameter/filter_declaration.rb', line 58

def min_only?
  @raw_range == :min_only
end

#minimum_range?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/filterameter/filter_declaration.rb', line 66

def minimum_range?
  @range_type == :minimum
end

#nested?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/filterameter/filter_declaration.rb', line 34

def nested?
  !@association.nil?
end

#partial_optionsObject



46
47
48
# File 'lib/filterameter/filter_declaration.rb', line 46

def partial_options
  @partial_options ||= @raw_partial_options ? Options::PartialOptions.new(@raw_partial_options) : nil
end

#partial_search?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/filterameter/filter_declaration.rb', line 42

def partial_search?
  partial_options.present?
end

#range?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/filterameter/filter_declaration.rb', line 54

def range?
  @raw_range == true
end

#range_enabled?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/filterameter/filter_declaration.rb', line 50

def range_enabled?
  @raw_range.present?
end

#sortable?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/filterameter/filter_declaration.rb', line 74

def sortable?
  @sortable
end

#to_sObject



78
79
80
81
82
83
84
85
86
# File 'lib/filterameter/filter_declaration.rb', line 78

def to_s
  options = {}
  options[:name] = ":#{@name}" if @parameter_name != @name
  options[:association] = @association if nested?
  options[:partial] = partial_options if partial_options

  (["filter :#{@parameter_name}"] + options.map { |k, v| "#{k}: #{v}" })
    .join(', ')
end

#validations?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/filterameter/filter_declaration.rb', line 38

def validations?
  !@validations.empty?
end