Module: CustomFields::Types::DateTime::Target::ClassMethods
- Defined in:
- lib/custom_fields/types/date_time.rb
Instance Method Summary collapse
-
#apply_date_time_custom_field(klass, rule) ⇒ Object
Adds a date_time field.
-
#date_time_attribute_get(instance, name) ⇒ Hash
Build a hash storing the formatted value for a date_time custom field of an instance.
-
#date_time_attribute_set(instance, name, attributes) ⇒ Object
Set the value for the instance and the date_time field specified by the 2 params.
Instance Method Details
#apply_date_time_custom_field(klass, rule) ⇒ Object
Adds a date_time field
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# 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) ⇒ Hash
Build a hash storing the formatted value for a date_time custom field of an instance.
39 40 41 42 43 44 45 |
# 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) ⇒ Object
Set the value for the instance and the date_time field specified by the 2 params.
54 55 56 57 58 59 60 |
# 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 |