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, values: nil, elements: {}) ⇒ Basic

Returns a new instance of Basic.



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

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

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

#modelObject (readonly)

Returns the value of attribute model.



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

def model
  @model
end

#valuesObject (readonly)

Returns the value of attribute values.



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

def values
  @values
end

Instance Method Details

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



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

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



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

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



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

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

#reader_value(name) ⇒ Object



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

def reader_value(name)
  model ? model.send(name) : values[name.to_sym]
end