Employer form mostly working with persist to db
This commit is contained in:
@@ -2,25 +2,31 @@ class EmployerSetupPlansForm
|
||||
include ActiveModel::Model
|
||||
include ActiveModel::Attributes
|
||||
|
||||
PLAN_COLORS = ['atmosphere', 'copper', 'bluemana', 'bronze', 'cobalt', 'verdigris']
|
||||
PLAN_COLORS = ['atmosphere', 'verdigris', 'cobalt', 'bluemana']
|
||||
|
||||
# attribute :plans, array: true, default: []
|
||||
attribute :plans, array: true, default: -> { [new_plan] }
|
||||
attribute :plans, array: true, default: -> { [Plan.new] }
|
||||
attribute :pl_plan_key, :string
|
||||
attribute :number_of_plans, :integer
|
||||
attribute :benefit_descs, hash: true, default: -> { new_plan }
|
||||
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(params = {})
|
||||
def initialize(employer_setup_process_id, params = {})
|
||||
@employer_setup_process_id = employer_setup_process_id
|
||||
if params.present?
|
||||
params = permitted_params(params)
|
||||
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
|
||||
super(params)
|
||||
|
||||
@plan_templates = IdCardBenefitsTemplate.where.not(title: "BLANK")
|
||||
@benefits_template = IdCardBenefitsTemplate.find_by(title: "BLANK").id_card_benefits.sort_by(&:sequence)
|
||||
@@ -61,6 +67,11 @@ class EmployerSetupPlansForm
|
||||
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,
|
||||
@@ -90,27 +101,54 @@ class EmployerSetupPlansForm
|
||||
end
|
||||
|
||||
def permited_plans_keys
|
||||
(1..14).map { |i| "benefit_#{i}".to_sym }.push(:plan_id)
|
||||
(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?
|
||||
plans.each do |plan|
|
||||
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.delete(:plan_id).to_i
|
||||
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
|
||||
Vhcs::HlEgglestonCardBenefit.create(
|
||||
plan_id: plan_id,
|
||||
benefit_desc: benefit_descs["#{key}"],
|
||||
benefit: value,
|
||||
sequence: sequence,
|
||||
plan_key: pl_plan_key
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user