Class: Admino::Query::SearchField

Inherits:
Object
  • Object
show all
Defined in:
lib/admino/query/search_field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, params) ⇒ SearchField

Returns a new instance of SearchField.



11
12
13
14
# File 'lib/admino/query/search_field.rb', line 11

def initialize(config, params)
  @config = config
  @params = ActiveSupport::HashWithIndifferentAccess.new(params)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/admino/query/search_field.rb', line 9

def config
  @config
end

#paramsObject (readonly)

Returns the value of attribute params.



8
9
10
# File 'lib/admino/query/search_field.rb', line 8

def params
  @params
end

Instance Method Details

#augment_scope(scope) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/admino/query/search_field.rb', line 16

def augment_scope(scope)
  if present?
    scope.send(scope_name, value)
  else
    scope
  end
end

#param_nameObject



43
44
45
# File 'lib/admino/query/search_field.rb', line 43

def param_name
  config.name
end

#present?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/admino/query/search_field.rb', line 39

def present?
  value.present?
end

#scope_nameObject



47
48
49
# File 'lib/admino/query/search_field.rb', line 47

def scope_name
  config.name
end

#valueObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/admino/query/search_field.rb', line 24

def value
  value = params.fetch(:query, {}).fetch(param_name, nil)

  if config.coerce_to
    value = begin
              coercer = Coercible::Coercer.new
              coercer[value.class].send(config.coerce_to, value)
            rescue Coercible::UnsupportedCoercion
              nil
            end
  end

  value || config.default_value
end