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

Pass in build strategy to evaluator class to have access to association

This commit is contained in:
Joshua Clayton 2011-12-02 13:17:57 -05:00
parent 7b1bf021af
commit cebabfee96
3 changed files with 11 additions and 8 deletions

View file

@ -1,9 +1,12 @@
module FactoryGirl
class Evaluator
def initialize(overrides = {})
def initialize(build_strategy, overrides = {})
@build_strategy = build_strategy
@cached_attributes = overrides
end
delegate :association, :to => :@build_strategy
def method_missing(method_name, *args, &block)
if @cached_attributes.key?(method_name)
@cached_attributes[method_name]

View file

@ -91,9 +91,6 @@ module FactoryGirl
@assigned_attributes = []
@evaluator_class_definer = EvaluatorClassDefiner.new
@evaluator_class_definer.evaluator_class.send(:define_method, :association) { |*args|
proxy.association(*args)
}
end
delegate :set, :attributes, :to => :@evaluator_class_definer
@ -112,7 +109,7 @@ module FactoryGirl
end
def anonymous_instance
@anonymous_instance ||= @evaluator_class_definer.evaluator_class.new
@anonymous_instance ||= @evaluator_class_definer.evaluator_class.new(@proxy)
end
private

View file

@ -1,14 +1,17 @@
require "spec_helper"
shared_examples "#set on an EvaluatorClassDefiner" do
let(:build_strategy) { stub("build strategy") }
let(:evaluator_instance) { subject.evaluator_class.new(build_strategy) }
it "adds the method to the evaluator" do
subject.set(attribute)
subject.evaluator_class.new.one.should == 1
evaluator_instance.one.should == 1
end
it "caches the result" do
subject.set(attribute)
subject.evaluator_class.new.tap do |obj|
evaluator_instance.tap do |obj|
obj.one.should == 1
obj.one.should == 1
end
@ -18,7 +21,7 @@ shared_examples "#set on an EvaluatorClassDefiner" do
subject.set(attribute)
second_attribute = stub("attribute", :name => :two, :to_proc => lambda { one + 1 }, :ignored => false)
subject.set(second_attribute)
subject.evaluator_class.new.two.should == 2
evaluator_instance.two.should == 2
end
end