factory_bot 5.1.2 → 5.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: af563cbc6de86098293f6454faa0bb33751a61c0848597b67839f122620cd809
4
- data.tar.gz: d68bd5a9eeb57667036e61159a6b289ed89d28a6d9c46e88aadd688fffa4fadd
3
+ metadata.gz: 802da48aa95dce3321f20d0173c5dcf041c5393fb0c270fb8ae2029ce12b6ed9
4
+ data.tar.gz: 0aa227896ef66ccf100f88b1aa4e9aa26c5bc657fee76739a73e15072bc80572
5
5
  SHA512:
6
- metadata.gz: acfcdaabe87c3c551093a11449b5a6f5e7a9ccb340ced5fb9d8fb2fb3fb1e207d3a7232abc3a9cde37b0038e894b8d3fe493b9e0a867f7e77b9de2ca60c35532
7
- data.tar.gz: 902f2234e9ead09c891399d098c663624bce10f3d56335262e0ee38c17ff60096d061db800ea82c928005e5085a043b51f8f83799d6cd0d4587006237fad9dc6
6
+ metadata.gz: 63554d6765398f0c524cb8cd45b1a0da83da1d8aba9f359d0e4c44e42cdbd0d93edb8e38c668e0310ad709905c5cd6a3c35e640ad8c45a06a8b7d4090b4e6161
7
+ data.tar.gz: fe3be69bdd601384da0a7f2ec7e1fcf00ae7d4984c65423e31e94c1b7f3d7bb04345371daaea19d794365972b59f69f9f3403b0ef8b8ef7d7349d89404284ac2
@@ -119,7 +119,7 @@ It is also possible to explicitly specify the class:
119
119
 
120
120
  ```ruby
121
121
  # This will use the User class (otherwise Admin would have been guessed)
122
- factory :admin, class: "User"
122
+ factory :admin, class: User
123
123
  ```
124
124
 
125
125
  If the constant is not available
@@ -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
data/NEWS.md CHANGED
@@ -1,5 +1,9 @@
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
+
3
7
  ## 5.1.2 (March 25, 2020)
4
8
  * Fixed: Ruby 2.7 keyword deprecation warning in FactoryBot.lint
5
9
 
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
 
@@ -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
 
@@ -75,19 +67,13 @@ module FactoryBot
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
@@ -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/MissingRespondToMissing, Style/MethodMissingSuper, Metrics/LineLength
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?
@@ -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)
@@ -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.2".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.2
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: 2020-03-25 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
@@ -263,7 +263,8 @@ files:
263
263
  homepage: https://github.com/thoughtbot/factory_bot
264
264
  licenses:
265
265
  - MIT
266
- metadata: {}
266
+ metadata:
267
+ changelog_uri: https://github.com/thoughtbot/factory_bot/blob/master/NEWS.md
267
268
  post_install_message:
268
269
  rdoc_options: []
269
270
  require_paths:
@@ -279,7 +280,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
279
280
  - !ruby/object:Gem::Version
280
281
  version: '0'
281
282
  requirements: []
282
- rubygems_version: 3.1.2
283
+ rubygems_version: 3.0.3
283
284
  signing_key:
284
285
  specification_version: 4
285
286
  summary: factory_bot provides a framework and DSL for defining and using model instance