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 module FactoryGirl
class Evaluator class Evaluator
def initialize(overrides = {}) def initialize(build_strategy, overrides = {})
@build_strategy = build_strategy
@cached_attributes = overrides @cached_attributes = overrides
end end
delegate :association, :to => :@build_strategy
def method_missing(method_name, *args, &block) def method_missing(method_name, *args, &block)
if @cached_attributes.key?(method_name) if @cached_attributes.key?(method_name)
@cached_attributes[method_name] @cached_attributes[method_name]

View file

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

View file

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