dry-rb / dry-rb/dry-core

Constant being resolved as anonymous class

Open
#25 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Ruby
Stars
182
Forks
41
PR merge metrics
No merged PRs in 30d

Description

Hello!

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

Here's [the branch](https://github.com/radar/exploding-rails-rom-dry-example-app/tree/dry-core-issue) 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`:

https://github.com/dry-rb/dry-core/blob/3b26625861b131f85106280e2b38647f8200db11/lib/dry/core/class_builder.rb#L69-L77

In particular:

https://github.com/dry-rb/dry-core/blob/3b26625861b131f85106280e2b38647f8200db11/lib/dry/core/class_builder.rb#L73

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 abdb106a23528774828a97dc1002d0ed0af33f70, 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).

Contributor guide

Open the contributing guide

Research direction

Start by cloning the linked reproduction branch and running bundle install followed by DISABLE_SPRING=1 rails runner projects.rb. Read lib/dry/core/class_builder.rb around lines 69-77, then inspect projects/project and project_repository.rb. Done means repo.all.first is the application Projects::Project and both final statements in projects.rb return true.

Written by the indexing model from the issue text.

Assessment

Tech stack
rails, ruby
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.