Class: ArrayField

Inherits:
Field
  • Object
show all
Defined in:
lib/yodel/models/core/fields/array_field.rb

Direct Known Subclasses

TagsField

Constant Summary

Constants inherited from Field

Field::TYPES

Instance Attribute Summary

Attributes inherited from Field

#name, #options

Instance Method Summary collapse

Methods inherited from Field

#default_input_type, #display?, field_from_type, from_options, #include_in_search_keywords?, #index?, #inherited?, #method_missing, #numeric?, #required?, #searchable?, #strip_nil?, #to_json, #to_str, #unique?, #validate

Constructor Details

#initialize(name, options = {}) ⇒ ArrayField

TODO: validate should defer to @element_type over each element



3
4
5
6
# File 'lib/yodel/models/core/fields/array_field.rb', line 3

def initialize(name, options={})
  @element_type = Field.from_options(name, 'type' => options['of'].to_s.singularize) if options['of']
  super
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Field

Instance Method Details

#from_json(value, record) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/yodel/models/core/fields/array_field.rb', line 40

def from_json(value, record)
  return [] if value.blank?
  if value.is_a?(String)
    if value.include?(',')
      value = value.split(',')
    else
      value = value.split(' ')
    end
  end
  process(value.to_a, record, :from_json)
end

#json_action(action, value, record) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/yodel/models/core/fields/array_field.rb', line 8

def json_action(action, value, record)
  array = record.get_raw(name)
  value = process(value, record, :from_json)
  value = [value] unless value.is_a?(Array)
  
  case action
  when 'set'
    array = value
  when 'add'
    array += value
  when 'add_unique'
    array |= value
  when 'remove'
    array -= value
  when 'clear'
    array = []
  end
  
  record.set_raw(name, array)
  record.changed!(name)
end

#typecast(value, record) ⇒ Object



30
31
32
33
# File 'lib/yodel/models/core/fields/array_field.rb', line 30

def typecast(value, record)
  value = [] unless value.is_a?(Array)
  ChangeSensitiveArray.new(record, name, process(value, record, :typecast))
end

#untypecast(value, record) ⇒ Object



35
36
37
38
# File 'lib/yodel/models/core/fields/array_field.rb', line 35

def untypecast(value, record)
  return [] if value.blank?
  process(value.to_a, record, :untypecast)
end