Sha256: 3dba709cccdc76bfd85c7e8d61d6d54cf4850524de859690f14906012abf1f65

Contents?: true

Size: 1.33 KB

Versions: 5

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

class Bulb < ActiveRecord::Base
  default_scope { where(name: "defaulty") }
  belongs_to :car, touch: true
  scope :awesome, -> { where(frickinawesome: true) }

  attr_reader :scope_after_initialize, :attributes_after_initialize, :count_after_create

  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

  after_create :record_count_after_create
  def record_count_after_create
    @count_after_create = Bulb.unscoped do
      car&.bulbs&.count
    end
  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
    throw(:abort)
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ibm_db-5.5.0 test/models/bulb.rb
ibm_db-5.4.1 test/models/bulb.rb
ibm_db-5.4.0 test/models/bulb.rb
ibm_db-5.3.2 test/models/bulb.rb
ibm_db-5.3.1 test/models/bulb.rb