Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 174 lines (149 sloc) 6.396 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
87615f2 update ActiveModel::Naming documentation
Francesco Rodriguez authored
85 # is required to pass the Active Model Lint test. So either extending the
86 # provided 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
87615f2 update ActiveModel::Naming documentation
Francesco Rodriguez authored
89 # used to retrieve all kinds of naming-related information
90 # (See ActiveModel::Name for more information).
1c4d28b @josh Move model naming into ActiveModel
josh authored
91 def model_name
9c57bd8 @thedarkone Fix Namind#model_name.
thedarkone authored
92 @_model_name ||= begin
fd86a1b @josevalim Rely on a public contract between railties instead of accessing railt…
josevalim authored
93 namespace = self.parents.detect do |n|
94 n.respond_to?(:use_relative_model_naming?) && n.use_relative_model_naming?
95 end
9c57bd8 @thedarkone Fix Namind#model_name.
thedarkone authored
96 ActiveModel::Name.new(self, namespace)
97 end
1c4d28b @josh Move model naming into ActiveModel
josh authored
98 end
6807b08 @drogus Moved a few methods from RecordIdentifier to ActiveModel::Naming
drogus authored
99
87615f2 update ActiveModel::Naming documentation
Francesco Rodriguez authored
100 # Returns the plural class name of a record or class.
6807b08 @drogus Moved a few methods from RecordIdentifier to ActiveModel::Naming
drogus authored
101 #
102 # ActiveModel::Naming.plural(post) # => "posts"
103 # ActiveModel::Naming.plural(Highrise::Person) # => "highrise_people"
104 def self.plural(record_or_class)
105 model_name_from_record_or_class(record_or_class).plural
106 end
107
87615f2 update ActiveModel::Naming documentation
Francesco Rodriguez authored
108 # Returns the singular class name of a record or class.
6807b08 @drogus Moved a few methods from RecordIdentifier to ActiveModel::Naming
drogus authored
109 #
110 # ActiveModel::Naming.singular(post) # => "post"
111 # ActiveModel::Naming.singular(Highrise::Person) # => "highrise_person"
112 def self.singular(record_or_class)
113 model_name_from_record_or_class(record_or_class).singular
114 end
115
87615f2 update ActiveModel::Naming documentation
Francesco Rodriguez authored
116 # Identifies whether the class name of a record or class is uncountable.
6807b08 @drogus Moved a few methods from RecordIdentifier to ActiveModel::Naming
drogus authored
117 #
118 # ActiveModel::Naming.uncountable?(Sheep) # => true
87615f2 update ActiveModel::Naming documentation
Francesco Rodriguez authored
119 # ActiveModel::Naming.uncountable?(Post) # => false
6807b08 @drogus Moved a few methods from RecordIdentifier to ActiveModel::Naming
drogus authored
120 def self.uncountable?(record_or_class)
121 plural(record_or_class) == singular(record_or_class)
122 end
123
bf1ac82 @drogus Add some documantation on new route_key and param_key in ActiveModel:…
drogus authored
124 # Returns string to use while generating route names. It differs for
125 # namespaced models regarding whether it's inside isolated engine.
126 #
127 # For isolated engine:
7280787 @josevalim Improve cache on route_key lookup.
josevalim authored
128 # ActiveModel::Naming.route_key(Blog::Post) #=> post
129 #
130 # For shared engine:
131 # ActiveModel::Naming.route_key(Blog::Post) #=> blog_post
132 def self.singular_route_key(record_or_class)
133 model_name_from_record_or_class(record_or_class).singular_route_key
134 end
135
136 # Returns string to use while generating route names. It differs for
137 # namespaced models regarding whether it's inside isolated engine.
138 #
139 # For isolated engine:
bf1ac82 @drogus Add some documantation on new route_key and param_key in ActiveModel:…
drogus authored
140 # ActiveModel::Naming.route_key(Blog::Post) #=> posts
141 #
142 # For shared engine:
143 # ActiveModel::Naming.route_key(Blog::Post) #=> blog_posts
7280787 @josevalim Improve cache on route_key lookup.
josevalim authored
144 #
145 # The route key also considers if the noun is uncountable and, in
146 # such cases, automatically appends _index.
6e5aed0 @drogus Prepared ActiveModel::Naming to handle cases for namespaced isolated …
drogus authored
147 def self.route_key(record_or_class)
148 model_name_from_record_or_class(record_or_class).route_key
149 end
150
bf1ac82 @drogus Add some documantation on new route_key and param_key in ActiveModel:…
drogus authored
151 # Returns string to use for params names. It differs for
152 # namespaced models regarding whether it's inside isolated engine.
153 #
154 # For isolated engine:
69a4c67 @carlosantoniodasilva Fix docs related to param_key in ActiveModel::Naming
carlosantoniodasilva authored
155 # ActiveModel::Naming.param_key(Blog::Post) #=> post
bf1ac82 @drogus Add some documantation on new route_key and param_key in ActiveModel:…
drogus authored
156 #
157 # For shared engine:
69a4c67 @carlosantoniodasilva Fix docs related to param_key in ActiveModel::Naming
carlosantoniodasilva authored
158 # ActiveModel::Naming.param_key(Blog::Post) #=> blog_post
6e5aed0 @drogus Prepared ActiveModel::Naming to handle cases for namespaced isolated …
drogus authored
159 def self.param_key(record_or_class)
160 model_name_from_record_or_class(record_or_class).param_key
161 end
162
6807b08 @drogus Moved a few methods from RecordIdentifier to ActiveModel::Naming
drogus authored
163 private
164 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
165 (record_or_class.is_a?(Class) ? record_or_class : convert_to_model(record_or_class).class).model_name
166 end
167
168 def self.convert_to_model(object)
169 object.respond_to?(:to_model) ? object.to_model : object
6807b08 @drogus Moved a few methods from RecordIdentifier to ActiveModel::Naming
drogus authored
170 end
566d717 @jeremy Move Class::ModelName to Active Support module core_ext
jeremy authored
171 end
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
172
566d717 @jeremy Move Class::ModelName to Active Support module core_ext
jeremy authored
173 end
Something went wrong with that request. Please try again.