Class: HungryForm::Form

Inherits:
Object
  • Object
show all
Defined in:
lib/hungryform/form.rb

Overview

HungryForm is an object that manages form creation and validation. A sample object could look like this:

form = HungryForm::Form.new :params => params do

page :about_yourself do
  text_field :first_name, :required => true
  text_field :last_name, :required => true
  checkbox :dog, label: "Do you have a dog?"
end
page :about_your_dog, visible: '{"SET": "about_yourself_dog"}' do
  text_field :name, :required
  text_area :what_can_it_do, label: "What can it do?"
end

end

A form must contain only pages. Whenever a specific form error occurres inside the form it raises a HungryFormException

When a new instance of a HungryForm::Form is created, it uses attributes to build a structure of itself. The pages with dependencies, that resolve during this process will be included in the form.pages array. Pages without dependencies will be allways resolved. The rest of the pages will be ignored.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}, &block) ⇒ Form

Returns a new instance of Form.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/hungryform/form.rb', line 38

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

  @resolver = Resolver.new(attributes.slice(:params))
  @pages = []

  instance_eval(&block)

  if @resolver.params[:page]
    @current_page = pages.find { |p| p.name.to_s == @resolver.params[:page] }
  end

  @current_page ||= pages.first
end

Instance Attribute Details

#current_pageObject (readonly)

Returns the value of attribute current_page.



36
37
38
# File 'lib/hungryform/form.rb', line 36

def current_page
  @current_page
end

#pagesObject (readonly)

Returns the value of attribute pages.



36
37
38
# File 'lib/hungryform/form.rb', line 36

def pages
  @pages
end

Instance Method Details

#elementsObject

Get all the elements the form consists of



83
84
85
# File 'lib/hungryform/form.rb', line 83

def elements
  @resolver.elements
end

#invalid?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/hungryform/form.rb', line 78

def invalid?
  !validate
end

#next_pageObject

Get the next page of the form



109
110
111
112
113
# File 'lib/hungryform/form.rb', line 109

def next_page
  pages.each_cons(2) do |page, next_page|
    return next_page if page == current_page
  end
end

#page(name, attributes = {}, &block) ⇒ Object

Create a new page



56
57
58
59
# File 'lib/hungryform/form.rb', line 56

def page(name, attributes = {}, &block)
  page = Elements::Page.new(name, nil, @resolver, attributes, &block)
  pages << page if page.visible?
end

#prev_pageObject

Get the previous page of the form



116
117
118
119
120
# File 'lib/hungryform/form.rb', line 116

def prev_page
  pages.each_cons(2) do |prev_page, page|
    return prev_page if page == current_page
  end
end

#to_hashObject

Get an entire hash of the form, including every element on every visible page



89
90
91
# File 'lib/hungryform/form.rb', line 89

def to_hash
  { pages: pages.map(&:to_hash) }
end

#to_jsonObject



93
94
95
# File 'lib/hungryform/form.rb', line 93

def to_json
  JSON.generate(to_hash)
end

#valid?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/hungryform/form.rb', line 74

def valid?
  validate
end

#validateObject

Entire form validation. Loops through the form pages and validates each page individually.



63
64
65
66
67
68
69
70
71
72
# File 'lib/hungryform/form.rb', line 63

def validate
  is_valid = true

  pages.each do |page|
    # Loop through pages to get all errors
    is_valid = false if page.invalid?
  end

  is_valid
end

#valuesObject

Create a hash of form elements values



98
99
100
101
102
103
104
105
106
# File 'lib/hungryform/form.rb', line 98

def values
  active_elements = elements.select do |name, el|
    el.is_a? Elements::Base::ActiveElement
  end

  active_elements.each_with_object({}) do |(name, el), o| 
    o[name.to_sym] = el.value 
  end
end