Module: ValidatesTimeliness::ORM::ActiveRecord::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#define_attribute_methodsObject



41
42
43
44
45
46
47
48
49
# File 'lib/validates_timeliness/orm/active_record.rb', line 41

def define_attribute_methods
  super.tap { 
    generated_timeliness_methods.synchronize do
      return if @timeliness_methods_generated
      define_timeliness_methods true
      @timeliness_methods_generated = true
    end
  }
end

#generated_timeliness_methodsObject

Override to overwrite methods in ActiveRecord attribute method module because in AR 4+ there is curious code which calls the method directly from the generated methods module via bind inside method_missing. This means our method in the formerly custom timeliness methods module was never reached.



64
65
66
# File 'lib/validates_timeliness/orm/active_record.rb', line 64

def generated_timeliness_methods
  generated_attribute_methods
end

#lookup_cast_type(sql_type) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/validates_timeliness/orm/active_record.rb', line 25

def lookup_cast_type(sql_type)
  case sql_type
  when 'datetime' then ::ActiveRecord::Type::DateTime.new
  when 'date' then ::ActiveRecord::Type::Date.new
  when 'time' then ::ActiveRecord::Type::Time.new
  end
end

#timeliness_attribute_timezone_aware?(attr_name) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/validates_timeliness/orm/active_record.rb', line 9

def timeliness_attribute_timezone_aware?(attr_name)
  create_time_zone_conversion_attribute?(attr_name, timeliness_column_for_attribute(attr_name))
end

#timeliness_attribute_type(attr_name) ⇒ Object



13
14
15
# File 'lib/validates_timeliness/orm/active_record.rb', line 13

def timeliness_attribute_type(attr_name)
  timeliness_column_for_attribute(attr_name).type
end

#timeliness_column_for_attribute(attr_name) ⇒ Object



18
19
20
21
22
23
# File 'lib/validates_timeliness/orm/active_record.rb', line 18

def timeliness_column_for_attribute(attr_name)
  columns_hash.fetch(attr_name.to_s) do |key|
    validation_type = _validators[key.to_sym].find {|v| v.kind == :timeliness }.type.to_s
    ::ActiveRecord::ConnectionAdapters::Column.new(key, nil, lookup_cast_type(validation_type), validation_type)
  end
end

#undefine_attribute_methodsObject



51
52
53
54
55
56
57
58
59
# File 'lib/validates_timeliness/orm/active_record.rb', line 51

def undefine_attribute_methods
  super.tap { 
    generated_timeliness_methods.synchronize do
      return unless @timeliness_methods_generated
      undefine_timeliness_attribute_methods 
      @timeliness_methods_generated = false
    end
  }
end