Created
July 6, 2015 14:14
-
-
Save tmichel/fa5548f43cc729cb8f0d to your computer and use it in GitHub Desktop.
Rails constant lookup WTF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Rails autoloading constant functionality is useful until it's not. | |
| # Every class is placed in the right file on the right path. | |
| # in app/models/users/serializer.rb | |
| module Users | |
| class Serializer | |
| #... | |
| end | |
| end | |
| # in app/models/users/publish.rb | |
| module Users | |
| class Publish | |
| # this works | |
| def self.user(user) | |
| publish(Serializer.new(user)) | |
| end | |
| class << self | |
| # this does not work, Rails cannot lookup Serializer constant | |
| def user(user) | |
| publish(Serilaizer.new(user) | |
| end | |
| # this does work because the full name of the class is specified | |
| def user(user) | |
| publish(Users::Serializer.new(user)) | |
| end | |
| end | |
| def self.publish(ser) | |
| # ... | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment