Skip to content
This repository
Browse code

Replace AssociationRunner with more generic FactoryRunner

  • Loading branch information...
commit 4145a6e7448cbbe2de6e6986520bdf2a1074f6ce 1 parent 09e1cea
Joshua Clayton authored February 17, 2012
3  lib/factory_girl.rb
... ...
@@ -1,7 +1,8 @@
1 1
 require "active_support/core_ext/module/delegation"
2 2
 
3 3
 require 'factory_girl/errors'
4  
-require 'factory_girl/association_runner'
  4
+require 'factory_girl/factory_runner'
  5
+require 'factory_girl/strategy_calculator'
5 6
 require 'factory_girl/strategy'
6 7
 require 'factory_girl/registry'
7 8
 require 'factory_girl/null_factory'
50  lib/factory_girl/association_runner.rb
... ...
@@ -1,50 +0,0 @@
1  
-module FactoryGirl
2  
-  class AssociationRunner
3  
-    def initialize(factory_name, strategy_name_or_object, overrides)
4  
-      @factory_name            = factory_name
5  
-      @strategy_name_or_object = strategy_name_or_object
6  
-      @overrides               = overrides
7  
-    end
8  
-
9  
-    def run(strategy_override = nil)
10  
-      strategy_name_or_object = strategy_override || @strategy_name_or_object
11  
-      strategy = StrategyCalculator.new(strategy_name_or_object).strategy
12  
-      factory.run(strategy, @overrides)
13  
-    end
14  
-
15  
-    private
16  
-
17  
-    def factory
18  
-      FactoryGirl.factory_by_name(@factory_name)
19  
-    end
20  
-
21  
-    class StrategyCalculator
22  
-      def initialize(name_or_object)
23  
-        @name_or_object = name_or_object
24  
-      end
25  
-
26  
-      def strategy
27  
-        if strategy_is_object?
28  
-          @name_or_object
29  
-        else
30  
-          strategy_name_to_object
31  
-        end
32  
-      end
33  
-
34  
-      private
35  
-
36  
-      def strategy_is_object?
37  
-        @name_or_object.is_a?(Class) && @name_or_object.ancestors.include?(::FactoryGirl::Strategy)
38  
-      end
39  
-
40  
-      def strategy_name_to_object
41  
-        case @name_or_object
42  
-        when :build  then Strategy::Build
43  
-        when :create then Strategy::Create
44  
-        when nil     then Strategy::Create
45  
-        else raise "unrecognized method #{@name_or_object}"
46  
-        end
47  
-      end
48  
-    end
49  
-  end
50  
-end
5  lib/factory_girl/evaluator.rb
@@ -36,9 +36,12 @@ def association(factory_name, overrides = {})
36 36
                          overrides[:method]
37 37
                        elsif overrides.has_key?(:strategy)
38 38
                          overrides[:strategy]
  39
+                       else
  40
+                         Strategy::Create
39 41
                        end
40 42
 
41  
-      runner = AssociationRunner.new(factory_name, build_strategy, overrides.except(:method, :strategy))
  43
+      build_strategy = StrategyCalculator.new(build_strategy).strategy
  44
+      runner = FactoryRunner.new(factory_name, build_strategy, [overrides.except(:method, :strategy)])
42 45
       @build_strategy.association(runner)
43 46
     end
44 47
 
26  lib/factory_girl/factory_runner.rb
... ...
@@ -0,0 +1,26 @@
  1
+module FactoryGirl
  2
+  class FactoryRunner
  3
+    def initialize(name, strategy, traits_and_overrides)
  4
+      @name     = name
  5
+      @strategy = strategy
  6
+
  7
+      @overrides = if traits_and_overrides.last.respond_to?(:has_key?)
  8
+                    traits_and_overrides.pop
  9
+                  else
  10
+                    {}
  11
+                  end
  12
+      @traits = traits_and_overrides
  13
+    end
  14
+
  15
+    def run(strategy_override = nil, &block)
  16
+      strategy_override ||= @strategy
  17
+      factory = FactoryGirl.factory_by_name(@name)
  18
+
  19
+      if @traits.any?
  20
+        factory = factory.with_traits(@traits)
  21
+      end
  22
+
  23
+      factory.run(strategy_override, @overrides, &block)
  24
+    end
  25
+  end
  26
+end
29  lib/factory_girl/strategy_calculator.rb
... ...
@@ -0,0 +1,29 @@
  1
+module FactoryGirl
  2
+  class StrategyCalculator
  3
+    def initialize(name_or_object)
  4
+      @name_or_object = name_or_object
  5
+    end
  6
+
  7
+    def strategy
  8
+      if strategy_is_object?
  9
+        @name_or_object
  10
+      else
  11
+        strategy_name_to_object
  12
+      end
  13
+    end
  14
+
  15
+    private
  16
+
  17
+    def strategy_is_object?
  18
+      @name_or_object.is_a?(Class) && @name_or_object.ancestors.include?(::FactoryGirl::Strategy)
  19
+    end
  20
+
  21
+    def strategy_name_to_object
  22
+      case @name_or_object
  23
+      when :build  then Strategy::Build
  24
+      when :create then Strategy::Create
  25
+      else raise "unrecognized method #{@name_or_object}"
  26
+      end
  27
+    end
  28
+  end
  29
+end
9  lib/factory_girl/syntax/generate.rb
@@ -42,23 +42,20 @@ def self.included(base) # :nodoc:
42 42
         module ClassMethods #:nodoc:
43 43
 
44 44
           def generate(overrides = {}, &block)
45  
-            factory = FactoryGirl.factory_by_name(name.underscore)
46  
-            instance = factory.run(Strategy::Build, overrides)
  45
+            instance = FactoryRunner.new(name.underscore, Strategy::Build, [overrides]).run
47 46
             instance.save
48 47
             yield(instance) if block_given?
49 48
             instance
50 49
           end
51 50
 
52 51
           def generate!(overrides = {}, &block)
53  
-            factory = FactoryGirl.factory_by_name(name.underscore)
54  
-            instance = factory.run(Strategy::Create, overrides)
  52
+            instance = FactoryRunner.new(name.underscore, Strategy::Create, [overrides]).run
55 53
             yield(instance) if block_given?
56 54
             instance
57 55
           end
58 56
 
59 57
           def spawn(overrides = {}, &block)
60  
-            factory = FactoryGirl.factory_by_name(name.underscore)
61  
-            instance = factory.run(Strategy::Build, overrides)
  58
+            instance = FactoryRunner.new(name.underscore, Strategy::Build, [overrides]).run
62 59
             yield(instance) if block_given?
63 60
             instance
64 61
           end
4  lib/factory_girl/syntax/make.rb
@@ -28,11 +28,11 @@ def self.included(base) # :nodoc:
28 28
         module ClassMethods #:nodoc:
29 29
 
30 30
           def make(overrides = {})
31  
-            FactoryGirl.factory_by_name(name.underscore).run(Strategy::Build, overrides)
  31
+            FactoryRunner.new(name.underscore, Strategy::Build, [overrides]).run
32 32
           end
33 33
 
34 34
           def make!(overrides = {})
35  
-            FactoryGirl.factory_by_name(name.underscore).run(Strategy::Create, overrides)
  35
+            FactoryRunner.new(name.underscore, Strategy::Create, [overrides]).run
36 36
           end
37 37
 
38 38
         end
26  lib/factory_girl/syntax/methods.rb
@@ -18,7 +18,7 @@ module Methods
18 18
       # A set of attributes that can be used to build an instance of the class
19 19
       # this factory generates.
20 20
       def attributes_for(name, *traits_and_overrides, &block)
21  
-        run_factory_girl_strategy(name, Strategy::AttributesFor, traits_and_overrides, &block)
  21
+        FactoryRunner.new(name, Strategy::AttributesFor, traits_and_overrides).run(&block)
22 22
       end
23 23
 
24 24
       # Generates and returns an instance from this factory. Attributes can be
@@ -37,7 +37,7 @@ def attributes_for(name, *traits_and_overrides, &block)
37 37
       # An instance of the class this factory generates, with generated attributes
38 38
       # assigned.
39 39
       def build(name, *traits_and_overrides, &block)
40  
-        run_factory_girl_strategy(name, Strategy::Build, traits_and_overrides, &block)
  40
+        FactoryRunner.new(name, Strategy::Build, traits_and_overrides).run(&block)
41 41
       end
42 42
 
43 43
       # Generates, saves, and returns an instance from this factory. Attributes can
@@ -60,7 +60,7 @@ def build(name, *traits_and_overrides, &block)
60 60
       # A saved instance of the class this factory generates, with generated
61 61
       # attributes assigned.
62 62
       def create(name, *traits_and_overrides, &block)
63  
-        run_factory_girl_strategy(name, Strategy::Create, traits_and_overrides, &block)
  63
+        FactoryRunner.new(name, Strategy::Create, traits_and_overrides).run(&block)
64 64
       end
65 65
 
66 66
       # Generates and returns an object with all attributes from this factory
@@ -79,7 +79,7 @@ def create(name, *traits_and_overrides, &block)
79 79
       # Returns: +Object+
80 80
       # An object with generated attributes stubbed out.
81 81
       def build_stubbed(name, *traits_and_overrides, &block)
82  
-        run_factory_girl_strategy(name, Strategy::Stub, traits_and_overrides, &block)
  82
+        FactoryRunner.new(name, Strategy::Stub, traits_and_overrides).run(&block)
83 83
       end
84 84
 
85 85
       # Builds and returns multiple instances from this factory as an array. Attributes can be
@@ -131,24 +131,6 @@ def create_list(name, amount, *traits_and_overrides)
131 131
       def generate(name)
132 132
         FactoryGirl.sequence_by_name(name).next
133 133
       end
134  
-
135  
-      private
136  
-
137  
-      def run_factory_girl_strategy(name, strategy, traits_and_overrides, &block)
138  
-        overrides = if traits_and_overrides.last.respond_to?(:has_key?)
139  
-                      traits_and_overrides.pop
140  
-                    else
141  
-                      {}
142  
-                    end
143  
-
144  
-        factory = FactoryGirl.factory_by_name(name)
145  
-
146  
-        if traits_and_overrides.any?
147  
-          factory = factory.with_traits(traits_and_overrides)
148  
-        end
149  
-
150  
-        factory.run(strategy, overrides, &block)
151  
-      end
152 134
     end
153 135
   end
154 136
 end
31  spec/factory_girl/association_runner_spec.rb
... ...
@@ -1,31 +0,0 @@
1  
-require "spec_helper"
2  
-
3  
-describe FactoryGirl::AssociationRunner do
4  
-  let(:factory)  { stub("factory", :run => instance) }
5  
-  let(:instance) { stub("instance") }
6  
-
7  
-  before do
8  
-    FactoryGirl.stubs(:factory_by_name => factory)
9  
-  end
10  
-
11  
-  it "passes all overrides to the factory" do
12  
-    FactoryGirl::AssociationRunner.new(:user, FactoryGirl::Strategy::Build, :strategy => :build, :name => "John").run
13  
-    factory.should have_received(:run).with(FactoryGirl::Strategy::Build, :strategy => :build, :name => "John")
14  
-  end
15  
-
16  
-  it "runs a strategy inferred by name based on a factory name" do
17  
-    FactoryGirl::AssociationRunner.new(:user, :build, :strategy => :build, :name => "John").run
18  
-    factory.should have_received(:run).with(FactoryGirl::Strategy::Build, :strategy => :build, :name => "John")
19  
-  end
20  
-
21  
-  it "allows overriding strategy" do
22  
-    FactoryGirl::AssociationRunner.new(:user, :build, :strategy => :build, :name => "John").run(FactoryGirl::Strategy::Create)
23  
-    factory.should have_received(:run).with(FactoryGirl::Strategy::Create, :strategy => :build, :name => "John")
24  
-  end
25  
-
26  
-  it "raises if the strategy cannot be inferred" do
27  
-    expect do
28  
-      FactoryGirl::AssociationRunner.new(:user, :bogus_strategy, :strategy => :build, :name => "John").run
29  
-    end.to raise_error("unrecognized method bogus_strategy")
30  
-  end
31  
-end
33  spec/factory_girl/strategy_calculator_spec.rb
... ...
@@ -0,0 +1,33 @@
  1
+require "spec_helper"
  2
+
  3
+describe FactoryGirl::StrategyCalculator, "with a FactoryGirl::Strategy object" do
  4
+  let(:strategy) { FactoryGirl::Strategy::Build }
  5
+
  6
+  it "returns the strategy object" do
  7
+    FactoryGirl::StrategyCalculator.new(strategy).strategy.should == strategy
  8
+  end
  9
+end
  10
+
  11
+describe FactoryGirl::StrategyCalculator, "with a non-FactoryGirl::Strategy object" do
  12
+  before { define_class "MyAwesomeStrategy" }
  13
+
  14
+  let(:strategy) { MyAwesomeStrategy }
  15
+
  16
+  it "returns the strategy object" do
  17
+    expect { FactoryGirl::StrategyCalculator.new(strategy).strategy }.to raise_error "unrecognized method MyAwesomeStrategy"
  18
+  end
  19
+end
  20
+
  21
+describe FactoryGirl::StrategyCalculator do
  22
+  it "returns the correct strategy object for :build" do
  23
+    FactoryGirl::StrategyCalculator.new(:build).strategy.should == FactoryGirl::Strategy::Build
  24
+  end
  25
+
  26
+  it "returns the correct strategy object for :create" do
  27
+    FactoryGirl::StrategyCalculator.new(:create).strategy.should == FactoryGirl::Strategy::Create
  28
+  end
  29
+
  30
+  it "raises when passing a bogus strategy" do
  31
+    expect { FactoryGirl::StrategyCalculator.new(:bogus_strategy).strategy }.to raise_error "unrecognized method bogus_strategy"
  32
+  end
  33
+end
8  spec/support/shared_examples/strategy.rb
@@ -2,7 +2,7 @@
2 2
   let(:attribute) { FactoryGirl::Attribute::Association.new(:user, :user, {}) }
3 3
 
4 4
   def association_named(name, overrides)
5  
-    runner = FactoryGirl::AssociationRunner.new(name, :build, overrides)
  5
+    runner = FactoryGirl::FactoryRunner.new(name, FactoryGirl::Strategy::Build, [overrides])
6 6
     subject.association(runner)
7 7
   end
8 8
 
@@ -21,7 +21,7 @@ def association_named(name, overrides)
21 21
   let(:factory) { stub("associate_factory") }
22 22
 
23 23
   def association_named(name, strategy, overrides)
24  
-    runner = FactoryGirl::AssociationRunner.new(name, strategy, overrides)
  24
+    runner = FactoryGirl::FactoryRunner.new(name, strategy, [overrides])
25 25
     subject.association(runner)
26 26
   end
27 27
 
@@ -45,8 +45,8 @@ def association_named(name, strategy, overrides)
45 45
   let(:factory) { stub("associate_factory") }
46 46
 
47 47
   def association_named(name, overrides)
48  
-    strategy = overrides[:strategy] || overrides[:method]
49  
-    runner = FactoryGirl::AssociationRunner.new(name, strategy, overrides.except(:strategy, :method))
  48
+    strategy = FactoryGirl::StrategyCalculator.new(overrides[:strategy] || overrides[:method]).strategy
  49
+    runner = FactoryGirl::FactoryRunner.new(name, strategy, [overrides.except(:strategy, :method)])
50 50
     subject.association(runner)
51 51
   end
52 52
 

0 notes on commit 4145a6e

Please sign in to comment.
Something went wrong with that request. Please try again.