Skip to content

Instantly share code, notes, and snippets.

@MSch
Created June 21, 2013 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MSch/5833209 to your computer and use it in GitHub Desktop.
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.
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