Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

overriding factory attributes is causing related field callbacks to fail #63

Open
lowang opened this issue Dec 28, 2020 · 5 comments
Open

Comments

@lowang
Copy link

lowang commented Dec 28, 2020

Describe the bug

Overriding attributes is causing factory field resolver to receive nil instead of overwritten value.

To Reproduce

create file app_spec.rb with:

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' }
end

RSpec.describe 'app' do
  let(:data) do
    { name: 'Alice' }
  end

  let(:user) { Factory[:user, data] }

  specify do
    pp user
  end
end

now rspec app_spec.rb is producing following error:

 NoMethodError:
       undefined method `downcase' for nil:NilClass
     # ./app_spec.rb:24:in `block (2 levels) in <top (required)>'
     # ./app_spec.rb:32:in `block (2 levels) in <top (required)>'
     # ./app_spec.rb:35:in `block (2 levels) in <top (required)>'

Expected behavior

I was expecting email callback to receive Alice value.

My environment

  • Affects my production application: YES/NO
  • Ruby version: 2.6.5
  • OS: 10.15.5
@solnic solnic added this to the 0.10.3 milestone Dec 30, 2020
@solnic
Copy link
Member

solnic commented Dec 30, 2020

Thanks for reporting this, clearly a bug. I think we should re-think this API in general. Yielding attribute values via block args can be problematic when you have attributes that match ruby keywords.

@flash-gordon
Copy link
Member

I still think having a convention for reserved words like do |then| -> do |then_| would be good enough

@solnic
Copy link
Member

solnic commented Dec 30, 2020

@flash-gordon yeah...seems like a pragmatic choice. I wonder though....maybe we could provide a struct with values that are lazy, this way you could still rely on more than one value but access it through one object.

@flash-gordon
Copy link
Member

@solnic yeah I think it'd be possible too

@yld
Copy link

yld commented Apr 29, 2024

This makes ROM factory somewhat unusable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants