diff options
author | Hongli Lai (Phusion) <hongli@phusion.nl> | 2008-09-06 19:43:14 +0200 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2008-09-09 13:13:12 -0700 |
commit | 1398db0128be7ae01700712eafc95be5de430f7c (patch) | |
tree | cecf4f2ef0fd7c2747826fd22c56ba8f596ed1c4 /activerecord/lib/active_record/associations/association_collection.rb | |
parent | 16929404417205792165153958ce5effa0b54645 (diff) | |
download | rails-1398db0128be7ae01700712eafc95be5de430f7c.tar.gz rails-1398db0128be7ae01700712eafc95be5de430f7c.tar.bz2 rails-1398db0128be7ae01700712eafc95be5de430f7c.zip |
Add special AssociationReflection methods for creating association objects, and modify the code base to use those methods instead of creating association objects directly. This allows plugins to hook into association object creation behavior.
[#986 state:resolved]
Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
Diffstat (limited to 'activerecord/lib/active_record/associations/association_collection.rb')
-rw-r--r-- | activerecord/lib/active_record/associations/association_collection.rb | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 168443e092..d4bb43970f 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -110,7 +110,7 @@ module ActiveRecord @owner.transaction do flatten_deeper(records).each do |record| - record = @reflection.klass.new(record) if @reflection.options[:accessible] && record.is_a?(Hash) + record = @reflection.build_association(record) if @reflection.options[:accessible] && record.is_a?(Hash) raise_on_type_mismatch(record) add_record_to_target_with_callbacks(record) do |r| @@ -287,7 +287,7 @@ module ActiveRecord # This will perform a diff and delete/add only records that have changed. def replace(other_array) other_array.map! do |val| - val.is_a?(Hash) ? @reflection.klass.new(val) : val + val.is_a?(Hash) ? @reflection.build_association(val) : val end if @reflection.options[:accessible] other_array.each { |val| raise_on_type_mismatch(val) } @@ -377,7 +377,9 @@ module ActiveRecord def create_record(attrs) attrs.update(@reflection.options[:conditions]) if @reflection.options[:conditions].is_a?(Hash) ensure_owner_is_not_new - record = @reflection.klass.send(:with_scope, :create => construct_scope[:create]) { @reflection.klass.new(attrs) } + record = @reflection.klass.send(:with_scope, :create => construct_scope[:create]) do + @reflection.build_association(attrs) + end if block_given? add_record_to_target_with_callbacks(record) { |*block_args| yield(*block_args) } else @@ -387,7 +389,7 @@ module ActiveRecord def build_record(attrs) attrs.update(@reflection.options[:conditions]) if @reflection.options[:conditions].is_a?(Hash) - record = @reflection.klass.new(attrs) + record = @reflection.build_association(attrs) if block_given? add_record_to_target_with_callbacks(record) { |*block_args| yield(*block_args) } else |