Class: Formular::Builders::Basic

Inherits:
Formular::Builder show all
Defined in:
lib/formular/builders/basic.rb

Overview

I’m not quite sure why I made this a seperate class But I kind of see myself having Builder as a generic viewbuilder and this basic class as Form

Direct Known Subclasses

Bootstrap3, Bootstrap4, Foundation6

Instance Attribute Summary collapse

Attributes inherited from Formular::Builder

#elements

Instance Method Summary collapse

Methods inherited from Formular::Builder

#call, #capture, define_element_method, define_element_methods, element_set

Constructor Details

#initialize(model: nil, path_prefix: nil, errors: nil, elements: {}) ⇒ Basic

Returns a new instance of Basic.



31
32
33
34
35
36
# File 'lib/formular/builders/basic.rb', line 31

def initialize(model: nil, path_prefix: nil, errors: nil, elements: {})
  @model = model
  @path_prefix = path_prefix
  @errors = errors || (model ? model.errors : {})
  super(elements)
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



37
38
39
# File 'lib/formular/builders/basic.rb', line 37

def errors
  @errors
end

#modelObject (readonly)

Returns the value of attribute model.



37
38
39
# File 'lib/formular/builders/basic.rb', line 37

def model
  @model
end

Instance Method Details

#collection(name, models: nil, builder: nil, &block) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/formular/builders/basic.rb', line 39

def collection(name, models: nil, builder: nil, &block)
  models ||= model ? model.send(name) : []

  models.map.with_index do |model, i|
    nested(name, nested_model: model, path_appendix: [name,i], builder: builder, &block)
  end.join('')
end

#nested(name, nested_model: nil, path_appendix: nil, builder: nil, &block) ⇒ Object



47
48
49
50
51
52
# File 'lib/formular/builders/basic.rb', line 47

def nested(name, nested_model: nil, path_appendix: nil, builder: nil, &block)
  nested_model ||= model.send(name) if model
  path_appendix ||= name
  builder ||= self.class
  builder.new(model: nested_model, path_prefix: path(path_appendix)).(&block)
end

#path(appendix = nil) ⇒ Object

these can be called from an element



55
56
57
# File 'lib/formular/builders/basic.rb', line 55

def path(appendix = nil)
  appendix ? Path[*@path_prefix, appendix] : Path[@path_prefix]
end

#reader_value(name) ⇒ Object



59
60
61
# File 'lib/formular/builders/basic.rb', line 59

def reader_value(name)
  model ? model.send(name) : nil
end