Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 68 lines (61 sloc) 2.897 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
6d30002 @lifo Revert "Refactoring attributes/types" [#3348 state:open]
lifo authored
19 # Defined for all +datetime+ and +timestamp+ attributes when +time_zone_aware_attributes+ are enabled.
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
20 # This 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
21 # zone stored in Time.zone.
6d30002 @lifo Revert "Refactoring attributes/types" [#3348 state:open]
lifo authored
22 def define_method_attribute(attr_name)
23 if create_time_zone_conversion_attribute?(attr_name, columns_hash[attr_name])
13e00ce @spastorino fix stack trace lines on class_eval
spastorino authored
24 method_body, line = <<-EOV, __LINE__ + 1
c5908a8 @adzap Fix before_type_cast for timezone aware attributes by caching convert…
adzap authored
25 def _#{attr_name}
6d30002 @lifo Revert "Refactoring attributes/types" [#3348 state:open]
lifo authored
26 cached = @attributes_cache['#{attr_name}']
c5908a8 @adzap Fix before_type_cast for timezone aware attributes by caching convert…
adzap authored
27 return cached if cached
e0e3adf @jeremy Cheaper attribute reads and respond_to?. Add underscore-prefixed meth…
jeremy authored
28 time = _read_attribute('#{attr_name}')
6d30002 @lifo Revert "Refactoring attributes/types" [#3348 state:open]
lifo authored
29 @attributes_cache['#{attr_name}'] = time.acts_like?(:time) ? time.in_time_zone : time
30 end
e0e3adf @jeremy Cheaper attribute reads and respond_to?. Add underscore-prefixed meth…
jeremy authored
31 alias #{attr_name} _#{attr_name}
6d30002 @lifo Revert "Refactoring attributes/types" [#3348 state:open]
lifo authored
32 EOV
13e00ce @spastorino fix stack trace lines on class_eval
spastorino authored
33 generated_attribute_methods.module_eval(method_body, __FILE__, line)
6d30002 @lifo Revert "Refactoring attributes/types" [#3348 state:open]
lifo authored
34 else
35 super
36 end
37 end
c2b075b @josh Concernify AR AttributeMethods
josh authored
38
6d30002 @lifo Revert "Refactoring attributes/types" [#3348 state:open]
lifo authored
39 # Defined for all +datetime+ and +timestamp+ attributes when +time_zone_aware_attributes+ are enabled.
40 # This enhanced write method will automatically convert the time passed to it to the zone stored in Time.zone.
41 def define_method_attribute=(attr_name)
42 if create_time_zone_conversion_attribute?(attr_name, columns_hash[attr_name])
13e00ce @spastorino fix stack trace lines on class_eval
spastorino authored
43 method_body, line = <<-EOV, __LINE__ + 1
817e370 @amatsuda Make before_type_cast available for datetime fields
amatsuda authored
44 def #{attr_name}=(original_time)
c5908a8 @adzap Fix before_type_cast for timezone aware attributes by caching convert…
adzap authored
45 time = original_time
6d30002 @lifo Revert "Refactoring attributes/types" [#3348 state:open]
lifo authored
46 unless time.acts_like?(:time)
47 time = time.is_a?(String) ? Time.zone.parse(time) : time.to_time rescue time
48 end
49 time = time.in_time_zone rescue nil if time
c5908a8 @adzap Fix before_type_cast for timezone aware attributes by caching convert…
adzap authored
50 write_attribute(:#{attr_name}, original_time)
51 @attributes_cache["#{attr_name}"] = time
6d30002 @lifo Revert "Refactoring attributes/types" [#3348 state:open]
lifo authored
52 end
53 EOV
13e00ce @spastorino fix stack trace lines on class_eval
spastorino authored
54 generated_attribute_methods.module_eval(method_body, __FILE__, line)
6d30002 @lifo Revert "Refactoring attributes/types" [#3348 state:open]
lifo authored
55 else
56 super
57 end
c2b075b @josh Concernify AR AttributeMethods
josh authored
58 end
59
6d30002 @lifo Revert "Refactoring attributes/types" [#3348 state:open]
lifo authored
60 private
61 def create_time_zone_conversion_attribute?(name, column)
a9f3c9d @sikachu Using Object#in? and Object#either? in various places
sikachu authored
62 time_zone_aware_attributes && !self.skip_time_zone_conversion_for_attributes.include?(name.to_sym) && column.type.either?(:datetime, :timestamp)
6d30002 @lifo Revert "Refactoring attributes/types" [#3348 state:open]
lifo authored
63 end
c2b075b @josh Concernify AR AttributeMethods
josh authored
64 end
65 end
66 end
67 end
Something went wrong with that request. Please try again.