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