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') }
|
2012-11-29 18:35:30 -05:00
|
|
|
belongs_to :car, :touch => true
|
2010-08-12 21:37:48 -04:00
|
|
|
|
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
|
2012-07-27 12:27:47 -04:00
|
|
|
@scope_after_initialize = self.class.all
|
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
|
|
|
|
|
2012-09-12 12:35:05 -04:00
|
|
|
def self.new(attributes = {}, &block)
|
2011-05-17 15:37:11 -04:00
|
|
|
bulb_type = (attributes || {}).delete(:bulb_type)
|
|
|
|
|
2012-09-12 12:35:05 -04:00
|
|
|
if bulb_type.present?
|
2011-05-17 15:37:11 -04:00
|
|
|
bulb_class = "#{bulb_type.to_s.camelize}Bulb".constantize
|
2012-09-12 12:35:05 -04:00
|
|
|
bulb_class.new(attributes, &block)
|
2011-05-17 15:37:11 -04:00
|
|
|
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
|
2013-05-13 17:10:23 -04:00
|
|
|
|
|
|
|
class FunkyBulb < Bulb
|
|
|
|
before_destroy do
|
|
|
|
raise "before_destroy was called"
|
|
|
|
end
|
|
|
|
end
|