Skip to content

Commit

Permalink
Add object counter to seed fixtures
Browse files Browse the repository at this point in the history
Use the key `:count` to define number of objects to create:

```yml
---
- type: user
  count: 3
  traits:
    - customer
```

Notice, that the counter can be set to 0 (or negative which is
the same as zero). This is useful to parameterize the seed:

```yml
---
- type: user
  count: <%= customers %>
  traits:
    - customer
```
  • Loading branch information
nepalez committed May 22, 2019
1 parent 91a3019 commit 20e8b05
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/fixturama/seed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ def call(opts)
type = opts[:type].to_sym
traits = Utils.symbolize_array opts[:traits]
params = Utils.symbolize_hash opts[:params]
count = opts.fetch(:count, 1).to_i

FactoryBot.create(type, *traits, **params)
FactoryBot.create_list(type, count, *traits, **params) if count.positive?
end
end
end
3 changes: 2 additions & 1 deletion spec/fixturama/seed_fixture/_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
end

it "runs the factory", aggregate_failures: true do
expect(FactoryBot).to receive(:create).and_return(bar: 99, baz: 77, qux: 42)
expect(FactoryBot).to receive(:create_list).with(:foo, 1, :baz, qux: 42)
expect(FactoryBot).to receive(:create_list).with(:foo, 3, :bar, {})

subject
end
Expand Down
11 changes: 10 additions & 1 deletion spec/fixturama/seed_fixture/seed.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
---
- type: foo
traits:
- bar
- baz
params:
qux: 42

- type: foo
count: 3
traits:
- bar

- type: foo
count: 0
traits:
- baz

0 comments on commit 20e8b05

Please sign in to comment.