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

Move some things around and make association not a class method anymore

This commit is contained in:
Joshua Clayton 2011-12-16 11:37:30 -05:00
parent 1a3693d7c3
commit 43c21a71e2
5 changed files with 20 additions and 12 deletions

View file

@ -4,7 +4,8 @@ module FactoryGirl
@build_strategy = build_strategy
@overrides = overrides.dup
@cached_attributes = overrides
@callbacks = callbacks
@build_strategy.add_observer(CallbackRunner.new(callbacks, self))
end
delegate :association, :to => :@build_strategy
@ -21,9 +22,18 @@ module FactoryGirl
@overrides
end
def update(name, result_instance)
@callbacks.select {|callback| callback.name == name }.each do |callback|
callback.run(result_instance, self)
private
class CallbackRunner
def initialize(callbacks, evaluator)
@callbacks = callbacks
@evaluator = evaluator
end
def update(name, result_instance)
@callbacks.select {|callback| callback.name == name }.each do |callback|
callback.run(result_instance, @evaluator)
end
end
end
end

View file

@ -37,12 +37,10 @@ module FactoryGirl
block ||= lambda {|result| result }
compile
evaluator = evaluator_class_definer.evaluator_class.new(proxy_class, overrides, callbacks)
attribute_assigner = AttributeAssigner.new(build_class, evaluator, attributes)
proxy = proxy_class.new :to_create => to_create
proxy.add_observer(evaluator)
evaluator = evaluator_class_definer.evaluator_class.new(proxy, overrides, callbacks)
attribute_assigner = AttributeAssigner.new(build_class, evaluator, attributes)
block[proxy.result(attribute_assigner)]
end

View file

@ -45,7 +45,7 @@ module FactoryGirl
# # author association, and saves the Post.
# FactoryGirl.create(:post)
#
def self.association(name, overrides = {})
def association(name, overrides = {})
end
def result

View file

@ -1,7 +1,7 @@
module FactoryGirl
class Proxy #:nodoc:
class Build < Proxy #:nodoc:
def self.association(factory_name, overrides = {})
def association(factory_name, overrides = {})
factory = FactoryGirl.factory_by_name(factory_name)
factory.run(get_method(overrides[:method]), overrides.except(:method))
end
@ -14,7 +14,7 @@ module FactoryGirl
private
def self.get_method(method)
def get_method(method)
case method
when :build then Proxy::Build
when :create then Proxy::Create

View file

@ -3,7 +3,7 @@ module FactoryGirl
class Stub < Proxy #:nodoc:
@@next_id = 1000
def self.association(factory_name, overrides = {})
def association(factory_name, overrides = {})
factory = FactoryGirl.factory_by_name(factory_name)
factory.run(Proxy::Stub, overrides.except(:method))
end