Class: ActiveModel::Name
- Includes:
- Comparable
- Defined in:
- activemodel/lib/active_model/naming.rb
Instance Attribute Summary collapse
-
#collection ⇒ Object
(also: #cache_key)
readonly
Returns the value of attribute collection.
-
#element ⇒ Object
readonly
Returns the value of attribute element.
-
#i18n_key ⇒ Object
readonly
Returns the value of attribute i18n_key.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#param_key ⇒ Object
readonly
Returns the value of attribute param_key.
-
#plural ⇒ Object
readonly
Returns the value of attribute plural.
-
#route_key ⇒ Object
readonly
Returns the value of attribute route_key.
-
#singular ⇒ Object
readonly
Returns the value of attribute singular.
-
#singular_route_key ⇒ Object
readonly
Returns the value of attribute singular_route_key.
Instance Method Summary collapse
-
#human(options = {}) ⇒ Object
Transform the model name into a more humane format, using I18n.
-
#initialize(klass, namespace = nil, name = nil) ⇒ Name
constructor
Returns a new ActiveModel::Name instance.
Constructor Details
#initialize(klass, namespace = nil, name = nil) ⇒ Name
Returns a new ActiveModel::Name instance. By default, the namespace
and name
option will take the namespace and name of the given class respectively.
module Foo
class Bar
end
end
ActiveModel::Name.new(Foo::Bar).to_s
# => "Foo::Bar"
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'activemodel/lib/active_model/naming.rb', line 145 def initialize(klass, namespace = nil, name = nil) @name = name || klass.name raise ArgumentError, "Class name cannot be blank. You need to supply a name argument when anonymous class given" if @name.blank? @unnamespaced = @name.sub(/^#{namespace.name}::/, '') if namespace @klass = klass @singular = _singularize(@name) @plural = ActiveSupport::Inflector.pluralize(@singular) @element = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(@name)) @human = ActiveSupport::Inflector.humanize(@element) @collection = ActiveSupport::Inflector.tableize(@name) @param_key = (namespace ? _singularize(@unnamespaced) : @singular) @i18n_key = @name.underscore.to_sym @route_key = (namespace ? ActiveSupport::Inflector.pluralize(@param_key) : @plural.dup) @singular_route_key = ActiveSupport::Inflector.singularize(@route_key) @route_key << "_index" if @plural == @singular end |
Instance Attribute Details
#collection ⇒ Object (readonly) Also known as: cache_key
Returns the value of attribute collection
8 9 10 |
# File 'activemodel/lib/active_model/naming.rb', line 8 def collection @collection end |
#element ⇒ Object (readonly)
Returns the value of attribute element
8 9 10 |
# File 'activemodel/lib/active_model/naming.rb', line 8 def element @element end |
#i18n_key ⇒ Object (readonly)
Returns the value of attribute i18n_key
8 9 10 |
# File 'activemodel/lib/active_model/naming.rb', line 8 def i18n_key @i18n_key end |
#name ⇒ Object (readonly)
Returns the value of attribute name
8 9 10 |
# File 'activemodel/lib/active_model/naming.rb', line 8 def name @name end |
#param_key ⇒ Object (readonly)
Returns the value of attribute param_key
8 9 10 |
# File 'activemodel/lib/active_model/naming.rb', line 8 def param_key @param_key end |
#plural ⇒ Object (readonly)
Returns the value of attribute plural
8 9 10 |
# File 'activemodel/lib/active_model/naming.rb', line 8 def plural @plural end |
#route_key ⇒ Object (readonly)
Returns the value of attribute route_key
8 9 10 |
# File 'activemodel/lib/active_model/naming.rb', line 8 def route_key @route_key end |
#singular ⇒ Object (readonly)
Returns the value of attribute singular
8 9 10 |
# File 'activemodel/lib/active_model/naming.rb', line 8 def singular @singular end |
#singular_route_key ⇒ Object (readonly)
Returns the value of attribute singular_route_key
8 9 10 |
# File 'activemodel/lib/active_model/naming.rb', line 8 def singular_route_key @singular_route_key end |
Instance Method Details
#human(options = {}) ⇒ Object
Transform the model name into a more humane format, using I18n. By default, it will underscore then humanize the class name.
class BlogPost
extend ActiveModel::Naming
end
BlogPost.model_name.human # => "Blog post"
Specify options
with additional translating options.
175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'activemodel/lib/active_model/naming.rb', line 175 def human(={}) return @human unless @klass.respond_to?(:lookup_ancestors) && @klass.respond_to?(:i18n_scope) defaults = @klass.lookup_ancestors.map do |klass| klass.model_name.i18n_key end defaults << [:default] if [:default] defaults << @human = { scope: [@klass.i18n_scope, :models], count: 1, default: defaults }.merge!(.except(:default)) I18n.translate(defaults.shift, ) end |