1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Extract #in_time_zone helper method duplication to a module

This commit is contained in:
Carlos Antonio da Silva 2012-11-17 13:35:39 -02:00
parent 6d2aadef9d
commit d4bf1c97ce
3 changed files with 17 additions and 24 deletions

View file

@ -12,6 +12,8 @@ require 'models/contact'
require 'models/keyboard'
class AttributeMethodsTest < ActiveRecord::TestCase
include InTimeZome
fixtures :topics, :developers, :companies, :computers
def setup
@ -800,18 +802,6 @@ class AttributeMethodsTest < ActiveRecord::TestCase
Topic.columns.select { |c| [:time, :date, :datetime, :timestamp].include?(c.type) }
end
def in_time_zone(zone)
old_zone = Time.zone
old_tz = ActiveRecord::Base.time_zone_aware_attributes
Time.zone = zone ? ActiveSupport::TimeZone[zone] : nil
ActiveRecord::Base.time_zone_aware_attributes = !zone.nil?
yield
ensure
Time.zone = old_zone
ActiveRecord::Base.time_zone_aware_attributes = old_tz
end
def privatize(method_signature)
@target.class_eval(<<-private_method, __FILE__, __LINE__ + 1)
private

View file

@ -27,6 +27,8 @@ class NumericData < ActiveRecord::Base
end
class DirtyTest < ActiveRecord::TestCase
include InTimeZome
# Dummy to force column loads so query counts are clean.
def setup
Person.create :first_name => 'foo'
@ -603,16 +605,4 @@ class DirtyTest < ActiveRecord::TestCase
assert_equal %w(parrot_id), pirate.changed
assert_nil pirate.parrot_id_was
end
def in_time_zone(zone)
old_zone = Time.zone
old_tz = ActiveRecord::Base.time_zone_aware_attributes
Time.zone = zone ? ActiveSupport::TimeZone[zone] : nil
ActiveRecord::Base.time_zone_aware_attributes = !zone.nil?
yield
ensure
Time.zone = old_zone
ActiveRecord::Base.time_zone_aware_attributes = old_tz
end
end

View file

@ -135,3 +135,16 @@ module LogIntercepter
end
end
module InTimeZome
def in_time_zone(zone)
old_zone = Time.zone
old_tz = ActiveRecord::Base.time_zone_aware_attributes
Time.zone = zone ? ActiveSupport::TimeZone[zone] : nil
ActiveRecord::Base.time_zone_aware_attributes = !zone.nil?
yield
ensure
Time.zone = old_zone
ActiveRecord::Base.time_zone_aware_attributes = old_tz
end
end