Hey! I found little bug: when developer wants to create new action in namespace via hanami action generator - he gets problem with constant lookup, because generator creates a module with non-exist constant in module name.
For example:
hanami new awesome_hanami
cd awesome_hanami
hanami generate action web 'users/registrations#new'
and then, try to run hanami console:
hanami console
and we get an error with this message:
awesome_hanami/apps/web/controllers/users/registrations/new.rb:1:in '<top (required)>': uninitialized constant Web::Controllers::Users (NameError)
ruby version is 2.4.0
hanami version is 1.0
I think you know about this behavior, but maybe we should to fix it?
Thanks! This is because we generate with the 'compact' module form instead of the 'nested' module form. 'nested' is more verbose & more natural, and allows you to leverage namespacing properly.
The 'Constants Refresher' section from the Rails guides is a very useful resource, if you want to learn more about the differences between the two.
Basically, Ruby sees module Web::Controllers::Users::Registrations and it hasn't seen Users before. It can't define it either because it doesn't know if it should be a Class or a Module, so it gives you an error.
To fix this, you have two options:
apps/web/controllers/users.rbmodule Web::Controllers::Users
end
apps/web/views/users.rbmodule Web::Views::Users
end
apps/web/controllers/users/registrations/new.rb should be edited to look like:module Web
module Controllers
module Users
module Registrations
class New
include Web::Action
...
(and similarly with the associated view, which will have the same problem)
@jodosha recently said we'll move to generating the nested style in the future, which will fix this issue.
Does that make sense? I hope it helps. Going to close this issue but feel free to comment further if you need anymore help! 馃尭
(Also, I know English isn't your first language, but you said 'he' referring to a developer and we shouldn't assume developers using Hanami are all men :) Instead you can say 'he or she' or 'they' instead of 'he'. 'they' as you know is usually third-person plural but can also be used for third-person singular, if you don't know the person's gender.)
@cllns thank you for the quick and detail answer, but I know how ruby constant lookup works and how to fix it for my case :) I wrote this issue for this reason: maybe we can fix this behavior in hanami action generator? I mean, make a generator which will be automatically generate actions with correct namespaces.
p.s. sorry for he pronoun
Thanks @we138, just reopened this issue so we can discuss that. (And re-named the issue, to be more clear)
I'm in favor of switching to 'nested' style, but I'm not sure if there are any reasons why that won't work, or that would make it hard to do right now.
Thoughts @hanami/core @jodosha?
@cllns @we138 maybe we can do the following: if that constant exists then use the 'compact' style, if not then use the nested style. What do you think?
@AlfonsoUceda mm, yep, i think it is good idea)
@cllns I think technically we should go with "he or she or it". Some people don't identify as belonging into either "he" or "she". Or maybe singular they ( https://en.wikipedia.org/wiki/Singular_they ) would work? Dunno, I suck at English so not sure what is the best.
But this should probably be solved and put into Code of Conduct so we know what pronouns to use and avoid situations like this which could trigger someone.
I think singular they is best. Agree we should have a note somewhere about not assuming gender (happens in the chat with "hey guys" a lot).
Implemented by https://github.com/hanami/hanami/pull/934
Most helpful comment
Thanks! This is because we generate with the 'compact' module form instead of the 'nested' module form. 'nested' is more verbose & more natural, and allows you to leverage namespacing properly.
The 'Constants Refresher' section from the Rails guides is a very useful resource, if you want to learn more about the differences between the two.
Basically, Ruby sees
module Web::Controllers::Users::Registrationsand it hasn't seenUsersbefore. It can't define it either because it doesn't know if it should be a Class or a Module, so it gives you an error.To fix this, you have two options:
apps/web/controllers/users.rbapps/web/views/users.rbSo
apps/web/controllers/users/registrations/new.rbshould be edited to look like:(and similarly with the associated view, which will have the same problem)
@jodosha recently said we'll move to generating the nested style in the future, which will fix this issue.
Does that make sense? I hope it helps. Going to close this issue but feel free to comment further if you need anymore help! 馃尭
(Also, I know English isn't your first language, but you said 'he' referring to a developer and we shouldn't assume developers using Hanami are all men :) Instead you can say 'he or she' or 'they' instead of 'he'. 'they' as you know is usually third-person plural but can also be used for third-person singular, if you don't know the person's gender.)