Methods
Attributes
[R] | singular | |
[R] | plural | |
[R] | element | |
[R] | collection | |
[R] | partial_path | |
[R] | singular_route_key | |
[R] | route_key | |
[R] | param_key | |
[R] | i18n_key |
Class Public methods
# File activemodel/lib/active_model/naming.rb, line 15 15: def initialize(klass, namespace = nil, name = nil) 16: name ||= klass.name 17: 18: raise ArgumentError, "Class name cannot be blank. You need to supply a name argument when anonymous class given" if name.blank? 19: 20: super(name) 21: 22: @unnamespaced = self.sub(/^#{namespace.name}::/, '') if namespace 23: @klass = klass 24: @singular = _singularize(self).freeze 25: @plural = ActiveSupport::Inflector.pluralize(@singular).freeze 26: @element = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self)).freeze 27: @human = ActiveSupport::Inflector.humanize(@element).freeze 28: @collection = ActiveSupport::Inflector.tableize(self).freeze 29: @partial_path = "#{@collection}/#{@element}".freeze 30: @param_key = (namespace ? _singularize(@unnamespaced) : @singular).freeze 31: @i18n_key = self.underscore.to_sym 32: 33: @route_key = (namespace ? ActiveSupport::Inflector.pluralize(@param_key) : @plural.dup) 34: @singular_route_key = ActiveSupport::Inflector.singularize(@route_key).freeze 35: @route_key << "_index" if @plural == @singular 36: @route_key.freeze 37: end
Instance Public methods
Transform the model name into a more humane format, using I18n. By default, it will underscore then humanize the class name
BlogPost.model_name.human # => "Blog post"
Specify options with additional translating options.
# File activemodel/lib/active_model/naming.rb, line 45 45: def human(options={}) 46: return @human unless @klass.respond_to?(:lookup_ancestors) && 47: @klass.respond_to?(:i18n_scope) 48: 49: defaults = @klass.lookup_ancestors.map do |klass| 50: klass.model_name.i18n_key 51: end 52: 53: defaults << options[:default] if options[:default] 54: defaults << @human 55: 56: options = {:scope => [@klass.i18n_scope, :models], :count => 1, :default => defaults}.merge(options.except(:default)) 57: I18n.translate(defaults.shift, options) 58: end