Closed cj closed 3 years ago
I thought maybe removing https://github.com/joshleblanc/view_component_reflex/blob/master/lib/view_component_reflex/reflex_factory.rb#L18 would fix it, but the error still persists.
Ok, so looks like it's a combination of:
@joshleblanc @hopsoft Is there a reason Reflex
or Component
is required in the name of a class?
@cj view_component_reflex implicitly creates a reflex with the name
VIewComponent's naming scheme has every component post-fixed by Component
. Absolutely nothing makes the developer maintain this pattern, but view_component_reflex operates on the assumption that it's followed.
The monkey patch you linked in engine is used to initialize my generated reflex. When we receive a request to stimulate a component, we initialize the reflex.
Just looking at it, I could probably constantize anything being sent and checking if it responds to #init_stimulus_reflex
instead of checking for the Component postfix
@joshleblanc thank you for the response and that makes sense.
Would it be possible to just remove this check https://github.com/joshleblanc/view_component_reflex/blob/master/lib/view_component_reflex/engine.rb#L16 as ViewComponent's work without having Component
appended to the end of a child class and it does not break anything for ViewComponentReflex?
I can submit a pull request if you like.
Cheers!
Can't just only remove the check. That will throw incorrect warnings.
You'd have to constantize the component name, suppress any errors that throws, then check if it responds to init_stimulus_reflex, and run it if so. If the constantize succeeds, and it doesn't respond to init_stimulus_reflex, you could omit the current warning.
Would something like this work?
component = component.constantize
if component.respond_to?(:init_stimulus_reflex)
begin
component.init_stimulus_reflex
rescue StandardError
p "Tried to initialize view_component_reflex on #{component_name}, but it's not a view_component_reflex"
end
end
Something more like this
component = begin
component_name.constantize
rescue
# Since every reflex runs through this monkey patch, we're just going to ignore the ones that aren't for components
end
if component&.respond_to?(:init_stimulus_reflex)
component.init_stimulus_reflex
else
p "Tried to initialize view_component_reflex on #{component_name}, but it's not a view_component_reflex"
end
That looks better.... (I had updated my comment because I made a mistake, I was writing it while eating lunch. lol)
@joshleblanc If you'd like me to open a pull request let me know. Thank you again!
Just testing it now
I am trying a simple example
and I am getting the following error:
rails 6.1
gem 'stimulus_reflex', '~> 3.4.0.pre9'
stimulus": "^2.0.0
stimulus_reflex": "3.4.0-pre8
gem 'view_component_reflex', '~> 3.0.0'
gem 'view_component', '~> 2.23.1'
Let me know if you need any more information!