factory_bot 6.3.0 → 6.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: be53175a79d79c70dbaae41a86638d7013b968cecf59aaaa1cedc813ef4e4bc6
4
- data.tar.gz: 4d3312a1ec50b608daa376d41e176ee713fba29ee80c1ce0acd0a646ce4a9956
3
+ metadata.gz: cd4158347cdd07c24704cef28823fee2a36da8ff245ca904dcc7a4f2554f1b35
4
+ data.tar.gz: 3ad522d063147cc2db17aba8a44328cb87a10b6dcf285adcfb4e02bc5ab86f1d
5
5
  SHA512:
6
- metadata.gz: 6077f43c7fc1486a2b1ac6d009f3c95e5c736afbe76c62d1818eb8ad26e77aa9f249b74dd1715b11da3bc989abee47a9a28f4da250e8fe10b036aa4121afc786
7
- data.tar.gz: c4de0a35286906b04e4d4d0e489e437135a1553241f80e5338332b90657e253f055084e5c040e4dc6097b79102a5781c5226a89d89c9108edfb97c1b616fdaa0
6
+ metadata.gz: bba849cae19f841877a2cfa2421e23a75560c9b75f96d1934a3db3128f1345b7349d86925795494c7b6e785bf0a19876d5b6dd6375c09dbed96a3ae2e86187cd
7
+ data.tar.gz: 2edd6b923df8ddb5e4a07c755bda1c9b279776236f587fc333043ee62bb82172dd0bf578c0f7f2297156b4dd057a775d5ef43b277cf6cb8d85ef8dc337065dba
data/GETTING_STARTED.md CHANGED
@@ -285,6 +285,9 @@ user = create(:user)
285
285
  # Returns a hash of attributes that can be used to build a User instance
286
286
  attrs = attributes_for(:user)
287
287
 
288
+ # Integrates with Ruby 3.0's support for pattern matching assignment
289
+ attributes_for(:user) => {email:, name:, **attrs}
290
+
288
291
  # Returns an object with all defined attributes stubbed out
289
292
  stub = build_stubbed(:user)
290
293
 
@@ -297,7 +300,7 @@ end
297
300
  ### Attribute overrides
298
301
 
299
302
  No matter which strategy is used, it's possible to override the defined
300
- attributes by passing a hash:
303
+ attributes by passing a Hash:
301
304
 
302
305
  ```ruby
303
306
  # Build a User instance and override the first_name property
@@ -306,6 +309,29 @@ user.first_name
306
309
  # => "Joe"
307
310
  ```
308
311
 
312
+ Overriding associations is also supported:
313
+
314
+ ```ruby
315
+ account = build(:account, :deluxe)
316
+ friends = build_list(:user, 2)
317
+
318
+ user = build(:user, account: account, friends: friends)
319
+ ```
320
+
321
+ Ruby 3.1's support for [omitting values][] from `Hash` literals dovetails with
322
+ attribute overrides and provides an opportunity to limit the repetition of
323
+ variable names:
324
+
325
+ ```ruby
326
+ account = build(:account, :deluxe)
327
+ friends = build_list(:user, 2)
328
+
329
+ # The keyword arguments correspond to local variable names, so omit their values
330
+ user = build(:user, account:, friends:)
331
+ ```
332
+
333
+ [omitting values]: https://docs.ruby-lang.org/en/3.1/syntax/literals_rdoc.html#label-Hash+Literals
334
+
309
335
  ### `build_stubbed` and `Marshal.dump`
310
336
 
311
337
  Note that objects created with `build_stubbed` cannot be serialized with
@@ -945,7 +971,7 @@ end
945
971
  Note that this approach works with `build`, `build_stubbed`, and `create`, but
946
972
  the associations will return `nil` when using `attributes_for`.
947
973
 
948
- Also, note that if you assign any attributes inside a custom `initialize_with`
974
+ Also, note that if you assign any attributes inside a custom `initialize_with`
949
975
  (e.g. `initialize_with { new(**attributes) }`), those attributes should not refer to `instance`,
950
976
  since it will be `nil`.
951
977
 
@@ -1008,6 +1034,17 @@ factory :user do
1008
1034
  end
1009
1035
  ```
1010
1036
 
1037
+ With Ruby 2.7's support for [numbered parameters][], inline definitions can be
1038
+ even more abbreviated:
1039
+
1040
+ ```ruby
1041
+ factory :user do
1042
+ sequence(:email) { "person#{_1}@example.com" }
1043
+ end
1044
+ ```
1045
+
1046
+ [numbered parameters]: https://ruby-doc.org/core-2.7.1/Proc.html#class-Proc-label-Numbered+parameters
1047
+
1011
1048
  ### Initial value
1012
1049
 
1013
1050
  You can override the initial value. Any value that responds to the `#next`
@@ -1222,11 +1259,11 @@ FactoryBot.define do
1222
1259
  created_at { 8.days.ago }
1223
1260
  updated_at { 4.days.ago }
1224
1261
  end
1225
-
1262
+
1226
1263
  factory :user, traits: [:timestamps] do
1227
1264
  username { "john_doe" }
1228
1265
  end
1229
-
1266
+
1230
1267
  factory :post do
1231
1268
  timestamps
1232
1269
  title { "Traits rock" }
data/NEWS.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # News
2
2
 
3
+ ## 6.4.1 (November 20, 2023)
4
+
5
+ * Fix: factories with traits pass their class to ActiveSupport::Notifications
6
+ (makicamel).
7
+
8
+ ## 6.4.0 (November 17, 2023)
9
+
10
+ * Added: if `build_stubbed` detects a UUID primary key, generate the correct
11
+ type (Peter Boling, Alexandre Ruban).
12
+ * Docs: show examples of Ruby 3 syntactic sugars (Sean Doyle).
13
+ * Internal: resolve test warning messages (Mike Burns).
14
+
15
+
3
16
  ## 6.3.0 (September 1, 2023)
4
17
 
5
18
  * Fix: link to changelog for RubyGems (Berkan Ünal).
@@ -2,6 +2,7 @@ module FactoryBot
2
2
  # @api private
3
3
  class Definition
4
4
  attr_reader :defined_traits, :declarations, :name, :registered_enums
5
+ attr_accessor :klass
5
6
 
6
7
  def initialize(name, base_traits = [])
7
8
  @name = name
@@ -52,6 +53,7 @@ module FactoryBot
52
53
  declarations.attributes
53
54
 
54
55
  defined_traits.each do |defined_trait|
56
+ defined_trait.klass ||= klass
55
57
  base_traits.each { |bt| bt.define_trait defined_trait }
56
58
  additional_traits.each { |at| at.define_trait defined_trait }
57
59
  end
@@ -62,7 +64,7 @@ module FactoryBot
62
64
  name: name,
63
65
  attributes: declarations.attributes,
64
66
  traits: defined_traits,
65
- class: klass
67
+ class: klass || self.klass
66
68
  }
67
69
  end
68
70
  end
@@ -47,13 +47,17 @@ module FactoryBot
47
47
 
48
48
  private
49
49
 
50
- def next_id
51
- @@next_id += 1
50
+ def next_id(result_instance)
51
+ if uuid_primary_key?(result_instance)
52
+ SecureRandom.uuid
53
+ else
54
+ @@next_id += 1
55
+ end
52
56
  end
53
57
 
54
58
  def stub_database_interaction_on_result(result_instance)
55
59
  if has_settable_id?(result_instance)
56
- result_instance.id ||= next_id
60
+ result_instance.id ||= next_id(result_instance)
57
61
  end
58
62
 
59
63
  result_instance.instance_eval do
@@ -83,6 +87,13 @@ module FactoryBot
83
87
  result_instance.class.primary_key
84
88
  end
85
89
 
90
+ def uuid_primary_key?(result_instance)
91
+ result_instance.respond_to?(:column_for_attribute) &&
92
+ (column = result_instance.column_for_attribute(result_instance.class.primary_key)) &&
93
+ column.respond_to?(:sql_type) &&
94
+ column.sql_type == "uuid"
95
+ end
96
+
86
97
  def clear_changes_information(result_instance)
87
98
  if result_instance.respond_to?(:clear_changes_information)
88
99
  result_instance.clear_changes_information
@@ -15,7 +15,7 @@ module FactoryBot
15
15
  end
16
16
 
17
17
  delegate :add_callback, :declare_attribute, :to_create, :define_trait, :constructor,
18
- :callbacks, :attributes, to: :@definition
18
+ :callbacks, :attributes, :klass, :klass=, to: :@definition
19
19
 
20
20
  def names
21
21
  [@name]
@@ -1,3 +1,3 @@
1
1
  module FactoryBot
2
- VERSION = "6.3.0".freeze
2
+ VERSION = "6.4.1".freeze
3
3
  end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: factory_bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.3.0
4
+ version: 6.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Clayton
8
8
  - Joe Ferris
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2023-09-01 00:00:00.000000000 Z
12
+ date: 2023-11-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -238,7 +238,7 @@ licenses:
238
238
  - MIT
239
239
  metadata:
240
240
  changelog_uri: https://github.com/thoughtbot/factory_bot/blob/main/NEWS.md
241
- post_install_message:
241
+ post_install_message:
242
242
  rdoc_options: []
243
243
  require_paths:
244
244
  - lib
@@ -253,8 +253,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
253
253
  - !ruby/object:Gem::Version
254
254
  version: '0'
255
255
  requirements: []
256
- rubygems_version: 3.4.13
257
- signing_key:
256
+ rubygems_version: 3.4.10
257
+ signing_key:
258
258
  specification_version: 4
259
259
  summary: factory_bot provides a framework and DSL for defining and using model instance
260
260
  factories.