Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Time columns should support time zone aware attributes
The types that are affected by `time_zone_aware_attributes` (which is on by default) have been made configurable, in case this is a breaking change for existing applications.
- Loading branch information
Showing
8 changed files
with
116 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ def type_cast_from_user(value) | |
value.map { |v| type_cast_from_user(v) } | ||
elsif value.respond_to?(:in_time_zone) | ||
begin | ||
value.in_time_zone || super | ||
user_input_in_time_zone(value) || super | ||
rescue ArgumentError | ||
nil | ||
end | ||
|
@@ -39,6 +39,9 @@ def convert_time_to_time_zone(value) | |
|
||
class_attribute :skip_time_zone_conversion_for_attributes, instance_writer: false | ||
self.skip_time_zone_conversion_for_attributes = [] | ||
|
||
class_attribute :time_zone_aware_types, instance_writer: false | ||
self.time_zone_aware_types = [:datetime, :not_explicitly_configured] | ||
end | ||
|
||
module ClassMethods | ||
|
@@ -59,9 +62,31 @@ def inherited(subclass) | |
end | ||
|
||
def create_time_zone_conversion_attribute?(name, cast_type) | ||
time_zone_aware_attributes && | ||
!self.skip_time_zone_conversion_for_attributes.include?(name.to_sym) && | ||
(:datetime == cast_type.type) | ||
enabled_for_column = time_zone_aware_attributes && | ||
!self.skip_time_zone_conversion_for_attributes.include?(name.to_sym) | ||
result = enabled_for_column && | ||
time_zone_aware_types.include?(cast_type.type) | ||
|
||
if enabled_for_column && | ||
!result && | ||
cast_type.type == :time && | ||
time_zone_aware_types.include?(:not_explicitly_configured) | ||
ActiveSupport::Deprecation.warn(<<-MESSAGE) | ||
Time columns will become time zone aware in Rails 5.1. This | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
sgrif
via email
Author
Contributor
|
||
sill cause `String`s to be parsed as if they were in `Time.zone`, | ||
and `Time`s to be converted to `Time.zone`. | ||
To keep the old behavior, you must add the following to your initializer: | ||
config.active_record.time_zone_aware_types = [:datetime] | ||
To silence this deprecation warning, add the following: | ||
config.active_record.time_zone_aware_types << :time | ||
MESSAGE | ||
end | ||
|
||
result | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@sgrif the changelog says 5.0 and the message says 5.1.