Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 63 lines (54 sloc) 2.477 kB
d7db6a8 @joshk class inheritable attributes is used no more! all internal use of cla…
joshk authored
1 require 'active_support/core_ext/class/attribute'
a9f3c9d @sikachu Using Object#in? and Object#either? in various places
sikachu authored
2 require 'active_support/core_ext/object/inclusion'
d7db6a8 @joshk class inheritable attributes is used no more! all internal use of cla…
joshk authored
3
c2b075b @josh Concernify AR AttributeMethods
josh authored
4 module ActiveRecord
5 module AttributeMethods
6 module TimeZoneConversion
7 extend ActiveSupport::Concern
8
9 included do
10 cattr_accessor :time_zone_aware_attributes, :instance_writer => false
11 self.time_zone_aware_attributes = false
12
d7db6a8 @joshk class inheritable attributes is used no more! all internal use of cla…
joshk authored
13 class_attribute :skip_time_zone_conversion_for_attributes, :instance_writer => false
c2b075b @josh Concernify AR AttributeMethods
josh authored
14 self.skip_time_zone_conversion_for_attributes = []
15 end
16
17 module ClassMethods
18 protected
9c172b2 @jonleighton Fix #3837.
jonleighton authored
19 # The enhanced read method automatically converts the UTC time stored in the database to the time
1ce40ca @neerajdotname ensuring that description does not exceed 100 columns
neerajdotname authored
20 # zone stored in Time.zone.
9c172b2 @jonleighton Fix #3837.
jonleighton authored
21 def attribute_cast_code(attr_name)
f1a534a @jonleighton Remove the need for type_cast_attribute.
jonleighton authored
22 column = columns_hash[attr_name]
23
24 if create_time_zone_conversion_attribute?(attr_name, column)
9c172b2 @jonleighton Fix #3837.
jonleighton authored
25 typecast = "v = #{super}"
26 time_zone_conversion = "v.acts_like?(:time) ? v.in_time_zone : v"
f1a534a @jonleighton Remove the need for type_cast_attribute.
jonleighton authored
27
9c172b2 @jonleighton Fix #3837.
jonleighton authored
28 "((#{typecast}) && (#{time_zone_conversion}))"
6d30002 @lifo Revert "Refactoring attributes/types" [#3348 state:open]
lifo authored
29 else
30 super
31 end
32 end
c2b075b @josh Concernify AR AttributeMethods
josh authored
33
6d30002 @lifo Revert "Refactoring attributes/types" [#3348 state:open]
lifo authored
34 # Defined for all +datetime+ and +timestamp+ attributes when +time_zone_aware_attributes+ are enabled.
35 # This enhanced write method will automatically convert the time passed to it to the zone stored in Time.zone.
36 def define_method_attribute=(attr_name)
37 if create_time_zone_conversion_attribute?(attr_name, columns_hash[attr_name])
13e00ce @spastorino fix stack trace lines on class_eval
spastorino authored
38 method_body, line = <<-EOV, __LINE__ + 1
817e370 @amatsuda Make before_type_cast available for datetime fields
amatsuda authored
39 def #{attr_name}=(original_time)
c5908a8 @adzap Fix before_type_cast for timezone aware attributes by caching convert…
adzap authored
40 time = original_time
6d30002 @lifo Revert "Refactoring attributes/types" [#3348 state:open]
lifo authored
41 unless time.acts_like?(:time)
42 time = time.is_a?(String) ? Time.zone.parse(time) : time.to_time rescue time
43 end
44 time = time.in_time_zone rescue nil if time
c5908a8 @adzap Fix before_type_cast for timezone aware attributes by caching convert…
adzap authored
45 write_attribute(:#{attr_name}, original_time)
46 @attributes_cache["#{attr_name}"] = time
6d30002 @lifo Revert "Refactoring attributes/types" [#3348 state:open]
lifo authored
47 end
48 EOV
13e00ce @spastorino fix stack trace lines on class_eval
spastorino authored
49 generated_attribute_methods.module_eval(method_body, __FILE__, line)
6d30002 @lifo Revert "Refactoring attributes/types" [#3348 state:open]
lifo authored
50 else
51 super
52 end
c2b075b @josh Concernify AR AttributeMethods
josh authored
53 end
54
6d30002 @lifo Revert "Refactoring attributes/types" [#3348 state:open]
lifo authored
55 private
56 def create_time_zone_conversion_attribute?(name, column)
733bfa6 @sikachu Remove `#among?` from Active Support
sikachu authored
57 time_zone_aware_attributes && !self.skip_time_zone_conversion_for_attributes.include?(name.to_sym) && column.type.in?([:datetime, :timestamp])
6d30002 @lifo Revert "Refactoring attributes/types" [#3348 state:open]
lifo authored
58 end
c2b075b @josh Concernify AR AttributeMethods
josh authored
59 end
60 end
61 end
62 end
Something went wrong with that request. Please try again.