2010-08-12 21:37:48 -04:00
|
|
|
class Bulb < ActiveRecord::Base
|
2012-03-21 18:18:18 -04:00
|
|
|
default_scope { where(:name => 'defaulty') }
|
2010-08-12 21:37:48 -04:00
|
|
|
belongs_to :car
|
|
|
|
|
2011-05-10 18:35:01 -04:00
|
|
|
attr_protected :car_id, :frickinawesome
|
|
|
|
|
2011-06-30 17:41:18 -04:00
|
|
|
attr_reader :scope_after_initialize, :attributes_after_initialize
|
2011-01-23 16:29:36 -05:00
|
|
|
|
2011-04-03 19:07:45 -04:00
|
|
|
after_initialize :record_scope_after_initialize
|
|
|
|
def record_scope_after_initialize
|
|
|
|
@scope_after_initialize = self.class.scoped
|
2011-01-23 16:29:36 -05:00
|
|
|
end
|
|
|
|
|
2011-06-30 17:41:18 -04:00
|
|
|
after_initialize :record_attributes_after_initialize
|
|
|
|
def record_attributes_after_initialize
|
|
|
|
@attributes_after_initialize = attributes.dup
|
|
|
|
end
|
|
|
|
|
2011-05-14 14:40:24 -04:00
|
|
|
def color=(color)
|
|
|
|
self[:color] = color.upcase + "!"
|
|
|
|
end
|
|
|
|
|
2011-05-17 15:37:11 -04:00
|
|
|
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
|
2010-08-12 21:37:48 -04:00
|
|
|
end
|
2011-05-17 15:37:11 -04:00
|
|
|
|
|
|
|
class CustomBulb < Bulb
|
2011-07-24 16:36:06 -04:00
|
|
|
after_initialize :set_awesomeness
|
|
|
|
|
|
|
|
def set_awesomeness
|
|
|
|
self.frickinawesome = true if name == 'Dude'
|
|
|
|
end
|
2011-06-30 17:41:18 -04:00
|
|
|
end
|