Files
baclight/app/models/id_card/field_exception.rb
T

54 lines
1.7 KiB
Ruby
Raw Normal View History

2026-03-13 08:47:13 -04:00
module IdCard
class FieldException < ApplicationRecord
2026-03-20 10:46:53 -04:00
belongs_to :setup
2026-03-13 08:47:13 -04:00
has_many :field_exception_items, dependent: :destroy
accepts_nested_attributes_for :field_exception_items, allow_destroy: true, reject_if: :all_blank
2026-04-15 08:12:47 -04:00
serialize :exception_values, coder: JSON
2026-05-06 13:28:16 -04:00
validates :exception_type, :exception_values, presence: true
2026-04-15 08:12:47 -04:00
VALID_TYPES = ['family_id', 'zipcode', 'state'].freeze
before_validation :format_exception_values, if: :exception_values_changed?
2026-03-13 08:47:13 -04:00
validates :exception_type, inclusion: { in: VALID_TYPES,
message: "%{value} is not a valid exception type" }
2026-04-15 08:12:47 -04:00
def to_card_attrs
self.field_exception_items.map(&:card_attrs).reduce({}, :merge)
end
2026-03-13 08:47:13 -04:00
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
field_exceptions_attributes: [
:exception_type,
2026-04-15 08:12:47 -04:00
:exception_values,
2026-03-13 08:47:13 -04:00
:_destroy,
field_exception_items_attributes: [
:field_name,
:field_value,
:network_logo_id,
:provider_section_id,
:_destroy
]
]
)
end
end
2026-04-15 08:12:47 -04:00
private
def format_exception_values
if self.exception_values.is_a?(String)
self.exception_values = self.exception_values.split(",").map(&:strip)
end
end
2026-03-13 08:47:13 -04:00
end
end