Module: ActiveRecord::RFC3339Attributes
- Defined in:
- lib/active_record/rfc_3339_attributes.rb
Constant Summary collapse
- RFC_3339_REGEXP =
/^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d*)?(Z|(([+-]\d\d):\d\d))$/
Instance Method Summary collapse
Instance Method Details
#rfc_3339_attributes(*attr_names) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/active_record/rfc_3339_attributes.rb', line 5 def rfc_3339_attributes(*attr_names) attr_names.each do |attr_name| define_method "#{attr_name}=" do |time| @attributes_cache.delete(attr_name.to_s) instance_variable_set("@#{attr_name}_before_type_cast", time) if time.respond_to?(:in_time_zone) time = time.in_time_zone elsif parsed_time = Time.zone.parse(time.to_s) time = parsed_time.in_time_zone end @attributes[attr_name.to_s] = time end end end |
#validates_rf_3339_format_of(*attr_names) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/active_record/rfc_3339_attributes.rb', line 22 def validates_rf_3339_format_of(*attr_names) configuration = { :on => :save, :with => RFC_3339_REGEXP, :message => "should be a valid RFC 3339 timestamp" }.update(attr_names.) send(validation_method(configuration[:on] || :save), configuration) do |record| attr_names.each do |attr_name| value = record.send(:instance_variable_get, "@#{attr_name}_before_type_cast") || record.send(attr_name) next if value.acts_like?(:time) if value.blank? and !configuration[:allow_blank] record.errors.add(attr_name, :invalid, :default => "can't be blank", :value => value) elsif value.to_s !~ configuration[:with] record.errors.add(attr_name, :invalid, :default => configuration[:message], :value => value) end end end end |