2010-06-24 09:45:57 -04:00
|
|
|
module FactoryGirl
|
2009-01-06 15:22:08 -05:00
|
|
|
class Proxy
|
2009-02-17 16:38:15 -05:00
|
|
|
class Stub < Proxy #:nodoc:
|
2009-04-28 10:39:32 -04:00
|
|
|
@@next_id = 1000
|
|
|
|
|
2011-10-20 14:19:09 -04:00
|
|
|
def initialize(klass, callbacks = [])
|
|
|
|
super
|
2011-11-22 18:01:01 -05:00
|
|
|
result_instance.id = next_id
|
|
|
|
result_instance.instance_eval do
|
2011-06-27 16:44:37 -04:00
|
|
|
def persisted?
|
|
|
|
!new_record?
|
|
|
|
end
|
|
|
|
|
2011-09-02 16:22:53 -04:00
|
|
|
def created_at
|
|
|
|
@created_at ||= Time.now
|
|
|
|
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)
|
|
|
|
raise "stubbed models are not allowed to access the database"
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy(*args)
|
|
|
|
raise "stubbed models are not allowed to access the database"
|
|
|
|
end
|
|
|
|
|
2009-04-28 10:39:32 -04:00
|
|
|
def connection
|
|
|
|
raise "stubbed models are not allowed to access the database"
|
|
|
|
end
|
|
|
|
|
|
|
|
def reload
|
|
|
|
raise "stubbed models are not allowed to access the database"
|
|
|
|
end
|
2010-08-25 11:12:15 -04:00
|
|
|
|
|
|
|
def update_attribute(*args)
|
|
|
|
raise "stubbed models are not allowed to access the database"
|
|
|
|
end
|
2009-04-28 10:39:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-11 15:00:14 -04:00
|
|
|
def association(factory_name, overrides = {})
|
2011-05-19 10:56:45 -04:00
|
|
|
factory = FactoryGirl.factory_by_name(factory_name)
|
2011-10-20 15:33:37 -04:00
|
|
|
factory.run(Proxy::Stub, overrides.except(:method))
|
2009-01-06 15:22:08 -05:00
|
|
|
end
|
2009-04-28 10:39:32 -04:00
|
|
|
|
2010-11-12 15:21:16 -06:00
|
|
|
def result(to_create)
|
2009-10-09 23:46:19 -05:00
|
|
|
run_callbacks(:after_stub)
|
2011-11-22 18:01:01 -05:00
|
|
|
result_instance
|
2009-01-06 15:22:08 -05:00
|
|
|
end
|
2011-11-23 13:47:24 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def next_id
|
|
|
|
@@next_id += 1
|
|
|
|
end
|
2009-01-06 15:22:08 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|