factory_girl 3.4.0 → 3.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- factory_girl (3.4.0)
4
+ factory_girl (3.4.1)
5
5
  activesupport (>= 3.0.0)
6
6
 
7
7
  GEM
data/NEWS CHANGED
@@ -1,3 +1,6 @@
1
+ 3.4.1 (June 18, 2012)
2
+ Fix traits so they can be nested and referred to from other traits
3
+
1
4
  3.4.0 (June 11, 2012)
2
5
  Sequences support Enumerators
3
6
  Optionally disable duplicate assignment of attributes in initialize_with
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/joshuaclayton/dev/gems/factory_girl
3
3
  specs:
4
- factory_girl (3.4.0)
4
+ factory_girl (3.4.1)
5
5
  activesupport (>= 3.0.0)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/joshuaclayton/dev/gems/factory_girl
3
3
  specs:
4
- factory_girl (3.4.0)
4
+ factory_girl (3.4.1)
5
5
  activesupport (>= 3.0.0)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /Users/joshuaclayton/dev/gems/factory_girl
3
3
  specs:
4
- factory_girl (3.4.0)
4
+ factory_girl (3.4.1)
5
5
  activesupport (>= 3.0.0)
6
6
 
7
7
  GEM
@@ -30,7 +30,6 @@ require 'factory_girl/attribute_list'
30
30
  require 'factory_girl/trait'
31
31
  require 'factory_girl/aliases'
32
32
  require 'factory_girl/definition'
33
- require 'factory_girl/definition_list'
34
33
  require 'factory_girl/definition_proxy'
35
34
  require 'factory_girl/syntax'
36
35
  require 'factory_girl/syntax_runner'
@@ -18,8 +18,8 @@ module FactoryGirl
18
18
  private
19
19
 
20
20
  def build
21
- factory_name = @options.delete(:factory) || name
22
- [Attribute::Association.new(name, factory_name, @options)]
21
+ factory_name = @options[:factory] || name
22
+ [Attribute::Association.new(name, factory_name, @options.except(:factory))]
23
23
  end
24
24
  end
25
25
  end
@@ -20,7 +20,7 @@ module FactoryGirl
20
20
  @overridable = true
21
21
  end
22
22
 
23
- def attribute_list
23
+ def attributes
24
24
  AttributeList.new(@name).tap do |list|
25
25
  to_attributes.each do |attribute|
26
26
  list.define_attribute(attribute)
@@ -1,7 +1,7 @@
1
1
  module FactoryGirl
2
2
  # @api private
3
3
  class Definition
4
- attr_reader :callbacks, :defined_traits, :declarations, :constructor
4
+ attr_reader :defined_traits, :declarations
5
5
 
6
6
  def initialize(name = nil, base_traits = [])
7
7
  @declarations = DeclarationList.new(name)
@@ -11,22 +11,48 @@ module FactoryGirl
11
11
  @base_traits = base_traits
12
12
  @additional_traits = []
13
13
  @constructor = nil
14
+ @attributes = nil
15
+ @compiled = false
14
16
  end
15
17
 
16
18
  delegate :declare_attribute, to: :declarations
17
19
 
18
20
  def attributes
19
- @attributes ||= declarations.attribute_list
21
+ @attributes ||= AttributeList.new.tap do |attribute_list|
22
+ attribute_lists = aggregate_from_traits_and_self(:attributes) { declarations.attributes }
23
+ attribute_lists.each do |attributes|
24
+ attribute_list.apply_attributes attributes
25
+ end
26
+ end
20
27
  end
21
28
 
22
- def compile
23
- attributes
29
+ def to_create(&block)
30
+ if block_given?
31
+ @to_create = block
32
+ else
33
+ aggregate_from_traits_and_self(:to_create) { @to_create }.last
34
+ end
35
+ end
36
+
37
+ def constructor
38
+ aggregate_from_traits_and_self(:constructor) { @constructor }.last
39
+ end
40
+
41
+ def callbacks
42
+ aggregate_from_traits_and_self(:callbacks) { @callbacks }
24
43
  end
25
44
 
26
- def definition_list
27
- DefinitionList.new(
28
- base_traits.map(&:definition) + [self] + additional_traits.map(&:definition)
29
- )
45
+ def compile
46
+ unless @compiled
47
+ declarations.attributes
48
+
49
+ defined_traits.each do |defined_trait|
50
+ base_traits.each {|bt| bt.define_trait defined_trait }
51
+ additional_traits.each {|bt| bt.define_trait defined_trait }
52
+ end
53
+
54
+ @compiled = true
55
+ end
30
56
  end
31
57
 
32
58
  def overridable
@@ -46,22 +72,6 @@ module FactoryGirl
46
72
  @callbacks << callback
47
73
  end
48
74
 
49
- def compiled_to_create
50
- definition_list.to_create
51
- end
52
-
53
- def compiled_constructor
54
- definition_list.constructor
55
- end
56
-
57
- def to_create(&block)
58
- if block_given?
59
- @to_create = block
60
- else
61
- @to_create
62
- end
63
- end
64
-
65
75
  def skip_create
66
76
  @to_create = ->(instance) { }
67
77
  end
@@ -91,5 +101,26 @@ module FactoryGirl
91
101
  def trait_for(name)
92
102
  defined_traits.detect {|trait| trait.name == name }
93
103
  end
104
+
105
+ def initialize_copy(source)
106
+ super
107
+ @attributes = nil
108
+ @compiled = false
109
+ end
110
+
111
+ def aggregate_from_traits_and_self(method_name, &block)
112
+ compile
113
+ [].tap do |list|
114
+ base_traits.each do |trait|
115
+ list << trait.send(method_name)
116
+ end
117
+
118
+ list << instance_exec(&block)
119
+
120
+ additional_traits.each do |trait|
121
+ list << trait.send(method_name)
122
+ end
123
+ end.flatten.compact
124
+ end
94
125
  end
95
126
  end
@@ -17,7 +17,7 @@ module FactoryGirl
17
17
  end
18
18
 
19
19
  delegate :add_callback, :declare_attribute, :to_create, :define_trait, :constructor,
20
- :defined_traits, :inherit_traits, :append_traits, :definition_list, to: :@definition
20
+ :defined_traits, :inherit_traits, :append_traits, to: :@definition
21
21
 
22
22
  def build_class
23
23
  @build_class ||= if class_name.is_a? Class
@@ -107,20 +107,20 @@ module FactoryGirl
107
107
  def attributes
108
108
  compile
109
109
  AttributeList.new(@name).tap do |list|
110
- list.apply_attributes definition_list.attributes
110
+ list.apply_attributes definition.attributes
111
111
  end
112
112
  end
113
113
 
114
114
  def callbacks
115
- parent.callbacks + definition_list.callbacks
115
+ parent.callbacks + definition.callbacks
116
116
  end
117
117
 
118
118
  def compiled_to_create
119
- @definition.compiled_to_create || parent.compiled_to_create || FactoryGirl.to_create
119
+ @definition.to_create || parent.compiled_to_create || FactoryGirl.to_create
120
120
  end
121
121
 
122
122
  def compiled_constructor
123
- @definition.compiled_constructor || parent.compiled_constructor || FactoryGirl.constructor
123
+ @definition.constructor || parent.compiled_constructor || FactoryGirl.constructor
124
124
  end
125
125
 
126
126
  private
@@ -4,12 +4,14 @@ module FactoryGirl
4
4
  attr_reader :definition
5
5
 
6
6
  def initialize
7
- @definition = Definition.new
7
+ @definition = Definition.new(:null_factory)
8
8
  end
9
9
 
10
10
  delegate :defined_traits, :callbacks, :attributes, :constructor,
11
- :compiled_to_create, :compiled_constructor, to: :definition
11
+ :to_create, to: :definition
12
12
 
13
+ def compiled_to_create; to_create; end
14
+ def compiled_constructor; constructor; end
13
15
  def compile; end
14
16
  def class_name; end
15
17
  def evaluator_class; FactoryGirl::Evaluator; end
@@ -1,3 +1,3 @@
1
1
  module FactoryGirl
2
- VERSION = '3.4.0'
2
+ VERSION = '3.4.1'
3
3
  end
@@ -563,3 +563,85 @@ describe "traits with initialize_with" do
563
563
  FactoryGirl.build(:child_user_with_trait_and_override, :overridden).name.should == "completely overridden"
564
564
  end
565
565
  end
566
+
567
+ describe "nested implicit traits" do
568
+ before do
569
+ define_class("User") do
570
+ attr_accessor :gender, :role
571
+ attr_reader :name
572
+
573
+ def initialize(name)
574
+ @name = name
575
+ end
576
+ end
577
+ end
578
+
579
+ shared_examples_for "assigning data from traits" do
580
+ it "assigns the correct values" do
581
+ user = FactoryGirl.create(:user, :female_admin)
582
+ user.gender.should == "FEMALE"
583
+ user.role.should == "ADMIN"
584
+ user.name.should == "Jane Doe"
585
+ end
586
+ end
587
+
588
+ context "defined outside the factory" do
589
+ before do
590
+ FactoryGirl.define do
591
+ trait :female do
592
+ gender "female"
593
+ to_create {|instance| instance.gender = instance.gender.upcase }
594
+ end
595
+
596
+ trait :jane_doe do
597
+ initialize_with { new("Jane Doe") }
598
+ end
599
+
600
+ trait :admin do
601
+ role "admin"
602
+ after(:build) {|instance| instance.role = instance.role.upcase }
603
+ end
604
+
605
+ trait :female_admin do
606
+ female
607
+ admin
608
+ jane_doe
609
+ end
610
+
611
+ factory :user
612
+ end
613
+ end
614
+
615
+ it_should_behave_like "assigning data from traits"
616
+ end
617
+
618
+ context "defined inside the factory" do
619
+ before do
620
+ FactoryGirl.define do
621
+ factory :user do
622
+ trait :female do
623
+ gender "female"
624
+ to_create {|instance| instance.gender = instance.gender.upcase }
625
+ end
626
+
627
+ trait :jane_doe do
628
+ initialize_with { new("Jane Doe") }
629
+ end
630
+
631
+ trait :admin do
632
+ role "admin"
633
+ after(:build) {|instance| instance.role = instance.role.upcase }
634
+ end
635
+
636
+ trait :female_admin do
637
+ female
638
+ admin
639
+ jane_doe
640
+ end
641
+ end
642
+ end
643
+ end
644
+
645
+ it_should_behave_like "assigning data from traits"
646
+ end
647
+ end
@@ -1,6 +1,6 @@
1
1
  require "spec_helper"
2
2
 
3
- describe FactoryGirl::DeclarationList, "#attribute_list" do
3
+ describe FactoryGirl::DeclarationList, "#attributes" do
4
4
  let(:static_attribute_1) { stub("static attribute 1") }
5
5
  let(:static_attribute_2) { stub("static attribute 2") }
6
6
  let(:dynamic_attribute_1) { stub("dynamic attribute 1") }
@@ -8,7 +8,7 @@ describe FactoryGirl::DeclarationList, "#attribute_list" do
8
8
  let(:dynamic_declaration) { stub("static declaration", to_attributes: [dynamic_attribute_1]) }
9
9
 
10
10
  it "returns an AttributeList" do
11
- subject.attribute_list.should be_a(FactoryGirl::AttributeList)
11
+ subject.attributes.should be_a(FactoryGirl::AttributeList)
12
12
  end
13
13
 
14
14
  let(:attribute_list) { stub("attribute list", define_attribute: true) }
@@ -19,7 +19,7 @@ describe FactoryGirl::DeclarationList, "#attribute_list" do
19
19
  subject.declare_attribute(static_declaration)
20
20
  subject.declare_attribute(dynamic_declaration)
21
21
 
22
- subject.attribute_list
22
+ subject.attributes
23
23
 
24
24
  attribute_list.should have_received(:define_attribute).with(static_attribute_1)
25
25
  attribute_list.should have_received(:define_attribute).with(static_attribute_2)
@@ -27,7 +27,7 @@ describe FactoryGirl::DeclarationList, "#attribute_list" do
27
27
  end
28
28
 
29
29
  it "creates a new attribute list upon every invocation" do
30
- subject.attribute_list.should_not == subject.attribute_list
30
+ subject.attributes.should_not == subject.attributes
31
31
  end
32
32
  end
33
33
 
@@ -2,7 +2,6 @@ require "spec_helper"
2
2
 
3
3
  describe FactoryGirl::Definition do
4
4
  it { should delegate(:declare_attribute).to(:declarations) }
5
- it { should delegate(:attributes).to(:declarations).as(:attribute_list) }
6
5
  end
7
6
 
8
7
  describe FactoryGirl::Definition, "with a name" do
@@ -38,8 +37,8 @@ describe FactoryGirl::Definition, "defining traits" do
38
37
  end
39
38
 
40
39
  describe FactoryGirl::Definition, "adding callbacks" do
41
- let(:callback_1) { stub("callback") }
42
- let(:callback_2) { stub("callback") }
40
+ let(:callback_1) { "callback1" }
41
+ let(:callback_2) { "callback2" }
43
42
 
44
43
  it "maintains a list of callbacks" do
45
44
  subject.add_callback(callback_1)
@@ -57,54 +56,3 @@ describe FactoryGirl::Definition, "#to_create" do
57
56
  subject.to_create.should == block
58
57
  end
59
58
  end
60
-
61
- describe FactoryGirl::Definition, "#definition_list" do
62
- let(:female_trait) { FactoryGirl::Trait.new(:female) }
63
- let(:admin_trait) { FactoryGirl::Trait.new(:admin) }
64
- let(:female_definition) { female_trait.definition }
65
- let(:admin_definition) { admin_trait.definition }
66
-
67
- before do
68
- subject.define_trait(female_trait)
69
- FactoryGirl.stubs(trait_by_name: admin_trait)
70
- end
71
-
72
- context "without base traits" do
73
- it "returns the definition without any traits" do
74
- subject.definition_list.should == [subject]
75
- end
76
-
77
- it "finds the correct definitions after appending" do
78
- subject.append_traits([:female])
79
- subject.definition_list.should == [subject, female_definition]
80
- end
81
-
82
- it "finds the correct definitions after inheriting" do
83
- subject.inherit_traits([:female])
84
- subject.definition_list.should == [female_definition, subject]
85
- end
86
-
87
- it "looks for the trait on FactoryGirl" do
88
- subject.append_traits([:female, :admin])
89
- subject.definition_list.should == [subject, female_definition, admin_definition]
90
- end
91
- end
92
-
93
- context "with base traits" do
94
- subject { FactoryGirl::Definition.new("my definition", [:female]) }
95
-
96
- it "returns the base traits and definition" do
97
- subject.definition_list.should == [female_definition, subject]
98
- end
99
-
100
- it "finds the correct definitions after appending" do
101
- subject.append_traits([:admin])
102
- subject.definition_list.should == [female_definition, subject, admin_definition]
103
- end
104
-
105
- it "finds the correct definitions after inheriting" do
106
- subject.inherit_traits([:admin])
107
- subject.definition_list.should == [female_definition, admin_definition, subject]
108
- end
109
- end
110
- end
@@ -257,25 +257,3 @@ describe FactoryGirl::Factory, "running a factory" do
257
257
  block_run.should == "changed"
258
258
  end
259
259
  end
260
-
261
- describe FactoryGirl::Factory, "#with_traits" do
262
- subject { FactoryGirl::Factory.new(:user) }
263
- let(:admin_trait) { FactoryGirl::Trait.new(:admin) }
264
- let(:female_trait) { FactoryGirl::Trait.new(:female) }
265
- let(:admin_definition) { admin_trait.definition }
266
- let(:female_definition) { female_trait.definition }
267
-
268
- before do
269
- FactoryGirl.register_trait(admin_trait)
270
- FactoryGirl.register_trait(female_trait)
271
- end
272
-
273
- it "returns a factory with the correct definitions" do
274
- subject.with_traits([:admin, :female]).definition_list[1, 2].should == [admin_definition, female_definition]
275
- end
276
-
277
- it "doesn't modify the original factory's processing order" do
278
- subject.with_traits([:admin, :female])
279
- subject.definition_list.should == [subject.definition]
280
- end
281
- end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: factory_girl
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.4.0
4
+ version: 3.4.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-06-11 00:00:00.000000000 Z
13
+ date: 2012-06-18 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -268,7 +268,6 @@ files:
268
268
  - lib/factory_girl/decorator/invocation_ignorer.rb
269
269
  - lib/factory_girl/decorator/invocation_tracker.rb
270
270
  - lib/factory_girl/definition.rb
271
- - lib/factory_girl/definition_list.rb
272
271
  - lib/factory_girl/definition_proxy.rb
273
272
  - lib/factory_girl/errors.rb
274
273
  - lib/factory_girl/evaluation.rb
@@ -1,31 +0,0 @@
1
- module FactoryGirl
2
- class DefinitionList
3
- include Enumerable
4
-
5
- def initialize(definitions = [])
6
- @definitions = definitions
7
- end
8
-
9
- def each(&block)
10
- @definitions.each &block
11
- end
12
-
13
- def callbacks
14
- map(&:callbacks).flatten
15
- end
16
-
17
- def attributes
18
- map {|definition| definition.attributes.to_a }.flatten
19
- end
20
-
21
- def to_create
22
- map(&:to_create).compact.last
23
- end
24
-
25
- def constructor
26
- map(&:constructor).compact.last
27
- end
28
-
29
- delegate :[], :==, to: :@definitions
30
- end
31
- end