class Trestle::ModelName
Constants
- I18N_PLURAL_COUNT
Matches :other i18n pluralization option for most languages
Attributes
klass[R]
Public Class Methods
new(klass)
click to toggle source
# File lib/trestle/model_name.rb, line 12 def initialize(klass) @klass = klass @name = klass.respond_to?(:model_name) ? klass.model_name : ActiveModel::Name.new(klass) end
Public Instance Methods
==(other)
click to toggle source
# File lib/trestle/model_name.rb, line 17 def ==(other) other.is_a?(self.class) && klass == other.klass end
plural(options={})
click to toggle source
# File lib/trestle/model_name.rb, line 30 def plural(options={}) if i18n_supported? && i18n_pluralizations_available? human(default_plural, { count: I18N_PLURAL_COUNT }.merge(options)) else default_plural end end
Also aliased as: pluralize
singular(options={})
click to toggle source
# File lib/trestle/model_name.rb, line 25 def singular(options={}) human(default_singular, options) end
Also aliased as: singularize
to_s()
click to toggle source
# File lib/trestle/model_name.rb, line 21 def to_s singular end
Protected Instance Methods
default_plural()
click to toggle source
Default plural version if it cannot be determined from i18n
# File lib/trestle/model_name.rb, line 46 def default_plural singular.pluralize(I18n.locale) end
default_singular()
click to toggle source
Default singular version if it cannot be determined from i18n
# File lib/trestle/model_name.rb, line 41 def default_singular @name.name.demodulize.titleize end
human(default, options={})
click to toggle source
Safely delegates to ActiveModel::Name#human, catching exceptions caused by missing pluralizations
# File lib/trestle/model_name.rb, line 51 def human(default, options={}) @name.human(options.merge(default: default)) rescue I18n::InvalidPluralizationData default end
i18n_pluralizations_available?()
click to toggle source
Checks if multiple pluralization forms (e.g. zero/one/few/many/other) are available from i18n
# File lib/trestle/model_name.rb, line 63 def i18n_pluralizations_available? @name.human(count: nil).is_a?(Hash) end
i18n_supported?()
click to toggle source
Checks if the model can be translated by ActiveModel
# File lib/trestle/model_name.rb, line 58 def i18n_supported? klass.respond_to?(:lookup_ancestors) && klass.respond_to?(:i18n_scope) end