class EmployerSetupPlansForm include ActiveModel::Model include ActiveModel::Attributes PLAN_COLORS = ['atmosphere', 'verdigris', 'cobalt', 'bluemana'] attribute :plans, array: true, default: -> { [Plan.new] } attribute :pl_plan_key, :string attribute :benefit_descs, hash: true, default: -> { Plan.new } attr_accessor :plan_templates attr_accessor :benefits_template attr_accessor :employer_setup_process_id validates :plans, presence: true validates :benefit_descs, presence: true def initialize(employer_setup_process_id, params = {}) @employer_setup_process_id = employer_setup_process_id if params.present? form_params = permitted_params(params) super(form_params) else super(params) process_plans = EmployerSetupProcess.find(@employer_setup_process_id).plans if process_plans.present? self.plans = process_plans end end @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 process_params(employer_setup_process_id) process = EmployerSetupProcess.find(employer_setup_process_id) process.plans 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, :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? process = EmployerSetupProcess.find(@employer_setup_process_id) # process.plans.update(plans: plans) planss = Array.wrap(plans) plans.each_with_index do |plan, i| plan_info = plan.last plan_id = plan_info[:plan_id].present? ? plan_info[:plan_id].to_i : "temp #{i}" plan_info.delete(:plan_id) new_plan = process.plans.create(plan_id: plan_id) plan_info.each do |key, value| sequence = key.delete_prefix("benefit_").to_i benefit = new_plan.plan_benefits.find_by(sequence: sequence) benefit.update( benefit: value ) # Vhcs::HlEgglestonCardBenefit.create( # plan_id: plan_id, # benefit_desc: benefit_descs["#{key}"], # benefit: value, # sequence: sequence, # plan_key: pl_plan_key # ) end end # 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