factory_bot 5.1.1 → 5.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1d19c695ef0088d163b82c6b32066d4385d4be7fb8c0dc0b29c350d2e2187f2d
4
- data.tar.gz: 55270eb96b553aa9b0c186bc7eeaf9d084fc79252b7fb8443d6276b1865168cc
3
+ metadata.gz: 802da48aa95dce3321f20d0173c5dcf041c5393fb0c270fb8ae2029ce12b6ed9
4
+ data.tar.gz: 0aa227896ef66ccf100f88b1aa4e9aa26c5bc657fee76739a73e15072bc80572
5
5
  SHA512:
6
- metadata.gz: d61c40f944ae69e27a32e0992d8c289e9d0d9d5a48efe2998b8c53eca17be8a3591f886762866155032662e0eb32a4d52f342cfc1c88b22e637cab3d6e999f81
7
- data.tar.gz: 2aa21a35633016a9e9871c9e56782af137a209ad1c155d51d768ba129d5dea016c54f9d1186191d2ed24e616f5d6516b78698488b8ee536fd929e28c7055ff1f
6
+ metadata.gz: 63554d6765398f0c524cb8cd45b1a0da83da1d8aba9f359d0e4c44e42cdbd0d93edb8e38c668e0310ad709905c5cd6a3c35e640ad8c45a06a8b7d4090b4e6161
7
+ data.tar.gz: fe3be69bdd601384da0a7f2ec7e1fcf00ae7d4984c65423e31e94c1b7f3d7bb04345371daaea19d794365972b59f69f9f3403b0ef8b8ef7d7349d89404284ac2
@@ -1018,6 +1018,14 @@ To set the attributes for each of the factories, you can pass in a hash as you n
1018
1018
  twenty_year_olds = build_list(:user, 25, date_of_birth: 20.years.ago)
1019
1019
  ```
1020
1020
 
1021
+ In order to set different attributes for each factory, these methods may be passed a block, with the factory and the index as parameters:
1022
+
1023
+ ```ruby
1024
+ twenty_somethings = build_list(:user, 10) do |user, i|
1025
+ user.date_of_birth = (20 + i).years.ago
1026
+ end
1027
+ ```
1028
+
1021
1029
  `build_stubbed_list` will give you fully stubbed out instances:
1022
1030
 
1023
1031
  ```ruby
@@ -1066,7 +1074,7 @@ Example Rake task:
1066
1074
  namespace :factory_bot do
1067
1075
  desc "Verify that all FactoryBot factories are valid"
1068
1076
  task lint: :environment do
1069
- if Rails.env.test?
1077
+ if Rails.env.test?
1070
1078
  conn = ActiveRecord::Base.connection
1071
1079
  conn.transaction do
1072
1080
  FactoryBot.lint
@@ -1413,12 +1421,12 @@ with associations, as below:
1413
1421
 
1414
1422
  ```ruby
1415
1423
  FactoryBot.define do
1416
- factory :united_states, class: Location do
1424
+ factory :united_states, class: "Location" do
1417
1425
  name { 'United States' }
1418
1426
  association :location_group, factory: :north_america
1419
1427
  end
1420
1428
 
1421
- factory :north_america, class: LocationGroup do
1429
+ factory :north_america, class: "LocationGroup" do
1422
1430
  name { 'North America' }
1423
1431
  end
1424
1432
  end
data/NEWS.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # News
2
2
 
3
+ ## 5.2.0 (April 24, 2020)
4
+ * Added: Pass index to block for `*_list` methods
5
+ * Deprecated: top-level methods meant only for internal use: `callbacks`, `configuration`, `constructor`, `initialize_with`, `register_sequence`, `resent_configuration`, `skip_create`, `to_create`
6
+
7
+ ## 5.1.2 (March 25, 2020)
8
+ * Fixed: Ruby 2.7 keyword deprecation warning in FactoryBot.lint
9
+
3
10
  ## 5.1.1 (October 2, 2019)
4
11
  * Improved: performance of traits
5
12
  * Fixed: registering strategies on JRuby
data/README.md CHANGED
@@ -72,14 +72,14 @@ Contributing
72
72
 
73
73
  Please see [CONTRIBUTING.md](https://github.com/thoughtbot/factory_bot/blob/master/CONTRIBUTING.md).
74
74
 
75
- factory_bot was originally written by Joe Ferris and is now maintained by Josh
76
- Clayton. Many improvements and bugfixes were contributed by the [open source
75
+ factory_bot was originally written by Joe Ferris and is maintained by thoughtbot.
76
+ Many improvements and bugfixes were contributed by the [open source
77
77
  community](https://github.com/thoughtbot/factory_bot/graphs/contributors).
78
78
 
79
79
  License
80
80
  -------
81
81
 
82
- factory_bot is Copyright © 2008-2019 Joe Ferris and thoughtbot. It is free
82
+ factory_bot is Copyright © 2008-2020 Joe Ferris and thoughtbot. It is free
83
83
  software, and may be redistributed under the terms specified in the
84
84
  [LICENSE] file.
85
85
 
@@ -89,7 +89,7 @@ software, and may be redistributed under the terms specified in the
89
89
  About thoughtbot
90
90
  ----------------
91
91
 
92
- ![thoughtbot](https://presskit.thoughtbot.com/images/thoughtbot-logo-for-readmes.svg)
92
+ ![thoughtbot](https://thoughtbot.com/brand_assets/93:44.svg)
93
93
 
94
94
  factory_bot is maintained and funded by thoughtbot, inc.
95
95
  The names and logos for thoughtbot are trademarks of thoughtbot, inc.
@@ -4,6 +4,7 @@ require "active_support/core_ext/module/attribute_accessors"
4
4
  require "active_support/deprecation"
5
5
  require "active_support/notifications"
6
6
 
7
+ require "factory_bot/internal"
7
8
  require "factory_bot/definition_hierarchy"
8
9
  require "factory_bot/configuration"
9
10
  require "factory_bot/errors"
@@ -45,19 +46,10 @@ require "factory_bot/decorator/invocation_tracker"
45
46
  require "factory_bot/decorator/new_constructor"
46
47
  require "factory_bot/linter"
47
48
  require "factory_bot/version"
48
- require "factory_bot/internal"
49
49
 
50
50
  module FactoryBot
51
51
  Deprecation = ActiveSupport::Deprecation.new("6.0", "factory_bot")
52
52
 
53
- def self.configuration
54
- Internal.configuration
55
- end
56
-
57
- def self.reset_configuration
58
- Internal.reset_configuration
59
- end
60
-
61
53
  mattr_accessor :use_parent_strategy, instance_accessor: false
62
54
  self.use_parent_strategy = true
63
55
 
@@ -71,23 +63,17 @@ module FactoryBot
71
63
  def self.lint(*args)
72
64
  options = args.extract_options!
73
65
  factories_to_lint = args[0] || FactoryBot.factories
74
- Linter.new(factories_to_lint, options).lint!
66
+ Linter.new(factories_to_lint, **options).lint!
75
67
  end
76
68
 
77
69
  class << self
78
- delegate :callbacks,
79
- :callback_names,
70
+ delegate :callback_names,
71
+ :callbacks,
72
+ :configuration,
80
73
  :constructor,
81
74
  :factories,
75
+ :factory_by_name,
82
76
  :initialize_with,
83
- :sequences,
84
- :skip_create,
85
- :strategies,
86
- :to_create,
87
- :traits,
88
- to: :configuration
89
-
90
- delegate :factory_by_name,
91
77
  :register_callback,
92
78
  :register_default_callbacks,
93
79
  :register_default_strategies,
@@ -95,10 +81,16 @@ module FactoryBot
95
81
  :register_sequence,
96
82
  :register_strategy,
97
83
  :register_trait,
84
+ :reset_configuration,
98
85
  :rewind_sequences,
99
86
  :sequence_by_name,
87
+ :sequences,
88
+ :skip_create,
89
+ :strategies,
100
90
  :strategy_by_name,
91
+ :to_create,
101
92
  :trait_by_name,
93
+ :traits,
102
94
  to: Internal
103
95
 
104
96
  attr_accessor :allow_class_lookup
@@ -106,15 +98,23 @@ module FactoryBot
106
98
  deprecate :allow_class_lookup,
107
99
  :allow_class_lookup=,
108
100
  :callback_names,
101
+ :callbacks,
102
+ :configuration,
103
+ :constructor,
109
104
  :factory_by_name,
105
+ :initialize_with,
110
106
  :register_callback,
111
107
  :register_default_callbacks,
112
108
  :register_default_strategies,
113
109
  :register_factory,
110
+ :register_sequence,
114
111
  :register_trait,
112
+ :reset_configuration,
115
113
  :sequence_by_name,
116
114
  :sequences,
115
+ :skip_create,
117
116
  :strategies,
117
+ :to_create,
118
118
  :trait_by_name,
119
119
  :traits,
120
120
  deprecator: Deprecation
@@ -16,6 +16,7 @@ module FactoryBot
16
16
  else instance_exec(&block)
17
17
  end
18
18
  raise SequenceAbuseError if FactoryBot::Sequence === value
19
+
19
20
  value
20
21
  }
21
22
  end
@@ -24,7 +24,7 @@ module FactoryBot
24
24
  end
25
25
 
26
26
  delegate :to_create, :skip_create, :constructor, :before, :after,
27
- :callback, :callbacks, to: :@definition
27
+ :callback, :callbacks, to: :@definition
28
28
 
29
29
  def initialize_with(&block)
30
30
  @definition.define_constructor(&block)
@@ -6,7 +6,7 @@ module FactoryBot
6
6
  @component = component
7
7
  end
8
8
 
9
- def method_missing(name, *args, &block) # rubocop:disable Style/MethodMissing
9
+ def method_missing(name, *args, &block) # rubocop:disable Style/MethodMissingSuper
10
10
  @component.send(name, *args, &block)
11
11
  end
12
12
 
@@ -6,7 +6,7 @@ module FactoryBot
6
6
  @invoked_methods = []
7
7
  end
8
8
 
9
- def method_missing(name, *args, &block) # rubocop:disable Style/MethodMissing
9
+ def method_missing(name, *args, &block) # rubocop:disable Style/MissingRespondToMissing
10
10
  @invoked_methods << name
11
11
  super
12
12
  end
@@ -49,7 +49,7 @@ module FactoryBot
49
49
 
50
50
  defined_traits.each do |defined_trait|
51
51
  base_traits.each { |bt| bt.define_trait defined_trait }
52
- additional_traits.each { |bt| bt.define_trait defined_trait }
52
+ additional_traits.each { |at| at.define_trait defined_trait }
53
53
  end
54
54
 
55
55
  @compiled = true
@@ -1,16 +1,6 @@
1
1
  module FactoryBot
2
2
  class DefinitionHierarchy
3
- def callbacks
4
- FactoryBot.callbacks
5
- end
6
-
7
- def constructor
8
- FactoryBot.constructor
9
- end
10
-
11
- def to_create
12
- FactoryBot.to_create
13
- end
3
+ delegate :callbacks, :constructor, :to_create, to: Internal
14
4
 
15
5
  def self.build_from_definition(definition)
16
6
  build_to_create(&definition.to_create)
@@ -88,7 +88,7 @@ module FactoryBot
88
88
  # end
89
89
  #
90
90
  # are equivalent.
91
- def method_missing(name, *args, &block) # rubocop:disable Style/MethodMissing
91
+ def method_missing(name, *args, &block) # rubocop:disable Style/MissingRespondToMissing, Style/MethodMissingSuper
92
92
  association_options = args.first
93
93
 
94
94
  if association_options.nil?
@@ -37,7 +37,7 @@ module FactoryBot
37
37
  @instance = object_instance
38
38
  end
39
39
 
40
- def method_missing(method_name, *args, &block) # rubocop:disable Style/MethodMissing
40
+ def method_missing(method_name, *args, &block) # rubocop:disable Style/MethodMissingSuper
41
41
  if @instance.respond_to?(method_name)
42
42
  @instance.send(method_name, *args, &block)
43
43
  else
@@ -1,24 +1,19 @@
1
1
  module FactoryBot
2
2
  # @api private
3
3
  module Internal
4
- DEFAULT_STRATEGIES = {
5
- build: FactoryBot::Strategy::Build,
6
- create: FactoryBot::Strategy::Create,
7
- attributes_for: FactoryBot::Strategy::AttributesFor,
8
- build_stubbed: FactoryBot::Strategy::Stub,
9
- null: FactoryBot::Strategy::Null,
10
- }.freeze
11
-
12
- DEFAULT_CALLBACKS = [
13
- :after_create, :after_build, :after_stub, :after_create
14
- ].freeze
15
-
16
4
  class << self
17
- delegate :callback_names,
5
+ delegate :after,
6
+ :before,
7
+ :callback_names,
8
+ :callbacks,
9
+ :constructor,
18
10
  :factories,
11
+ :initialize_with,
19
12
  :inline_sequences,
20
13
  :sequences,
14
+ :skip_create,
21
15
  :strategies,
16
+ :to_create,
22
17
  :traits,
23
18
  to: :configuration
24
19
 
@@ -86,11 +81,18 @@ module FactoryBot
86
81
  end
87
82
 
88
83
  def register_default_strategies
89
- DEFAULT_STRATEGIES.each { |name, klass| register_strategy(name, klass) }
84
+ register_strategy(:build, FactoryBot::Strategy::Build)
85
+ register_strategy(:create, FactoryBot::Strategy::Create)
86
+ register_strategy(:attributes_for, FactoryBot::Strategy::AttributesFor)
87
+ register_strategy(:build_stubbed, FactoryBot::Strategy::Stub)
88
+ register_strategy(:null, FactoryBot::Strategy::Null)
90
89
  end
91
90
 
92
91
  def register_default_callbacks
93
- DEFAULT_CALLBACKS.each(&method(:register_callback))
92
+ register_callback(:after_create)
93
+ register_callback(:after_build)
94
+ register_callback(:after_stub)
95
+ register_callback(:before_create)
94
96
  end
95
97
 
96
98
  def register_callback(name)
@@ -72,8 +72,8 @@ module FactoryBot
72
72
  result = []
73
73
  begin
74
74
  FactoryBot.public_send(factory_strategy, factory.name)
75
- rescue StandardError => error
76
- result |= [FactoryError.new(error, factory)]
75
+ rescue StandardError => e
76
+ result |= [FactoryError.new(e, factory)]
77
77
  end
78
78
  result
79
79
  end
@@ -83,9 +83,9 @@ module FactoryBot
83
83
  factory.definition.defined_traits.map(&:name).each do |trait_name|
84
84
  begin
85
85
  FactoryBot.public_send(factory_strategy, factory.name, trait_name)
86
- rescue StandardError => error
86
+ rescue StandardError => e
87
87
  result |=
88
- [FactoryTraitError.new(error, factory, trait_name)]
88
+ [FactoryTraitError.new(e, factory, trait_name)]
89
89
  end
90
90
  end
91
91
  result
@@ -8,7 +8,7 @@ module FactoryBot
8
8
  end
9
9
 
10
10
  delegate :defined_traits, :callbacks, :attributes, :constructor,
11
- :to_create, to: :definition
11
+ :to_create, to: :definition
12
12
 
13
13
  def compile; end
14
14
 
@@ -21,8 +21,8 @@ module FactoryBot
21
21
 
22
22
  def find(name)
23
23
  @items.fetch(name)
24
- rescue KeyError => key_error
25
- raise key_error_with_custom_message(key_error)
24
+ rescue KeyError => e
25
+ raise key_error_with_custom_message(e)
26
26
  end
27
27
 
28
28
  alias :[] :find
@@ -11,6 +11,14 @@ module FactoryBot
11
11
  define_pair_strategy_method
12
12
  end
13
13
 
14
+ def self.with_index(block, index)
15
+ if block&.arity == 2
16
+ ->(instance) { block.call(instance, index) }
17
+ else
18
+ block
19
+ end
20
+ end
21
+
14
22
  private
15
23
 
16
24
  def define_singular_strategy_method
@@ -29,7 +37,10 @@ module FactoryBot
29
37
  raise ArgumentError, "count missing for #{strategy_name}_list"
30
38
  end
31
39
 
32
- Array.new(amount) { send(strategy_name, name, *traits_and_overrides, &block) }
40
+ Array.new(amount) do |i|
41
+ block_with_index = StrategySyntaxMethodRegistrar.with_index(block, i)
42
+ send(strategy_name, name, *traits_and_overrides, &block_with_index)
43
+ end
33
44
  end
34
45
  end
35
46
 
@@ -33,29 +33,17 @@ module FactoryBot
33
33
  Internal.register_trait(Trait.new(name, &block))
34
34
  end
35
35
 
36
- def to_create(&block)
37
- FactoryBot.to_create(&block)
38
- end
39
-
40
- def skip_create
41
- FactoryBot.skip_create
42
- end
43
-
44
- def initialize_with(&block)
45
- FactoryBot.initialize_with(&block)
46
- end
47
-
48
36
  def self.run(block)
49
37
  new.instance_eval(&block)
50
38
  end
51
39
 
52
- delegate :before, :after, :callback, to: :configuration
53
-
54
- private
55
-
56
- def configuration
57
- Internal.configuration
58
- end
40
+ delegate :after,
41
+ :before,
42
+ :callback,
43
+ :initialize_with,
44
+ :skip_create,
45
+ :to_create,
46
+ to: FactoryBot::Internal
59
47
  end
60
48
 
61
49
  class ModifyDSL
@@ -1,3 +1,3 @@
1
1
  module FactoryBot
2
- VERSION = "5.1.1".freeze
2
+ VERSION = "5.2.0".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: factory_bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.1.1
4
+ version: 5.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Clayton
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-10-02 00:00:00.000000000 Z
12
+ date: 2020-04-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -127,16 +127,44 @@ dependencies:
127
127
  name: rubocop
128
128
  requirement: !ruby/object:Gem::Requirement
129
129
  requirements:
130
- - - '='
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ type: :development
134
+ prerelease: false
135
+ version_requirements: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ - !ruby/object:Gem::Dependency
141
+ name: rubocop-performance
142
+ requirement: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ type: :development
148
+ prerelease: false
149
+ version_requirements: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
131
152
  - !ruby/object:Gem::Version
132
- version: '0.54'
153
+ version: '0'
154
+ - !ruby/object:Gem::Dependency
155
+ name: rubocop-rails
156
+ requirement: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
133
161
  type: :development
134
162
  prerelease: false
135
163
  version_requirements: !ruby/object:Gem::Requirement
136
164
  requirements:
137
- - - '='
165
+ - - ">="
138
166
  - !ruby/object:Gem::Version
139
- version: '0.54'
167
+ version: '0'
140
168
  - !ruby/object:Gem::Dependency
141
169
  name: simplecov
142
170
  requirement: !ruby/object:Gem::Requirement
@@ -235,7 +263,8 @@ files:
235
263
  homepage: https://github.com/thoughtbot/factory_bot
236
264
  licenses:
237
265
  - MIT
238
- metadata: {}
266
+ metadata:
267
+ changelog_uri: https://github.com/thoughtbot/factory_bot/blob/master/NEWS.md
239
268
  post_install_message:
240
269
  rdoc_options: []
241
270
  require_paths: