Class: Avo::Fields::FieldManager

Inherits:
Object
  • Object
show all
Defined in:
lib/avo/fields/field_manager.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFieldManager

Returns a new instance of FieldManager.



14
15
16
# File 'lib/avo/fields/field_manager.rb', line 14

def initialize
  @fields = []
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



12
13
14
# File 'lib/avo/fields/field_manager.rb', line 12

def fields
  @fields
end

Class Method Details

.buildObject



5
6
7
8
9
# File 'lib/avo/fields/field_manager.rb', line 5

def build
  instance = new
  instance.init_fields
  instance
end

Instance Method Details

#allObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/avo/fields/field_manager.rb', line 18

def all
  fields
    .map do |field|
      field[:name] = field[:name].to_s

      field
    end
    .uniq do |field|
      field[:name]
    end
end

#init_fieldsObject

This method will find all fields available in the Avo::Fields namespace and add them to the fields class_variable array so later we can instantiate them on our resources.

If the field has their ‘def_method` set up it will follow that convention, if not it will snake_case the name:

Avo::Fields::TextField -> text Avo::Fields::DateTimeField -> date_time



37
38
39
40
41
42
43
44
45
# File 'lib/avo/fields/field_manager.rb', line 37

def init_fields
  Avo::Fields::BaseField.descendants.each do |class_name|
    next if class_name.to_s == "BaseField"

    if class_name.to_s.end_with? "Field"
      load_field class_name.get_field_name, class_name
    end
  end
end

#load_field(name, klass) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/avo/fields/field_manager.rb', line 47

def load_field(name, klass)
  return if field_exists?(name)

  fields.push(
    name: name.to_s,
    class: klass
  )
end