Skip to content

HTTPS clone URL

Subversion checkout URL

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