hungryform 0.0.2 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +1 -0
- data/Gemfile.lock +9 -1
- data/README.md +48 -12
- data/Rakefile +3 -1
- data/hungryform.gemspec +3 -0
- data/lib/hungryform/elements/base/active_element.rb +53 -0
- data/lib/hungryform/elements/base/element.rb +57 -0
- data/lib/hungryform/elements/base/group.rb +89 -0
- data/lib/hungryform/elements/base/hashable.rb +39 -0
- data/lib/hungryform/elements/base/options_element.rb +35 -0
- data/lib/hungryform/elements/group.rb +5 -3
- data/lib/hungryform/elements/html.rb +12 -3
- data/lib/hungryform/elements/page.rb +28 -24
- data/lib/hungryform/elements/radio_group.rb +17 -0
- data/lib/hungryform/elements/select_field.rb +33 -0
- data/lib/hungryform/elements/text_area_field.rb +6 -0
- data/lib/hungryform/elements/text_field.rb +5 -3
- data/lib/hungryform/elements.rb +13 -5
- data/lib/hungryform/form.rb +86 -0
- data/lib/hungryform/resolver.rb +30 -25
- data/lib/hungryform/validator.rb +8 -5
- data/lib/hungryform/version.rb +2 -2
- data/lib/hungryform.rb +8 -77
- data/spec/elements/group_spec.rb +1 -1
- data/spec/elements/html_spec.rb +5 -6
- data/spec/elements/page_spec.rb +4 -4
- data/spec/elements/radio_group_spec.rb +29 -0
- data/spec/elements/select_field_spec.rb +47 -0
- data/spec/elements/text_area_field_spec.rb +7 -0
- data/spec/elements/text_field_spec.rb +2 -2
- data/spec/{hungryform_spec.rb → form_spec.rb} +9 -8
- data/spec/resolver_spec.rb +1 -1
- data/spec/support/shared_active_element.rb +9 -1
- data/spec/support/shared_element.rb +9 -1
- data/spec/support/shared_group.rb +8 -2
- data/spec/support/shared_options_element.rb +36 -0
- data/spec/validator_spec.rb +2 -2
- metadata +38 -9
- data/lib/hungryform/elements/base_active_element.rb +0 -44
- data/lib/hungryform/elements/base_element.rb +0 -34
- data/lib/hungryform/elements/base_group.rb +0 -76
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'active_support/core_ext/string/inflections'
|
3
|
+
require 'active_support/core_ext/class/attribute'
|
4
|
+
require 'hungryform/version'
|
5
|
+
require 'hungryform/resolver'
|
6
|
+
require 'hungryform/validator'
|
7
|
+
require 'hungryform/elements'
|
8
|
+
|
9
|
+
module HungryForm
|
10
|
+
# HungryForm is an object that manages form creation and validation.
|
11
|
+
# A sample object could look like this:
|
12
|
+
#
|
13
|
+
# form = HungryForm::Form.new :params => params do
|
14
|
+
# page :about_yourself do
|
15
|
+
# text_field :first_name, :required => true
|
16
|
+
# text_field :last_name, :required => true
|
17
|
+
# checkbox :dog, label: "Do you have a dog?"
|
18
|
+
# end
|
19
|
+
# page :about_your_dog, visible: '{"SET": "about_yourself_dog"}' do
|
20
|
+
# text_field :name, :required
|
21
|
+
# text_area :what_can_it_do, label: "What can it do?"
|
22
|
+
# end
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
# A form must contain only pages.
|
26
|
+
# Whenever a specific form error occurres inside the form it
|
27
|
+
# raises a HungryFormException
|
28
|
+
#
|
29
|
+
# When a new instance of a HungryForm::Form is created, it uses
|
30
|
+
# attributes[:params] to build a structure of itself. The pages
|
31
|
+
# with dependencies, that resolve during this process will be included
|
32
|
+
# in the form.pages array. Pages without dependencies will be allways
|
33
|
+
# resolved. The rest of the pages will be ignored.
|
34
|
+
class Form
|
35
|
+
attr_reader :pages
|
36
|
+
|
37
|
+
def initialize(attributes = {}, &block)
|
38
|
+
unless block_given?
|
39
|
+
fail HungryFormException, 'No form structure block given'
|
40
|
+
end
|
41
|
+
|
42
|
+
@resolver = Resolver.new(attributes.slice(:params))
|
43
|
+
@pages = []
|
44
|
+
|
45
|
+
instance_eval(&block)
|
46
|
+
end
|
47
|
+
|
48
|
+
# Create a form page
|
49
|
+
def page(name, attributes = {}, &block)
|
50
|
+
page = Elements::Page.new(name, nil, @resolver, attributes, &block)
|
51
|
+
pages << page if page.visible?
|
52
|
+
end
|
53
|
+
|
54
|
+
# Entire form validation. Loops through the form pages and
|
55
|
+
# validates each page individually.
|
56
|
+
def valid?
|
57
|
+
is_valid = true
|
58
|
+
|
59
|
+
pages.each do |page|
|
60
|
+
# Loop through pages to get all errors
|
61
|
+
is_valid = false if page.invalid?
|
62
|
+
end
|
63
|
+
|
64
|
+
is_valid
|
65
|
+
end
|
66
|
+
|
67
|
+
def invalid?
|
68
|
+
!valid?
|
69
|
+
end
|
70
|
+
|
71
|
+
# Get all the elements that the form consists of
|
72
|
+
def elements
|
73
|
+
@resolver.elements
|
74
|
+
end
|
75
|
+
|
76
|
+
# Get an entire hash of the form, including every element
|
77
|
+
# on every visible page
|
78
|
+
def to_hash
|
79
|
+
{ pages: pages.map(&:to_hash) }
|
80
|
+
end
|
81
|
+
|
82
|
+
def to_json
|
83
|
+
JSON.generate(to_hash)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
data/lib/hungryform/resolver.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
module HungryForm
|
2
2
|
# The class is responsible for dependency resolving.
|
3
3
|
# It contains all form elements and params
|
4
4
|
class Resolver
|
@@ -9,7 +9,7 @@ class HungryForm
|
|
9
9
|
@elements = {}
|
10
10
|
end
|
11
11
|
|
12
|
-
# Gets element value by element's name.
|
12
|
+
# Gets element value by element's name.
|
13
13
|
# If name is lambda - returns lambda's result
|
14
14
|
# If name is present in the resolvers' elements hash - returns element's value
|
15
15
|
# If name is present in the resolvers' params hash - returns params value
|
@@ -17,18 +17,17 @@ class HungryForm
|
|
17
17
|
def get_value(name, element = nil)
|
18
18
|
return name.call(element) if name.respond_to? :call
|
19
19
|
|
20
|
+
# We don't want to mess up elements names
|
20
21
|
name = name.to_s.dup
|
21
|
-
|
22
|
+
|
22
23
|
# Apply placeholders to the name.
|
23
24
|
# A sample name string can look like this: page1_group[GROUP_NUMBER]_field
|
24
25
|
# where [GROUP_NUMBER] is a placeholder. When an element is present
|
25
26
|
# we get its placeholders and replace substrings in the name argument
|
26
|
-
if element
|
27
|
-
element.placeholders.each { |k, v| name[k] &&= v }
|
28
|
-
end
|
27
|
+
element.placeholders.each { |k, v| name[k] &&= v } if element
|
29
28
|
|
30
|
-
return @elements[name].value if @elements.
|
31
|
-
return @params[name] if @params.
|
29
|
+
return @elements[name].value if @elements.key?(name)
|
30
|
+
return @params[name] if @params.key?(name)
|
32
31
|
|
33
32
|
name
|
34
33
|
end
|
@@ -39,29 +38,17 @@ class HungryForm
|
|
39
38
|
dependency.each do |operator, arguments|
|
40
39
|
case operator
|
41
40
|
when 'AND'
|
42
|
-
|
43
|
-
|
44
|
-
arguments.each do |argument|
|
45
|
-
return false unless resolve_dependency(argument)
|
46
|
-
end
|
47
|
-
|
48
|
-
return true
|
41
|
+
return resolve_multi_dependency(:and, arguments)
|
49
42
|
when 'OR'
|
50
|
-
|
51
|
-
|
52
|
-
arguments.each do |argument|
|
53
|
-
return true if resolve_dependency(argument)
|
54
|
-
end
|
55
|
-
|
56
|
-
return false
|
43
|
+
return resolve_multi_dependency(:or, arguments)
|
57
44
|
when 'NOT'
|
58
45
|
return !resolve_dependency(arguments)
|
59
46
|
end
|
60
47
|
|
61
48
|
arguments = [arguments] unless arguments.is_a?(Array)
|
62
49
|
|
63
|
-
arguments = arguments[0..1].map {|name| get_value(name)}
|
64
|
-
return false if arguments.any?
|
50
|
+
arguments = arguments[0..1].map { |name| get_value(name) }
|
51
|
+
return false if arguments.any?(&:nil?)
|
65
52
|
|
66
53
|
case operator
|
67
54
|
when 'EQ'
|
@@ -75,5 +62,23 @@ class HungryForm
|
|
75
62
|
end
|
76
63
|
end
|
77
64
|
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
# Helper method to resolve AND or OR conditions.
|
69
|
+
# Walks through the arguments and resolves their dependencies.
|
70
|
+
def resolve_multi_dependency(type, arguments)
|
71
|
+
if arguments.size == 0
|
72
|
+
fail HungryFormException, "No arguments for #{type.upcase} comparison: #{arguments}"
|
73
|
+
end
|
74
|
+
|
75
|
+
result = type == :and
|
76
|
+
|
77
|
+
arguments.each do |argument|
|
78
|
+
return !result unless resolve_dependency(argument)
|
79
|
+
end
|
80
|
+
|
81
|
+
result
|
82
|
+
end
|
78
83
|
end
|
79
|
-
end
|
84
|
+
end
|
data/lib/hungryform/validator.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module HungryForm
|
2
|
+
module Validator
|
3
3
|
class << self
|
4
4
|
# Check if the element's value is not empty.
|
5
5
|
# The rule argument can be a boolean or a callable object
|
@@ -7,16 +7,19 @@ class HungryForm
|
|
7
7
|
if rule.respond_to? :call
|
8
8
|
rule.call(element)
|
9
9
|
else
|
10
|
-
|
10
|
+
'is required' if element.value.to_s.empty? && rule
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
14
|
# Custom validation check
|
15
15
|
# Use when you need to create a custom validation in the structure
|
16
16
|
def validation(element, callable)
|
17
|
-
|
17
|
+
unless callable.respond_to? :call
|
18
|
+
fail HungryFormError 'Validation must respond to call'
|
19
|
+
end
|
20
|
+
|
18
21
|
callable.call(element)
|
19
22
|
end
|
20
23
|
end
|
21
24
|
end
|
22
|
-
end
|
25
|
+
end
|
data/lib/hungryform/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
|
2
|
-
VERSION = "0.0.
|
1
|
+
module HungryForm
|
2
|
+
VERSION = "0.0.4"
|
3
3
|
end
|
data/lib/hungryform.rb
CHANGED
@@ -1,81 +1,12 @@
|
|
1
1
|
require 'json'
|
2
2
|
require 'active_support/core_ext/string/inflections'
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
# form = HungryForm.new :params => params do
|
12
|
-
# page :about_yourself do
|
13
|
-
# text_field :first_name, :required => true
|
14
|
-
# text_field :last_name, :required => true
|
15
|
-
# checkbox :dog, label: "Do you have a dog?"
|
16
|
-
# end
|
17
|
-
# page :about_your_dog, visible: '{"SET": "about_yourself_dog"}' do
|
18
|
-
# text_field :name, :required
|
19
|
-
# text_area :what_can_it_do, label: "What can it do?"
|
20
|
-
# end
|
21
|
-
# end
|
22
|
-
#
|
23
|
-
# A form must contain only pages.
|
24
|
-
# Whenever a specific form error occurres inside the form it raises a HungryFormException
|
25
|
-
#
|
26
|
-
# When a new instance of a HungryForm is created, it uses options[:params] to
|
27
|
-
# build a structure of itself. The pages with dependencies, that resolve during this
|
28
|
-
# process will be included in the form.pages array. Pages without dependencies will be allways resolved.
|
29
|
-
# The rest of the pages will be ignored
|
30
|
-
class HungryForm
|
3
|
+
require 'active_support/core_ext/class/attribute'
|
4
|
+
require 'hungryform/version'
|
5
|
+
require 'hungryform/form'
|
6
|
+
require 'hungryform/resolver'
|
7
|
+
require 'hungryform/validator'
|
8
|
+
require 'hungryform/elements'
|
9
|
+
|
10
|
+
module HungryForm
|
31
11
|
HungryFormException = Class.new(StandardError)
|
32
|
-
|
33
|
-
attr_reader :pages
|
34
|
-
|
35
|
-
def initialize(options = {}, &block)
|
36
|
-
raise HungryFormException, 'No form structure block given' unless block_given?
|
37
|
-
|
38
|
-
@resolver = Resolver.new(options.slice(:params))
|
39
|
-
@pages = []
|
40
|
-
|
41
|
-
instance_eval(&block)
|
42
|
-
end
|
43
|
-
|
44
|
-
# Create a form page
|
45
|
-
def page(name, options = {}, &block)
|
46
|
-
page = Page.new(name, nil, @resolver, options, &block)
|
47
|
-
pages << page if page.visible?
|
48
|
-
end
|
49
|
-
|
50
|
-
# Entire form validation. Loops through the form pages and validates each page
|
51
|
-
def valid?
|
52
|
-
is_valid = true
|
53
|
-
|
54
|
-
pages.each do |page|
|
55
|
-
#Loop through pages to get all errors
|
56
|
-
is_valid = false if page.invalid?
|
57
|
-
end
|
58
|
-
|
59
|
-
is_valid
|
60
|
-
end
|
61
|
-
|
62
|
-
def invalid?
|
63
|
-
!valid?
|
64
|
-
end
|
65
|
-
|
66
|
-
# Get all the elements that the form consists of
|
67
|
-
def elements
|
68
|
-
@resolver.elements
|
69
|
-
end
|
70
|
-
|
71
|
-
# Create a JSON string from the form elements (name => value)
|
72
|
-
def to_json
|
73
|
-
elements_hash = {}
|
74
|
-
|
75
|
-
self.elements.each do |name, el|
|
76
|
-
elements_hash[name] = el.value if el.is_a?(BaseActiveElement)
|
77
|
-
end
|
78
|
-
|
79
|
-
JSON.generate(elements_hash)
|
80
|
-
end
|
81
12
|
end
|
data/spec/elements/group_spec.rb
CHANGED
data/spec/elements/html_spec.rb
CHANGED
@@ -1,18 +1,17 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
-
describe HungryForm::Html do
|
3
|
+
describe HungryForm::Elements::Html do
|
4
4
|
it_behaves_like "an element" do
|
5
5
|
let(:element_options) { {} }
|
6
6
|
end
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
subject { HungryForm::Html.new(:html, nil, resolver, options) {} }
|
8
|
+
let(:resolver) { HungryForm::Resolver.new() }
|
9
|
+
let(:options) { {value: "body text"} }
|
10
|
+
subject { HungryForm::Elements::Html.new(:html, nil, resolver, options) {} }
|
12
11
|
|
12
|
+
describe ".new" do
|
13
13
|
it "should have a value" do
|
14
14
|
expect(subject.value).to eq "body text"
|
15
15
|
end
|
16
|
-
|
17
16
|
end
|
18
17
|
end
|
data/spec/elements/page_spec.rb
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
-
describe HungryForm::Page do
|
3
|
+
describe HungryForm::Elements::Page do
|
4
4
|
let(:resolver) { HungryForm::Resolver.new() }
|
5
5
|
let(:options) { {} }
|
6
|
-
let(:page) { HungryForm::Page.new(:pagename, nil, resolver, options) {} }
|
6
|
+
let(:page) { HungryForm::Elements::Page.new(:pagename, nil, resolver, options) {} }
|
7
7
|
|
8
8
|
it_behaves_like "a group"
|
9
9
|
|
10
10
|
describe ".new" do
|
11
11
|
it "should have one element" do
|
12
|
-
page = HungryForm::Page.new(:pagename, nil, resolver, options) do
|
12
|
+
page = HungryForm::Elements::Page.new(:pagename, nil, resolver, options) do
|
13
13
|
html :html_name, value: "<p>Test html block</p>"
|
14
14
|
end
|
15
15
|
expect(page.elements.size).to eq 1
|
@@ -19,7 +19,7 @@ describe HungryForm::Page do
|
|
19
19
|
describe "#group" do
|
20
20
|
it "should contain a group" do
|
21
21
|
page.group(:group_name, {}) {}
|
22
|
-
expect(page.elements.first.class).to eq HungryForm::Group
|
22
|
+
expect(page.elements.first.class).to eq HungryForm::Elements::Group
|
23
23
|
end
|
24
24
|
end
|
25
25
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe HungryForm::Elements::RadioGroup do
|
4
|
+
let(:resolver_options) { {} }
|
5
|
+
let(:resolver) { HungryForm::Resolver.new(resolver_options) }
|
6
|
+
let(:group) { HungryForm::Elements::Group.new(:group, nil, resolver, {}) {} }
|
7
|
+
|
8
|
+
let(:element_options) do
|
9
|
+
{
|
10
|
+
:options => {
|
11
|
+
"1" => "First",
|
12
|
+
"2" => "Second",
|
13
|
+
"3" => "Third"
|
14
|
+
}
|
15
|
+
}
|
16
|
+
end
|
17
|
+
subject { HungryForm::Elements::RadioGroup.new(:element_name, group, resolver, element_options) {} }
|
18
|
+
|
19
|
+
it_behaves_like "an element with options" do
|
20
|
+
let(:options_element_options) do
|
21
|
+
{
|
22
|
+
:options => {
|
23
|
+
"1" => "First",
|
24
|
+
"element_value" => "Second" # element_value is checked in shared_active_element
|
25
|
+
}
|
26
|
+
}
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe HungryForm::Elements::SelectField do
|
4
|
+
let(:resolver_options) { {} }
|
5
|
+
let(:resolver) { HungryForm::Resolver.new(resolver_options) }
|
6
|
+
|
7
|
+
let(:group_options) { {} }
|
8
|
+
let(:group) { HungryForm::Elements::Group.new(:group, nil, resolver, group_options) {} }
|
9
|
+
|
10
|
+
let(:element_options) do
|
11
|
+
{
|
12
|
+
:options => {
|
13
|
+
"1" => "First",
|
14
|
+
"2" => "Second",
|
15
|
+
"3" => "Third"
|
16
|
+
}
|
17
|
+
}
|
18
|
+
end
|
19
|
+
subject { HungryForm::Elements::SelectField.new(:element_name, group, resolver, element_options) {} }
|
20
|
+
|
21
|
+
it_behaves_like "an element with options" do
|
22
|
+
let(:options_element_options) do
|
23
|
+
{
|
24
|
+
:options => {
|
25
|
+
"1" => "First",
|
26
|
+
"element_value" => "Second" # element_value is checked in shared_active_element
|
27
|
+
}
|
28
|
+
}
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "#set_value" do
|
33
|
+
context "when multiple enabled" do
|
34
|
+
it "assigns multiple values" do
|
35
|
+
resolver_options[:params] = {"group_element_name" => ["1", "2", "3"]}
|
36
|
+
element_options[:multiple] = true
|
37
|
+
expect(subject.value).to eq(["1", "2", "3"])
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "#to_hash" do
|
43
|
+
it "should include multiple" do
|
44
|
+
expect(subject.to_hash).to include(:multiple)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -7,13 +7,13 @@ describe HungryForm do
|
|
7
7
|
"first_first_name" => "John",
|
8
8
|
"first_last_name" => "Doe",
|
9
9
|
"second_email" => "john.doe@yahoo.com",
|
10
|
-
"third_occupation" => "
|
10
|
+
"third_occupation" => "programmer"
|
11
11
|
}
|
12
12
|
}
|
13
13
|
end
|
14
14
|
|
15
|
-
subject
|
16
|
-
HungryForm.new(options) do
|
15
|
+
subject do
|
16
|
+
HungryForm::Form.new(options) do
|
17
17
|
page :first do
|
18
18
|
text_field :first_name
|
19
19
|
text_field :last_name
|
@@ -22,7 +22,7 @@ describe HungryForm do
|
|
22
22
|
text_field :email
|
23
23
|
end
|
24
24
|
page :third do
|
25
|
-
|
25
|
+
select_field :occupation, :options => {"programmer" => "Programmer", "other" => "Other"}
|
26
26
|
group :employment_history do
|
27
27
|
html :before, value: "Employment history over the last 5 years"
|
28
28
|
text_field :history, value: "Default value"
|
@@ -38,18 +38,19 @@ describe HungryForm do
|
|
38
38
|
end
|
39
39
|
|
40
40
|
describe "#page" do
|
41
|
-
subject(:form) { HungryForm.new() {} }
|
41
|
+
subject(:form) { HungryForm::Form.new() {} }
|
42
42
|
|
43
43
|
it "should contain a page" do
|
44
44
|
form.page(:page_name, {}) {}
|
45
|
-
expect(form.pages.first.class).to eq HungryForm::Page
|
45
|
+
expect(form.pages.first.class).to eq HungryForm::Elements::Page
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
49
|
describe "#to_json" do
|
50
50
|
it "should create a json string from the form objects" do
|
51
|
-
|
52
|
-
expect(
|
51
|
+
hash = JSON.parse(subject.to_json)
|
52
|
+
expect(hash["pages"].size).to eq 2
|
53
|
+
expect(hash["pages"].first["elements"].size).to eq 2
|
53
54
|
end
|
54
55
|
end
|
55
56
|
end
|
data/spec/resolver_spec.rb
CHANGED
@@ -16,7 +16,7 @@ describe HungryForm::Resolver do
|
|
16
16
|
end
|
17
17
|
|
18
18
|
describe "#get_value" do
|
19
|
-
let(:element) { HungryForm::Html.new(:html_name, nil, resolver, { value: "value" }) {} }
|
19
|
+
let(:element) { HungryForm::Elements::Html.new(:html_name, nil, resolver, { value: "value" }) {} }
|
20
20
|
|
21
21
|
it "should get value from lambda param" do
|
22
22
|
value = subject.get_value( ->(el){ "value" } )
|
@@ -3,7 +3,7 @@ RSpec.shared_examples "an active element" do
|
|
3
3
|
let(:resolver) { HungryForm::Resolver.new(resolver_options) }
|
4
4
|
|
5
5
|
let(:group_options) { {} }
|
6
|
-
let(:group) { HungryForm::Group.new(:group, nil, resolver, group_options) {} }
|
6
|
+
let(:group) { HungryForm::Elements::Group.new(:group, nil, resolver, group_options) {} }
|
7
7
|
|
8
8
|
let(:element) { described_class.new(:element_name, group, resolver, active_element_options) {} }
|
9
9
|
|
@@ -54,4 +54,12 @@ RSpec.shared_examples "an active element" do
|
|
54
54
|
end
|
55
55
|
end
|
56
56
|
end
|
57
|
+
|
58
|
+
describe "#to_hash" do
|
59
|
+
it "should include required, value and error" do
|
60
|
+
active_element_options[:value] = ''
|
61
|
+
active_element_options[:required] = true
|
62
|
+
expect(element.to_hash).to include(:required, :value, :error)
|
63
|
+
end
|
64
|
+
end
|
57
65
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
RSpec.shared_examples "an element" do
|
2
2
|
let(:resolver) { HungryForm::Resolver.new() }
|
3
3
|
|
4
|
-
let(:group) { HungryForm::Group.new(:group, nil, resolver, {}) {} }
|
4
|
+
let(:group) { HungryForm::Elements::Group.new(:group, nil, resolver, {}) {} }
|
5
5
|
|
6
6
|
let(:element) { described_class.new(:element_name, group, resolver, element_options) {} }
|
7
7
|
|
@@ -51,4 +51,12 @@ RSpec.shared_examples "an element" do
|
|
51
51
|
expect(element.other_html_param?).to eq false
|
52
52
|
end
|
53
53
|
end
|
54
|
+
|
55
|
+
describe "#to_hash" do
|
56
|
+
it "should include visible, dependency, name and label" do
|
57
|
+
element_options[:dependency] = '{"EQ": [1, 1]}'
|
58
|
+
element_options[:name] = 'name'
|
59
|
+
expect(element.to_hash).to include(:visible, :dependency, :name, :label)
|
60
|
+
end
|
61
|
+
end
|
54
62
|
end
|
@@ -13,7 +13,7 @@ RSpec.shared_examples "a group" do
|
|
13
13
|
describe "#group" do
|
14
14
|
it "creates a nested group" do
|
15
15
|
group.group(:nested, {}) {}
|
16
|
-
expect(group.elements.first.class).to eq HungryForm::Group
|
16
|
+
expect(group.elements.first.class).to eq HungryForm::Elements::Group
|
17
17
|
end
|
18
18
|
|
19
19
|
it "concatenates nested element's name with the parent's one" do
|
@@ -22,10 +22,16 @@ RSpec.shared_examples "a group" do
|
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
+
describe "#to_hash" do
|
26
|
+
it "should include group elements" do
|
27
|
+
expect(group.to_hash).to include(:elements)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
25
31
|
describe ".method_missing" do
|
26
32
|
it "creates a nested element" do
|
27
33
|
group.html(:name)
|
28
|
-
expect(group.elements.first.class).to eq HungryForm::Html
|
34
|
+
expect(group.elements.first.class).to eq HungryForm::Elements::Html
|
29
35
|
end
|
30
36
|
|
31
37
|
it "concatenates nested element's name with the parent's one" do
|
@@ -0,0 +1,36 @@
|
|
1
|
+
RSpec.shared_examples "an element with options" do
|
2
|
+
let(:resolver_options) { {} }
|
3
|
+
let(:resolver) { HungryForm::Resolver.new(resolver_options) }
|
4
|
+
|
5
|
+
let(:group_options) { {} }
|
6
|
+
let(:group) { HungryForm::Elements::Group.new(:group, nil, resolver, group_options) {} }
|
7
|
+
|
8
|
+
let(:element) { described_class.new(:element_name, group, resolver, options_element_options) {} }
|
9
|
+
|
10
|
+
it_behaves_like "an active element" do
|
11
|
+
let(:active_element_options) { options_element_options }
|
12
|
+
end
|
13
|
+
|
14
|
+
describe ".new" do
|
15
|
+
it "should raise an exception if there is no options provided" do
|
16
|
+
options_element_options.delete(:options) if options_element_options[:options]
|
17
|
+
expect { described_class.new(:element_name, group, resolver, options_element_options) }.to raise_error(HungryForm::HungryFormException)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should have options" do
|
21
|
+
options_element_options[:options] = {"1" => "First", "2" => "Last"}
|
22
|
+
expect(element.options).to eq({"1" => "First", "2" => "Last"})
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should convert options to hash" do
|
26
|
+
options_element_options[:options] = ->(el) {{"1" => "First", "2" => "Last"}}
|
27
|
+
expect(element.options).to eq({"1" => "First", "2" => "Last"})
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "#to_hash" do
|
32
|
+
it "should include options" do
|
33
|
+
expect(element.to_hash).to include(:options)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/spec/validator_spec.rb
CHANGED
@@ -2,10 +2,10 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
describe HungryForm::Validator do
|
4
4
|
let(:resolver) { HungryForm::Resolver.new() }
|
5
|
-
let(:group) { HungryForm::Group.new(:group, nil, resolver, {}) {} }
|
5
|
+
let(:group) { HungryForm::Elements::Group.new(:group, nil, resolver, {}) {} }
|
6
6
|
|
7
7
|
let(:element_options) { {} }
|
8
|
-
let(:element) { HungryForm::TextField.new(:element_name, group, resolver, element_options) {} }
|
8
|
+
let(:element) { HungryForm::Elements::TextField.new(:element_name, group, resolver, element_options) {} }
|
9
9
|
|
10
10
|
describe "required" do
|
11
11
|
it "should return nil when the element's value is present" do
|