Module: ValidatesTimeliness::ActiveRecord::AttributeMethods

Defined in:
lib/validates_timeliness/active_record/attribute_methods.rb

Overview

Overrides write method for date, time and datetime columns to use plugin parser. Also adds mechanism to store value before type cast.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/validates_timeliness/active_record/attribute_methods.rb', line 15

def self.included(base)
  base.extend ClassMethods
  base.class_eval do
    alias_method_chain :read_attribute_before_type_cast, :timeliness
    class << self
      alias_method_chain :define_attribute_methods, :timeliness
    end
  end
end

Instance Method Details

#read_attribute_before_type_cast_with_timeliness(attr_name) ⇒ Object



37
38
39
40
41
# File 'lib/validates_timeliness/active_record/attribute_methods.rb', line 37

def read_attribute_before_type_cast_with_timeliness(attr_name)
  cached_attr = "_#{attr_name}_before_type_cast"
  return @attributes_cache[cached_attr] if @attributes_cache.has_key?(cached_attr)
  read_attribute_before_type_cast_without_timeliness(attr_name)
end

#write_date_time_attribute(attr_name, value, type, time_zone_aware) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/validates_timeliness/active_record/attribute_methods.rb', line 25

def write_date_time_attribute(attr_name, value, type, time_zone_aware)
  @attributes_cache["_#{attr_name}_before_type_cast"] = value
  value = ValidatesTimeliness::Parser.parse(value, type)

  if value && type != :date
    value = value.to_time unless Time === value
    value = value.in_time_zone if time_zone_aware
  end

  write_attribute(attr_name.to_sym, value)
end