hungryform 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/hungryform/elements/base/active_element.rb +38 -7
- data/lib/hungryform/elements/checkbox_field.rb +20 -4
- data/lib/hungryform/resolver.rb +6 -6
- data/lib/hungryform/version.rb +1 -1
- data/spec/elements/checkbox_field_spec.rb +36 -10
- data/spec/elements/shared_examples/active_element.rb +2 -7
- data/spec/elements/shared_examples/hashable_element.rb +8 -0
- data/spec/resolver_spec.rb +33 -29
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 25ffe91aae5e6e1817a43a158fc9d7bd7343b59b
|
4
|
+
data.tar.gz: 8c04ae6d5245c30ce22e2f4e48dd18c83f5f4288
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e12e8aa25f40d6449b7ae034d8701ebb11b4dc78095532eb8824c9a5abd31ab277f37ada50a11c0b6ebc6fd80f103cbfd15216008d33b915672fa87e8c700101
|
7
|
+
data.tar.gz: a602d7919da04118f13d67e50b09cdc3edced4eb9c3c1b2f39407e82471915c68f64b08c23f5ce0a37b61de1a801e9d89388fe16f062a1e6301759e318243e41
|
@@ -20,34 +20,65 @@ module HungryForm
|
|
20
20
|
self.required = false
|
21
21
|
end
|
22
22
|
|
23
|
-
#
|
24
|
-
|
23
|
+
# Determine validation attributes
|
24
|
+
# They can be only the methods of the HungryForm::Validator class
|
25
|
+
# or methods of the Validator module of the current class
|
26
|
+
methods = HungryForm::Validator.singleton_class.instance_methods(false)
|
27
|
+
if self.class.const_defined?(:Validator)
|
28
|
+
methods += self.class.const_get(:Validator).instance_methods(false)
|
29
|
+
end
|
30
|
+
|
31
|
+
@validation_rules = @attributes.select { |key, _| methods.include?(key) }
|
25
32
|
@attributes.delete_if { |key, _| @validation_rules.key?(key) }
|
26
33
|
|
27
34
|
set_value
|
28
35
|
end
|
29
36
|
|
37
|
+
# Element valid?
|
38
|
+
# Performs element validation
|
30
39
|
def valid?
|
31
40
|
clear_error
|
32
|
-
is_valid = true
|
33
41
|
return true unless visible?
|
34
42
|
|
35
43
|
@validation_rules.each do |key, rule|
|
36
|
-
|
44
|
+
validate_rule(key, rule)
|
45
|
+
|
37
46
|
unless error.empty?
|
38
47
|
self.error = "This field #{error}"
|
39
|
-
|
40
|
-
break
|
48
|
+
return false
|
41
49
|
end
|
42
50
|
end
|
43
51
|
|
44
|
-
|
52
|
+
true
|
45
53
|
end
|
46
54
|
|
55
|
+
# Element invalid?
|
56
|
+
# Performs elemen validation
|
47
57
|
def invalid?
|
48
58
|
!valid?
|
49
59
|
end
|
50
60
|
|
61
|
+
# Validate a particular validation rule
|
62
|
+
# Searches for the validation attribute in the Validator module
|
63
|
+
# of the element's class or in the global Validator module
|
64
|
+
#
|
65
|
+
# Sample:
|
66
|
+
# text_field :txt, :required => true
|
67
|
+
# will search for the "required" singleton method
|
68
|
+
# in the HungryForm::Elements::TextField::Validator and in the
|
69
|
+
# HungryGorm::Validator
|
70
|
+
def validate_rule(attribute, rule)
|
71
|
+
if self.class.const_defined?(:Validator)
|
72
|
+
validator = self.class.const_get(:Validator)
|
73
|
+
end
|
74
|
+
|
75
|
+
if validator.nil? || !validator.singleton_class.instance_methods(false).include?(attribute)
|
76
|
+
validator = HungryForm::Validator
|
77
|
+
end
|
78
|
+
|
79
|
+
self.error = validator.send(attribute, self, rule) || ''
|
80
|
+
end
|
81
|
+
|
51
82
|
def set_value
|
52
83
|
self.value = resolver.params[name] || attributes.delete(:value)
|
53
84
|
end
|
@@ -2,12 +2,28 @@ module HungryForm
|
|
2
2
|
module Elements
|
3
3
|
class CheckboxField < Base::ActiveElement
|
4
4
|
def set_value
|
5
|
-
|
5
|
+
self.value = resolver.params[name] || attributes.delete(:value)
|
6
6
|
|
7
|
-
if
|
8
|
-
|
7
|
+
if value == 0
|
8
|
+
attributes.delete(:checked)
|
9
9
|
else
|
10
|
-
self.
|
10
|
+
self.value = 1
|
11
|
+
self.attributes[:checked] = true
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def checked?
|
16
|
+
!!attributes[:checked]
|
17
|
+
end
|
18
|
+
|
19
|
+
# Overriding the Validator methods
|
20
|
+
module Validator
|
21
|
+
def self.required(element, rule)
|
22
|
+
if rule.respond_to? :call
|
23
|
+
rule.call(element)
|
24
|
+
else
|
25
|
+
'is required' if element.value == 0 && rule
|
26
|
+
end
|
11
27
|
end
|
12
28
|
end
|
13
29
|
end
|
data/lib/hungryform/resolver.rb
CHANGED
@@ -45,18 +45,18 @@ module HungryForm
|
|
45
45
|
|
46
46
|
arguments = [arguments] unless arguments.is_a?(Array)
|
47
47
|
|
48
|
-
|
49
|
-
return false if
|
48
|
+
values = arguments[0..1].map { |name| get_value(name) }
|
49
|
+
return false if values.any?(&:nil?)
|
50
50
|
|
51
51
|
case operator
|
52
52
|
when :eq
|
53
|
-
return
|
53
|
+
return values[0].to_s == values[1].to_s
|
54
54
|
when :lt
|
55
|
-
return
|
55
|
+
return values[0].to_f < values[1].to_f
|
56
56
|
when :gt
|
57
|
-
return
|
57
|
+
return values[0].to_f > values[1].to_f
|
58
58
|
when :set
|
59
|
-
return !
|
59
|
+
return !values[0].empty?
|
60
60
|
end
|
61
61
|
end
|
62
62
|
end
|
data/lib/hungryform/version.rb
CHANGED
@@ -10,21 +10,47 @@ describe HungryForm::Elements::CheckboxField do
|
|
10
10
|
subject { HungryForm::Elements::CheckboxField.new(:element_name, group, resolver, options) {} }
|
11
11
|
|
12
12
|
it_behaves_like 'an element'
|
13
|
-
it_behaves_like '
|
13
|
+
it_behaves_like 'a hashable element'
|
14
14
|
|
15
|
-
describe "
|
16
|
-
|
17
|
-
|
15
|
+
describe ".new" do
|
16
|
+
it "should have empty error" do
|
17
|
+
expect(subject.error).to eq ""
|
18
|
+
end
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
20
|
+
it "should not be required if its parent is not visible" do
|
21
|
+
group_options[:visible] = false
|
22
|
+
options[:required] = true
|
23
|
+
expect(subject.required?).to eq false
|
22
24
|
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "#valid?" do
|
28
|
+
context "when required" do
|
29
|
+
before(:each) { options[:required] = true }
|
30
|
+
|
31
|
+
it "is valid" do
|
32
|
+
subject.value = 1
|
33
|
+
expect(subject.valid?).to eq true
|
34
|
+
expect(subject.error).to eq ''
|
35
|
+
end
|
23
36
|
|
24
|
-
|
25
|
-
|
26
|
-
expect(subject.
|
37
|
+
it "is invalid" do
|
38
|
+
subject.value = 0
|
39
|
+
expect(subject.valid?).to eq false
|
40
|
+
expect(subject.error).to eq 'This field is required'
|
27
41
|
end
|
28
42
|
end
|
29
43
|
end
|
44
|
+
|
45
|
+
describe "#set_value" do
|
46
|
+
it "should be checked" do
|
47
|
+
resolver_options[:params] = { "group_element_name" => 1 }
|
48
|
+
expect(subject.checked?).to eq true
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should not be checked" do
|
52
|
+
resolver_options[:params] = { "group_element_name" => 0 }
|
53
|
+
expect(subject.checked?).to eq false
|
54
|
+
end
|
55
|
+
end
|
30
56
|
end
|
@@ -1,4 +1,6 @@
|
|
1
1
|
RSpec.shared_examples "an active element" do
|
2
|
+
it_behaves_like 'a hashable element'
|
3
|
+
|
2
4
|
describe ".new" do
|
3
5
|
it "should have empty error" do
|
4
6
|
expect(subject.error).to eq ""
|
@@ -42,11 +44,4 @@ RSpec.shared_examples "an active element" do
|
|
42
44
|
end
|
43
45
|
end
|
44
46
|
end
|
45
|
-
|
46
|
-
describe "#to_hash" do
|
47
|
-
it 'should include required, value and error' do
|
48
|
-
options.merge!(value: '', required: true)
|
49
|
-
expect(subject.to_hash).to include(:required, :value, :error)
|
50
|
-
end
|
51
|
-
end
|
52
47
|
end
|
data/spec/resolver_spec.rb
CHANGED
@@ -3,6 +3,8 @@ require "spec_helper"
|
|
3
3
|
describe HungryForm::Resolver do
|
4
4
|
let(:resolver_params) { {:params => {}} }
|
5
5
|
subject(:resolver) { HungryForm::Resolver.new(resolver_params) }
|
6
|
+
let(:group_options) { {} }
|
7
|
+
let(:group) { HungryForm::Elements::Group.new(:group, nil, resolver, group_options) {} }
|
6
8
|
|
7
9
|
describe ".new" do
|
8
10
|
it "should initialize resolver empty params" do
|
@@ -16,7 +18,7 @@ describe HungryForm::Resolver do
|
|
16
18
|
end
|
17
19
|
|
18
20
|
describe "#get_value" do
|
19
|
-
let(:element) { HungryForm::Elements::Html.new(:html_name,
|
21
|
+
let(:element) { HungryForm::Elements::Html.new(:html_name, group, subject, { value: "value" }) {} }
|
20
22
|
|
21
23
|
it "should get value from lambda param" do
|
22
24
|
value = subject.get_value( ->(el){ "value" } )
|
@@ -25,7 +27,7 @@ describe HungryForm::Resolver do
|
|
25
27
|
|
26
28
|
it "should get value from a form element" do
|
27
29
|
subject.elements[element.name] = element
|
28
|
-
expect(subject.get_value("
|
30
|
+
expect(subject.get_value("group_html_name")).to eq "value"
|
29
31
|
end
|
30
32
|
|
31
33
|
it "should get value from a form params" do
|
@@ -39,39 +41,41 @@ describe HungryForm::Resolver do
|
|
39
41
|
end
|
40
42
|
|
41
43
|
describe "#resolve_dependency" do
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
44
|
+
context "when dependency is of a scalar type" do
|
45
|
+
it "should resolve EQ dependency" do
|
46
|
+
dependency = { eq: ["Text", "Text"] }
|
47
|
+
expect(subject.resolve_dependency(dependency)).to eq true
|
48
|
+
end
|
46
49
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
50
|
+
it "should resolve LT dependency" do
|
51
|
+
dependency = { lt: ["0", "1"] }
|
52
|
+
expect(subject.resolve_dependency(dependency)).to eq true
|
53
|
+
end
|
51
54
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
55
|
+
it "should resolve GT dependency" do
|
56
|
+
dependency = { gt: ["1", "0"] }
|
57
|
+
expect(subject.resolve_dependency(dependency)).to eq true
|
58
|
+
end
|
56
59
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
60
|
+
it "should resolve SET dependency" do
|
61
|
+
dependency = { set: "1" }
|
62
|
+
expect(subject.resolve_dependency(dependency)).to eq true
|
63
|
+
end
|
61
64
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
65
|
+
it "should resolve AND dependency" do
|
66
|
+
dependency = { and: [{set: "1"}, {set: "1"}] }
|
67
|
+
expect(subject.resolve_dependency(dependency)).to eq true
|
68
|
+
end
|
66
69
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
70
|
+
it "should resolve OR dependency" do
|
71
|
+
dependency = { or: [{set: ""}, {set: "1"}] }
|
72
|
+
expect(subject.resolve_dependency(dependency)).to eq true
|
73
|
+
end
|
71
74
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
+
it "should resolve NOT dependency" do
|
76
|
+
dependency = { not: {set: ""} }
|
77
|
+
expect(subject.resolve_dependency(dependency)).to eq true
|
78
|
+
end
|
75
79
|
end
|
76
80
|
end
|
77
81
|
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.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrey Bazhutkin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-05-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -112,6 +112,7 @@ files:
|
|
112
112
|
- spec/elements/shared_examples/active_element.rb
|
113
113
|
- spec/elements/shared_examples/element.rb
|
114
114
|
- spec/elements/shared_examples/group.rb
|
115
|
+
- spec/elements/shared_examples/hashable_element.rb
|
115
116
|
- spec/elements/shared_examples/options_element.rb
|
116
117
|
- spec/elements/text_area_spec.rb
|
117
118
|
- spec/elements/text_field_spec.rb
|
@@ -153,6 +154,7 @@ test_files:
|
|
153
154
|
- spec/elements/shared_examples/active_element.rb
|
154
155
|
- spec/elements/shared_examples/element.rb
|
155
156
|
- spec/elements/shared_examples/group.rb
|
157
|
+
- spec/elements/shared_examples/hashable_element.rb
|
156
158
|
- spec/elements/shared_examples/options_element.rb
|
157
159
|
- spec/elements/text_area_spec.rb
|
158
160
|
- spec/elements/text_field_spec.rb
|