Sha256: 494e7adac4d6cc7b98f2a558af510d8f0658293f1ac039a6e5c8d5239930b183

Contents?: true

Size: 1.08 KB

Versions: 10

Compression:

Stored size: 1.08 KB

Contents

class Bulb < ActiveRecord::Base
  default_scope { where(:name => 'defaulty') }
  belongs_to :car, :touch => true

  attr_reader :scope_after_initialize, :attributes_after_initialize

  after_initialize :record_scope_after_initialize
  def record_scope_after_initialize
    @scope_after_initialize = self.class.all
  end

  after_initialize :record_attributes_after_initialize
  def record_attributes_after_initialize
    @attributes_after_initialize = attributes.dup
  end

  def color=(color)
    self[:color] = color.upcase + "!"
  end

  def self.new(attributes = {}, &block)
    bulb_type = (attributes || {}).delete(:bulb_type)

    if bulb_type.present?
      bulb_class = "#{bulb_type.to_s.camelize}Bulb".constantize
      bulb_class.new(attributes, &block)
    else
      super
    end
  end
end

class CustomBulb < Bulb
  after_initialize :set_awesomeness

  def set_awesomeness
    self.frickinawesome = true if name == 'Dude'
  end
end

class FunkyBulb < Bulb
  before_destroy do
    raise "before_destroy was called"
  end
end

class FailedBulb < Bulb
  before_destroy do
    false
  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
ibm_db-3.0.4-x86-mingw32 test/models/bulb.rb
ibm_db-3.0.4 test/models/bulb.rb
ibm_db-3.0.3-x86-mingw32 test/models/bulb.rb
ibm_db-3.0.3 test/models/bulb.rb
ibm_db-3.0.2-x86-mingw32 test/models/bulb.rb
ibm_db-3.0.2 test/models/bulb.rb
activejob-lock-0.0.2 rails/activerecord/test/models/bulb.rb
ibm_db-3.0.1 test/models/bulb.rb
ibm_db-3.0.1-x86-mingw32 test/models/bulb.rb
activejob-lock-0.0.1 rails/activerecord/test/models/bulb.rb