dry-rb / dry-rails

The official dry-rb railtie
https://dry-rb.org/gems/dry-rails
MIT License
270 stars 27 forks source link

Container constant to be configurable #21

Closed jandudulski closed 3 years ago

jandudulski commented 4 years ago

In my current application, there is an ActiveRecord model named Container. This is unfortunately in conflict with dry-rails while booting and failed with undefined method features for Container since https://github.com/dry-rb/dry-rails/blob/23709e3a7cd08433480e85ab68189818d2af2af1/lib/dry/rails/railtie.rb#L73 loads model class.

Examples

Dry::Rails.container do
  config.container_constant = 'MyContainer'
end
solnic commented 4 years ago

Sounds like a good feature to have, in line with #18

diegotoral commented 4 years ago

@solnic I can work on this one! 😃

solnic commented 4 years ago

@diegotoral OK grab it then :)

diegotoral commented 4 years ago

Hey @solnic, I am having problems to make the container name customizable. I was able to make the container created with a custom name but I don't know exactly how to access the container name later on other parts of the code.

For example, in lib/dry/rails/rails/railtie.rb file some methods reference :Container like in the container method and reloading?. How can I access the custom container name from that point in code, since the configuration is loaded for each container instance?

solnic commented 4 years ago

@diegotoral for consistency's sake, this should be a new config setting, defined in the container class. Then in places where :Container is referenced, you need to change it to use the config. This would have to be stored at the Railtie instance level too, otherwise you'll have a chicken-egg problem. So ie app_namespace.const_get(:Container, false) should become something like app_namespace.const_get(container_const_name, false) where container_const_name is a reader defined in the Railtie class.

In the Railtie#finalize! you could have something like self.container_const_name = container.config.const_name (so an attr reader).

diegotoral commented 4 years ago

That makes sense! I'll give it a try. Thanks @solnic