mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
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.
31 lines
735 B
Ruby
31 lines
735 B
Ruby
class Bulb < ActiveRecord::Base
|
|
default_scope where(:name => 'defaulty')
|
|
belongs_to :car
|
|
|
|
attr_protected :car_id, :frickinawesome
|
|
|
|
attr_reader :scope_after_initialize
|
|
|
|
after_initialize :record_scope_after_initialize
|
|
def record_scope_after_initialize
|
|
@scope_after_initialize = self.class.scoped
|
|
end
|
|
|
|
def color=(color)
|
|
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 |