module CustomFields::Types::DateTime::Target::ClassMethods
Public Instance Methods
apply_date_time_custom_field(klass, rule)
click to toggle source
Adds a date_time field
@param [ Class ] klass The class to modify @param [ Hash ] rule It contains the name of the field and if it is required or not
# File lib/custom_fields/types/date_time.rb, line 17 def apply_date_time_custom_field(klass, rule) name = rule['name'] klass.field name, type: ::DateTime, localize: rule['localized'] || false # other methods klass.send(:define_method, :"formatted_#{name}") { _get_formatted_date_time(name) } klass.send(:define_method, :"formatted_#{name}=") { |value| _set_formatted_date_time(name, value) } return unless rule['required'] klass.validates_presence_of name, :"formatted_#{name}" end
date_time_attribute_get(instance, name)
click to toggle source
Build a hash storing the formatted value for a date_time custom field of an instance.
@param [ Object ] instance An instance of the class enhanced by the custom_fields @param [ String
] name The name of the date_time custom field
@return [ Hash ] field name => formatted date_time
# File lib/custom_fields/types/date_time.rb, line 39 def date_time_attribute_get(instance, name) if value = instance.send(:"formatted_#{name}") { name => value, "formatted_#{name}" => value } else {} end end
date_time_attribute_set(instance, name, attributes)
click to toggle source
Set the value for the instance and the date_time field specified by the 2 params.
@param [ Object ] instance An instance of the class enhanced by the custom_fields @param [ String
] name The name of the date_time custom field @param [ Hash ] attributes The attributes used to fetch the values
# File lib/custom_fields/types/date_time.rb, line 54 def date_time_attribute_set(instance, name, attributes) return unless attributes.key?(name) || attributes.key?("formatted_#{name}") value = attributes[name] || attributes["formatted_#{name}"] instance.send(:"formatted_#{name}=", value) end