class EmployerSetupPlansForm include ActiveModel::Model include ActiveModel::Attributes PLAN_COLORS = ['atmosphere', 'copper', 'bluemana', 'bronze', 'cobalt', 'verdigris'] # attribute :plans, array: true, default: [] attribute :plans, array: true, default: -> { [new_plan] } attribute :pl_plan_key, :string attribute :number_of_plans, :integer attribute :benefit_descs, hash: true, default: -> { new_plan } attr_accessor :plan_templates attr_accessor :benefits_template validates :plans, presence: true validates :benefit_descs, presence: true def initialize(params = {}) if params.present? params = permitted_params(params) end super(params) @plan_templates = IdCardBenefitsTemplate.where.not(title: "BLANK") @benefits_template = IdCardBenefitsTemplate.find_by(title: "BLANK").id_card_benefits.sort_by(&:sequence) # if attributes.present? && attributes[:plans].present? # self.plans = Array(attributes[:plans]) # else # self.plans = [new_plan] # end end # def plans_attributes=(attributes) # self.plans = attributes.values.map { |plan_attrs| Plan.new(plan_attrs) } # end def persisted? false end # The core method to initialize the array with a single blank item. def ensure_one_plan_exists if plans.empty? self.plans << PlanForm.new self.plans << PlanForm.new end end def bulild_descs_plan self.benefit_descs = IdCardBenefitsTemplate.find_by(title: "BLANK").id_card_benefits.sort_by(&:sequence) end def self.new_plan plan = {} self.permited_plans_keys.each do |key| plan[key] = "" end plan end def permitted_params(params) params.require(:employer_setup_plans_form).permit( :pl_plan_key, plans: self.permited_plans_keys, benefit_descs: self.permited_plans_keys ) end def pull_from_employer_setup_form(attributes = {}) if attributes['pl_plan_key'] self.pl_plan_key = attributes['pl_plan_key'] end if attributes['number_of_plans'] self.number_of_plans = attributes['number_of_plans'] end end # def form_strong_params(attributes) # attributes.require(:employer_setup_plans_form).permit( # plans: permited_plans_keys, # benefit_descs: benefit_sequence_keys # ) # end def self.permited_plans_keys (1..14).map { |i| "benefit_#{i}".to_sym }.push(:plan_id) end def permited_plans_keys (1..14).map { |i| "benefit_#{i}".to_sym }.push(:plan_id) end def save # Implement logic to save data to models after all steps are complete # For example, create a User record with the collected data if valid? plans.each do |plan| plan_info = plan.last plan_id = plan_info.delete(:plan_id).to_i plan_info.each do |key, value| sequence = key.delete_prefix("benefit_").to_i Vhcs::HlEgglestonCardBenefit.create( plan_id: plan_id, benefit_desc: benefit_descs["#{key}"], benefit: value, sequence: sequence, plan_key: pl_plan_key ) end end true else false end end end class PlanForm include ActiveModel::Model include ActiveModel::Attributes attribute :plan_id, :string def initialize(attributes = {}) self.generate_attributes(14, "benefit_") super(attributes) end def persisted? false end def generate_attributes(count, prefix) count.times do |i| # attr_accessor :"#{prefix}#{i + 1}" self.class.attribute "#{prefix}#{i + 1}".to_sym, :string end end end # dynamic_attribute_names.each do |attr_name| # # Define each attribute as a String type (you can customize the type) # self.class.attribute attr_name.to_sym, :string # end