Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 63 lines (53 sloc) 2.274 kb
f28bd95 @jeremy Fix dependencies revealed by testing in isolation
jeremy authored
1 require 'active_support/inflector'
2
1c4d28b @josh Move model naming into ActiveModel
josh authored
3 module ActiveModel
fbc7c2b @mikel Adding ActiveModel::Naming documentation
mikel authored
4
1c4d28b @josh Move model naming into ActiveModel
josh authored
5 class Name < String
6334006 @spastorino Revert "Refactor of active_model/naming.rb and allow collection and elem...
spastorino authored
6 attr_reader :singular, :plural, :element, :collection, :partial_path
7 alias_method :cache_key, :collection
566d717 @jeremy Move Class::ModelName to Active Support module core_ext
jeremy authored
8
e31077c @josevalim Small clean up in Naming and TTranslation tests.
josevalim authored
9 def initialize(klass)
10 super(klass.name)
e714b49 @josevalim Move validator, human_name and human_attribute_name to ActiveModel, remo...
josevalim authored
11 @klass = klass
f28bd95 @jeremy Fix dependencies revealed by testing in isolation
jeremy authored
12 @singular = ActiveSupport::Inflector.underscore(self).tr('/', '_').freeze
13 @plural = ActiveSupport::Inflector.pluralize(@singular).freeze
6334006 @spastorino Revert "Refactor of active_model/naming.rb and allow collection and elem...
spastorino authored
14 @element = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self)).freeze
e714b49 @josevalim Move validator, human_name and human_attribute_name to ActiveModel, remo...
josevalim authored
15 @human = ActiveSupport::Inflector.humanize(@element).freeze
6334006 @spastorino Revert "Refactor of active_model/naming.rb and allow collection and elem...
spastorino authored
16 @collection = ActiveSupport::Inflector.tableize(self).freeze
17 @partial_path = "#{@collection}/#{@element}".freeze
566d717 @jeremy Move Class::ModelName to Active Support module core_ext
jeremy authored
18 end
e31077c @josevalim Small clean up in Naming and TTranslation tests.
josevalim authored
19
20 # Transform the model name into a more humane format, using I18n. By default,
21 # it will underscore then humanize the class name (BlogPost.model_name.human #=> "Blog post").
22 # Specify +options+ with additional translating options.
23 def human(options={})
24 return @human unless @klass.respond_to?(:lookup_ancestors) &&
25 @klass.respond_to?(:i18n_scope)
26
27 defaults = @klass.lookup_ancestors.map do |klass|
28 klass.model_name.underscore.to_sym
29 end
30
31 defaults << options.delete(:default) if options[:default]
32 defaults << @human
33
34 options.reverse_merge! :scope => [@klass.i18n_scope, :models], :count => 1, :default => defaults
35 I18n.translate(defaults.shift, options)
36 end
566d717 @jeremy Move Class::ModelName to Active Support module core_ext
jeremy authored
37 end
38
fbc7c2b @mikel Adding ActiveModel::Naming documentation
mikel authored
39 # ActiveModel::Naming is a module that creates a +model_name+ method on your
40 # object.
41 #
42 # To implement, just extend ActiveModel::Naming in your object:
43 #
44 # class BookCover
9acd686 @snusnu Adds #key and #to_param to the AMo interface
snusnu authored
45 # extend ActiveModel::Naming
fbc7c2b @mikel Adding ActiveModel::Naming documentation
mikel authored
46 # end
47 #
48 # BookCover.model_name #=> "BookCover"
49 # BookCover.model_name.human #=> "Book cover"
50 #
51 # Providing the functionality that ActiveModel::Naming provides in your object
52 # is required to pass the ActiveModel Lint test. So either extending the provided
53 # method below, or rolling your own is required..
1c4d28b @josh Move model naming into ActiveModel
josh authored
54 module Naming
55 # Returns an ActiveModel::Name object for module. It can be
56 # used to retrieve all kinds of naming-related information.
57 def model_name
e31077c @josevalim Small clean up in Naming and TTranslation tests.
josevalim authored
58 @_model_name ||= ActiveModel::Name.new(self)
1c4d28b @josh Move model naming into ActiveModel
josh authored
59 end
566d717 @jeremy Move Class::ModelName to Active Support module core_ext
jeremy authored
60 end
fbc7c2b @mikel Adding ActiveModel::Naming documentation
mikel authored
61
566d717 @jeremy Move Class::ModelName to Active Support module core_ext
jeremy authored
62 end
Something went wrong with that request. Please try again.