fixturebot-rails 0.3.0 → 0.4.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: 69f408df6cb364c659b025d97fd71b406257c0070ff766b3cf3da918ee0202c8
4
- data.tar.gz: 977217381d7a79a9764ff658976c93cd95c24dac603b07cd52c9e8c0f8c148e7
3
+ metadata.gz: 8c83c05f009409eb651e9e464fcc70bad3b3abeb377943eef42bfff31b29f9ac
4
+ data.tar.gz: 79973f2828cb2dcb83c4f238ad1023681c0a241d0f7ca1daab13ad1627685f64
5
5
  SHA512:
6
- metadata.gz: 7a9f672b0cf194ab3b5523fb3b96a6ba284f06b19cfc29a60b2d11c92109b0c13aa2cc39e2588f26c8f27b2114675ad257fda1085e0588bc5de9a67d3ea3bd93
7
- data.tar.gz: c7bc233c2d13a9790348fcb9944da073b0340f50f21b9c83da4dcc5c02a35d1526b26dbff4a3b39041cbf985d7d12411c30869bf7c793d4376f5cfa7b25cec9a
6
+ metadata.gz: b3c5cfd9eeb975bb9209a2a3e37ca98d7a00225b6c63f638c3f4bdc2a44ff2782eebfcc4616a327d1134d3bcf3dcbbc0a5a661918ab1f4b6fefdffc4ebefed96
7
+ data.tar.gz: 3ea33f5090363d4ee7c8be75720e7fa1a8364c7665304824071d08fd2e5912be7f0d293f9255c7c69f61a0bafc9c06d9ca600954e33d6adb321dba1a748b10eb
data/README.md CHANGED
@@ -346,6 +346,40 @@ end
346
346
 
347
347
  In Rails, this is auto-detected from the database.
348
348
 
349
+ ### Multiple files
350
+
351
+ For larger apps, split fixtures across multiple files using `FixtureBot.require`:
352
+
353
+ ```ruby
354
+ # spec/fixtures.rb
355
+ FixtureBot.require "spec/fixtures/**/*.rb"
356
+
357
+ FixtureBot.define do
358
+ user.email { |fixture| "#{fixture.key}@example.com" }
359
+ end
360
+ ```
361
+
362
+ ```ruby
363
+ # spec/fixtures/users.rb
364
+ FixtureBot.define do
365
+ user :brad do
366
+ name "Brad"
367
+ end
368
+ end
369
+ ```
370
+
371
+ ```ruby
372
+ # spec/fixtures/posts.rb
373
+ FixtureBot.define do
374
+ post :hello do
375
+ title "Hello"
376
+ author :brad
377
+ end
378
+ end
379
+ ```
380
+
381
+ Each file calls `FixtureBot.define` with its own block. Files are loaded in alphabetical order. References across files work because everything is resolved after all files are loaded.
382
+
349
383
  ### Implicit vs explicit style
350
384
 
351
385
  By default, the block is evaluated implicitly. Table methods like `user` and `post` are available directly:
@@ -388,6 +422,40 @@ FixtureBot::Schema.define do
388
422
  end
389
423
  ```
390
424
 
425
+ ## Migrating from FactoryBot
426
+
427
+ FixtureBot provides `build`, `create`, `attributes_for`, and related methods that mirror FactoryBot's API. The key difference is that you pass both a table name and a fixture name instead of just a factory name:
428
+
429
+ ```ruby
430
+ # FactoryBot # FixtureBot
431
+ build(:user) # build(:user, :brad)
432
+ create(:user) # create(:user, :brad)
433
+ build(:user, name: "X") # build(:user, :brad, name: "X")
434
+ attributes_for(:user) # attributes_for(:user, :brad)
435
+ build_list(:user, 3) # build_list(:user, :brad, :alice, :bob)
436
+ create_list(:user, 3) # create_list(:user, :brad, :alice, :bob)
437
+ build_pair(:user) # build_pair(:user, :brad, :alice)
438
+ create_pair(:user) # create_pair(:user, :brad, :alice)
439
+ build_stubbed(:user) # build_stubbed(:user, :brad)
440
+ ```
441
+
442
+ ### Method reference
443
+
444
+ | Method | Behavior |
445
+ |---|---|
446
+ | `build(:user, :brad, **attrs)` | Duplicates the fixture, applies overrides. Returns unpersisted. |
447
+ | `create(:user, :brad, **attrs)` | Without overrides: returns the fixture (already persisted). With overrides: `build` + `save!`. |
448
+ | `build_stubbed(:user, :brad, **attrs)` | Like `build` but retains `id`. Looks persisted without touching DB. |
449
+ | `attributes_for(:user, :brad, **attrs)` | Returns attributes hash, strips `id`/`created_at`/`updated_at`. |
450
+ | `build_list(:user, :brad, :alice, **attrs)` | Maps each name through `build`. |
451
+ | `create_list(:user, :brad, :alice, **attrs)` | Maps each name through `create`. |
452
+ | `build_pair(:user, :brad, :alice, **attrs)` | Alias for `build_list` with 2 names. |
453
+ | `create_pair(:user, :brad, :alice, **attrs)` | Alias for `create_list` with 2 names. |
454
+ | `build_stubbed_list(:user, :brad, :alice, **attrs)` | Maps each name through `build_stubbed`. |
455
+ | `build_stubbed_pair(:user, :brad, :alice, **attrs)` | Alias for `build_stubbed_list` with 2 names. |
456
+
457
+ These methods are automatically available in your tests when you require `fixturebot/rspec` or `fixturebot/minitest`. They call the standard Rails fixture accessors under the hood, so `build(:user, :brad)` is equivalent to `users(:brad).dup`.
458
+
391
459
  ## Prior art
392
460
 
393
461
  ### [Rails fixtures](https://guides.rubyonrails.org/testing.html#the-low-down-on-fixtures)
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "fixturebot/rails"
4
+ require "fixturebot/syntax"
4
5
 
5
6
  FixtureBot::Rails.compile
7
+
8
+ ActiveSupport::TestCase.include FixtureBot::Syntax::Methods
@@ -1,9 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "fixturebot/rails"
4
+ require "fixturebot/syntax"
4
5
 
5
6
  RSpec.configure do |config|
6
7
  config.before(:suite) do
7
8
  FixtureBot::Rails.compile
8
9
  end
10
+
11
+ config.include FixtureBot::Syntax::Methods
9
12
  end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/string/inflections"
4
+ require "active_support/core_ext/hash/keys"
5
+
6
+ module FixtureBot
7
+ module Syntax
8
+ module Methods
9
+ def build(table_name, fixture_name, **attributes)
10
+ record = send(table_name.to_s.pluralize, fixture_name).dup
11
+ record.assign_attributes(attributes) if attributes.any?
12
+ record
13
+ end
14
+
15
+ def create(table_name, fixture_name, **attributes)
16
+ if attributes.any?
17
+ build(table_name, fixture_name, **attributes).tap(&:save!)
18
+ else
19
+ send(table_name.to_s.pluralize, fixture_name)
20
+ end
21
+ end
22
+
23
+ def build_stubbed(table_name, fixture_name, **attributes)
24
+ source = send(table_name.to_s.pluralize, fixture_name)
25
+ attrs = source.attributes
26
+ attrs.merge!(attributes.stringify_keys) if attributes.any?
27
+ source.class.instantiate(attrs)
28
+ end
29
+
30
+ def attributes_for(table_name, fixture_name, **attributes)
31
+ send(table_name.to_s.pluralize, fixture_name)
32
+ .attributes
33
+ .symbolize_keys
34
+ .except(:id, :created_at, :updated_at)
35
+ .merge(attributes)
36
+ end
37
+
38
+ def build_list(table_name, *fixture_names, **attributes)
39
+ fixture_names.map { |name| build(table_name, name, **attributes) }
40
+ end
41
+
42
+ def create_list(table_name, *fixture_names, **attributes)
43
+ fixture_names.map { |name| create(table_name, name, **attributes) }
44
+ end
45
+
46
+ def build_pair(table_name, *fixture_names, **attributes)
47
+ build_list(table_name, *fixture_names, **attributes)
48
+ end
49
+
50
+ def create_pair(table_name, *fixture_names, **attributes)
51
+ create_list(table_name, *fixture_names, **attributes)
52
+ end
53
+
54
+ def build_stubbed_list(table_name, *fixture_names, **attributes)
55
+ fixture_names.map { |name| build_stubbed(table_name, name, **attributes) }
56
+ end
57
+
58
+ def build_stubbed_pair(table_name, *fixture_names, **attributes)
59
+ build_stubbed_list(table_name, *fixture_names, **attributes)
60
+ end
61
+ end
62
+ end
63
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FixtureBot
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
data/lib/fixturebot.rb CHANGED
@@ -29,6 +29,10 @@ module FixtureBot
29
29
  end
30
30
  end
31
31
 
32
+ def self.require(glob)
33
+ Dir.glob(glob).sort.each { |f| load f }
34
+ end
35
+
32
36
  def self.define_from_file(schema, fixtures_path)
33
37
  definition = Definition.new(schema)
34
38
  Thread.current[:fixturebot_definition] = definition
@@ -1,3 +1,5 @@
1
+ FixtureBot.require "<%= rspec? ? "spec" : "test" %>/fixtures/**/*.rb"
2
+
1
3
  FixtureBot.define do
2
4
  # Define your fixtures here. Example:
3
5
  #
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fixturebot-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brad Gessler
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2026-03-08 00:00:00.000000000 Z
10
+ date: 2026-03-11 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: activerecord
@@ -85,6 +85,7 @@ files:
85
85
  - lib/fixturebot/row.rb
86
86
  - lib/fixturebot/rspec.rb
87
87
  - lib/fixturebot/schema.rb
88
+ - lib/fixturebot/syntax.rb
88
89
  - lib/fixturebot/version.rb
89
90
  - lib/generators/fixturebot/install_generator.rb
90
91
  - lib/generators/fixturebot/templates/fixtures.rb.tt