Skip to content

HTTPS clone URL

Subversion checkout URL

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