Class: TimeField

Inherits:
Field show all
Defined in:
lib/yodel/models/core/fields/time_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/time_field.rb', line 6

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

#before_update(record) ⇒ Object



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

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

#default_input_typeObject



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

def default_input_type
  :datetime
end

#from_json(value, record) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/yodel/models/core/fields/time_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', 'hour', 'min'].all? {|field| value.key?(field) && !value[field].blank?}
    sec = value['sec'] || 0
    Time.new(value['year'], value['month'], value['day'], value['hour'], value['min'], sec).utc
  else
    Time.parse(value).utc
  end
end

#typecast(value, record) ⇒ Object



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

def typecast(value, record)
  value.nil? ? nil : Time.at(value)
end

#untypecast(value, record) ⇒ Object



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

def untypecast(value, record)
  value.blank? ? nil : Time.at(value.to_i).utc
end