Class: CmAdmin::Models::FormField

Inherits:
Object
  • Object
show all
Defined in:
lib/cm_admin/models/form_field.rb

Constant Summary collapse

VALID_INPUT_TYPES =
[:integer, :decimal, :string, :single_select, :multi_select, :date, :date_time, :text, :single_file_upload, :multi_file_upload, :hidden, :rich_text].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field_name, input_type, attributes = {}) ⇒ FormField

Returns a new instance of FormField.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
# File 'lib/cm_admin/models/form_field.rb', line 7

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
  self.display_if = lambda { |arg| return true } if self.display_if.nil?
  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

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



4
5
6
# File 'lib/cm_admin/models/form_field.rb', line 4

def collection
  @collection
end

#disabledObject

Returns the value of attribute disabled.



4
5
6
# File 'lib/cm_admin/models/form_field.rb', line 4

def disabled
  @disabled
end

#display_ifObject

Returns the value of attribute display_if.



4
5
6
# File 'lib/cm_admin/models/form_field.rb', line 4

def display_if
  @display_if
end

#field_nameObject

Returns the value of attribute field_name.



4
5
6
# File 'lib/cm_admin/models/form_field.rb', line 4

def field_name
  @field_name
end

#headerObject

Returns the value of attribute header.



4
5
6
# File 'lib/cm_admin/models/form_field.rb', line 4

def header
  @header
end

#helper_methodObject

Returns the value of attribute helper_method.



4
5
6
# File 'lib/cm_admin/models/form_field.rb', line 4

def helper_method
  @helper_method
end

#html_attrObject

Returns the value of attribute html_attr.



4
5
6
# File 'lib/cm_admin/models/form_field.rb', line 4

def html_attr
  @html_attr
end

#input_typeObject

Returns the value of attribute input_type.



4
5
6
# File 'lib/cm_admin/models/form_field.rb', line 4

def input_type
  @input_type
end

#labelObject

Returns the value of attribute label.



4
5
6
# File 'lib/cm_admin/models/form_field.rb', line 4

def label
  @label
end

#placeholderObject

Returns the value of attribute placeholder.



4
5
6
# File 'lib/cm_admin/models/form_field.rb', line 4

def placeholder
  @placeholder
end

Instance Method Details

#set_default_valuesObject



17
18
19
20
21
22
23
# File 'lib/cm_admin/models/form_field.rb', line 17

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('_', ' ')}"
  self.html_attr = {}
end