1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00

Set timestamps after clearing changes

Rather than redefining #created_at and #updated_at, we can use the
existing methods and just set them if there aren't already values
present. We need to do this after clearing changes information,
otherwise the times end up getting converted to the wrong zone.
This commit is contained in:
Daniel Colson 2018-10-26 14:47:44 -04:00
parent dc327388fa
commit e7f66fb2ba

View file

@ -32,6 +32,7 @@ module FactoryBot
evaluation.object.tap do |instance|
stub_database_interaction_on_result(instance)
clear_changes_information(instance)
set_timestamps(instance)
evaluation.notify(:after_stub, instance)
end
end
@ -64,35 +65,6 @@ module FactoryBot
end
end
end
created_at_missing_default = result_instance.respond_to?(:created_at) && !result_instance.created_at
if created_at_missing_default
result_instance.instance_eval do
def created_at
@created_at ||= Time.now.in_time_zone
end
def created_at=(date)
@created_at = date
end
end
end
has_updated_at = result_instance.respond_to?(:updated_at)
updated_at_no_default = has_updated_at && !result_instance.updated_at
if updated_at_no_default
result_instance.instance_eval do
def updated_at
@updated_at ||= Time.current
end
def updated_at=(date)
@updated_at = date
end
end
end
end
def clear_changes_information(result_instance)
@ -100,6 +72,28 @@ module FactoryBot
result_instance.clear_changes_information
end
end
def set_timestamps(result_instance)
if missing_created_at?(result_instance)
result_instance.created_at = Time.current
end
if missing_updated_at?(result_instance)
result_instance.updated_at = Time.current
end
end
def missing_created_at?(result_instance)
result_instance.respond_to?(:created_at) &&
result_instance.respond_to?(:created_at=) &&
result_instance.created_at.blank?
end
def missing_updated_at?(result_instance)
result_instance.respond_to?(:updated_at) &&
result_instance.respond_to?(:updated_at=) &&
result_instance.updated_at.blank?
end
end
end
end