1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

update ActiveModel::Conversion documentation [ci skip]

This commit is contained in:
Francesco Rodriguez 2012-06-22 10:44:53 -05:00
parent 35ee8fa3d8
commit d9238d044a

View file

@ -18,11 +18,10 @@ module ActiveModel
# end # end
# #
# cm = ContactMessage.new # cm = ContactMessage.new
# cm.to_model == self # => true # cm.to_model == cm # => true
# cm.to_key # => nil # cm.to_key # => nil
# cm.to_param # => nil # cm.to_param # => nil
# cm.to_partial_path # => "contact_messages/contact_message" # cm.to_partial_path # => "contact_messages/contact_message"
#
module Conversion module Conversion
extend ActiveSupport::Concern extend ActiveSupport::Concern
@ -30,6 +29,13 @@ module ActiveModel
# you can use the default <tt>:to_model</tt> implementation, which simply # you can use the default <tt>:to_model</tt> implementation, which simply
# returns self. # returns self.
# #
# class Person
# include ActiveModel::Conversion
# end
#
# person = Person.new
# person.to_model == person # => true
#
# If your model does not act like an Active Model object, then you should # If your model does not act like an Active Model object, then you should
# define <tt>:to_model</tt> yourself returning a proxy object that wraps # define <tt>:to_model</tt> yourself returning a proxy object that wraps
# your object with Active Model compliant methods. # your object with Active Model compliant methods.
@ -37,21 +43,28 @@ module ActiveModel
self self
end end
# Returns an Enumerable of all key attributes if any is set, regardless # Returns an Enumerable of all key attributes if any is set, regardless if
# if the object is persisted or not. # the object is persisted or not. If there no key attributes, returns +nil+.
def to_key def to_key
key = respond_to?(:id) && id key = respond_to?(:id) && id
key ? [key] : nil key ? [key] : nil
end end
# Returns a string representing the object's key suitable for use in URLs, # Returns a string representing the object's key suitable for use in URLs,
# or nil if <tt>persisted?</tt> is false. # or +nil+ if <tt>persisted?</tt> is false.
def to_param def to_param
persisted? ? to_key.join('-') : nil persisted? ? to_key.join('-') : nil
end end
# Returns a string identifying the path associated with the object. # Returns a string identifying the path associated with the object.
# ActionPack uses this to find a suitable partial to represent the object. # ActionPack uses this to find a suitable partial to represent the object.
#
# class Person
# include ActiveModel::Conversion
# end
#
# person = Person.new
# person.to_partial_path # => "people/person"
def to_partial_path def to_partial_path
self.class._to_partial_path self.class._to_partial_path
end end