Skip to content
This repository
Newer
Older
100644 175 lines (150 sloc) 6.495 kb
f28bd955 »
2009-04-22 Fix dependencies revealed by testing in isolation
1 require 'active_support/inflector'
75a960ca »
2010-08-12 Don't act destructively on ActiveModel::Name#human options hash. [#5366
2 require 'active_support/core_ext/hash/except'
973b9087 »
2010-10-29 Added missing AS require to active_model/naming.rb
3 require 'active_support/core_ext/module/introspection'
bf812074 »
2011-07-08 Let ActiveModel instances define partial paths.
4 require 'active_support/core_ext/module/deprecation'
72cbccb5 »
2012-03-26 ActiveModel::Name does not inherit from string
5 require 'active_support/core_ext/module/delegation'
2b97b8fb »
2012-03-09 Added missing ActiveModel::Naming dependency.
6 require 'active_support/core_ext/object/blank'
f28bd955 »
2009-04-22 Fix dependencies revealed by testing in isolation
7
1c4d28ba »
2009-06-17 Move model naming into ActiveModel
8 module ActiveModel
72cbccb5 »
2012-03-26 ActiveModel::Name does not inherit from string
9 class Name
10 include Comparable
11
bf8d7574 »
2012-03-10 Remove ActiveModel::Naming#partial_path
12 attr_reader :singular, :plural, :element, :collection,
72cbccb5 »
2012-03-26 ActiveModel::Name does not inherit from string
13 :singular_route_key, :route_key, :param_key, :i18n_key,
14 :name
7280787a »
2011-12-08 Improve cache on route_key lookup.
15
99cf77be »
2009-06-08 Add #element and #collection to ModelName
16 alias_method :cache_key, :collection
566d717d »
2008-06-06 Move Class::ModelName to Active Support module core_ext
17
72cbccb5 »
2012-03-26 ActiveModel::Name does not inherit from string
18 delegate :==, :===, :<=>, :=~, :"!~", :eql?, :to_s,
19 :to_str, :to => :name
dc39af0a »
2011-11-24 make ActiveModel::Name fail gracefully with anonymous classes
20
72cbccb5 »
2012-03-26 ActiveModel::Name does not inherit from string
21 def initialize(klass, namespace = nil, name = nil)
22 @name = name || klass.name
dc39af0a »
2011-11-24 make ActiveModel::Name fail gracefully with anonymous classes
23
72cbccb5 »
2012-03-26 ActiveModel::Name does not inherit from string
24 raise ArgumentError, "Class name cannot be blank. You need to supply a name argument when anonymous class given" if @name.blank?
6e5aed05 »
2010-08-31 Prepared ActiveModel::Naming to handle cases for namespaced isolated …
25
72cbccb5 »
2012-03-26 ActiveModel::Name does not inherit from string
26 @unnamespaced = @name.sub(/^#{namespace.name}::/, '') if namespace
fd86a1b6 »
2011-11-23 Rely on a public contract between railties instead of accessing railt…
27 @klass = klass
72cbccb5 »
2012-03-26 ActiveModel::Name does not inherit from string
28 @singular = _singularize(@name).freeze
fd86a1b6 »
2011-11-23 Rely on a public contract between railties instead of accessing railt…
29 @plural = ActiveSupport::Inflector.pluralize(@singular).freeze
72cbccb5 »
2012-03-26 ActiveModel::Name does not inherit from string
30 @element = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(@name)).freeze
fd86a1b6 »
2011-11-23 Rely on a public contract between railties instead of accessing railt…
31 @human = ActiveSupport::Inflector.humanize(@element).freeze
72cbccb5 »
2012-03-26 ActiveModel::Name does not inherit from string
32 @collection = ActiveSupport::Inflector.tableize(@name).freeze
fd86a1b6 »
2011-11-23 Rely on a public contract between railties instead of accessing railt…
33 @param_key = (namespace ? _singularize(@unnamespaced) : @singular).freeze
72cbccb5 »
2012-03-26 ActiveModel::Name does not inherit from string
34 @i18n_key = @name.underscore.to_sym
7280787a »
2011-12-08 Improve cache on route_key lookup.
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
566d717d »
2008-06-06 Move Class::ModelName to Active Support module core_ext
40 end
e31077c9 »
2009-12-23 Small clean up in Naming and TTranslation tests.
41
42 # Transform the model name into a more humane format, using I18n. By default,
755af497 »
2010-07-30 edit pass to apply API guideline wrt the use of "# =>" in example code
43 # it will underscore then humanize the class name
44 #
45 # BlogPost.model_name.human # => "Blog post"
46 #
e31077c9 »
2009-12-23 Small clean up in Naming and TTranslation tests.
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|
410a4286 »
2011-06-11 Get rid of the alternate namespace lookup.
53 klass.model_name.i18n_key
54 end
e31077c9 »
2009-12-23 Small clean up in Naming and TTranslation tests.
55
75a960ca »
2010-08-12 Don't act destructively on ActiveModel::Name#human options hash. [#5366
56 defaults << options[:default] if options[:default]
e31077c9 »
2009-12-23 Small clean up in Naming and TTranslation tests.
57 defaults << @human
58
75a960ca »
2010-08-12 Don't act destructively on ActiveModel::Name#human options hash. [#5366
59 options = {:scope => [@klass.i18n_scope, :models], :count => 1, :default => defaults}.merge(options.except(:default))
e31077c9 »
2009-12-23 Small clean up in Naming and TTranslation tests.
60 I18n.translate(defaults.shift, options)
61 end
6e5aed05 »
2010-08-31 Prepared ActiveModel::Naming to handle cases for namespaced isolated …
62
63 private
4f90b28e »
2011-03-30 Bring back AMo#i18n_key method
64
65 def _singularize(string, replacement='_')
66 ActiveSupport::Inflector.underscore(string).tr('/', replacement)
67 end
566d717d »
2008-06-06 Move Class::ModelName to Active Support module core_ext
68 end
2572af11 »
2010-06-14 Revised and added headings.
69
de12b5e6 »
2010-06-14 Revised naming.rb and lint.rb
70 # == Active Model Naming
71 #
72 # Creates a +model_name+ method on your object.
b451de0d »
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
73 #
fbc7c2be »
2010-01-17 Adding ActiveModel::Naming documentation
74 # To implement, just extend ActiveModel::Naming in your object:
b451de0d »
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
75 #
fbc7c2be »
2010-01-17 Adding ActiveModel::Naming documentation
76 # class BookCover
9acd6867 »
2010-02-20 Adds #key and #to_param to the AMo interface
77 # extend ActiveModel::Naming
fbc7c2be »
2010-01-17 Adding ActiveModel::Naming documentation
78 # end
b451de0d »
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
79 #
755af497 »
2010-07-30 edit pass to apply API guideline wrt the use of "# =>" in example code
80 # BookCover.model_name # => "BookCover"
81 # BookCover.model_name.human # => "Book cover"
b451de0d »
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
82 #
d834755d »
2011-12-05 ActiveModel::Name#i18n_key: Fix doc and add tests
83 # BookCover.model_name.i18n_key # => :book_cover
84 # BookModule::BookCover.model_name.i18n_key # => :"book_module/book_cover"
4f90b28e »
2011-03-30 Bring back AMo#i18n_key method
85 #
fbc7c2be »
2010-01-17 Adding ActiveModel::Naming documentation
86 # Providing the functionality that ActiveModel::Naming provides in your object
4554983a »
2011-04-26 Removed extra whitespace
87 # is required to pass the Active Model Lint test. So either extending the provided
a906deab »
2011-02-17 fix type: remove extra period
88 # method below, or rolling your own is required.
1c4d28ba »
2009-06-17 Move model naming into ActiveModel
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
9c57bd85 »
2010-09-26 Fix Namind#model_name.
93 @_model_name ||= begin
fd86a1b6 »
2011-11-23 Rely on a public contract between railties instead of accessing railt…
94 namespace = self.parents.detect do |n|
95 n.respond_to?(:use_relative_model_naming?) && n.use_relative_model_naming?
96 end
9c57bd85 »
2010-09-26 Fix Namind#model_name.
97 ActiveModel::Name.new(self, namespace)
98 end
1c4d28ba »
2009-06-17 Move model naming into ActiveModel
99 end
6807b080 »
2010-07-21 Moved a few methods from RecordIdentifier to ActiveModel::Naming
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
bf1ac82c »
2010-09-01 Add some documantation on new route_key and param_key in ActiveModel:…
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:
7280787a »
2011-12-08 Improve cache on route_key lookup.
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:
bf1ac82c »
2010-09-01 Add some documantation on new route_key and param_key in ActiveModel:…
141 # ActiveModel::Naming.route_key(Blog::Post) #=> posts
142 #
143 # For shared engine:
144 # ActiveModel::Naming.route_key(Blog::Post) #=> blog_posts
7280787a »
2011-12-08 Improve cache on route_key lookup.
145 #
146 # The route key also considers if the noun is uncountable and, in
147 # such cases, automatically appends _index.
6e5aed05 »
2010-08-31 Prepared ActiveModel::Naming to handle cases for namespaced isolated …
148 def self.route_key(record_or_class)
149 model_name_from_record_or_class(record_or_class).route_key
150 end
151
bf1ac82c »
2010-09-01 Add some documantation on new route_key and param_key in ActiveModel:…
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:
69a4c67a »
2010-09-03 Fix docs related to param_key in ActiveModel::Naming
156 # ActiveModel::Naming.param_key(Blog::Post) #=> post
bf1ac82c »
2010-09-01 Add some documantation on new route_key and param_key in ActiveModel:…
157 #
158 # For shared engine:
69a4c67a »
2010-09-03 Fix docs related to param_key in ActiveModel::Naming
159 # ActiveModel::Naming.param_key(Blog::Post) #=> blog_post
6e5aed05 »
2010-08-31 Prepared ActiveModel::Naming to handle cases for namespaced isolated …
160 def self.param_key(record_or_class)
161 model_name_from_record_or_class(record_or_class).param_key
162 end
163
6807b080 »
2010-07-21 Moved a few methods from RecordIdentifier to ActiveModel::Naming
164 private
165 def self.model_name_from_record_or_class(record_or_class)
33aaa15f »
2010-10-04 Convert to model before calling model_name on a record in ActiveModel…
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
6807b080 »
2010-07-21 Moved a few methods from RecordIdentifier to ActiveModel::Naming
171 end
566d717d »
2008-06-06 Move Class::ModelName to Active Support module core_ext
172 end
b451de0d »
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
173
566d717d »
2008-06-06 Move Class::ModelName to Active Support module core_ext
174 end
Something went wrong with that request. Please try again.