aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2011-12-08 19:52:26 +0100
committerJosé Valim <jose.valim@gmail.com>2011-12-08 19:53:16 +0100
commit7280787a53436046e992305a235e66e4fb458e0f (patch)
tree43dc2c9511e1b159ff055aa9e1cf624c4843f303 /activemodel/lib/active_model
parent3cae753963f4c7b6c676a20c407a56bb8d98712a (diff)
downloadrails-7280787a53436046e992305a235e66e4fb458e0f.tar.gz
rails-7280787a53436046e992305a235e66e4fb458e0f.tar.bz2
rails-7280787a53436046e992305a235e66e4fb458e0f.zip
Improve cache on route_key lookup.
Diffstat (limited to 'activemodel/lib/active_model')
-rw-r--r--activemodel/lib/active_model/naming.rb25
1 files changed, 23 insertions, 2 deletions
diff --git a/activemodel/lib/active_model/naming.rb b/activemodel/lib/active_model/naming.rb
index 3caa61e41b..755e54efcd 100644
--- a/activemodel/lib/active_model/naming.rb
+++ b/activemodel/lib/active_model/naming.rb
@@ -5,7 +5,9 @@ require 'active_support/core_ext/module/deprecation'
module ActiveModel
class Name < String
- attr_reader :singular, :plural, :element, :collection, :partial_path, :route_key, :param_key, :i18n_key
+ attr_reader :singular, :plural, :element, :collection, :partial_path,
+ :singular_route_key, :route_key, :param_key, :i18n_key
+
alias_method :cache_key, :collection
deprecate :partial_path => "ActiveModel::Name#partial_path is deprecated. Call #to_partial_path on model instances directly instead."
@@ -26,8 +28,12 @@ module ActiveModel
@collection = ActiveSupport::Inflector.tableize(self).freeze
@partial_path = "#{@collection}/#{@element}".freeze
@param_key = (namespace ? _singularize(@unnamespaced) : @singular).freeze
- @route_key = (namespace ? ActiveSupport::Inflector.pluralize(@param_key) : @plural).freeze
@i18n_key = self.underscore.to_sym
+
+ @route_key = (namespace ? ActiveSupport::Inflector.pluralize(@param_key) : @plural.dup)
+ @singular_route_key = ActiveSupport::Inflector.singularize(@route_key).freeze
+ @route_key << "_index" if @plural == @singular
+ @route_key.freeze
end
# Transform the model name into a more humane format, using I18n. By default,
@@ -117,10 +123,25 @@ module ActiveModel
# namespaced models regarding whether it's inside isolated engine.
#
# For isolated engine:
+ # ActiveModel::Naming.route_key(Blog::Post) #=> post
+ #
+ # For shared engine:
+ # ActiveModel::Naming.route_key(Blog::Post) #=> blog_post
+ def self.singular_route_key(record_or_class)
+ model_name_from_record_or_class(record_or_class).singular_route_key
+ end
+
+ # Returns string to use while generating route names. It differs for
+ # namespaced models regarding whether it's inside isolated engine.
+ #
+ # For isolated engine:
# ActiveModel::Naming.route_key(Blog::Post) #=> posts
#
# For shared engine:
# ActiveModel::Naming.route_key(Blog::Post) #=> blog_posts
+ #
+ # The route key also considers if the noun is uncountable and, in
+ # such cases, automatically appends _index.
def self.route_key(record_or_class)
model_name_from_record_or_class(record_or_class).route_key
end