janlelis / irbtools

Improvements for Ruby's IRB console 💎︎
MIT License
920 stars 27 forks source link

FactoryBot incompatibility - consider renaming `code` on main object #58

Open mculp opened 3 months ago

mculp commented 3 months ago

I’m running into an incompatibility issue with irbtools because of the way FactoryBot uses method_missing.

When you define a factory with an attribute code, it tries to call the code defined on the main object by irbtools.

I realize that’s not exactly the fault of irbtools, but it seems like in order to maintain compatibility with application code, that any method defined on the main object should have a pretty unique name.

None of the methods defined would be 100% compatible in all use cases, but I guess the point I’m trying to make is that code is a very, very common attribute name. Similar to defining a top-level method called name or id.

Pry uses show-source (not exactly sure how they use a -) for a similar feature.

Something like show_code would be less likely to be incompatible.

Here’s the relevant part of the stacktrace:

~/.gem/ruby/3.2.2/gems/irbtools-4.0.9/lib/irbtools/libraries.rb:59:in `code': wrong number of arguments (given 0, expected 1..2) (ArgumentError)
        from ~/app/lib/factories/address_factory.rb:11:in `block (2 levels) in <main>'
        from ~/.gem/ruby/3.2.2/gems/factory_bot-6.2.1/lib/factory_bot/syntax/default.rb:18:in `instance_eval'

With a standard defined factory:

FactoryBot.define do
  factory :address do
    code { "123" }
  end
end