aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/models/bulb.rb
blob: 0dcc8d59705fb4fb3043f5e6b7c9b78afe741e15 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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