Class: FieldableForm::Field

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/fieldable_form/field.rb

Direct Known Subclasses

CheckBox, DropDown, TextField

Constant Summary collapse

DESCENDANTS =
%w(FieldableForm::TextField FieldableForm::DropDown FieldableForm::CheckBox)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(*args, &block) ⇒ Object

Create the appropriate children instance according to the type.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fieldable_form/field.rb', line 16

def self.new(*args, &block)
  # Children should use the original method
  return super(*args, &block) if self != ::FieldableForm::Field
  
  attributes = args.first
  raise CannotInitializeAbstractClass.new('FieldableForm::Field is an abstract class') unless attributes.is_a?(Hash)

  type = attributes[:type] || attributes['type']
  return type.constantize.new(*args, &block) if DESCENDANTS.include?(type)
  
  raise CannotInitializeAbstractClass.new('FieldableForm::Field is an abstract class')
end

Instance Method Details

#render_methodObject



29
30
31
# File 'lib/fieldable_form/field.rb', line 29

def render_method
  raise NotImplementedError('Subclass must implement this')
end

#render_optionsObject



33
34
35
# File 'lib/fieldable_form/field.rb', line 33

def render_options
  raise NotImplementedError('Subclass must implement this')
end

#validatorsObject



41
42
43
# File 'lib/fieldable_form/field.rb', line 41

def validators
  raise NotImplementedError('Subclass must implement this')
end

#view_partial_nameObject



37
38
39
# File 'lib/fieldable_form/field.rb', line 37

def view_partial_name
  raise NotImplementedError('Subclass must implement this')
end