From 8209a461ee6be587f753b29230d30dd056795602 Mon Sep 17 00:00:00 2001 From: Joshua Clayton Date: Tue, 24 Jul 2012 18:50:11 -0400 Subject: [PATCH] Remove dependency on build_class in Evaluator --- lib/factory_girl.rb | 1 + lib/factory_girl/attribute_assigner.rb | 8 +++++++- lib/factory_girl/decorator/new_constructor.rb | 12 ++++++++++++ lib/factory_girl/evaluator.rb | 5 +---- lib/factory_girl/factory.rb | 2 +- spec/factory_girl/evaluator_class_definer_spec.rb | 2 +- 6 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 lib/factory_girl/decorator/new_constructor.rb diff --git a/lib/factory_girl.rb b/lib/factory_girl.rb index 6966c7b..692151c 100644 --- a/lib/factory_girl.rb +++ b/lib/factory_girl.rb @@ -42,6 +42,7 @@ require 'factory_girl/decorator/class_key_hash' require 'factory_girl/decorator/disallows_duplicates_registry' require 'factory_girl/decorator/invocation_tracker' require 'factory_girl/decorator/invocation_ignorer' +require 'factory_girl/decorator/new_constructor' require 'factory_girl/version' module FactoryGirl diff --git a/lib/factory_girl/attribute_assigner.rb b/lib/factory_girl/attribute_assigner.rb index 33b553e..ed20c41 100644 --- a/lib/factory_girl/attribute_assigner.rb +++ b/lib/factory_girl/attribute_assigner.rb @@ -31,7 +31,13 @@ module FactoryGirl private def method_tracking_evaluator - @method_tracking_evaluator ||= Decorator::AttributeHash.new(invocation_decorator.new(@evaluator), attribute_names_to_assign) + @method_tracking_evaluator ||= Decorator::AttributeHash.new(decorated_evaluator, attribute_names_to_assign) + end + + def decorated_evaluator + invocation_decorator.new( + Decorator::NewConstructor.new(@evaluator, @build_class) + ) end def invocation_decorator diff --git a/lib/factory_girl/decorator/new_constructor.rb b/lib/factory_girl/decorator/new_constructor.rb new file mode 100644 index 0000000..0d2845e --- /dev/null +++ b/lib/factory_girl/decorator/new_constructor.rb @@ -0,0 +1,12 @@ +module FactoryGirl + class Decorator + class NewConstructor < Decorator + def initialize(component, build_class) + super(component) + @build_class = build_class + end + + delegate :new, to: :@build_class + end + end +end diff --git a/lib/factory_girl/evaluator.rb b/lib/factory_girl/evaluator.rb index ba97822..de7b105 100644 --- a/lib/factory_girl/evaluator.rb +++ b/lib/factory_girl/evaluator.rb @@ -10,8 +10,7 @@ module FactoryGirl undef_method(method) unless method =~ /^__|initialize/ end - def initialize(build_class, build_strategy, overrides = {}) - @build_class = build_class + def initialize(build_strategy, overrides = {}) @build_strategy = build_strategy @overrides = overrides @cached_attributes = overrides @@ -21,8 +20,6 @@ module FactoryGirl end end - delegate :new, to: :@build_class - def association(factory_name, *traits_and_overrides) overrides = traits_and_overrides.extract_options! strategy_override = overrides.fetch(:strategy) { :create } diff --git a/lib/factory_girl/factory.rb b/lib/factory_girl/factory.rb index 364d475..133a272 100644 --- a/lib/factory_girl/factory.rb +++ b/lib/factory_girl/factory.rb @@ -33,7 +33,7 @@ module FactoryGirl strategy = StrategyCalculator.new(build_strategy).strategy.new - evaluator = evaluator_class.new(build_class, strategy, overrides.symbolize_keys) + evaluator = evaluator_class.new(strategy, overrides.symbolize_keys) attribute_assigner = AttributeAssigner.new(evaluator, build_class, &compiled_constructor) evaluation = Evaluation.new(attribute_assigner, compiled_to_create) diff --git a/spec/factory_girl/evaluator_class_definer_spec.rb b/spec/factory_girl/evaluator_class_definer_spec.rb index 2683a05..3e65f34 100644 --- a/spec/factory_girl/evaluator_class_definer_spec.rb +++ b/spec/factory_girl/evaluator_class_definer_spec.rb @@ -7,7 +7,7 @@ describe FactoryGirl::EvaluatorClassDefiner do let(:attributes) { [simple_attribute, relative_attribute, attribute_that_raises_a_second_time] } let(:class_definer) { FactoryGirl::EvaluatorClassDefiner.new(attributes, FactoryGirl::Evaluator) } - let(:evaluator) { class_definer.evaluator_class.new(Object, stub("build strategy", add_observer: true)) } + let(:evaluator) { class_definer.evaluator_class.new(stub("build strategy", add_observer: true)) } it "returns an evaluator when accessing the evaluator class" do evaluator.should be_a(FactoryGirl::Evaluator)