Thanks for the great gem!
I'm looking for a way to seed my Fabricators with custom data, stored as an array. The problem is I'd like one line of sample data to be dispatch between different Fabricators (due to database normalization).
In short here is what I'm trying to do:
1 Fabricator(:machine) do
2 transient sample_line: FakeData::Machine.new.random_line
4 internal_name { |attrs| attrs[:sample_line][1] }
5 commercial_name { |attrs| attrs[:sample_line][2] }
8 Fabricator(:valid_machine, from: :machine) do
10 Fabricate.build(:valid_brand) do
11 name { |attrs| attrs[:sample_line][0] }
The problem with this approach is that once the transient attribute is loaded, line 2 will not be re-evaluated. I can't pass a lambda as a transient attribute either.
Ideally I would like to have my own object instance passed around the fabricators. That way I could call methods inside each block definitions such as :
transient sample: -> { FakeData::RandomItem.new }
name { |attrs| attrs[:sample].name }
speed { |attrs| attrs[:sample].speed }
Another approach I took is too use an external module to keep track of which line of sample data is currently being used, here:
1 Fabricator(:machine) do
2 internal_name { FakeData::Machine.current_sample[1] } # On the first call I grab a random line
3 commercial_name { FakeData::Machine.current_sample[2] }
5 after_build { FakeData::Machine.delete_current_sample }
8 Fabricator(:valid_machine, from: :machine) do
10 Fabricate.build(:valid_brand) do
11 name { FakeData::Machine.current_sample[0] }
Here I'm just grabbing a random line of sample data, then deleting it from the a array since I don't want to use that line twice in my tests.
This works however it's pretty messy and doesn't feel right.
Is there some other means to achieve this ? Thanks for your input !