Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 172 lines (147 sloc) 6.478 kB
f28bd95 @jeremy Fix dependencies revealed by testing in isolation
jeremy authored
1 require 'active_support/inflector'
75a960c Don't act destructively on ActiveModel::Name#human options hash. [#53…
John Firebaugh authored
2 require 'active_support/core_ext/hash/except'
973b908 @snusnu Added missing AS require to active_model/naming.rb
snusnu authored
3 require 'active_support/core_ext/module/introspection'
75d58fe @pwim allow requiring of 'active_model/naming'
pwim authored
4 require 'active_support/deprecation'
f28bd95 @jeremy Fix dependencies revealed by testing in isolation
jeremy authored
5
1c4d28b @josh Move model naming into ActiveModel
josh authored
6 module ActiveModel
7 class Name < String
7280787 @josevalim Improve cache on route_key lookup.
josevalim authored
8 attr_reader :singular, :plural, :element, :collection, :partial_path,
9 :singular_route_key, :route_key, :param_key, :i18n_key
10
99cf77b @jeremy Add #element and #collection to ModelName
jeremy authored
11 alias_method :cache_key, :collection
566d717 @jeremy Move Class::ModelName to Active Support module core_ext
jeremy authored
12
dc8773b @josevalim Rename new method to_path to to_partial_path to avoid conflicts with …
josevalim authored
13 deprecate :partial_path => "ActiveModel::Name#partial_path is deprecated. Call #to_partial_path on model instances directly instead."
bf81207 Let ActiveModel instances define partial paths.
Grant Hutchins & Peter Jaros authored
14
e5429b7 @senny Specify the name to be used for ActiveModel::Name fixes #1168
senny authored
15 def initialize(klass, namespace = nil, name = nil)
16 name ||= klass.name
dc39af0 @qoobaa make ActiveModel::Name fail gracefully with anonymous classes
qoobaa authored
17
18 raise ArgumentError, "Class name cannot be blank. You need to supply a name argument when anonymous class given" if name.blank?
19
e5429b7 @senny Specify the name to be used for ActiveModel::Name fixes #1168
senny authored
20 super(name)
6e5aed0 @drogus Prepared ActiveModel::Naming to handle cases for namespaced isolated …
drogus authored
21
fd86a1b @josevalim Rely on a public contract between railties instead of accessing railt…
josevalim authored
22 @unnamespaced = self.sub(/^#{namespace.name}::/, '') if namespace
23 @klass = klass
24 @singular = _singularize(self).freeze
25 @plural = ActiveSupport::Inflector.pluralize(@singular).freeze
26 @element = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self)).freeze
27 @human = ActiveSupport::Inflector.humanize(@element).freeze
28 @collection = ActiveSupport::Inflector.tableize(self).freeze
99cf77b @jeremy Add #element and #collection to ModelName
jeremy authored
29 @partial_path = "#{@collection}/#{@element}".freeze
fd86a1b @josevalim Rely on a public contract between railties instead of accessing railt…
josevalim authored
30 @param_key = (namespace ? _singularize(@unnamespaced) : @singular).freeze
31 @i18n_key = self.underscore.to_sym
7280787 @josevalim Improve cache on route_key lookup.
josevalim authored
32
33 @route_key = (namespace ? ActiveSupport::Inflector.pluralize(@param_key) : @plural.dup)
34 @singular_route_key = ActiveSupport::Inflector.singularize(@route_key).freeze
35 @route_key << "_index" if @plural == @singular
36 @route_key.freeze
566d717 @jeremy Move Class::ModelName to Active Support module core_ext
jeremy authored
37 end
e31077c @josevalim Small clean up in Naming and TTranslation tests.
josevalim authored
38
39 # 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
40 # it will underscore then humanize the class name
41 #
42 # BlogPost.model_name.human # => "Blog post"
43 #
e31077c @josevalim Small clean up in Naming and TTranslation tests.
josevalim authored
44 # Specify +options+ with additional translating options.
45 def human(options={})
46 return @human unless @klass.respond_to?(:lookup_ancestors) &&
47 @klass.respond_to?(:i18n_scope)
48
49 defaults = @klass.lookup_ancestors.map do |klass|
410a428 @josevalim Get rid of the alternate namespace lookup.
josevalim authored
50 klass.model_name.i18n_key
51 end
e31077c @josevalim Small clean up in Naming and TTranslation tests.
josevalim authored
52
75a960c Don't act destructively on ActiveModel::Name#human options hash. [#53…
John Firebaugh authored
53 defaults << options[:default] if options[:default]
e31077c @josevalim Small clean up in Naming and TTranslation tests.
josevalim authored
54 defaults << @human
55
75a960c Don't act destructively on ActiveModel::Name#human options hash. [#53…
John Firebaugh authored
56 options = {:scope => [@klass.i18n_scope, :models], :count => 1, :default => defaults}.merge(options.except(:default))
e31077c @josevalim Small clean up in Naming and TTranslation tests.
josevalim authored
57 I18n.translate(defaults.shift, options)
58 end
6e5aed0 @drogus Prepared ActiveModel::Naming to handle cases for namespaced isolated …
drogus authored
59
60 private
4f90b28 @spastorino Bring back AMo#i18n_key method
spastorino authored
61
62 def _singularize(string, replacement='_')
63 ActiveSupport::Inflector.underscore(string).tr('/', replacement)
64 end
566d717 @jeremy Move Class::ModelName to Active Support module core_ext
jeremy authored
65 end
2572af1 @rizwanreza Revised and added headings.
rizwanreza authored
66
de12b5e @rizwanreza Revised naming.rb and lint.rb
rizwanreza authored
67 # == Active Model Naming
68 #
69 # Creates a +model_name+ method on your object.
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
70 #
fbc7c2b @mikel Adding ActiveModel::Naming documentation
mikel authored
71 # To implement, just extend ActiveModel::Naming in your object:
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
72 #
fbc7c2b @mikel Adding ActiveModel::Naming documentation
mikel authored
73 # class BookCover
9acd686 @snusnu Adds #key and #to_param to the AMo interface
snusnu authored
74 # extend ActiveModel::Naming
fbc7c2b @mikel Adding ActiveModel::Naming documentation
mikel authored
75 # end
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
76 #
755af49 @fxn edit pass to apply API guideline wrt the use of "# =>" in example code
fxn authored
77 # BookCover.model_name # => "BookCover"
78 # BookCover.model_name.human # => "Book cover"
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
79 #
d834755 @marcandre ActiveModel::Name#i18n_key: Fix doc and add tests
marcandre authored
80 # BookCover.model_name.i18n_key # => :book_cover
81 # BookModule::BookCover.model_name.i18n_key # => :"book_module/book_cover"
4f90b28 @spastorino Bring back AMo#i18n_key method
spastorino authored
82 #
fbc7c2b @mikel Adding ActiveModel::Naming documentation
mikel authored
83 # Providing the functionality that ActiveModel::Naming provides in your object
4554983 @smartinez87 Removed extra whitespace
smartinez87 authored
84 # is required to pass the Active Model Lint test. So either extending the provided
a906dea @nixterrimus fix type: remove extra period
nixterrimus authored
85 # method below, or rolling your own is required.
1c4d28b @josh Move model naming into ActiveModel
josh authored
86 module Naming
87 # Returns an ActiveModel::Name object for module. It can be
88 # used to retrieve all kinds of naming-related information.
89 def model_name
9c57bd8 @thedarkone Fix Namind#model_name.
thedarkone authored
90 @_model_name ||= begin
fd86a1b @josevalim Rely on a public contract between railties instead of accessing railt…
josevalim authored
91 namespace = self.parents.detect do |n|
92 n.respond_to?(:use_relative_model_naming?) && n.use_relative_model_naming?
93 end
9c57bd8 @thedarkone Fix Namind#model_name.
thedarkone authored
94 ActiveModel::Name.new(self, namespace)
95 end
1c4d28b @josh Move model naming into ActiveModel
josh authored
96 end
6807b08 @drogus Moved a few methods from RecordIdentifier to ActiveModel::Naming
drogus authored
97
98 # Returns the plural class name of a record or class. Examples:
99 #
100 # ActiveModel::Naming.plural(post) # => "posts"
101 # ActiveModel::Naming.plural(Highrise::Person) # => "highrise_people"
102 def self.plural(record_or_class)
103 model_name_from_record_or_class(record_or_class).plural
104 end
105
106 # Returns the singular class name of a record or class. Examples:
107 #
108 # ActiveModel::Naming.singular(post) # => "post"
109 # ActiveModel::Naming.singular(Highrise::Person) # => "highrise_person"
110 def self.singular(record_or_class)
111 model_name_from_record_or_class(record_or_class).singular
112 end
113
114 # Identifies whether the class name of a record or class is uncountable. Examples:
115 #
116 # ActiveModel::Naming.uncountable?(Sheep) # => true
117 # ActiveModel::Naming.uncountable?(Post) => false
118 def self.uncountable?(record_or_class)
119 plural(record_or_class) == singular(record_or_class)
120 end
121
bf1ac82 @drogus Add some documantation on new route_key and param_key in ActiveModel:…
drogus authored
122 # Returns string to use while generating route names. It differs for
123 # namespaced models regarding whether it's inside isolated engine.
124 #
125 # For isolated engine:
7280787 @josevalim Improve cache on route_key lookup.
josevalim authored
126 # ActiveModel::Naming.route_key(Blog::Post) #=> post
127 #
128 # For shared engine:
129 # ActiveModel::Naming.route_key(Blog::Post) #=> blog_post
130 def self.singular_route_key(record_or_class)
131 model_name_from_record_or_class(record_or_class).singular_route_key
132 end
133
134 # Returns string to use while generating route names. It differs for
135 # namespaced models regarding whether it's inside isolated engine.
136 #
137 # For isolated engine:
bf1ac82 @drogus Add some documantation on new route_key and param_key in ActiveModel:…
drogus authored
138 # ActiveModel::Naming.route_key(Blog::Post) #=> posts
139 #
140 # For shared engine:
141 # ActiveModel::Naming.route_key(Blog::Post) #=> blog_posts
7280787 @josevalim Improve cache on route_key lookup.
josevalim authored
142 #
143 # The route key also considers if the noun is uncountable and, in
144 # such cases, automatically appends _index.
6e5aed0 @drogus Prepared ActiveModel::Naming to handle cases for namespaced isolated …
drogus authored
145 def self.route_key(record_or_class)
146 model_name_from_record_or_class(record_or_class).route_key
147 end
148
bf1ac82 @drogus Add some documantation on new route_key and param_key in ActiveModel:…
drogus authored
149 # Returns string to use for params names. It differs for
150 # namespaced models regarding whether it's inside isolated engine.
151 #
152 # For isolated engine:
69a4c67 @carlosantoniodasilva Fix docs related to param_key in ActiveModel::Naming
carlosantoniodasilva authored
153 # ActiveModel::Naming.param_key(Blog::Post) #=> post
bf1ac82 @drogus Add some documantation on new route_key and param_key in ActiveModel:…
drogus authored
154 #
155 # For shared engine:
69a4c67 @carlosantoniodasilva Fix docs related to param_key in ActiveModel::Naming
carlosantoniodasilva authored
156 # ActiveModel::Naming.param_key(Blog::Post) #=> blog_post
6e5aed0 @drogus Prepared ActiveModel::Naming to handle cases for namespaced isolated …
drogus authored
157 def self.param_key(record_or_class)
158 model_name_from_record_or_class(record_or_class).param_key
159 end
160
6807b08 @drogus Moved a few methods from RecordIdentifier to ActiveModel::Naming
drogus authored
161 private
162 def self.model_name_from_record_or_class(record_or_class)
33aaa15 @ernie Convert to model before calling model_name on a record in ActiveModel…
ernie authored
163 (record_or_class.is_a?(Class) ? record_or_class : convert_to_model(record_or_class).class).model_name
164 end
165
166 def self.convert_to_model(object)
167 object.respond_to?(:to_model) ? object.to_model : object
6807b08 @drogus Moved a few methods from RecordIdentifier to ActiveModel::Naming
drogus authored
168 end
566d717 @jeremy Move Class::ModelName to Active Support module core_ext
jeremy authored
169 end
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
170
566d717 @jeremy Move Class::ModelName to Active Support module core_ext
jeremy authored
171 end
Something went wrong with that request. Please try again.