Files

87 lines
3.2 KiB
Ruby
Raw Permalink Normal View History

module IdCard
class Plan < ApplicationRecord
2026-03-20 10:46:53 -04:00
belongs_to :setup, optional: true
has_many :plan_benefits, dependent: :destroy
accepts_nested_attributes_for :plan_benefits, allow_destroy: true, reject_if: :all_blank
2026-06-17 23:23:36 -04:00
has_many :members, class_name: 'Member', foreign_key: :id_card_plan_id
2026-05-06 13:28:16 -04:00
validates :title, presence: true
validates :pb_product_key, :pl_plan_key, presence: true, if: -> { setup&.active }
validates :pb_product_key, uniqueness: true, allow_nil: true
validate :validate_plan_benefits_count, unless: -> { template }
2026-03-13 08:47:13 -04:00
scope :templates, -> { where(template: true) }
2026-04-17 15:35:10 -04:00
FARIOS_BENEFIT_FIELDS = ["Primary Visit", "Specialist Visit", "Urgent Care", "INN-Ind Ded", "INN-Family Ded", "OON-Ind Ded", "OON-Family Ded", "Co-Insurance", "INN-Ind OOP", "INN-Family OOP", "OON-Ind OOP", "OON-Family OOP", "Emergency Room", "Preventive Care"].freeze
TANDEMLOC_BENEFIT_FIELDS = ["Physician Visit", "Specialist Visit", "Urgent Care", "Deductible", "Co-Insurance", "Out-of-Pocket", "Emergency Room", "Preventive Care"].freeze
SMART_BENEFIT_FIELDS = [].freeze
2026-03-13 08:47:13 -04:00
after_initialize :build_plan_benefits, if: :new_record?
def build_plan_benefits
2026-04-17 15:35:10 -04:00
if plan_benefits.empty?
plan_benefit_fields = case setup&.card_template
when "TandemlocIDCard"
TANDEMLOC_BENEFIT_FIELDS
when "SmartIDCard"
SMART_BENEFIT_FIELDS
else
FARIOS_BENEFIT_FIELDS
end
plan_benefit_fields.each_with_index do |bene, i|
self.plan_benefits.build(benefit_desc: bene, sequence: (i + 1))
end
2026-03-13 08:47:13 -04:00
end
end
def format_template
formatted_title = title
if pl_plan_key.present?
employer_name = Employer.find_by(pl_plan_key: pl_plan_key).pluck(:name)
formatted_title.concat( " (#{employer_name})" )
end
{ id: id, title: formatted_title }
end
class << self
def permitted_params(params)
2026-03-20 10:46:53 -04:00
params.require(:id_card_setup).permit(
2026-03-13 08:47:13 -04:00
plans_attributes: [
2026-03-19 00:42:27 -04:00
:id,
2026-03-13 08:47:13 -04:00
:title,
:pb_product_key,
:pl_plan_key,
:_destroy,
2026-03-13 08:47:13 -04:00
plan_benefits_attributes: [
:id,
:benefit_desc,
:benefit,
:sequence,
:_destroy,
]
]
)
end
2026-03-13 08:47:13 -04:00
end
private
2026-05-06 13:28:16 -04:00
def validate_plan_benefits_count
plan_benefits_size = plan_benefits.size
template_benefits_size = case setup&.card_template
when "TandemlocIDCard"
TANDEMLOC_BENEFIT_FIELDS.size
when "SmartIDCard"
SMART_BENEFIT_FIELDS.size
else
FARIOS_BENEFIT_FIELDS.size
end
unless plan_benefits_size == template_benefits_size
errors.add(:base, "Plan Benefits size (#{plan_benefits_size}) does not match ID Card Template (#{template_benefits_size})")
end
end
2026-05-06 13:28:16 -04:00
end
end