factory_girl 3.6.2 → 4.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,9 +0,0 @@
1
- module FactoryGirl
2
- class Decorator
3
- class InvocationIgnorer < Decorator
4
- def __invoked_methods__
5
- []
6
- end
7
- end
8
- end
9
- end
@@ -1,151 +0,0 @@
1
- require 'active_support/deprecation'
2
-
3
- # @api private
4
- module FactoryGirlStepHelpers
5
- def convert_human_hash_to_attribute_hash(human_hash, associations = [])
6
- HumanHashToAttributeHash.new(human_hash, associations).attributes
7
- end
8
-
9
- class HumanHashToAttributeHash
10
- attr_reader :associations
11
-
12
- def initialize(human_hash, associations)
13
- @human_hash = human_hash
14
- @associations = associations
15
- end
16
-
17
- def attributes(strategy = CreateAttributes)
18
- @human_hash.inject({}) do |attribute_hash, (human_key, value)|
19
- attributes = strategy.new(self, *process_key_value(human_key, value))
20
- attribute_hash.merge({ attributes.key => attributes.value })
21
- end
22
- end
23
-
24
- private
25
-
26
- def process_key_value(key, value)
27
- value = value.strip if value.is_a?(String)
28
- [key.downcase.gsub(' ', '_').to_sym, value]
29
- end
30
-
31
- class AssociationManager
32
- def initialize(human_hash_to_attributes_hash, key, value)
33
- @human_hash_to_attributes_hash = human_hash_to_attributes_hash
34
- @key = key
35
- @value = value
36
- end
37
-
38
- def association
39
- @human_hash_to_attributes_hash.associations.detect {|association| association.name == @key }
40
- end
41
-
42
- def association_instance
43
- return unless association
44
-
45
- if attributes_hash = nested_attribute_hash
46
- factory.build_class.where(attributes_hash.attributes(FindAttributes)).first or
47
- FactoryGirl.create(association.factory, attributes_hash.attributes)
48
- end
49
- end
50
-
51
- private
52
-
53
- def factory
54
- FactoryGirl.factory_by_name(association.factory)
55
- end
56
-
57
- def nested_attribute_hash
58
- attribute, value = @value.split(':', 2)
59
- return if value.blank?
60
-
61
- HumanHashToAttributeHash.new({ attribute => value }, factory.associations)
62
- end
63
- end
64
-
65
- class AttributeStrategy
66
- attr_reader :key, :value, :association_manager
67
-
68
- def initialize(human_hash_to_attributes_hash, key, value)
69
- @association_manager = AssociationManager.new(human_hash_to_attributes_hash, key, value)
70
- @key = key
71
- @value = value
72
- end
73
- end
74
-
75
- class FindAttributes < AttributeStrategy
76
- def initialize(human_hash_to_attributes_hash, key, value)
77
- super
78
-
79
- if association_manager.association
80
- @key = "#{@key}_id"
81
- @value = association_manager.association_instance.try(:id)
82
- end
83
- end
84
- end
85
-
86
- class CreateAttributes < AttributeStrategy
87
- def initialize(human_hash_to_attributes_hash, key, value)
88
- super
89
-
90
- if association_manager.association
91
- @value = association_manager.association_instance
92
- end
93
- end
94
- end
95
- end
96
- end
97
-
98
- World(FactoryGirlStepHelpers)
99
-
100
- FactoryGirl.factories.each do |factory|
101
- factory.compile
102
- factory.human_names.each do |human_name|
103
- attribute_names_for_model = if factory.build_class.respond_to?(:attribute_names)
104
- factory.build_class.attribute_names
105
- elsif factory.build_class.respond_to?(:columns)
106
- factory.build_class.columns.map do |column|
107
- column.respond_to?(:name) ? column.name : column.to_s
108
- end
109
- else
110
- []
111
- end
112
-
113
- Given /^the following (?:#{human_name}|#{human_name.pluralize}) exists?:?$/i do |table|
114
- ActiveSupport::Deprecation.warn %{The step 'Given the following #{human_name} exists:' is deprecated and will be removed in 4.0}
115
-
116
- table.hashes.each do |human_hash|
117
- attributes = convert_human_hash_to_attribute_hash(human_hash, factory.associations)
118
- FactoryGirl.create(factory.name, attributes)
119
- end
120
- end
121
-
122
- Given /^an? #{human_name} exists$/i do
123
- ActiveSupport::Deprecation.warn %{The step 'Given a #{human_name} exists' is deprecated and will be removed in 4.0}
124
-
125
- FactoryGirl.create(factory.name)
126
- end
127
-
128
- Given /^(\d+) #{human_name.pluralize} exist$/i do |count|
129
- ActiveSupport::Deprecation.warn %{The step 'Given #{count} #{human_name.pluralize} exist' is deprecated and will be removed in 4.0}
130
-
131
- FactoryGirl.create_list(factory.name, count.to_i)
132
- end
133
-
134
- attribute_names_for_model.each do |attribute_name|
135
- human_column_name = attribute_name.downcase.gsub('_', ' ')
136
-
137
- Given /^an? #{human_name} exists with an? #{human_column_name} of "([^"]*)"$/i do |value|
138
- ActiveSupport::Deprecation.warn %{The step 'Given a #{human_name} exists with a #{human_column_name} of "#{value}"' is deprecated and will be removed in 4.0}
139
-
140
- FactoryGirl.create(factory.name, attribute_name => value)
141
- end
142
-
143
- Given /^(\d+) #{human_name.pluralize} exist with an? #{human_column_name} of "([^"]*)"$/i do |count, value|
144
- ActiveSupport::Deprecation.warn %{The step 'Given #{count} #{human_name.pluralize} exists with a #{human_column_name} of "#{value}"' is deprecated and will be removed in 4.0}
145
-
146
- FactoryGirl.create_list(factory.name, count.to_i, attribute_name => value)
147
- end
148
- end
149
- end
150
- end
151
-
@@ -1,40 +0,0 @@
1
- module FactoryGirl
2
- module Syntax
3
-
4
- # Extends ActiveRecord::Base to provide a make class method, which is an
5
- # alternate syntax for defining factories.
6
- #
7
- # Usage:
8
- #
9
- # require 'factory_girl/syntax/blueprint'
10
- #
11
- # User.blueprint do
12
- # name { 'Billy Bob' }
13
- # email { 'billy@bob.example.com' }
14
- # end
15
- #
16
- # FactoryGirl.create(:user, name: 'Johnny')
17
- #
18
- # This syntax was derived from Pete Yandell's machinist.
19
- # @api private
20
- module Blueprint
21
- module ActiveRecord
22
- def self.included(base)
23
- base.extend ClassMethods
24
- end
25
-
26
- module ClassMethods
27
- def blueprint(&block)
28
- ActiveSupport::Deprecation.warn 'Model.blueprint is deprecated; use the FactoryGirl.define syntax instead', caller
29
- instance = Factory.new(name.underscore, class: self)
30
- proxy = FactoryGirl::DefinitionProxy.new(instance)
31
- proxy.instance_eval(&block)
32
- FactoryGirl.register_factory(instance)
33
- end
34
- end
35
- end
36
- end
37
- end
38
- end
39
-
40
- ActiveRecord::Base.send(:include, FactoryGirl::Syntax::Blueprint::ActiveRecord)
@@ -1,70 +0,0 @@
1
- module FactoryGirl
2
- module Syntax
3
-
4
- # Extends ActiveRecord::Base to provide generation methods for factories.
5
- #
6
- # Usage:
7
- #
8
- # require 'factory_girl/syntax/generate'
9
- #
10
- # FactoryGirl.define do
11
- # factory :user do
12
- # name 'Billy Bob'
13
- # email 'billy@bob.example.com'
14
- # end
15
- # end
16
- #
17
- # # Creates a saved instance without raising (same as saving the result
18
- # # of FactoryGirl.build)
19
- # User.generate(name: 'Johnny')
20
- #
21
- # # Creates a saved instance and raises when invalid (same as
22
- # # FactoryGirl.create)
23
- # User.generate!
24
- #
25
- # # Creates an unsaved instance (same as FactoryGirl.build)
26
- # User.spawn
27
- #
28
- # # Creates an instance and yields it to the passed block
29
- # User.generate do |user|
30
- # # ...do something with user...
31
- # end
32
- #
33
- # This syntax was derived from Rick Bradley and Yossef Mendelssohn's
34
- # object_daddy.
35
- # @api private
36
- module Generate
37
- module ActiveRecord
38
- def self.included(base)
39
- base.extend ClassMethods
40
- end
41
-
42
- module ClassMethods
43
- def generate(overrides = {}, &block)
44
- ActiveSupport::Deprecation.warn 'Model.generate is deprecated; use FactoryGirl.build(:name) instead.', caller
45
- instance = FactoryRunner.new(name.underscore, :build, [overrides]).run
46
- instance.save
47
- yield(instance) if block_given?
48
- instance
49
- end
50
-
51
- def generate!(overrides = {}, &block)
52
- ActiveSupport::Deprecation.warn 'Model.generate! is deprecated; use FactoryGirl.create(:name) instead.', caller
53
- instance = FactoryRunner.new(name.underscore, :create, [overrides]).run
54
- yield(instance) if block_given?
55
- instance
56
- end
57
-
58
- def spawn(overrides = {}, &block)
59
- ActiveSupport::Deprecation.warn 'Model.spawn is deprecated; use FactoryGirl.build(:name) instead.', caller
60
- instance = FactoryRunner.new(name.underscore, :build, [overrides]).run
61
- yield(instance) if block_given?
62
- instance
63
- end
64
- end
65
- end
66
- end
67
- end
68
- end
69
-
70
- ActiveRecord::Base.send(:include, FactoryGirl::Syntax::Generate::ActiveRecord)
@@ -1,44 +0,0 @@
1
- module FactoryGirl
2
- module Syntax
3
-
4
- # Extends ActiveRecord::Base to provide a make class method, which is a
5
- # shortcut for FactoryGirl.create.
6
- #
7
- # Usage:
8
- #
9
- # require 'factory_girl/syntax/make'
10
- #
11
- # FactoryGirl.define do
12
- # factory :user do
13
- # name 'Billy Bob'
14
- # email 'billy@bob.example.com'
15
- # end
16
- # end
17
- #
18
- # User.make(name: 'Johnny')
19
- #
20
- # This syntax was derived from Pete Yandell's machinist.
21
- # @api private
22
- module Make
23
- module ActiveRecord
24
- def self.included(base)
25
- base.extend ClassMethods
26
- end
27
-
28
- module ClassMethods
29
- def make(overrides = {})
30
- ActiveSupport::Deprecation.warn 'Model.make is deprecated; use FactoryGirl.build(:model) instead.', caller
31
- FactoryRunner.new(name.underscore, :build, [overrides]).run
32
- end
33
-
34
- def make!(overrides = {})
35
- ActiveSupport::Deprecation.warn 'Model.make! is deprecated; use FactoryGirl.create(:model) instead.', caller
36
- FactoryRunner.new(name.underscore, :create, [overrides]).run
37
- end
38
- end
39
- end
40
- end
41
- end
42
- end
43
-
44
- ActiveRecord::Base.send(:include, FactoryGirl::Syntax::Make::ActiveRecord)
@@ -1,47 +0,0 @@
1
- module FactoryGirl
2
- module Syntax
3
-
4
- # Adds a Sham module, which provides an alternate interface to
5
- # FactoryGirl::Sequence.
6
- #
7
- # Usage:
8
- #
9
- # require 'factory_girl/syntax/sham'
10
- #
11
- # Sham.email {|n| "somebody#{n}@example.com" }
12
- #
13
- # FactoryGirl.define do
14
- # factory :user do
15
- # email
16
- # end
17
- # end
18
- #
19
- # Note that you can also use Faker, but it is recommended that you simply
20
- # use a sequence as in the above example, as factory_girl does not provide
21
- # protection against duplication in randomized sequences, and a randomized
22
- # value does not provide any tangible benefits over an ascending sequence.
23
- #
24
- # This syntax was derived from Pete Yandell's machinist.
25
- # @api private
26
- module Sham
27
- module Sham
28
- def self.method_missing(name, *args, &block)
29
- if block_given?
30
- ActiveSupport::Deprecation.warn 'Sham.sequence is deprecated; use the FactoryGirl.define syntax instead', caller
31
- start_value = args.first
32
- FactoryGirl.register_sequence(Sequence.new(name, start_value || 1, &block))
33
- else
34
- FactoryGirl.sequence_by_name(name).next
35
- end
36
- end
37
-
38
- # overrides name on Module
39
- def self.name(&block)
40
- method_missing('name', &block)
41
- end
42
- end
43
- end
44
- end
45
- end
46
-
47
- include FactoryGirl::Syntax::Sham
@@ -1,130 +0,0 @@
1
- module FactoryGirl
2
- module Syntax
3
- module Vintage
4
- module ::Factory
5
- # Defines a new factory that can be used by the build strategies (create and
6
- # build) to build new objects.
7
- #
8
- # Arguments:
9
- # * name: +Symbol+ or +String+
10
- # A unique name used to identify this factory.
11
- # * options: +Hash+
12
- #
13
- # Options:
14
- # * class: +Symbol+, +Class+, or +String+
15
- # The class that will be used when generating instances for this factory. If not specified, the class will be guessed from the factory name.
16
- # * parent: +Symbol+
17
- # The parent factory. If specified, the attributes from the parent
18
- # factory will be copied to the current one with an ability to override
19
- # them.
20
- #
21
- # Yields: +Factory+
22
- # The newly created factory.
23
- def self.define(name, options = {})
24
- ActiveSupport::Deprecation.warn 'Factory.define is deprecated; use the FactoryGirl.define block syntax to declare your factory.', caller
25
- factory = FactoryGirl::Factory.new(name, options)
26
- proxy = FactoryGirl::DefinitionProxy.new(factory)
27
- yield(proxy)
28
- FactoryGirl.register_factory(factory)
29
- end
30
-
31
- # Defines a new sequence that can be used to generate unique values in a specific format.
32
- #
33
- # Arguments:
34
- # name: (Symbol)
35
- # A unique name for this sequence. This name will be referenced when
36
- # calling next to generate new values from this sequence.
37
- # block: (Proc)
38
- # The code to generate each value in the sequence. This block will be
39
- # called with a unique number each time a value in the sequence is to be
40
- # generated. The block should return the generated value for the
41
- # sequence.
42
- #
43
- # Example:
44
- #
45
- # Factory.sequence(:email) {|n| "somebody_#{n}@example.com" }
46
- def self.sequence(name, start_value = 1, &block)
47
- ActiveSupport::Deprecation.warn 'Factory.sequence is deprecated; use the FactoryGirl.define block syntax to declare your sequence.', caller
48
- FactoryGirl.register_sequence(Sequence.new(name, start_value, &block))
49
- end
50
-
51
- # Generates and returns the next value in a sequence.
52
- #
53
- # Arguments:
54
- # name: (Symbol)
55
- # The name of the sequence that a value should be generated for.
56
- #
57
- # Returns:
58
- # The next value in the sequence. (Object)
59
- def self.next(name)
60
- ActiveSupport::Deprecation.warn 'Factory.next is deprecated; use FactoryGirl.generate instead.', caller
61
- FactoryGirl.generate(name)
62
- end
63
-
64
- # Defines a new alias for attributes.
65
- #
66
- # Arguments:
67
- # * pattern: +Regexp+
68
- # A pattern that will be matched against attributes when looking for
69
- # aliases. Contents captured in the pattern can be used in the alias.
70
- # * replace: +String+
71
- # The alias that results from the matched pattern. Captured strings can
72
- # be substituted like with +String#sub+.
73
- #
74
- # Example:
75
- #
76
- # Factory.alias /(.*)_confirmation/, '\1'
77
- #
78
- # factory_girl starts with aliases for foreign keys, so that a :user
79
- # association can be overridden by a :user_id parameter:
80
- #
81
- # Factory.define :post do |p|
82
- # p.association :user
83
- # end
84
- #
85
- # # The user association will not be built in this example. The user_id
86
- # # will be used instead.
87
- # Factory(:post, user_id: 1)
88
- def self.alias(pattern, replace)
89
- ActiveSupport::Deprecation.warn 'Factory.alias is deprecated; use FactoryGirl.aliases << [pattern, replace] instead.', caller
90
- FactoryGirl.aliases << [pattern, replace]
91
- end
92
-
93
- # Alias for FactoryGirl.attributes_for
94
- def self.attributes_for(name, overrides = {})
95
- ActiveSupport::Deprecation.warn 'Factory.attributes_for is deprecated; use FactoryGirl.attributes_for instead.', caller
96
- FactoryGirl.attributes_for(name, overrides)
97
- end
98
-
99
- # Alias for FactoryGirl.build
100
- def self.build(name, overrides = {})
101
- ActiveSupport::Deprecation.warn 'Factory.build is deprecated; use FactoryGirl.build instead.', caller
102
- FactoryGirl.build(name, overrides)
103
- end
104
-
105
- # Alias for FactoryGirl.create
106
- def self.create(name, overrides = {})
107
- ActiveSupport::Deprecation.warn 'Factory.create is deprecated; use FactoryGirl.create instead.', caller
108
- FactoryGirl.create(name, overrides)
109
- end
110
-
111
- # Alias for FactoryGirl.build_stubbed.
112
- def self.stub(name, overrides = {})
113
- ActiveSupport::Deprecation.warn 'Factory.stub is deprecated; use FactoryGirl.build_stubbed instead.', caller
114
- FactoryGirl.build_stubbed(name, overrides)
115
- end
116
- end
117
-
118
- # Shortcut for Factory.create.
119
- #
120
- # Example:
121
- # Factory(:user, name: 'Joe')
122
- def Factory(name, attrs = {})
123
- ActiveSupport::Deprecation.warn 'Factory(:name) is deprecated; use FactoryGirl.create(:name) instead.', caller
124
- FactoryGirl.create(name, attrs)
125
- end
126
- end
127
- end
128
- end
129
-
130
- include FactoryGirl::Syntax::Vintage