Class: DateField

Inherits:
Field show all
Defined in:
lib/yodel/models/core/fields/date_field.rb

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

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

Constructor Details

This class inherits a constructor from Field

Dynamic Method Handling

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

Instance Method Details

#before_create(record) ⇒ Object



6
7
8
9
# File 'lib/yodel/models/core/fields/date_field.rb', line 6

def before_create(record)
  return unless name == 'created_at' || name == 'updated_at'
  record.set(name, Time.now.utc.to_date)
end

#before_update(record) ⇒ Object



11
12
13
14
# File 'lib/yodel/models/core/fields/date_field.rb', line 11

def before_update(record)
  return unless name == 'updated_at'
  record.set(name, Time.now.utc.to_date)
end

#default_input_typeObject



2
3
4
# File 'lib/yodel/models/core/fields/date_field.rb', line 2

def default_input_type
  :date
end

#from_json(value, record) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/yodel/models/core/fields/date_field.rb', line 24

def from_json(value, record)
  return nil unless value.present? && (value.is_a?(String) || value.is_a?(Hash))
  if value.is_a?(Hash)
    return nil unless ['year', 'month', 'day'].all? {|field| value.key?(field) && !value[field].blank?}
    Time.new(value['year'], value['month'], value['day'])
  else
    Time.parse(value)
  end
end

#typecast(value, record) ⇒ Object



16
17
18
# File 'lib/yodel/models/core/fields/date_field.rb', line 16

def typecast(value, record)
  value.blank? ? nil : value.to_date
end

#untypecast(value, record) ⇒ Object



20
21
22
# File 'lib/yodel/models/core/fields/date_field.rb', line 20

def untypecast(value, record)
  value.blank? ? nil : Time.utc(value.year, value.month, value.day)
end