Class: SimpleFormWithClientValidation::Wrappers::Many

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_form_with_client_validation/wrappers/many.rb

Overview

A wrapper is an object that holds several components and render them. A component may either be a symbol or any object that responds to ‘render`. This API allows inputs/components to be easily wrapped, removing the need to modify the code only to wrap input in an extra tag.

‘Many` represents a wrapper around several components at the same time. It may optionally receive a namespace, allowing it to be configured on demand on input generation. The find function returns if it matches namespace

Direct Known Subclasses

Root, Single

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace, components, defaults = {}) ⇒ Many

Returns a new instance of Many.



16
17
18
19
20
21
22
# File 'lib/simple_form_with_client_validation/wrappers/many.rb', line 16

def initialize(namespace, components, defaults={})
  @namespace  = namespace
  @components = components
  @defaults   = defaults
  @defaults[:tag]   = :div unless @defaults.key?(:tag)
  @defaults[:class] = Array.wrap(@defaults[:class])
end

Instance Attribute Details

#componentsObject (readonly)

Returns the value of attribute components.



13
14
15
# File 'lib/simple_form_with_client_validation/wrappers/many.rb', line 13

def components
  @components
end

#defaultsObject (readonly)

Returns the value of attribute defaults.



13
14
15
# File 'lib/simple_form_with_client_validation/wrappers/many.rb', line 13

def defaults
  @defaults
end

#namespaceObject (readonly) Also known as: to_sym

Returns the value of attribute namespace.



13
14
15
# File 'lib/simple_form_with_client_validation/wrappers/many.rb', line 13

def namespace
  @namespace
end

Instance Method Details

#find(name) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/simple_form_with_client_validation/wrappers/many.rb', line 37

def find(name)
  return self if namespace == name

  @components.each do |c|
    if c.is_a?(Symbol)
      return nil if c == namespace
    elsif value = c.find(name)
      return value
    end
  end

  nil
end

#render(input) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/simple_form_with_client_validation/wrappers/many.rb', line 24

def render(input)
  content = "".html_safe
  options = input.options

  components.each do |component|
    next if options[component] == false
    rendered = component.respond_to?(:render) ? component.render(input) : input.send(component)
    content.safe_concat rendered.to_s if rendered
  end

  wrap(input, options, content)
end