Skip to content

Commit

Permalink
Merge pull request #5618 from lest/patch-1
Browse files Browse the repository at this point in the history
force datetime attributes to be changed
  • Loading branch information
drogus committed Mar 27, 2012
2 parents 5f37260 + fdcfc75 commit c4048b3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def #{attr_name}=(original_time)
end end
time = time.in_time_zone rescue nil if time time = time.in_time_zone rescue nil if time
write_attribute(:#{attr_name}, original_time) write_attribute(:#{attr_name}, original_time)
#{attr_name}_will_change!
@attributes_cache["#{attr_name}"] = time @attributes_cache["#{attr_name}"] = time
end end
EOV EOV
Expand Down
14 changes: 14 additions & 0 deletions activerecord/test/cases/attribute_methods_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -620,6 +620,20 @@ def test_setting_time_zone_aware_attribute_with_string
end end
end end


def test_time_zone_aware_attribute_saved
old_default, ActiveRecord::Base.default_timezone = ActiveRecord::Base.default_timezone, :utc

in_time_zone 1 do
record = @target.create(:written_on => '2012-02-20 10:00')

record.written_on = '2012-02-20 09:00'
record.save
assert_equal Time.zone.local(2012, 02, 20, 9), record.reload.written_on
end
ensure
ActiveRecord::Base.default_timezone = old_default
end

def test_setting_time_zone_aware_attribute_to_blank_string_returns_nil def test_setting_time_zone_aware_attribute_to_blank_string_returns_nil
in_time_zone "Pacific Time (US & Canada)" do in_time_zone "Pacific Time (US & Canada)" do
record = @target.new record = @target.new
Expand Down

0 comments on commit c4048b3

Please sign in to comment.