Sha256: d5055802cd7b3866e4b243f3fd54648915d95c8fabd2c96596d5b5ae50aa7909

Contents?: true

Size: 907 Bytes

Versions: 5

Compression:

Stored size: 907 Bytes

Contents

class DateField < Field
  def default_input_type
    :date
  end
  
  def before_create(record)
    return unless name == 'created_at' || name == 'updated_at'
    record.set(name, Time.now.utc.to_date)
  end

  def before_update(record)
    return unless name == 'updated_at'
    record.set(name, Time.now.utc.to_date)
  end
  
  def typecast(value, record)
    value.blank? ? nil : value.to_date
  end
  
  def untypecast(value, record)
    value.blank? ? nil : Time.utc(value.year, value.month, value.day)
  end
  
  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
end

Field::TYPES['date'] = DateField

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
yodel-0.0.7 lib/yodel/models/core/fields/date_field.rb
yodel-0.0.4 lib/yodel/models/core/fields/date_field.rb
yodel-0.0.3 lib/yodel/models/core/fields/date_field.rb
yodel-0.0.2 lib/yodel/models/core/fields/date_field.rb
yodel-0.0.1 lib/yodel/models/core/fields/date_field.rb