提交 d4bf1c97 编写于 作者: C Carlos Antonio da Silva

Extract #in_time_zone helper method duplication to a module

上级 6d2aadef
...@@ -12,6 +12,8 @@ ...@@ -12,6 +12,8 @@
require 'models/keyboard' require 'models/keyboard'
class AttributeMethodsTest < ActiveRecord::TestCase class AttributeMethodsTest < ActiveRecord::TestCase
include InTimeZome
fixtures :topics, :developers, :companies, :computers fixtures :topics, :developers, :companies, :computers
def setup def setup
...@@ -800,18 +802,6 @@ def time_related_columns_on_topic ...@@ -800,18 +802,6 @@ def time_related_columns_on_topic
Topic.columns.select { |c| [:time, :date, :datetime, :timestamp].include?(c.type) } Topic.columns.select { |c| [:time, :date, :datetime, :timestamp].include?(c.type) }
end 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) def privatize(method_signature)
@target.class_eval(<<-private_method, __FILE__, __LINE__ + 1) @target.class_eval(<<-private_method, __FILE__, __LINE__ + 1)
private private
......
...@@ -27,6 +27,8 @@ class NumericData < ActiveRecord::Base ...@@ -27,6 +27,8 @@ class NumericData < ActiveRecord::Base
end end
class DirtyTest < ActiveRecord::TestCase class DirtyTest < ActiveRecord::TestCase
include InTimeZome
# Dummy to force column loads so query counts are clean. # Dummy to force column loads so query counts are clean.
def setup def setup
Person.create :first_name => 'foo' Person.create :first_name => 'foo'
...@@ -603,16 +605,4 @@ def check_pirate_after_save_failure(pirate) ...@@ -603,16 +605,4 @@ def check_pirate_after_save_failure(pirate)
assert_equal %w(parrot_id), pirate.changed assert_equal %w(parrot_id), pirate.changed
assert_nil pirate.parrot_id_was assert_nil pirate.parrot_id_was
end 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 end
...@@ -135,3 +135,16 @@ def log(sql, name, binds = [], &block) ...@@ -135,3 +135,16 @@ def log(sql, name, binds = [], &block)
end end
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册