rom-rb / rom-factory

Data generator with support for persistence backends
MIT License
83 stars 42 forks source link

Introduce ManyToMany and OneToOneThrough builder #86

Closed solnic closed 7 months ago

solnic commented 10 months ago

This adds support for building associated structs in case of ManyToMany and OneToOneThrough association types, where intermediate join tables are involved. It works with persistable and in-memory structs.

factories.define(:user) do |f|
  f.first_name { fake(:name, :first_name) }
  f.last_name { fake(:name, :last_name) }
  f.email { fake(:internet, :email) }
  f.timestamps

  f.association :address
end

factories.define(:user_address) do |f|
  f.association(:user)
  f.association(:address)
  f.timestamps
end

factories.define(:address) do |f|
  f.full_address { fake(:address, :full_address) }
end

# Persisted struct
factories[:user]
# #<ROM::Struct::User id=2 last_name="Leuschke" first_name="Don" alias=nil email="hyman.halvorson@jerde-dach.example" created_at=2023-11-24 10:19:25.693242 +0000 updated_at=2023-11-24 10:19:25.693246 +0000 age=nil type=nil address=#<ROM::Struct::Address id=2 full_address="44090 Patrina Vista, Barrowsburgh, SC 87402" user_id=2>>

# In-memory struct works too
factories.structs[:user]
#<ROM::Struct::User id=1 last_name="Zieme" first_name="Mallory" alias=nil email="lu_nolan@hermiston-hansen.example" created_at=2023-11-24 10:20:05.920642677 +0000 updated_at=2023-11-24 10:20:05.920648427 +0000 age=nil type=nil address=#<ROM::Struct::Address id=1 full_address="Suite 376 7225 Ortiz Centers, East Desmondland, MT 16834-2493" user_id=1>>
solnic commented 9 months ago

Switching to Draft again as there are a couple of issues to address