module Mks module Rate class ChargeableService < ApplicationRecord after_save :generate_bundle validates :code, :name, presence: true validates :code, uniqueness: { scope: :service_delivery_unit_id } belongs_to :service_type belongs_to :base_unit, class_name: 'UnitOfCharge' belongs_to :utilization_unit, class_name: 'UnitOfCharge', optional: true belongs_to :service_delivery_unit delegate(:name, to: :service_delivery_unit, prefix: true) delegate(:name, to: :base_unit, prefix: true) delegate(:name, to: :utilization_unit, prefix: true, allow_nil: true) delegate(:name, to: :service_type, prefix: true) def generate_bundle sb = ServiceBundle.create(code: code, name: name, percent_discount: 0, description: "Default bundle for service '#{name}'", service_delivery_unit_id: service_delivery_unit_id) sb.chargeable_services << self end end end end