aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/conversion.rb
diff options
context:
space:
mode:
authorGrant Hutchins & Peter Jaros <pair+grant+pjaros@pivotallabs.com>2011-07-08 17:54:15 -0400
committerXavier Noria <fxn@hashref.com>2011-08-13 16:22:21 -0700
commit6e671a8536f17b6f23b5251652015ce8c1557f1f (patch)
tree98177efb062b64203d0b33122515fd7b72909392 /activemodel/lib/active_model/conversion.rb
parenta636a9358e184bd1a3e2d2522584247be7045cdf (diff)
downloadrails-6e671a8536f17b6f23b5251652015ce8c1557f1f.tar.gz
rails-6e671a8536f17b6f23b5251652015ce8c1557f1f.tar.bz2
rails-6e671a8536f17b6f23b5251652015ce8c1557f1f.zip
Let ActiveModel instances define partial paths.
Deprecate ActiveModel::Name#partial_path. Now you should call #to_path directly on ActiveModel instances.
Diffstat (limited to 'activemodel/lib/active_model/conversion.rb')
-rw-r--r--activemodel/lib/active_model/conversion.rb26
1 files changed, 24 insertions, 2 deletions
diff --git a/activemodel/lib/active_model/conversion.rb b/activemodel/lib/active_model/conversion.rb
index 1405b1bfe3..dca1c1aa44 100644
--- a/activemodel/lib/active_model/conversion.rb
+++ b/activemodel/lib/active_model/conversion.rb
@@ -1,9 +1,12 @@
+require 'active_support/concern'
+require 'active_support/inflector'
+
module ActiveModel
# == Active Model Conversions
#
- # Handles default conversions: to_model, to_key and to_param.
+ # Handles default conversions: to_model, to_key, to_param, and to_path.
#
- # Let's take for example this non persisted object.
+ # Let's take for example this non-persisted object.
#
# class ContactMessage
# include ActiveModel::Conversion
@@ -18,8 +21,11 @@ module ActiveModel
# cm.to_model == self # => true
# cm.to_key # => nil
# cm.to_param # => nil
+ # cm.to_path # => "contact_messages/contact_message"
#
module Conversion
+ extend ActiveSupport::Concern
+
# If your object is already designed to implement all of the Active Model
# you can use the default <tt>:to_model</tt> implementation, which simply
# returns self.
@@ -45,5 +51,21 @@ module ActiveModel
def to_param
persisted? ? to_key.join('-') : nil
end
+
+ # Returns a string identifying the path associated with the object.
+ # ActionPack uses this to find a suitable partial to represent the object.
+ def to_path
+ self.class.to_path
+ end
+
+ module ClassMethods
+ def to_path
+ @_to_path ||= begin
+ element = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self))
+ collection = ActiveSupport::Inflector.tableize(self)
+ "#{collection}/#{element}".freeze
+ end
+ end
+ end
end
end