2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2010-08-12 21:37:48 -04:00
|
|
|
class Bulb < ActiveRecord::Base
|
2016-08-06 13:37:57 -04:00
|
|
|
default_scope { where(name: "defaulty") }
|
|
|
|
belongs_to :car, touch: true
|
2015-11-15 09:56:52 -05:00
|
|
|
scope :awesome, -> { where(frickinawesome: true) }
|
2010-08-12 21:37:48 -04:00
|
|
|
|
2020-03-21 10:10:34 -04:00
|
|
|
attr_reader :scope_after_initialize, :attributes_after_initialize, :count_after_create
|
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
|
2020-05-07 05:30:33 -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
|
|
|
|
|
2020-03-21 10:10:34 -04:00
|
|
|
after_create :record_count_after_create
|
|
|
|
def record_count_after_create
|
|
|
|
@count_after_create = Bulb.unscoped do
|
|
|
|
car&.bulbs&.count
|
|
|
|
end
|
|
|
|
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
|
2016-08-06 12:26:20 -04:00
|
|
|
self.frickinawesome = true if name == "Dude"
|
2011-07-24 16:36:06 -04:00
|
|
|
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
|
2013-11-23 06:24:52 -05:00
|
|
|
|
|
|
|
class FailedBulb < Bulb
|
|
|
|
before_destroy do
|
2014-12-15 01:10:15 -05:00
|
|
|
throw(:abort)
|
2013-11-23 06:24:52 -05:00
|
|
|
end
|
|
|
|
end
|