module IdCard class FieldException < ApplicationRecord belongs_to :setup has_many :field_exception_items, dependent: :destroy accepts_nested_attributes_for :field_exception_items, allow_destroy: true, reject_if: :all_blank serialize :exception_values, coder: JSON validates :exception_type, :exception_values, presence: true VALID_TYPES = ['family_id', 'zipcode', 'state'].freeze before_validation :format_exception_values, if: :exception_values_changed? validates :exception_type, inclusion: { in: VALID_TYPES, message: "%{value} is not a valid exception type" } def to_card_attrs self.field_exception_items.map(&:card_attrs).reduce({}, :merge) end class << self def permitted_params(params) params.require(:id_card_setup).permit( field_exceptions_attributes: [ :exception_type, :exception_values, :_destroy, field_exception_items_attributes: [ :field_name, :field_value, :network_logo_id, :provider_section_id, :_destroy ] ] ) end end private def format_exception_values if self.exception_values.is_a?(String) self.exception_values = self.exception_values.split(",").map(&:strip) end end end end