Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 95 lines (82 sloc) 3.432 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
4 class Name < String
e31077c @josevalim Small clean up in Naming and TTranslation tests.
josevalim authored
5 attr_reader :singular, :plural, :element, :collection, :partial_path
99cf77b @jeremy Add #element and #collection to ModelName
jeremy authored
6 alias_method :cache_key, :collection
566d717 @jeremy Move Class::ModelName to Active Support module core_ext
jeremy authored
7
e31077c @josevalim Small clean up in Naming and TTranslation tests.
josevalim authored
8 def initialize(klass)
9 super(klass.name)
e714b49 @josevalim Move validator, human_name and human_attribute_name to ActiveModel, r…
josevalim authored
10 @klass = klass
f28bd95 @jeremy Fix dependencies revealed by testing in isolation
jeremy authored
11 @singular = ActiveSupport::Inflector.underscore(self).tr('/', '_').freeze
12 @plural = ActiveSupport::Inflector.pluralize(@singular).freeze
99cf77b @jeremy Add #element and #collection to ModelName
jeremy authored
13 @element = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self)).freeze
e714b49 @josevalim Move validator, human_name and human_attribute_name to ActiveModel, r…
josevalim authored
14 @human = ActiveSupport::Inflector.humanize(@element).freeze
99cf77b @jeremy Add #element and #collection to ModelName
jeremy authored
15 @collection = ActiveSupport::Inflector.tableize(self).freeze
16 @partial_path = "#{@collection}/#{@element}".freeze
566d717 @jeremy Move Class::ModelName to Active Support module core_ext
jeremy authored
17 end
e31077c @josevalim Small clean up in Naming and TTranslation tests.
josevalim authored
18
19 # Transform the model name into a more humane format, using I18n. By default,
755af49 @fxn edit pass to apply API guideline wrt the use of "# =>" in example code
fxn authored
20 # it will underscore then humanize the class name
21 #
22 # BlogPost.model_name.human # => "Blog post"
23 #
e31077c @josevalim Small clean up in Naming and TTranslation tests.
josevalim authored
24 # Specify +options+ with additional translating options.
25 def human(options={})
26 return @human unless @klass.respond_to?(:lookup_ancestors) &&
27 @klass.respond_to?(:i18n_scope)
28
29 defaults = @klass.lookup_ancestors.map do |klass|
30 klass.model_name.underscore.to_sym
31 end
32
33 defaults << options.delete(:default) if options[:default]
34 defaults << @human
35
36 options.reverse_merge! :scope => [@klass.i18n_scope, :models], :count => 1, :default => defaults
37 I18n.translate(defaults.shift, options)
38 end
566d717 @jeremy Move Class::ModelName to Active Support module core_ext
jeremy authored
39 end
2572af1 @rizwanreza Revised and added headings.
rizwanreza authored
40
de12b5e @rizwanreza Revised naming.rb and lint.rb
rizwanreza authored
41 # == Active Model Naming
42 #
43 # Creates a +model_name+ method on your object.
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
44 #
fbc7c2b @mikel Adding ActiveModel::Naming documentation
mikel authored
45 # To implement, just extend ActiveModel::Naming in your object:
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
46 #
fbc7c2b @mikel Adding ActiveModel::Naming documentation
mikel authored
47 # class BookCover
9acd686 @snusnu Adds #key and #to_param to the AMo interface
snusnu authored
48 # extend ActiveModel::Naming
fbc7c2b @mikel Adding ActiveModel::Naming documentation
mikel authored
49 # end
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
50 #
755af49 @fxn edit pass to apply API guideline wrt the use of "# =>" in example code
fxn authored
51 # BookCover.model_name # => "BookCover"
52 # BookCover.model_name.human # => "Book cover"
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
53 #
fbc7c2b @mikel Adding ActiveModel::Naming documentation
mikel authored
54 # Providing the functionality that ActiveModel::Naming provides in your object
f17159b @fxn edit pass: the names of Rails components have a space, ie, "Active Re…
fxn authored
55 # is required to pass the Active Model Lint test. So either extending the provided
fbc7c2b @mikel Adding ActiveModel::Naming documentation
mikel authored
56 # method below, or rolling your own is required..
1c4d28b @josh Move model naming into ActiveModel
josh authored
57 module Naming
58 # Returns an ActiveModel::Name object for module. It can be
59 # used to retrieve all kinds of naming-related information.
60 def model_name
e31077c @josevalim Small clean up in Naming and TTranslation tests.
josevalim authored
61 @_model_name ||= ActiveModel::Name.new(self)
1c4d28b @josh Move model naming into ActiveModel
josh authored
62 end
6807b08 @drogus Moved a few methods from RecordIdentifier to ActiveModel::Naming
drogus authored
63
64 # Returns the plural class name of a record or class. Examples:
65 #
66 # ActiveModel::Naming.plural(post) # => "posts"
67 # ActiveModel::Naming.plural(Highrise::Person) # => "highrise_people"
68 def self.plural(record_or_class)
69 model_name_from_record_or_class(record_or_class).plural
70 end
71
72 # Returns the singular class name of a record or class. Examples:
73 #
74 # ActiveModel::Naming.singular(post) # => "post"
75 # ActiveModel::Naming.singular(Highrise::Person) # => "highrise_person"
76 def self.singular(record_or_class)
77 model_name_from_record_or_class(record_or_class).singular
78 end
79
80 # Identifies whether the class name of a record or class is uncountable. Examples:
81 #
82 # ActiveModel::Naming.uncountable?(Sheep) # => true
83 # ActiveModel::Naming.uncountable?(Post) => false
84 def self.uncountable?(record_or_class)
85 plural(record_or_class) == singular(record_or_class)
86 end
87
88 private
89 def self.model_name_from_record_or_class(record_or_class)
90 (record_or_class.is_a?(Class) ? record_or_class : record_or_class.class).model_name
91 end
566d717 @jeremy Move Class::ModelName to Active Support module core_ext
jeremy authored
92 end
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
93
566d717 @jeremy Move Class::ModelName to Active Support module core_ext
jeremy authored
94 end
Something went wrong with that request. Please try again.