-
-
Notifications
You must be signed in to change notification settings - Fork 27
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
Injection issue in rails 7 & ruby 3.1 #53
Comments
This is a change in Rails that broke your code, by using auto-inject you define a constructor and it looks like it's no longer compatible with Rails 7. dry-auto_inject shouldn't be used in 3rd-party libraries exactly because of this. We can't do anything about this here so I'm going to move this issue to dry-rails and we can see how it could be done there. |
This was moved from dry-auto_inject because we could come up with a nice integration with the controller API. Marking it as help-wanted as I don't have time (for now at least) to work on this. |
I thank you @solnic for your response and help to moved this issue. I thought I need to find another way to make it work without big changes, for the moment I try to use Add a path
and in controller I can call looks like
What do you think about this? |
I should clarify - We should definitely explain this in the docs 🙂
Actually, dry-rails gives you a def method_missing(name, *args)
if container.key?("v1.#{name}")
resolve("v1.#{name}")
else
super
end
end This is obviously a hack but it should help with the transition 🙂 |
On our project we updated dry-rb to the latest version and faced the same issue, so we decided to build our own injection strategy for controllers: # frozen_string_literal: true
require "dry/auto_inject/dependency_map"
module Dry
module AutoInject
class Strategies
class Resolve < Module
InstanceMethods = Class.new(Module)
attr_reader :container
attr_reader :dependency_map
attr_reader :instance_mod
def initialize(container, *dependency_names)
super()
@container = container
@dependency_map = DependencyMap.new(*dependency_names)
@instance_mod = InstanceMethods.new
end
# @api private
def included(klass)
define_resolvers
klass.send(:include, instance_mod)
super
end
private
def define_resolvers
instance_mod.class_exec(container, dependency_map) do |container, dependency_map|
dependency_map.to_h.each do |name, key|
define_method name do
container[key]
end
end
end
end
end
register :resolve, Resolve
end
end
end and then in controllers we do: include YourApp::Import.resolve[
'your.operations.name',
] |
Hi @solnic ! I'm also struggling with this issue after upgrading from class ApiUsersController < ActionController::API
include Dummy::Deps[
mailer: "mailer"
]
...
end
I think there is something wrong with dry-auto_inject but I don't know yet what exactly. Maybe you can give some advice? Thanks! |
I'm not sure that this is a smarter one but it fixes the problem k0va1@767154a |
Hi @solnic I just got informed, it's weird the problem is solved when I use action controller base instead of using the api mode. meanwhile the api mode is a lightweight version, which is exclude some modules from base. |
My app worked without problems using Rails 7.0 and Ruby 3.3. Problem appeared after upgrading to Rails 7.1. |
Same problem with @oskargargas after upgrading to Rails 7.1 I think it breaks due to this change: My
The class/module after inject is changed to add |
Is there any expectation of movement on this? Is this known to be something that will be tackled? |
@charlieoliver I'd love it if someone could submit a PR to address this. |
Not in my skill set but I hope that putting the ask out there may spark something! Thank you for responding! |
We have more than 20 rails app using rails 6 & below and ruby 2.7 & below, there are implemented service pattern using dry injection, it is broken on rails 7 & ruby 3.1
I'm getting an error looks like
To Reproduce
di_container.rb
onlib/marka/
directory and register and initialize the PostService classposts_controller
underapp/controllers/api/v1/
directory, and try to inject the method which registered onMarka
moduleExpected behavior
Upgrade our apps to rails 7 using the same pattern.
My environment
stakeoverflow question :
https://stackoverflow.com/q/71131422/2858044
The text was updated successfully, but these errors were encountered: