Skip to content
This repository
Newer
Older
100644 151 lines (129 sloc) 5.718 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'
f28bd955 »
2009-04-22 Fix dependencies revealed by testing in isolation
5
1c4d28ba »
2009-06-17 Move model naming into ActiveModel
6 module ActiveModel
7 class Name < String
4f90b28e »
2011-03-30 Bring back AMo#i18n_key method
8 attr_reader :singular, :plural, :element, :collection, :partial_path, :route_key, :param_key, :i18n_key
99cf77be »
2009-06-08 Add #element and #collection to ModelName
9 alias_method :cache_key, :collection
566d717d »
2008-06-06 Move Class::ModelName to Active Support module core_ext
10
dc8773b1 »
2011-08-01 Rename new method to_path to to_partial_path to avoid conflicts with …
11 deprecate :partial_path => "ActiveModel::Name#partial_path is deprecated. Call #to_partial_path on model instances directly instead."
bf812074 »
2011-07-08 Let ActiveModel instances define partial paths.
12
e5429b78 »
2011-05-20 Specify the name to be used for ActiveModel::Name fixes #1168
13 def initialize(klass, namespace = nil, name = nil)
14 name ||= klass.name
dc39af0a »
2011-11-24 make ActiveModel::Name fail gracefully with anonymous classes
15
16 raise ArgumentError, "Class name cannot be blank. You need to supply a name argument when anonymous class given" if name.blank?
17
e5429b78 »
2011-05-20 Specify the name to be used for ActiveModel::Name fixes #1168
18 super(name)
6e5aed05 »
2010-08-31 Prepared ActiveModel::Naming to handle cases for namespaced isolated …
19
fd86a1b6 »
2011-11-23 Rely on a public contract between railties instead of accessing railt…
20 @unnamespaced = self.sub(/^#{namespace.name}::/, '') if namespace
21 @klass = klass
22 @singular = _singularize(self).freeze
23 @plural = ActiveSupport::Inflector.pluralize(@singular).freeze
24 @element = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self)).freeze
25 @human = ActiveSupport::Inflector.humanize(@element).freeze
26 @collection = ActiveSupport::Inflector.tableize(self).freeze
99cf77be »
2009-06-08 Add #element and #collection to ModelName
27 @partial_path = "#{@collection}/#{@element}".freeze
fd86a1b6 »
2011-11-23 Rely on a public contract between railties instead of accessing railt…
28 @param_key = (namespace ? _singularize(@unnamespaced) : @singular).freeze
29 @route_key = (namespace ? ActiveSupport::Inflector.pluralize(@param_key) : @plural).freeze
30 @i18n_key = self.underscore.to_sym
566d717d »
2008-06-06 Move Class::ModelName to Active Support module core_ext
31 end
e31077c9 »
2009-12-23 Small clean up in Naming and TTranslation tests.
32
33 # 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
34 # it will underscore then humanize the class name
35 #
36 # BlogPost.model_name.human # => "Blog post"
37 #
e31077c9 »
2009-12-23 Small clean up in Naming and TTranslation tests.
38 # Specify +options+ with additional translating options.
39 def human(options={})
40 return @human unless @klass.respond_to?(:lookup_ancestors) &&
41 @klass.respond_to?(:i18n_scope)
42
43 defaults = @klass.lookup_ancestors.map do |klass|
410a4286 »
2011-06-11 Get rid of the alternate namespace lookup.
44 klass.model_name.i18n_key
45 end
e31077c9 »
2009-12-23 Small clean up in Naming and TTranslation tests.
46
75a960ca »
2010-08-12 Don't act destructively on ActiveModel::Name#human options hash. [#5366
47 defaults << options[:default] if options[:default]
e31077c9 »
2009-12-23 Small clean up in Naming and TTranslation tests.
48 defaults << @human
49
75a960ca »
2010-08-12 Don't act destructively on ActiveModel::Name#human options hash. [#5366
50 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.
51 I18n.translate(defaults.shift, options)
52 end
6e5aed05 »
2010-08-31 Prepared ActiveModel::Naming to handle cases for namespaced isolated …
53
54 private
4f90b28e »
2011-03-30 Bring back AMo#i18n_key method
55
56 def _singularize(string, replacement='_')
57 ActiveSupport::Inflector.underscore(string).tr('/', replacement)
58 end
566d717d »
2008-06-06 Move Class::ModelName to Active Support module core_ext
59 end
2572af11 »
2010-06-14 Revised and added headings.
60
de12b5e6 »
2010-06-14 Revised naming.rb and lint.rb
61 # == Active Model Naming
62 #
63 # Creates a +model_name+ method on your object.
b451de0d »
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
64 #
fbc7c2be »
2010-01-17 Adding ActiveModel::Naming documentation
65 # To implement, just extend ActiveModel::Naming in your object:
b451de0d »
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
66 #
fbc7c2be »
2010-01-17 Adding ActiveModel::Naming documentation
67 # class BookCover
9acd6867 »
2010-02-20 Adds #key and #to_param to the AMo interface
68 # extend ActiveModel::Naming
fbc7c2be »
2010-01-17 Adding ActiveModel::Naming documentation
69 # end
b451de0d »
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
70 #
755af497 »
2010-07-30 edit pass to apply API guideline wrt the use of "# =>" in example code
71 # BookCover.model_name # => "BookCover"
72 # BookCover.model_name.human # => "Book cover"
b451de0d »
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
73 #
4f90b28e »
2011-03-30 Bring back AMo#i18n_key method
74 # BookCover.model_name.i18n_key # => "book_cover"
75 # BookModule::BookCover.model_name.i18n_key # => "book_module.book_cover"
76 #
fbc7c2be »
2010-01-17 Adding ActiveModel::Naming documentation
77 # Providing the functionality that ActiveModel::Naming provides in your object
4554983a »
2011-04-26 Removed extra whitespace
78 # is required to pass the Active Model Lint test. So either extending the provided
a906deab »
2011-02-17 fix type: remove extra period
79 # method below, or rolling your own is required.
1c4d28ba »
2009-06-17 Move model naming into ActiveModel
80 module Naming
81 # Returns an ActiveModel::Name object for module. It can be
82 # used to retrieve all kinds of naming-related information.
83 def model_name
9c57bd85 »
2010-09-26 Fix Namind#model_name.
84 @_model_name ||= begin
fd86a1b6 »
2011-11-23 Rely on a public contract between railties instead of accessing railt…
85 namespace = self.parents.detect do |n|
86 n.respond_to?(:use_relative_model_naming?) && n.use_relative_model_naming?
87 end
9c57bd85 »
2010-09-26 Fix Namind#model_name.
88 ActiveModel::Name.new(self, namespace)
89 end
1c4d28ba »
2009-06-17 Move model naming into ActiveModel
90 end
6807b080 »
2010-07-21 Moved a few methods from RecordIdentifier to ActiveModel::Naming
91
92 # Returns the plural class name of a record or class. Examples:
93 #
94 # ActiveModel::Naming.plural(post) # => "posts"
95 # ActiveModel::Naming.plural(Highrise::Person) # => "highrise_people"
96 def self.plural(record_or_class)
97 model_name_from_record_or_class(record_or_class).plural
98 end
99
100 # Returns the singular class name of a record or class. Examples:
101 #
102 # ActiveModel::Naming.singular(post) # => "post"
103 # ActiveModel::Naming.singular(Highrise::Person) # => "highrise_person"
104 def self.singular(record_or_class)
105 model_name_from_record_or_class(record_or_class).singular
106 end
107
108 # Identifies whether the class name of a record or class is uncountable. Examples:
109 #
110 # ActiveModel::Naming.uncountable?(Sheep) # => true
111 # ActiveModel::Naming.uncountable?(Post) => false
112 def self.uncountable?(record_or_class)
113 plural(record_or_class) == singular(record_or_class)
114 end
115
bf1ac82c »
2010-09-01 Add some documantation on new route_key and param_key in ActiveModel:…
116 # Returns string to use while generating route names. It differs for
117 # namespaced models regarding whether it's inside isolated engine.
118 #
119 # For isolated engine:
120 # ActiveModel::Naming.route_key(Blog::Post) #=> posts
121 #
122 # For shared engine:
123 # ActiveModel::Naming.route_key(Blog::Post) #=> blog_posts
6e5aed05 »
2010-08-31 Prepared ActiveModel::Naming to handle cases for namespaced isolated …
124 def self.route_key(record_or_class)
125 model_name_from_record_or_class(record_or_class).route_key
126 end
127
bf1ac82c »
2010-09-01 Add some documantation on new route_key and param_key in ActiveModel:…
128 # Returns string to use for params names. It differs for
129 # namespaced models regarding whether it's inside isolated engine.
130 #
131 # For isolated engine:
69a4c67a »
2010-09-03 Fix docs related to param_key in ActiveModel::Naming
132 # ActiveModel::Naming.param_key(Blog::Post) #=> post
bf1ac82c »
2010-09-01 Add some documantation on new route_key and param_key in ActiveModel:…
133 #
134 # For shared engine:
69a4c67a »
2010-09-03 Fix docs related to param_key in ActiveModel::Naming
135 # ActiveModel::Naming.param_key(Blog::Post) #=> blog_post
6e5aed05 »
2010-08-31 Prepared ActiveModel::Naming to handle cases for namespaced isolated …
136 def self.param_key(record_or_class)
137 model_name_from_record_or_class(record_or_class).param_key
138 end
139
6807b080 »
2010-07-21 Moved a few methods from RecordIdentifier to ActiveModel::Naming
140 private
141 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…
142 (record_or_class.is_a?(Class) ? record_or_class : convert_to_model(record_or_class).class).model_name
143 end
144
145 def self.convert_to_model(object)
146 object.respond_to?(:to_model) ? object.to_model : object
6807b080 »
2010-07-21 Moved a few methods from RecordIdentifier to ActiveModel::Naming
147 end
566d717d »
2008-06-06 Move Class::ModelName to Active Support module core_ext
148 end
b451de0d »
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
149
566d717d »
2008-06-06 Move Class::ModelName to Active Support module core_ext
150 end
Something went wrong with that request. Please try again.