2010-06-24 09:45:57 -04:00
|
|
|
module FactoryGirl
|
2012-04-13 14:20:19 -04:00
|
|
|
module Strategy
|
|
|
|
class Stub
|
2009-04-28 10:39:32 -04:00
|
|
|
@@next_id = 1000
|
|
|
|
|
2012-02-10 16:56:07 -05:00
|
|
|
def association(runner)
|
2012-04-25 14:29:02 -05:00
|
|
|
runner.run(:build_stubbed)
|
2011-12-02 21:32:38 -05:00
|
|
|
end
|
|
|
|
|
2012-04-13 14:20:19 -04:00
|
|
|
def result(evaluation)
|
|
|
|
evaluation.object.tap do |instance|
|
|
|
|
stub_database_interaction_on_result(instance)
|
|
|
|
evaluation.notify(:after_stub, instance)
|
2011-12-16 11:30:04 -05:00
|
|
|
end
|
2011-12-02 21:32:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def next_id
|
|
|
|
@@next_id += 1
|
|
|
|
end
|
|
|
|
|
2011-12-16 11:30:04 -05:00
|
|
|
def stub_database_interaction_on_result(result_instance)
|
2012-11-07 09:22:38 -05:00
|
|
|
result_instance.id ||= next_id
|
2012-06-22 14:33:30 -04:00
|
|
|
|
2011-11-22 18:01:01 -05:00
|
|
|
result_instance.instance_eval do
|
2011-06-27 16:44:37 -04:00
|
|
|
def persisted?
|
|
|
|
!new_record?
|
|
|
|
end
|
|
|
|
|
2009-04-28 10:39:32 -04:00
|
|
|
def new_record?
|
|
|
|
id.nil?
|
|
|
|
end
|
|
|
|
|
2010-04-22 11:45:19 -04:00
|
|
|
def save(*args)
|
2014-03-13 16:44:24 +00:00
|
|
|
raise "stubbed models are not allowed to access the database - #{self.class.to_s}#save(#{args.join(",")})"
|
2010-04-22 11:45:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy(*args)
|
2014-03-13 16:44:24 +00:00
|
|
|
raise "stubbed models are not allowed to access the database - #{self.class.to_s}#destroy(#{args.join(",")})"
|
2010-04-22 11:45:19 -04:00
|
|
|
end
|
|
|
|
|
2009-04-28 10:39:32 -04:00
|
|
|
def connection
|
2014-03-13 16:44:24 +00:00
|
|
|
raise "stubbed models are not allowed to access the database - #{self.class.to_s}#connection()"
|
2009-04-28 10:39:32 -04:00
|
|
|
end
|
|
|
|
|
2014-11-18 06:20:51 +00:00
|
|
|
def reload(*args)
|
2014-03-13 16:44:24 +00:00
|
|
|
raise "stubbed models are not allowed to access the database - #{self.class.to_s}#reload()"
|
2009-04-28 10:39:32 -04:00
|
|
|
end
|
2010-08-25 11:12:15 -04:00
|
|
|
|
|
|
|
def update_attribute(*args)
|
2014-03-13 16:44:24 +00:00
|
|
|
raise "stubbed models are not allowed to access the database - #{self.class.to_s}#update_attribute(#{args.join(",")})"
|
2010-08-25 11:12:15 -04:00
|
|
|
end
|
2012-08-02 11:06:18 -04:00
|
|
|
|
|
|
|
def update_column(*args)
|
2014-03-13 16:44:24 +00:00
|
|
|
raise "stubbed models are not allowed to access the database - #{self.class.to_s}#update_column(#{args.join(",")})"
|
2012-08-02 11:06:18 -04:00
|
|
|
end
|
2016-02-05 15:04:57 -05:00
|
|
|
|
|
|
|
def increment!(*args)
|
|
|
|
raise "stubbed models are not allowed to access the database - #{self.class}#increment!(#{args.join(',')})"
|
|
|
|
end
|
|
|
|
|
|
|
|
def decrement!(*args)
|
|
|
|
raise "stubbed models are not allowed to access the database - #{self.class}#decrement!(#{args.join(',')})"
|
|
|
|
end
|
2009-04-28 10:39:32 -04:00
|
|
|
end
|
2012-06-22 14:33:30 -04:00
|
|
|
|
|
|
|
created_at_missing_default = result_instance.respond_to?(:created_at) && !result_instance.created_at
|
|
|
|
result_instance_missing_created_at = !result_instance.respond_to?(:created_at)
|
|
|
|
|
|
|
|
if created_at_missing_default || result_instance_missing_created_at
|
|
|
|
result_instance.instance_eval do
|
|
|
|
def created_at
|
2012-12-20 15:59:20 -05:00
|
|
|
@created_at ||= Time.now.in_time_zone
|
2012-06-22 14:33:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-04-28 10:39:32 -04:00
|
|
|
end
|
2009-01-06 15:22:08 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|