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



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

def param_name
  config.name
end

#present?Boolean

Returns:

  • (Boolean)


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

def present?
  value.present?
end

#scope_nameObject



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

def scope_name
  config.name
end

#valueObject



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

def value
  value = params.fetch(:query, {}).fetch(param_name, nil)
  if config.coerce_to
    begin
      coercer = Coercible::Coercer.new
      coercer[value.class].send(config.coerce_to, value)
    rescue Coercible::UnsupportedCoercion
      nil
    end
  else
    value
  end
end