Skip to content

HTTPS clone URL

Subversion checkout URL

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