mirror of
https://github.com/github/rails.git
synced 2026-01-31 09:18:17 -05:00
Added ActiveRecord::Base#becomes to turn a record into one of another class (mostly relevant for STIs) [DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8259 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
*SVN*
|
||||
|
||||
* Added ActiveRecord::Base#becomes to turn a record into one of another class (mostly relevant for STIs) [DHH]. Example:
|
||||
|
||||
render :partial => @client.becomes(Company) # renders companies/company instead of clients/client
|
||||
|
||||
* Fixed that to_xml should not automatically pass :procs to associations included with :include #10162 [chuyeow]
|
||||
|
||||
* Fix documentation typo introduced in [8250]. Closes #10339 [Henrik N]
|
||||
|
||||
@@ -1806,6 +1806,20 @@ module ActiveRecord #:nodoc:
|
||||
record
|
||||
end
|
||||
|
||||
# Returns an instance of the specified klass with the attributes of the current record. This is mostly useful in relation to
|
||||
# single-table inheritance structures where you want a subclass to appear as the superclass. This can be used along with record
|
||||
# identification in Action Pack to allow, say, Client < Company to do something like render :partial => @client.becomes(Company)
|
||||
# to render that instance using the companies/company partial instead of clients/client.
|
||||
#
|
||||
# Note: The new instance will share a link to the same attributes as the original class. So any change to the attributes in either
|
||||
# instance will affect the other.
|
||||
def becomes(klass)
|
||||
returning klass.new do |became|
|
||||
became.instance_variable_set("@attributes", @attributes)
|
||||
became.instance_variable_set("@new_record", new_record?)
|
||||
end
|
||||
end
|
||||
|
||||
# Updates a single attribute and saves the record. This is especially useful for boolean flags on existing records.
|
||||
# Note: This method is overwritten by the Validation module that'll make sure that updates made with this method
|
||||
# aren't subjected to validation checks. Hence, attributes can be updated even if the full object isn't valid.
|
||||
|
||||
@@ -1737,4 +1737,9 @@ class BasicsTest < Test::Unit::TestCase
|
||||
assert_equal %("#{t.written_on.to_s(:db)}"), t.attribute_for_inspect(:written_on)
|
||||
assert_equal '"This is some really long content, longer than 50 ch..."', t.attribute_for_inspect(:content)
|
||||
end
|
||||
end
|
||||
|
||||
def test_becomes
|
||||
assert_kind_of Reply, topics(:first).becomes(Reply)
|
||||
assert_equal "The First Topic", topics(:first).becomes(Reply).title
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user