Sha256: 6655aab2018539198105f853be20a57069d0112c7d6deb07463239c5a93436b1
Contents?: true
Size: 1.53 KB
Versions: 5
Compression:
Stored size: 1.53 KB
Contents
class ArrayField < Field # TODO: validate should defer to @element_type over each element def initialize(name, options={}) @element_type = Field.from_options(name, 'type' => options['of'].to_s.singularize) if options['of'] super end 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 def typecast(value, record) value = [] unless value.is_a?(Array) ChangeSensitiveArray.new(record, name, process(value, record, :typecast)) end def untypecast(value, record) return [] if value.blank? process(value.to_a, record, :untypecast) end 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 private def process(values, record, method) return values if @element_type.nil? if values.is_a?(Array) values.collect {|element| @element_type.send(method, element, record)} else @element_type.send(method, values, record) end end end Field::TYPES['array'] = ArrayField
Version data entries
5 entries across 5 versions & 1 rubygems