diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2012-11-29 08:45:31 -0700 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2012-11-29 08:56:09 -0700 |
commit | f2902eb8e0c382545b383cc57d46450fa3fa483f (patch) | |
tree | b96e1feae0376c0b26b647fb1bd9958d90834495 /activerecord/lib/active_record/persistence.rb | |
parent | 7ad590d25f9a4b1040cf663b8387626d03ffcf87 (diff) | |
download | rails-f2902eb8e0c382545b383cc57d46450fa3fa483f.tar.gz rails-f2902eb8e0c382545b383cc57d46450fa3fa483f.tar.bz2 rails-f2902eb8e0c382545b383cc57d46450fa3fa483f.zip |
Move instantiation responsibilities from Inheritance to Persistence. Have Inheritance#discriminate_class_for_record handle STI lookup duties.
Diffstat (limited to 'activerecord/lib/active_record/persistence.rb')
-rw-r--r-- | activerecord/lib/active_record/persistence.rb | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index 81e56a3e2e..94c109e72b 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -38,6 +38,32 @@ module ActiveRecord object end end + + # Given an attributes hash, +instantiate+ returns a new instance of + # the appropriate class. + # + # For example, +Post.all+ may return Comments, Messages, and Emails + # by storing the record's subclass in a +type+ attribute. By calling + # +instantiate+ instead of +new+, finder methods ensure they get new + # instances of the appropriate class for each record. + # + # See +ActiveRecord::Inheritance#discriminate_class_for_record+ to see + # how this "single-table" inheritance mapping is implemented. + def instantiate(record, column_types = {}) + klass = discriminate_class_for_record(record) + column_types = klass.decorate_columns(column_types) + klass.allocate.init_with('attributes' => record, 'column_types' => column_types) + end + + private + # Called by +instantiate+ to decide which class to use for a new + # record instance. + # + # See +ActiveRecord::Inheritance#discriminate_class_for_record+ for + # the single-table inheritance discriminator. + def discriminate_class_for_record(record) + self + end end # Returns true if this object hasn't been saved yet -- that is, a record |