From 42f1ad68655be08a0ff7e5a261d1ad21d3fc90a5 Mon Sep 17 00:00:00 2001 From: Andrew White Date: Tue, 17 May 2011 20:37:11 +0100 Subject: Pass the attribute and option hashes to build_association The build_association method was added as an API for plugins to hook into in 1398db0. This commit restores this API and the ability to override class.new to return a subclass based on a virtual attribute in the attributes hash. --- activerecord/test/models/bulb.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'activerecord/test/models') diff --git a/activerecord/test/models/bulb.rb b/activerecord/test/models/bulb.rb index 7d90963720..0dcc8d5970 100644 --- a/activerecord/test/models/bulb.rb +++ b/activerecord/test/models/bulb.rb @@ -15,4 +15,17 @@ class Bulb < ActiveRecord::Base self[:color] = color.upcase + "!" end + def self.new(attributes = {}, options = {}, &block) + bulb_type = (attributes || {}).delete(:bulb_type) + + if options && options[:as] == :admin && bulb_type.present? + bulb_class = "#{bulb_type.to_s.camelize}Bulb".constantize + bulb_class.new(attributes, options, &block) + else + super + end + end end + +class CustomBulb < Bulb +end \ No newline at end of file -- cgit v1.2.3