rom-rb / rom-factory

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

Overriding attributes doesn't affect dependent attributes #67

Open Morozzzko opened 3 years ago

Morozzzko commented 3 years ago

Describe the bug

Whenever I have an attribute which depends on another one, overriding the dependency (i.e. with a trait) doesn't affect the calculated attribute.

Seems similar to #63, but a bit different

To Reproduce

require 'bundler/inline'

gemfile do
  source 'https://rubygems.org'
  gem 'rom'
  gem 'rom-sql'
  gem 'sqlite3'
  gem 'rom-factory', '~> 0.10.2'
end

require 'rom'
require 'rom-factory'

ROM_CONTAINER = ROM.container(:sql, 'sqlite::memory') do |conf|
  conf.default.create_table(:users) do
    primary_key :id
    column :name, String, null: false
    column :email, String, null: false
  end

  class Users < ROM::Relation[:sql]
    schema(infer: true)
  end

  conf.register_relation(Users)
end

Factory = ROM::Factory.configure do |config|
  config.rom = ROM_CONTAINER
end

Factory.define(:user) do |f|
  f.name 'John'
  f.email { |name| name.downcase + '@example.com' }

  f.trait :jane do |t|
    t.name 'Jane'
  end
end

pp Factory[:user, :jane] # #<ROM::Struct::User id=1 name="Jane" email="john@example.com">

Expected behavior

I expect calculated field to change its value depending on name, even if I override name in a trait.

My environment

solnic commented 3 years ago

Thanks for reporting this. I'll be revisiting this gem in general just before rom 6.0 / hanami 2.0 so hopefully I'll address all the known issues. In the meantime....help-wanted 🙂