Skip to content

Instantly share code, notes, and snippets.

@justinxreese
Forked from anonymous/heirarhess.rb
Created April 8, 2016 20:34
Show Gist options
  • Save justinxreese/e660a6962ac534bdf0ec17ae78ad9114 to your computer and use it in GitHub Desktop.
Save justinxreese/e660a6962ac534bdf0ec17ae78ad9114 to your computer and use it in GitHub Desktop.
module ProjectTypes
def self.assignable_types
ProjectTypes.constants.map{|klass|
[ProjectTypes.const_get(klass)::ENGLISH_NAME, klass]
}
end
end
class ProjectTypes::TypeIII
ENGLISH_NAME = 'Type III'
end
class ProjectTypes::TypeII
ENGLISH_NAME = 'Type II'
end
puts ProjectTypes.assignable_types
@mattsears
Copy link

This should work. What output are you expecting?

@justinxreese
Copy link
Author

@mattsears - it just ended up not being autoloaded. Here's a conversation I had with @georgeclaghorn that solved it

George Claghorn [9:12 PM] 
@justinxreese Where are those classes? Separate files?

Justin Reese [9:22 PM] 
@george: the classes don't even exist yet. I'm just trying to call `ProjectTypes.assignable_types` in a view and let it fail

new messages
George Claghorn [9:22 PM] 
Do you get an error, unexpected result, ...?

Justin Reese [9:23 PM] 
undefined method `assignable_types` on ProjectTypes

George Claghorn [9:25 PM] 
what's the path of the file containing the definition of ProjectTypes.assignable_types?

Justin Reese [9:26 PM] 
`app/models/project_types/project_types.rb`

George Claghorn [9:26 PM] 
Is `app/models/project_types` in the autoload path?

Justin Reese [9:26 PM] 
well, it knows about the module

[9:26] 
doesn't complain about that

[9:27] 
but I am not positive

George Claghorn [9:27 PM] 
any chance you define a ProjectTypes module elsewhere?

Justin Reese [9:27 PM] 
no

George Claghorn [9:28 PM] 
not sure, but I think Rails may automatically define modules for subdirs of `app/models` :neutral_face: (edited)

[9:28] 
your definition of `ProjectTypes` is never autoloaded, since it's not at the expected path

[9:29] 
reproduced in a fresh Rails app by defining a module `Bar` in `app/models/foo/bar.rb`

[9:29] 
`Foo` exists, `Bar` doesn't

[9:33] 
confirmation: https://github.com/rails/rails/blob/a26a3a075637215c9028308436ca89cba8da2ed5/activesupport/lib/active_support/dependencies.rb#L450

GitHub
rails/rails
rails - Ruby on Rails

Justin Reese [9:33 PM] 
huh

[9:34] 
so I was kind of going go it creating the module

[9:34] 
but i'm surprised it doesn't allow me to reopen it and add methods

George Claghorn [9:34 PM] 
Rails expects `ProjectTypes` to be defined in `#{some_autoload_path}/project_types.rb`

Justin Reese [9:35 PM] 
i feel like line 458 there shouldn't happen if the constant is already defined

George Claghorn [9:35 PM] 
it doesn't

Justin Reese [9:36 PM] 
ohhh ohhh.. I bet my module is `ProjectTypes::ProjectTypes` because of that

George Claghorn [9:37 PM] 
no, but that's what Rails would expect from `app/models/project_types/project_types.rb`

Justin Reese [9:37 PM] 
yeah... that's what i'm saying

[9:38] 
the one that has `assignable_types` defined

[9:38] 
`ProjectTypes::ProjectTypes.assignable_types`

George Claghorn [9:38 PM] 
`ProjectTypes::ProjectTypes` doesn't exist if you don't define it

Justin Reese [9:38 PM] 
yeah... I did

George Claghorn [9:38 PM] 
you do define `ProjectTypes.assignable_types`, but Rails never sees it

[9:39] 
when you try to access `ProjectTypes` in the view, Rails gets a const_missing and looks for a file that defines it (for example, `app/models/project_types.rb`)

[9:40] 
doesn't find one, but there is a directory named `project_types` in `app/models`, so it creates the top-level module

Justin Reese [9:40 PM] 
yeah. i'm on board with all of that

[9:40] 
i'm just saying, that I bet I can call `ProjectTypes::ProjectTypes.assignable_types` right now and get something, because I defined it in `app/models/project_types/project_types.rb`, which would confirm I just got the file structure wrong

George Claghorn [9:41 PM] 
I think you'll get the same error

Justin Reese [9:41 PM] 
i am gonna test you :simple_smile: let's find out

[9:42] 
whaaaaaaaat

[9:43] 
`Unable to autoload constant ProjectTypes::ProjectTypes, expected /Users/justin/github/builderworksco/redacted/app/models/project_types/project_types.rb to define it`

[9:43] 
that's EXACTLY where it's defined rails!

George Claghorn [9:43 PM] 
no, that's where the top-level `ProjectTypes` is reopened :simple_smile:

Justin Reese [9:43 PM] 
hahahahah

[9:43] 
ughhhhh

George Claghorn [9:43 PM] 
fix is to move that file to `app/models/project_types.rb`

Justin Reese [9:43 PM] 
yeah. Was fun toying with that though

[9:44] 
gonna post this convo on the gist. for the internet's sake

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment