Created
June 21, 2013 18:23
-
-
Save MSch/5833209 to your computer and use it in GitHub Desktop.
If you prefer to keep your models namespaced then ActiveModel::Naming really makes that harder for you than it would have to be.
This file contains 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
module MyApp::ModelNameFix | |
def model_name | |
@_model_name ||= ActiveModel::Name.new(self, nil, self.name.sub(/^#{self.parent.name}::/, '')) | |
end | |
def namespaced_model_name | |
@_namespaced_model_name ||= ActiveModel::Name.new(self) | |
end | |
# Work with Draper | |
def decorator_class | |
prefix = self.namespaced_model_name | |
decorator_name = "#{prefix}Decorator" | |
decorator_name.constantize | |
rescue NameError => error | |
raise unless error.missing_name?(decorator_name) | |
raise Draper::UninferrableDecoratorError.new(self) | |
end | |
end | |
class ActiveRecord::Base | |
extend MyApp::ModelNameFix | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment