From 343f594f188c4d41827836fbbd7ea6aff20e8a66 Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Sun, 16 May 2021 17:22:46 +0530 Subject: [PATCH] dynamically set field attributes based on static list --- lib/custom_wizard/field.rb | 79 +++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 35 deletions(-) diff --git a/lib/custom_wizard/field.rb b/lib/custom_wizard/field.rb index a8160b4c..0562b332 100644 --- a/lib/custom_wizard/field.rb +++ b/lib/custom_wizard/field.rb @@ -3,47 +3,56 @@ class CustomWizard::Field include ActiveModel::SerializerSupport - attr_reader :raw, - :id, - :type, - :required, - :value, - :label, - :description, - :image, - :key, - :validations, - :min_length, - :max_length, - :char_counter, - :file_types, - :format, - :limit, - :property, - :content + def self.attributes + %i{ + raw + id + index + type + step + required + value + description + image + key + validations + min_length + max_length + char_counter + file_types + format + limit + property + content + } + end - attr_accessor :index, - :step + def self.accessible_attributes + %i{ + index + step + } + end + + def self.excluded_attributes + %i{ + label + } + end + + attr_reader *(attributes - accessible_attributes - excluded_attributes) + attr_accessor *accessible_attributes def initialize(attrs) + attrs.each do |k, v| + if self.singleton_class.attributes.include?(k.to_sym) + instance_variable_set("@#{k}", v) + end + end + @raw = attrs || {} - @id = attrs[:id] - @index = attrs[:index] - @type = attrs[:type] @required = !!attrs[:required] @value = attrs[:value] || default_value - @description = attrs[:description] - @image = attrs[:image] - @key = attrs[:key] - @validations = attrs[:validations] - @min_length = attrs[:min_length] - @max_length = attrs[:max_length] - @char_counter = attrs[:char_counter] - @file_types = attrs[:file_types] - @format = attrs[:format] - @limit = attrs[:limit] - @property = attrs[:property] - @content = attrs[:content] end def label