class Formular::Builders::Basic

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

Attributes

errors[R]
model[R]
values[R]

Public Class Methods

new(model: nil, path_prefix: nil, errors: nil, values: nil, elements: {}) click to toggle source
Calls superclass method Formular::Builder::new
# 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

Public Instance Methods

collection(name, models: nil, builder: nil, &block) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

these can be called from an element

# File lib/formular/builders/basic.rb, line 56
def path(appendix = nil)
  appendix ? Path[*@path_prefix, appendix] : Path[@path_prefix]
end
reader_value(name) click to toggle source
# File lib/formular/builders/basic.rb, line 60
def reader_value(name)
  model ? model.send(name) : values[name.to_sym]
end