From 098c16cd47bbacd4ff28d88c2dc7464d58f3945b Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Tue, 20 Jul 2021 19:37:46 +0530 Subject: [PATCH] built step attributes dynamically --- controllers/custom_wizard/admin/wizard.rb | 15 ++--- lib/custom_wizard/step.rb | 72 +++++++++++++++++------ 2 files changed, 58 insertions(+), 29 deletions(-) diff --git a/controllers/custom_wizard/admin/wizard.rb b/controllers/custom_wizard/admin/wizard.rb index 2f2efe9a..433e3c1d 100644 --- a/controllers/custom_wizard/admin/wizard.rb +++ b/controllers/custom_wizard/admin/wizard.rb @@ -82,17 +82,10 @@ class CustomWizard::AdminWizardController < CustomWizard::AdminController :theme_id, permitted: mapped_params, steps: [ - :id, - :index, - :title, - :key, - :banner, - :raw_description, - :required_data_message, - :force_final, - required_data: mapped_params, - permitted_params: mapped_params, - condition: mapped_params, + *CustomWizard::Step.type_attributes(:permitted), + CustomWizard::Step.type_attributes(:mapped).map do |attribute| + [attribute, mapped_params] + end.to_h , fields: [ *CustomWizard::Field.type_attributes(:permitted), CustomWizard::Field.type_attributes(:mapped).map do |attribute| diff --git a/lib/custom_wizard/step.rb b/lib/custom_wizard/step.rb index 5ffd8024..ee7755e2 100644 --- a/lib/custom_wizard/step.rb +++ b/lib/custom_wizard/step.rb @@ -3,25 +3,61 @@ class CustomWizard::Step include ActiveModel::SerializerSupport - attr_reader :id, - :updater + def self.attribute_map + { + id: [:serializable, :permitted], + updater: [], + index: [:accessible, :serializable, :permitted], + title: [:accessible, :serializable, :permitted], + description: [:accessible, :serializable], + key: [:accessible, :permitted], + permitted: [:accessible, :serializable], + permitted_message: [:accessible, :serializable], + fields: [:accessible], + next: [:accessible, :serializable], + previous: [:accessible, :serializable], + banner: [:accessible, :serializable, :permitted], + disabled: [:accessible], + description_vars: [:accessible], + last_step: [:accessible], + force_final: [:accessible], + conditional_final_step: [:accessible], + wizard: [:accessible], + raw_description: [:permitted], + required_data_message: [:permitted], + required_data: [:permitted, :mapped], + permitted_params: [:permitted, :mapped], + condition: [:permitted, :mapped], + final: [:serializable] + } + end - attr_accessor :index, - :title, - :description, - :key, - :permitted, - :permitted_message, - :fields, - :next, - :previous, - :banner, - :disabled, - :description_vars, - :last_step, - :force_final, - :conditional_final_step, - :wizard + def self.type_attributes(type) + attribute_map.map { |attribute, props| props.include?(type.to_sym) ? attribute : nil }.compact + end + + def self.all_attributes + attribute_map.keys + end + + def self.accessible_attributes + type_attributes(:accessible) + end + + def self.included_attributes + all_attributes - excluded_attributes + end + + def self.readonly_attributes + included_attributes - accessible_attributes + end + + def self.excluded_attributes + type_attributes(:excluded) + end + + attr_reader *readonly_attributes + attr_accessor *accessible_attributes def initialize(id) @id = id