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:
parent
1a3693d7c3
commit
43c21a71e2
5 changed files with 20 additions and 12 deletions
|
@ -4,7 +4,8 @@ module FactoryGirl
|
||||||
@build_strategy = build_strategy
|
@build_strategy = build_strategy
|
||||||
@overrides = overrides.dup
|
@overrides = overrides.dup
|
||||||
@cached_attributes = overrides
|
@cached_attributes = overrides
|
||||||
@callbacks = callbacks
|
|
||||||
|
@build_strategy.add_observer(CallbackRunner.new(callbacks, self))
|
||||||
end
|
end
|
||||||
|
|
||||||
delegate :association, :to => :@build_strategy
|
delegate :association, :to => :@build_strategy
|
||||||
|
@ -21,9 +22,18 @@ module FactoryGirl
|
||||||
@overrides
|
@overrides
|
||||||
end
|
end
|
||||||
|
|
||||||
def update(name, result_instance)
|
private
|
||||||
@callbacks.select {|callback| callback.name == name }.each do |callback|
|
|
||||||
callback.run(result_instance, self)
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -37,12 +37,10 @@ module FactoryGirl
|
||||||
block ||= lambda {|result| result }
|
block ||= lambda {|result| result }
|
||||||
compile
|
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 = 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)]
|
block[proxy.result(attribute_assigner)]
|
||||||
end
|
end
|
||||||
|
|
|
@ -45,7 +45,7 @@ module FactoryGirl
|
||||||
# # author association, and saves the Post.
|
# # author association, and saves the Post.
|
||||||
# FactoryGirl.create(:post)
|
# FactoryGirl.create(:post)
|
||||||
#
|
#
|
||||||
def self.association(name, overrides = {})
|
def association(name, overrides = {})
|
||||||
end
|
end
|
||||||
|
|
||||||
def result
|
def result
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
module FactoryGirl
|
module FactoryGirl
|
||||||
class Proxy #:nodoc:
|
class Proxy #:nodoc:
|
||||||
class Build < 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 = FactoryGirl.factory_by_name(factory_name)
|
||||||
factory.run(get_method(overrides[:method]), overrides.except(:method))
|
factory.run(get_method(overrides[:method]), overrides.except(:method))
|
||||||
end
|
end
|
||||||
|
@ -14,7 +14,7 @@ module FactoryGirl
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def self.get_method(method)
|
def get_method(method)
|
||||||
case method
|
case method
|
||||||
when :build then Proxy::Build
|
when :build then Proxy::Build
|
||||||
when :create then Proxy::Create
|
when :create then Proxy::Create
|
||||||
|
|
|
@ -3,7 +3,7 @@ module FactoryGirl
|
||||||
class Stub < Proxy #:nodoc:
|
class Stub < Proxy #:nodoc:
|
||||||
@@next_id = 1000
|
@@next_id = 1000
|
||||||
|
|
||||||
def self.association(factory_name, overrides = {})
|
def association(factory_name, overrides = {})
|
||||||
factory = FactoryGirl.factory_by_name(factory_name)
|
factory = FactoryGirl.factory_by_name(factory_name)
|
||||||
factory.run(Proxy::Stub, overrides.except(:method))
|
factory.run(Proxy::Stub, overrides.except(:method))
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue