Sha256: 30f7232a0ce39462be2b8ebbcd46f303784ccd149c0269a8bb9c3b312083b359

Contents?: true

Size: 1.06 KB

Versions: 3

Compression:

Stored size: 1.06 KB

Contents

module CmAdmin
  module Models
    class FormField
      attr_accessor :field_name, :label, :header, :input_type, :collection, :disabled, :helper_method, :placeholder
      VALID_INPUT_TYPES = [:integer, :decimal, :string, :single_select, :multi_select, :date, :date_time, :text, :single_file_upload, :multi_file_upload, :hidden, :rich_text].freeze

      def initialize(field_name, input_type, attributes = {})
        @field_name = field_name
        set_default_values
        attributes.each do |key, value|
          self.send("#{key.to_s}=", value)
        end
        raise ArgumentError, "Kindly select a valid input type like #{VALID_INPUT_TYPES.sort.to_sentence(last_word_connector: ', or ')} instead of #{self.input_type} for form field #{field_name}" unless VALID_INPUT_TYPES.include?(self.input_type.to_sym)
      end

      def set_default_values
        self.disabled = false
        self.label = self.field_name.to_s.titleize
        self.input_type = :string
        self.placeholder = "Enter #{self.field_name.to_s.downcase.gsub('_', ' ')}"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cm-admin-0.8.5 lib/cm_admin/models/form_field.rb
cm-admin-0.8.4 lib/cm_admin/models/form_field.rb
cm-admin-0.8.3 lib/cm_admin/models/form_field.rb