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

Constant being resolved as anonymous class #25

Open
radar opened this issue Apr 25, 2018 · 1 comment
Open

Constant being resolved as anonymous class #25

radar opened this issue Apr 25, 2018 · 1 comment

Comments

@radar
Copy link

radar commented Apr 25, 2018

Hello!

I have a Rails application which has some unusual behaviour when paired with dry-core.

Here's the branch that replicates the issue. If you clone down this app with this branch and run:

bundle install
DISABLE_SPRING=1 rails runner projects.rb

Then you will see the issue that I am facing. It's a two-parter:

  1. The class of instance that is returned by repo.all.first should be a Projects::Project, and it is... but it's seemingly coming from Dry::Core::ClassBuilder, rather than the one defined in the application.
  2. The instance should respond to a to_model call, but it doesn't. This call should be inherited from ApplicationModel, by way of the Projects::Project class within the application.

So in effect, both of the last statements in projects.rb should return true if this code is working as it should.

To attempt to work around this, I've tried doing require "projects/project" in project_repository.rb, but this flips it around:

  1. It defines the class as an anonymous class.
  2. The instance does respond to a to_model call.

The source of this problem appears to be this code within ClassBuilder:

namespace.module_eval do
remove_const(name)
const_set(name, klass)
const_get(name).name if RUBY_VERSION < '2.4'
remove_const(name)
const_set(name, base)
end

In particular:

const_get(name).name if RUBY_VERSION < '2.4'

If I remove the if statement from this line and manually require the projects/project file in my ProjectRepository class, everything works correctly, at least in my application.

That line -- in its entirety -- was added in abdb106, but the reasoning for this is opaque to me. Seems to be fixing / working around some sort of Ruby bug that only existed pre-Ruby 2.4? Well, my app is using Ruby 2.5 so this line doesn't do anything.

I'm betting this is going to be down to some weird combination between dry-core and Rails' own auto-loading mumbo-jumbo. I'd just like to get an expert opinion before I go blaming Rails (again).

radar pushed a commit to radar/exploding-rails-rom-dry-example-app that referenced this issue Apr 25, 2018
Due to a potential issue between dry-core / rails autoloading: dry-rb/dry-core#25
@radar
Copy link
Author

radar commented Apr 25, 2018

I suspect if I turn eager_load to true in both development.rb and test.rb it would work around my problem, but I want to see if there's another potential solution first.

radar pushed a commit to radar/exploding-rails-rom-dry-example-app that referenced this issue May 1, 2018
Due to a potential issue between dry-core / rails autoloading: dry-rb/dry-core#25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant