Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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