hungryform 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/Gemfile.lock +2 -4
- data/README.md +113 -10
- data/hungryform.gemspec +0 -2
- data/lib/hungryform.rb +21 -3
- data/lib/hungryform/elements.rb +3 -1
- data/lib/hungryform/elements/base_active_element.rb +12 -6
- data/lib/hungryform/elements/base_element.rb +22 -10
- data/lib/hungryform/elements/base_group.rb +4 -9
- data/lib/hungryform/elements/group.rb +0 -4
- data/lib/hungryform/resolver.rb +1 -1
- data/lib/hungryform/validator.rb +11 -2
- data/lib/hungryform/version.rb +1 -1
- data/spec/elements/html_spec.rb +3 -1
- data/spec/elements/text_field_spec.rb +3 -1
- data/spec/hungryform_spec.rb +33 -5
- data/spec/resolver_spec.rb +13 -2
- data/spec/support/shared_active_element.rb +22 -6
- data/spec/support/shared_element.rb +13 -7
- data/spec/support/shared_group.rb +3 -1
- data/spec/validator_spec.rb +33 -0
- metadata +4 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4c86c2c1ea0c43d499e7e0ace99b385e64f9f907
|
4
|
+
data.tar.gz: f12a4223b04ba00cf056c3ec9f1ff135b4abb6c5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 528cf1d7b5809415a13b518526fcef94ab2a48e379e8870b398a582694c230c3222c19ccf603d4f0c51419192a257a123086e552e426a0a92fe862b2e85f8a07
|
7
|
+
data.tar.gz: 37600c6257d3b50c5b456f8a534c7abde7c14a6024b19aa12d564286684f21f0c847103cf7978865ee38b96e6841f9f0f07a95ed9a3b8c4290d7b06ea85fba0e
|
data/.gitignore
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
*.gem
|
data/Gemfile.lock
CHANGED
@@ -1,9 +1,8 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
hungryform (0.0.
|
4
|
+
hungryform (0.0.2)
|
5
5
|
activesupport
|
6
|
-
hashie
|
7
6
|
|
8
7
|
GEM
|
9
8
|
remote: https://rubygems.org/
|
@@ -15,7 +14,6 @@ GEM
|
|
15
14
|
thread_safe (~> 0.1)
|
16
15
|
tzinfo (~> 1.1)
|
17
16
|
diff-lcs (1.2.5)
|
18
|
-
hashie (3.3.1)
|
19
17
|
i18n (0.6.11)
|
20
18
|
json (1.8.1)
|
21
19
|
minitest (5.4.2)
|
@@ -43,4 +41,4 @@ DEPENDENCIES
|
|
43
41
|
bundler (~> 1.6)
|
44
42
|
hungryform!
|
45
43
|
rake
|
46
|
-
rspec
|
44
|
+
rspec (~> 3.0)
|
data/README.md
CHANGED
@@ -1,24 +1,127 @@
|
|
1
1
|
# HungryForm
|
2
2
|
|
3
|
-
|
3
|
+
HungryForm is a gem for managing multiple page forms. The main purpose of this gem is to give developers an easy DSL to build complex forms.
|
4
4
|
|
5
|
-
##
|
5
|
+
## Usage
|
6
6
|
|
7
|
-
|
7
|
+
```ruby
|
8
|
+
require 'hungryform'
|
8
9
|
|
9
|
-
|
10
|
+
form = HungryForm.new do
|
11
|
+
page :first do
|
12
|
+
text_field :first_name
|
13
|
+
text_field :last_name
|
14
|
+
end
|
15
|
+
page :second, do
|
16
|
+
text_field :address
|
17
|
+
end
|
18
|
+
page :third do
|
19
|
+
text_field :occupation
|
20
|
+
|
21
|
+
# Show this group only when the occupation field is not empty
|
22
|
+
group :employment_history, visible: false, dependency: '{"SET": "third_occupation"}' do
|
23
|
+
html :before, value: "Employment history over the last 5 years"
|
24
|
+
text_field :history, value: "Default value"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
```
|
10
29
|
|
11
|
-
|
30
|
+
To assign values to the form elements pass them as a hash on form initialization. The params hash must consist of elements names and their values. Please note, that the elements names must contain the full path to the element, starting from the page name.
|
12
31
|
|
13
|
-
|
32
|
+
```ruby
|
33
|
+
params = {
|
34
|
+
"first_first_name" => "John",
|
35
|
+
"first_last_name" => "Doe",
|
36
|
+
"second_address" => "John's address",
|
37
|
+
"third_occupation" => "Software engineer",
|
38
|
+
"third_employment_history_history" => "John's employment hisotory"
|
39
|
+
}
|
14
40
|
|
15
|
-
|
41
|
+
form = HungryForm.new :params => params do
|
42
|
+
...
|
43
|
+
end
|
16
44
|
|
17
|
-
|
45
|
+
```
|
18
46
|
|
19
|
-
|
47
|
+
You can assign default value to a form element:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
text_field :email, value: "john.doe@yahoo.com"
|
51
|
+
```
|
52
|
+
## Dependencies
|
53
|
+
|
54
|
+
Each element of HungryForm, including pages and groups, can have a dependency parameter. This parameter must be a json string with an expression, that resolves to a boolean result. Within this expression you can use and combine the following operators, creating complex dependencies that can involve multiple elements:
|
55
|
+
|
56
|
+
```json
|
57
|
+
# val1 == val2
|
58
|
+
{"EQ": ["val1", "val2"]}
|
59
|
+
|
60
|
+
# val1 > val2
|
61
|
+
{"GT": ["val1", "val2"]}
|
62
|
+
|
63
|
+
# val1 < val2
|
64
|
+
{"LT": ["val1", "val2"]}
|
65
|
+
|
66
|
+
# val1 is not empty
|
67
|
+
{"SET": "val1"}
|
68
|
+
|
69
|
+
# Get the opposite result of the expression
|
70
|
+
{"NOT": {"EQ": ["1", "1"]}}
|
71
|
+
|
72
|
+
# Check if all the expressions are true
|
73
|
+
{"AND": [
|
74
|
+
{"EQ": ["1", "1"]},
|
75
|
+
{"EQ": ["2", "2"]}
|
76
|
+
]}
|
77
|
+
|
78
|
+
# Check if any of the expressions is true
|
79
|
+
{"OR": [
|
80
|
+
{"NOT": {"EQ": ["1", "1"]}},
|
81
|
+
{"EQ": ["2", "2"]}
|
82
|
+
]}
|
83
|
+
```
|
84
|
+
|
85
|
+
If the dependency is resolved positively it makes the element visible. Otherwise the element will be hidden and not required. It is allowed to use element names or params keys as parameters inside expressions.
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
HungryForm.new do
|
89
|
+
page :about do
|
90
|
+
text_field :age
|
91
|
+
text_field :favourite_alcohol, required: true, dependency: '{"GT": ["about_age", "18"]}'
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
```
|
96
|
+
|
97
|
+
## Validation
|
98
|
+
|
99
|
+
Each active element of a form can be assigned with validation rules.
|
100
|
+
|
101
|
+
- required - accepts boolean or proc
|
102
|
+
- validation - accepts proc
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
text_field :name, required: true
|
106
|
+
text_field :email, validation: ->(el) { "is unexpected email" unless el.value == "me@yahoo.com" }
|
107
|
+
```
|
108
|
+
|
109
|
+
You can extend the list of validation rules by opening the HungryForm::Validator singleton class and creating your own validation methods:
|
20
110
|
|
21
|
-
|
111
|
+
```ruby
|
112
|
+
class HungryForm
|
113
|
+
class Validator
|
114
|
+
class << self
|
115
|
+
def my_validation_method(element, rule)
|
116
|
+
"is not #{rule}" unless element.value == rule
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
|
123
|
+
text_field :vegetable, value: "tomato", my_validation_method: "potato" # => is not potato
|
124
|
+
```
|
22
125
|
|
23
126
|
## Contributing
|
24
127
|
|
data/hungryform.gemspec
CHANGED
@@ -2,7 +2,6 @@
|
|
2
2
|
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
4
|
require 'hungryform/version'
|
5
|
-
require 'json'
|
6
5
|
|
7
6
|
Gem::Specification.new do |spec|
|
8
7
|
spec.name = "hungryform"
|
@@ -22,5 +21,4 @@ Gem::Specification.new do |spec|
|
|
22
21
|
spec.add_development_dependency "rake"
|
23
22
|
spec.add_development_dependency "rspec", "~> 3.0"
|
24
23
|
spec.add_dependency "activesupport"
|
25
|
-
spec.add_dependency "hashie", "~> 3.3"
|
26
24
|
end
|
data/lib/hungryform.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
|
-
require '
|
1
|
+
require 'json'
|
2
2
|
require 'active_support/core_ext/string/inflections'
|
3
|
-
require 'hashie'
|
4
3
|
require "hungryform/version"
|
5
4
|
require "hungryform/resolver"
|
6
5
|
require "hungryform/validator"
|
@@ -31,12 +30,14 @@ require "hungryform/elements"
|
|
31
30
|
class HungryForm
|
32
31
|
HungryFormException = Class.new(StandardError)
|
33
32
|
|
34
|
-
attr_reader :
|
33
|
+
attr_reader :pages
|
35
34
|
|
36
35
|
def initialize(options = {}, &block)
|
37
36
|
raise HungryFormException, 'No form structure block given' unless block_given?
|
37
|
+
|
38
38
|
@resolver = Resolver.new(options.slice(:params))
|
39
39
|
@pages = []
|
40
|
+
|
40
41
|
instance_eval(&block)
|
41
42
|
end
|
42
43
|
|
@@ -49,6 +50,7 @@ class HungryForm
|
|
49
50
|
# Entire form validation. Loops through the form pages and validates each page
|
50
51
|
def valid?
|
51
52
|
is_valid = true
|
53
|
+
|
52
54
|
pages.each do |page|
|
53
55
|
#Loop through pages to get all errors
|
54
56
|
is_valid = false if page.invalid?
|
@@ -60,4 +62,20 @@ class HungryForm
|
|
60
62
|
def invalid?
|
61
63
|
!valid?
|
62
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
|
63
81
|
end
|
data/lib/hungryform/elements.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
require_relative 'elements/base_element'
|
2
2
|
require_relative 'elements/base_active_element'
|
3
|
+
require_relative 'elements/base_options_element'
|
3
4
|
require_relative 'elements/base_group'
|
4
5
|
require_relative 'elements/page'
|
5
6
|
require_relative 'elements/group'
|
6
7
|
require_relative 'elements/html'
|
7
|
-
require_relative 'elements/text_field'
|
8
|
+
require_relative 'elements/text_field'
|
9
|
+
require_relative 'elements/select'
|
@@ -1,21 +1,24 @@
|
|
1
1
|
class HungryForm
|
2
2
|
class BaseActiveElement < BaseElement
|
3
|
-
|
3
|
+
|
4
|
+
attr_accessor :error, :value, :required
|
5
|
+
alias_method :required?, :required
|
4
6
|
|
5
7
|
def initialize(name, parent, resolver, options = {}, &block)
|
6
8
|
super
|
9
|
+
|
7
10
|
self.error = ''
|
8
11
|
|
9
12
|
# Filter only the options that are present in the HungryForm::Validator singleton class
|
10
13
|
@validation_rules = options.select { |key, val| HungryForm::Validator.respond_to?(key) }
|
11
14
|
|
12
|
-
|
13
|
-
|
14
|
-
if options.has_key?(:params)
|
15
|
-
self.value = options[:params][self.name] || ''
|
15
|
+
if parent.visible?
|
16
|
+
self.required = options[:required] || false
|
16
17
|
else
|
17
|
-
self.
|
18
|
+
self.required = false
|
18
19
|
end
|
20
|
+
|
21
|
+
self.value = resolver.params.has_key?(self.name)? resolver.params[self.name] : options[:value]
|
19
22
|
end
|
20
23
|
|
21
24
|
def valid?
|
@@ -24,6 +27,7 @@ class HungryForm
|
|
24
27
|
return true if !visible?
|
25
28
|
|
26
29
|
is_valid = true
|
30
|
+
|
27
31
|
@validation_rules.each do |key, rule|
|
28
32
|
error = HungryForm::Validator.send(key, self, rule) || ''
|
29
33
|
unless error.empty?
|
@@ -34,5 +38,7 @@ class HungryForm
|
|
34
38
|
|
35
39
|
is_valid
|
36
40
|
end
|
41
|
+
|
42
|
+
|
37
43
|
end
|
38
44
|
end
|
@@ -1,22 +1,34 @@
|
|
1
1
|
class HungryForm
|
2
|
-
class BaseElement
|
3
|
-
attr_accessor :name, :placeholders, :resolver
|
2
|
+
class BaseElement
|
3
|
+
attr_accessor :name, :placeholders, :resolver, :visible, :label, :dependency
|
4
|
+
alias_method :visible?, :visible
|
4
5
|
|
5
6
|
def initialize(name, parent, resolver, options = {})
|
6
|
-
|
7
|
-
self.resolver = resolver
|
7
|
+
@_options = options
|
8
8
|
|
9
|
-
|
9
|
+
@placeholders ||= {}
|
10
|
+
@resolver = resolver
|
10
11
|
|
11
|
-
|
12
|
-
|
12
|
+
# The element is visible if no visible parameter passed or
|
13
|
+
# visible param equals true and the dependency is resolved positively
|
14
|
+
self.visible = options.has_key?(:visible)? options[:visible] : true
|
15
|
+
self.visible &&= resolver.resolve_dependency(::JSON.parse(options[:dependency])) if options[:dependency]
|
16
|
+
self.dependency = options[:dependency] || ''
|
13
17
|
self.name = (parent.nil?? "" : "#{parent.name}_") + resolver.get_value(name, self)
|
14
18
|
|
15
|
-
|
16
|
-
self.label = resolver.get_value(self.label, self)
|
17
|
-
else
|
19
|
+
unless options[:label]
|
18
20
|
self.label = resolver.get_value(name, self).humanize
|
21
|
+
else
|
22
|
+
self.label = resolver.get_value(options[:label], self)
|
19
23
|
end
|
20
24
|
end
|
25
|
+
|
26
|
+
def method_missing(name, *args, &block)
|
27
|
+
# Check if an option exists
|
28
|
+
return @_options.has_key?(name.to_s[0..-2].to_sym) if name.to_s[-1] == '?'
|
29
|
+
# Return an option
|
30
|
+
return @_options[name] if @_options.has_key?(name)
|
31
|
+
super
|
32
|
+
end
|
21
33
|
end
|
22
34
|
end
|
@@ -26,18 +26,13 @@ class HungryForm
|
|
26
26
|
raise HungryFormException, 'No group structure block given' unless block_given?
|
27
27
|
|
28
28
|
super
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
self.errors = {}
|
29
|
+
|
30
|
+
@elements = []
|
31
|
+
@errors = {}
|
33
32
|
|
34
33
|
instance_eval(&block)
|
35
34
|
end
|
36
35
|
|
37
|
-
def group(name, options = {}, &block)
|
38
|
-
elements << HungryForm::Group.new(name, self, @resolver, options, &block)
|
39
|
-
end
|
40
|
-
|
41
36
|
# Validates an entire group. If a group consists of nested groups
|
42
37
|
# they will be validated recursively
|
43
38
|
def valid?
|
@@ -69,7 +64,7 @@ class HungryForm
|
|
69
64
|
return super if klass.nil?
|
70
65
|
|
71
66
|
# Create a new element based on a symbol provided and push it into the group elements array
|
72
|
-
element = HungryForm::const_get(klass).send(:new,
|
67
|
+
element = HungryForm::const_get(klass).send(:new, args[0], self, @resolver, *(args[1..-1]), &block)
|
73
68
|
elements << element
|
74
69
|
|
75
70
|
#Resolver keeps a hash of all elements of the form
|
data/lib/hungryform/resolver.rb
CHANGED
data/lib/hungryform/validator.rb
CHANGED
@@ -1,13 +1,22 @@
|
|
1
1
|
class HungryForm
|
2
2
|
class Validator
|
3
3
|
class << self
|
4
|
+
# Check if the element's value is not empty.
|
5
|
+
# The rule argument can be a boolean or a callable object
|
4
6
|
def required(element, rule)
|
5
7
|
if rule.respond_to? :call
|
6
|
-
|
8
|
+
rule.call(element)
|
7
9
|
else
|
8
|
-
|
10
|
+
"is required" if element.value.to_s.empty? && rule
|
9
11
|
end
|
10
12
|
end
|
13
|
+
|
14
|
+
# Custom validation check
|
15
|
+
# Use when you need to create a custom validation in the structure
|
16
|
+
def validation(element, callable)
|
17
|
+
raise HungryFormError "Validation must respond to call" unless callable.respond_to? :call
|
18
|
+
callable.call(element)
|
19
|
+
end
|
11
20
|
end
|
12
21
|
end
|
13
22
|
end
|
data/lib/hungryform/version.rb
CHANGED
data/spec/elements/html_spec.rb
CHANGED
data/spec/hungryform_spec.rb
CHANGED
@@ -1,17 +1,38 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
3
|
describe HungryForm do
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
let(:options) do
|
5
|
+
{
|
6
|
+
:params => {
|
7
|
+
"first_first_name" => "John",
|
8
|
+
"first_last_name" => "Doe",
|
9
|
+
"second_email" => "john.doe@yahoo.com",
|
10
|
+
"third_occupation" => "Software developer"
|
11
|
+
}
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
subject(:form) do
|
16
|
+
HungryForm.new(options) do
|
17
|
+
page :first do
|
18
|
+
text_field :first_name
|
19
|
+
text_field :last_name
|
7
20
|
end
|
8
21
|
page :second, visible: false do
|
22
|
+
text_field :email
|
9
23
|
end
|
10
24
|
page :third do
|
25
|
+
text_field :occupation
|
26
|
+
group :employment_history do
|
27
|
+
html :before, value: "Employment history over the last 5 years"
|
28
|
+
text_field :history, value: "Default value"
|
29
|
+
end
|
11
30
|
end
|
12
|
-
|
31
|
+
end
|
32
|
+
end
|
13
33
|
|
14
|
-
|
34
|
+
describe ".new" do
|
35
|
+
it "should contain pages" do
|
15
36
|
expect(subject.pages.size).to eq 2
|
16
37
|
end
|
17
38
|
end
|
@@ -24,4 +45,11 @@ describe HungryForm do
|
|
24
45
|
expect(form.pages.first.class).to eq HungryForm::Page
|
25
46
|
end
|
26
47
|
end
|
48
|
+
|
49
|
+
describe "#to_json" do
|
50
|
+
it "should create a json string from the form objects" do
|
51
|
+
form_elements_hash = options[:params].merge({"third_employment_history_history" => "Default value"})
|
52
|
+
expect(form.to_json).to eq(JSON.generate(form_elements_hash))
|
53
|
+
end
|
54
|
+
end
|
27
55
|
end
|
data/spec/resolver_spec.rb
CHANGED
@@ -4,7 +4,18 @@ describe HungryForm::Resolver do
|
|
4
4
|
let(:resolver_params) { {:params => {}} }
|
5
5
|
subject(:resolver) { HungryForm::Resolver.new(resolver_params) }
|
6
6
|
|
7
|
-
describe ".
|
7
|
+
describe ".new" do
|
8
|
+
it "should initialize resolver empty params" do
|
9
|
+
expect(subject.params).to eq({})
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should initialize resolver params" do
|
13
|
+
resolver_params[:params] = {"key" => "value"}
|
14
|
+
expect(subject.params).to eq({"key" => "value"})
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "#get_value" do
|
8
19
|
let(:element) { HungryForm::Html.new(:html_name, nil, resolver, { value: "value" }) {} }
|
9
20
|
|
10
21
|
it "should get value from lambda param" do
|
@@ -27,7 +38,7 @@ describe HungryForm::Resolver do
|
|
27
38
|
end
|
28
39
|
end
|
29
40
|
|
30
|
-
describe "
|
41
|
+
describe "#resolve_dependency" do
|
31
42
|
it "should resolve EQ dependency" do
|
32
43
|
dependency = { "EQ" => ["Text", "Text"] }
|
33
44
|
expect(subject.resolve_dependency(dependency)).to eq true
|
@@ -1,13 +1,15 @@
|
|
1
1
|
RSpec.shared_examples "an active element" do
|
2
|
-
let(:
|
2
|
+
let(:resolver_options) { {} }
|
3
|
+
let(:resolver) { HungryForm::Resolver.new(resolver_options) }
|
3
4
|
|
4
5
|
let(:group_options) { {} }
|
5
6
|
let(:group) { HungryForm::Group.new(:group, nil, resolver, group_options) {} }
|
6
7
|
|
7
|
-
let(:
|
8
|
-
let(:element) { described_class.new(:element_name, group, resolver, element_options) {} }
|
8
|
+
let(:element) { described_class.new(:element_name, group, resolver, active_element_options) {} }
|
9
9
|
|
10
|
-
it_behaves_like "an element"
|
10
|
+
it_behaves_like "an element" do
|
11
|
+
let(:element_options) { active_element_options }
|
12
|
+
end
|
11
13
|
|
12
14
|
describe ".new" do
|
13
15
|
it "should have empty error" do
|
@@ -16,15 +18,29 @@ RSpec.shared_examples "an active element" do
|
|
16
18
|
|
17
19
|
it "should not be required if its parent is not visible" do
|
18
20
|
group_options[:visible] = false
|
19
|
-
|
21
|
+
active_element_options[:required] = true
|
20
22
|
expect(element.required?).to eq false
|
21
23
|
end
|
24
|
+
|
25
|
+
it "should have a nil value" do
|
26
|
+
expect(element.value).to be nil
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should have a value from form params" do
|
30
|
+
resolver_options[:params]= {"group_element_name" => "element_value" }
|
31
|
+
expect(element.value).to eq "element_value"
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should have a value from element structure" do
|
35
|
+
active_element_options[:value] = "element_value"
|
36
|
+
expect(element.value).to eq "element_value"
|
37
|
+
end
|
22
38
|
end
|
23
39
|
|
24
40
|
describe "#valid?" do
|
25
41
|
describe "when required" do
|
26
42
|
before(:each) do
|
27
|
-
|
43
|
+
active_element_options[:required] = true
|
28
44
|
end
|
29
45
|
|
30
46
|
it "is valid" do
|
@@ -3,7 +3,6 @@ RSpec.shared_examples "an element" do
|
|
3
3
|
|
4
4
|
let(:group) { HungryForm::Group.new(:group, nil, resolver, {}) {} }
|
5
5
|
|
6
|
-
let(:element_options) { {} }
|
7
6
|
let(:element) { described_class.new(:element_name, group, resolver, element_options) {} }
|
8
7
|
|
9
8
|
describe "#visible?" do
|
@@ -22,12 +21,6 @@ RSpec.shared_examples "an element" do
|
|
22
21
|
expect(element.visible?).to eq false
|
23
22
|
end
|
24
23
|
|
25
|
-
it "should never be visible" do
|
26
|
-
element_options[:visible] = false
|
27
|
-
element_options[:dependency] = '{"EQ": ["1", "1"]}'
|
28
|
-
expect(element.visible?).to eq false
|
29
|
-
end
|
30
|
-
|
31
24
|
it "should be visible" do
|
32
25
|
element_options[:dependency] = '{"EQ": ["1", "1"]}'
|
33
26
|
expect(element.visible?).to eq true
|
@@ -45,4 +38,17 @@ RSpec.shared_examples "an element" do
|
|
45
38
|
expect(element.label).to eq "Special Label"
|
46
39
|
end
|
47
40
|
end
|
41
|
+
|
42
|
+
describe "#method_missing" do
|
43
|
+
it "should return existing param" do
|
44
|
+
element_options[:html_param] = "param"
|
45
|
+
expect(element.html_param).to eq "param"
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should check whether param exists" do
|
49
|
+
element_options[:html_param] = "param"
|
50
|
+
expect(element.html_param?).to eq true
|
51
|
+
expect(element.other_html_param?).to eq false
|
52
|
+
end
|
53
|
+
end
|
48
54
|
end
|
@@ -6,7 +6,9 @@ RSpec.shared_examples "a group" do
|
|
6
6
|
let(:group_options) { {} }
|
7
7
|
let(:group) { described_class.new(:name, page, resolver, group_options) {} }
|
8
8
|
|
9
|
-
it_behaves_like "an element"
|
9
|
+
it_behaves_like "an element" do
|
10
|
+
let(:element_options) { group_options }
|
11
|
+
end
|
10
12
|
|
11
13
|
describe "#group" do
|
12
14
|
it "creates a nested group" do
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe HungryForm::Validator do
|
4
|
+
let(:resolver) { HungryForm::Resolver.new() }
|
5
|
+
let(:group) { HungryForm::Group.new(:group, nil, resolver, {}) {} }
|
6
|
+
|
7
|
+
let(:element_options) { {} }
|
8
|
+
let(:element) { HungryForm::TextField.new(:element_name, group, resolver, element_options) {} }
|
9
|
+
|
10
|
+
describe "required" do
|
11
|
+
it "should return nil when the element's value is present" do
|
12
|
+
element_options[:value] = "value"
|
13
|
+
expect(HungryForm::Validator.required(element, true)).to be nil
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should return error when the element's value is not present" do
|
17
|
+
element_options[:value] = ""
|
18
|
+
expect(HungryForm::Validator.required(element, true)).to eq "is required"
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should return the result of the custom required validation" do
|
22
|
+
element_options[:value] = "value"
|
23
|
+
expect(HungryForm::Validator.required(element, ->(el) { "not ok" if el.value != "custom" })).to eq "not ok"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "validation" do
|
28
|
+
it "should return the result of the custom validation" do
|
29
|
+
element_options[:value] = "value"
|
30
|
+
expect(HungryForm::Validator.validation(element, ->(el) { "not ok" if el.value != "custom" })).to eq "not ok"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hungryform
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrey Bazhutkin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-11-
|
11
|
+
date: 2014-11-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,20 +66,6 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: hashie
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - "~>"
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '3.3'
|
76
|
-
type: :runtime
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - "~>"
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version: '3.3'
|
83
69
|
description:
|
84
70
|
email:
|
85
71
|
- andrey.bazhutkin@gmail.com
|
@@ -117,6 +103,7 @@ files:
|
|
117
103
|
- spec/support/shared_active_element.rb
|
118
104
|
- spec/support/shared_element.rb
|
119
105
|
- spec/support/shared_group.rb
|
106
|
+
- spec/validator_spec.rb
|
120
107
|
homepage: https://github.com/andrba/hungryform
|
121
108
|
licenses:
|
122
109
|
- MIT
|
@@ -152,3 +139,4 @@ test_files:
|
|
152
139
|
- spec/support/shared_active_element.rb
|
153
140
|
- spec/support/shared_element.rb
|
154
141
|
- spec/support/shared_group.rb
|
142
|
+
- spec/validator_spec.rb
|