Class: ActiveModel::Name
- Includes:
- Comparable
- Defined in:
- activemodel/lib/active_model/naming.rb
Instance Attribute Summary collapse
-
#collection ⇒ Object
(also: #cache_key)
Returns the value of attribute collection.
-
#element ⇒ Object
Returns the value of attribute element.
-
#i18n_key ⇒ Object
Returns the value of attribute i18n_key.
-
#name ⇒ Object
Returns the value of attribute name.
-
#param_key ⇒ Object
Returns the value of attribute param_key.
-
#plural ⇒ Object
Returns the value of attribute plural.
-
#route_key ⇒ Object
Returns the value of attribute route_key.
-
#singular ⇒ Object
Returns the value of attribute singular.
-
#singular_route_key ⇒ Object
Returns the value of attribute singular_route_key.
Instance Method Summary collapse
-
#human(options = {}) ⇒ Object
Transform the model name into a more human format, using I18n.
-
#initialize(klass, namespace = nil, name = nil, locale = :en) ⇒ Name
constructor
Returns a new ActiveModel::Name instance.
- #uncountable? ⇒ Boolean
Constructor Details
#initialize(klass, namespace = nil, name = nil, locale = :en) ⇒ 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. Use locale
argument for singularize and pluralize model name.
module Foo
class Bar
end
end
ActiveModel::Name.new(Foo::Bar).to_s
# => "Foo::Bar"
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'activemodel/lib/active_model/naming.rb', line 166 def initialize(klass, namespace = nil, name = nil, locale = :en) @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.delete_prefix("#{namespace.name}::") if namespace @klass = klass @singular = _singularize(@name) @plural = ActiveSupport::Inflector.pluralize(@singular, locale) @uncountable = @plural == @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, locale) : @plural.dup) @singular_route_key = ActiveSupport::Inflector.singularize(@route_key, locale) @route_key << "_index" if @uncountable end |
Instance Attribute Details
#collection ⇒ Object Also known as: cache_key
Returns the value of attribute collection
12 13 14 |
# File 'activemodel/lib/active_model/naming.rb', line 12 def collection @collection end |
#element ⇒ Object
Returns the value of attribute element
12 13 14 |
# File 'activemodel/lib/active_model/naming.rb', line 12 def element @element end |
#i18n_key ⇒ Object
Returns the value of attribute i18n_key
12 13 14 |
# File 'activemodel/lib/active_model/naming.rb', line 12 def i18n_key @i18n_key end |
#name ⇒ Object
Returns the value of attribute name
12 13 14 |
# File 'activemodel/lib/active_model/naming.rb', line 12 def name @name end |
#param_key ⇒ Object
Returns the value of attribute param_key
12 13 14 |
# File 'activemodel/lib/active_model/naming.rb', line 12 def param_key @param_key end |
#plural ⇒ Object
Returns the value of attribute plural
12 13 14 |
# File 'activemodel/lib/active_model/naming.rb', line 12 def plural @plural end |
#route_key ⇒ Object
Returns the value of attribute route_key
12 13 14 |
# File 'activemodel/lib/active_model/naming.rb', line 12 def route_key @route_key end |
#singular ⇒ Object
Returns the value of attribute singular
12 13 14 |
# File 'activemodel/lib/active_model/naming.rb', line 12 def singular @singular end |
#singular_route_key ⇒ Object
Returns the value of attribute singular_route_key
12 13 14 |
# File 'activemodel/lib/active_model/naming.rb', line 12 def singular_route_key @singular_route_key end |
Instance Method Details
#human(options = {}) ⇒ Object
Transform the model name into a more human 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.
197 198 199 200 201 202 203 204 205 206 207 |
# File 'activemodel/lib/active_model/naming.rb', line 197 def human( = {}) return @human if i18n_keys.empty? || i18n_scope.empty? key, *defaults = i18n_keys defaults << [:default] if [:default] defaults << MISSING_TRANSLATION translation = I18n.translate(key, scope: i18n_scope, count: 1, **, default: defaults) translation = @human if translation == MISSING_TRANSLATION translation end |
#uncountable? ⇒ Boolean
209 210 211 |
# File 'activemodel/lib/active_model/naming.rb', line 209 def uncountable? @uncountable end |