Class: HungryForm::Elements::Base::Group

Inherits:
Element
  • Object
show all
Defined in:
lib/hungryform/elements/base/group.rb

Overview

A base group object is used to handle nested elements. Nested element can be a regular BaseElement or BaseActiveElement, as well as a BaseGroup element.

The following sample has three BaseGroup elements (page, group and nested group) that define a structure of a single form page

page :about do

group :about_yourself do
  html :about, value: "Tell us about yourself"
  group :address do
    text_field :street
    text_field :city
  end
  group :contact do
    text_field :phone
  end
end

end

Direct Known Subclasses

Page

Instance Attribute Summary collapse

Attributes inherited from Element

#attributes, #dependency, #label, #name, #placeholders, #resolver, #visible

Instance Method Summary collapse

Methods inherited from Element

#configuration, #dependency_json

Methods included from Hashable

included

Constructor Details

#initialize(name, parent, resolver, attributes = {}, &block) ⇒ Group

Returns a new instance of Group.



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/hungryform/elements/base/group.rb', line 28

def initialize(name, parent, resolver, attributes = {}, &block)
  unless block_given?
    fail HungryFormException, 'No group structure block given'
  end

  super

  @elements = []
  @errors = {}

  instance_eval(&block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/hungryform/elements/base/group.rb', line 71

def method_missing(method_name, *args, &block)
  # Find a matching class
  klass = HungryForm::Elements.find_class(method_name)
  return super if klass.nil?

  # Create a new element based on a symbol provided and push it into the group elements array
  element = HungryForm::Elements.const_get(klass).send(:new, args[0], self, @resolver, *(args[1..-1]), &block)
  elements << element

  # Resolver keeps a hash of all elements of the form
  @resolver.elements[element.name] = element

  element
end

Instance Attribute Details

#elementsObject

Returns the value of attribute elements.



24
25
26
# File 'lib/hungryform/elements/base/group.rb', line 24

def elements
  @elements
end

#errorsObject

Returns the value of attribute errors.



24
25
26
# File 'lib/hungryform/elements/base/group.rb', line 24

def errors
  @errors
end

Instance Method Details

#invalid?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/hungryform/elements/base/group.rb', line 63

def invalid?
  !valid?
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/hungryform/elements/base/group.rb', line 86

def respond_to_missing?(method_name, include_private = false)
  HungryForm::Elements.find_class(method_name) || super
end

#to_hashObject



67
68
69
# File 'lib/hungryform/elements/base/group.rb', line 67

def to_hash
  super.merge(elements: elements.map(&:to_hash))
end

#valid?Boolean

Validates an entire group. If a group consists of nested groups they will be validated recursively

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/hungryform/elements/base/group.rb', line 43

def valid?
  errors.clear
  is_valid = true
  return true unless visible?
  
  elements.each do |el|
    next if !el.respond_to?(:valid?) || el.valid?

    is_valid = false
    case el
    when Base::ActiveElement
      errors[el.name] = el.error
    when Base::Group
      errors.merge!(el.errors)
    end
  end

  is_valid
end