Class: KonoUtils::SearchAttribute

Inherits:
Object
  • Object
show all
Defined in:
lib/kono_utils/search_attribute.rb

Overview

 Classe che mi rappresenta un attributo di ricerca  Di default utilizza il tipo string come renderizzazione

  • Args :

    • form_options -> Hash con opzioni da passare a formtastic

    • field_options -> Hash con opzioni:

      cast -> Proc per eseguire il cast del valore
      

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field, options = {}) ⇒ SearchAttribute

Returns a new instance of SearchAttribute.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/kono_utils/search_attribute.rb', line 14

def initialize(field, options = {})
  self.field = field

  self.field_options = {}
  unless options.is_a? Proc
    if options[:field_options]
      self.field_options = options[:field_options]
      options.delete(:field_options)
    end
  end

  self.form_options = options
end

Instance Attribute Details

#fieldObject

Returns the value of attribute field.



12
13
14
# File 'lib/kono_utils/search_attribute.rb', line 12

def field
  @field
end

#field_optionsObject

Returns the value of attribute field_options.



12
13
14
# File 'lib/kono_utils/search_attribute.rb', line 12

def field_options
  @field_options
end

#form_optionsObject

Returns the value of attribute form_options.



12
13
14
# File 'lib/kono_utils/search_attribute.rb', line 12

def form_options
  @form_options
end

Instance Method Details

#cast_value(value) ⇒ Object

Esegue un casting dei valori rispetto al tipo di campo da utilizzare per formtastic



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/kono_utils/search_attribute.rb', line 30

def cast_value(value)
  return value if value.blank?
  return value if form_options.is_a? Proc
  return field_options[:cast].call(value) if field_options[:cast].is_a? Proc
  case form_options[:as]
    when :bs_datetimepicker
      if value.is_a? String
        DateTime.parse(value)
      elsif value.is_a? Date
        value.to_time
      else
        value
      end
    when :bs_datepicker
      if value.is_a? String
        DateTime.parse(value).to_date
      elsif value.is_a? DateTime
        value.to_date
      else
        value
      end
    else
      value
  end

end