Permalink
Browse files

Move constructor to definition

  • Loading branch information...
1 parent 8e0d492 commit 8ff5fc274af8c2cecb365603193cb9ab16d87d2e @joshuaclayton joshuaclayton committed May 6, 2012
Showing with 16 additions and 8 deletions.
  1. +9 −1 lib/factory_girl/definition.rb
  2. +7 −7 lib/factory_girl/factory.rb
@@ -10,7 +10,7 @@ def initialize(name = nil, base_traits = [])
@to_create = ->(instance) { instance.save! }
@base_traits = base_traits
@additional_traits = []
- @constructor = nil
+ @constructor = default_constructor
end
delegate :declare_attribute, to: :declarations
@@ -56,8 +56,16 @@ def define_constructor(&block)
@constructor = block
end
+ def custom_constructor?
+ @constructor != default_constructor
+ end
+
private
+ def default_constructor
+ @default_constructor ||= -> { new }
+ end
+
def base_traits
@base_traits.map { |name| trait_by_name(name) }
end
@@ -34,7 +34,7 @@ def run(build_strategy, overrides, &block)
strategy = StrategyCalculator.new(build_strategy).strategy.new
evaluator = evaluator_class.new(build_class, strategy, overrides.symbolize_keys)
- attribute_assigner = AttributeAssigner.new(evaluator, build_class, &instance_builder)
+ attribute_assigner = AttributeAssigner.new(evaluator, build_class, &constructor)
evaluation = Evaluation.new(attribute_assigner, to_create)
evaluation.add_observer(CallbacksObserver.new(callbacks, evaluator))
@@ -118,7 +118,12 @@ def callbacks
end
def constructor
- @constructor ||= @definition.constructor || parent.constructor
+ @constructor ||=
+ if @definition.custom_constructor?
+ @definition.constructor
+ else
+ parent.constructor
+ end
end
private
@@ -135,11 +140,6 @@ def parent
end
end
- def instance_builder
- build_class = self.build_class
- constructor || -> { build_class.new }
- end
-
def initialize_copy(source)
super
@definition = @definition.clone

0 comments on commit 8ff5fc2

Please sign in to comment.