formgroups-rails 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/form_groups/configuration.rb +11 -2
- data/lib/form_groups/field_builder.rb +34 -27
- data/lib/form_groups/group_builder.rb +8 -1
- data/lib/form_groups/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9f9479def9f703406cbe035f829f43440db7ae07
|
4
|
+
data.tar.gz: 6c10c93e4056cb5a69fc84092b4f7d3a0dc7aa8f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 54263a4b0bde87d236b1d88cb292df0859acb4a727d8c632dbad028399416a39f140f6aeac68ef2ddc09addb038990b7da7215eb0ff79000ebba4948541622f2
|
7
|
+
data.tar.gz: 18d258814ac6de81b2320ee475a3efc7e76b06f4c68759cc062f66a9f7830c6bc36aa2d9c24a55f7a29c95553fbea77a53f3fb00f5257583f9e079df47317786
|
@@ -6,16 +6,25 @@ module FormGroups
|
|
6
6
|
mattr_reader :validator_mapping do
|
7
7
|
mappings = {}
|
8
8
|
|
9
|
-
mappings[
|
9
|
+
mappings[ActiveModel::Validations::LengthValidator] = Proc.new do |validator, result|
|
10
10
|
result['minlength'] = validator.options[:minimum] if validator.options[:minimum]
|
11
11
|
result['maxlength'] = validator.options[:maximum] if validator.options[:maximum]
|
12
12
|
end
|
13
13
|
|
14
|
-
mappings[
|
14
|
+
mappings[ActiveModel::Validations::PresenceValidator] = Proc.new do |validator, result|
|
15
15
|
result['required'] = 'true'
|
16
16
|
result['aria-required'] = 'true'
|
17
17
|
end
|
18
18
|
|
19
|
+
mappings[ActiveModel::Validations::InclusionValidator] = Proc.new do |validator, result|
|
20
|
+
range = validator.options[:within]
|
21
|
+
|
22
|
+
if range.is_a? Range
|
23
|
+
result['min'] = range.min
|
24
|
+
result['max'] = range.max
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
19
28
|
mappings
|
20
29
|
end
|
21
30
|
|
@@ -15,8 +15,8 @@ module FormGroups
|
|
15
15
|
end
|
16
16
|
|
17
17
|
OTHER_HELPERS.each do |selector|
|
18
|
-
define_method selector do |options = {}|
|
19
|
-
@template.send selector, @method, validated_options(options)
|
18
|
+
define_method selector do |value, options = {}|
|
19
|
+
@template.send selector, @method, value, validated_options(options)
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
@@ -31,12 +31,15 @@ module FormGroups
|
|
31
31
|
@object.errors[@method]
|
32
32
|
end
|
33
33
|
|
34
|
-
def label text,
|
35
|
-
|
34
|
+
def label text, sub_id = '', **options
|
35
|
+
full_id = @method.to_s
|
36
|
+
full_id += "_#{sub_id}" unless sub_id.nil? or sub_id.to_s.empty?
|
37
|
+
|
38
|
+
@template.label full_id.to_sym, text, object_options.merge(options)
|
36
39
|
end
|
37
40
|
|
38
41
|
def text_area placeholder = nil, **options
|
39
|
-
options[
|
42
|
+
options['placeholder' ] = placeholder
|
40
43
|
options['aria-multiline'] = true
|
41
44
|
|
42
45
|
@template.text_area @method, validated_options(options)
|
@@ -45,39 +48,43 @@ module FormGroups
|
|
45
48
|
def select placeholder = nil, **options, &block
|
46
49
|
options['data-placeholder'] = placeholder
|
47
50
|
|
48
|
-
@template.select @method, nil,
|
51
|
+
@template.select @method, nil, object_options, html_options(options), &block
|
49
52
|
end
|
50
53
|
|
51
54
|
private
|
52
55
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
+
def object_options
|
57
|
+
@default_options.merge(object: @object)
|
58
|
+
end
|
56
59
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
+
def html_options options = {}
|
61
|
+
options = options.merge(validations) if FormGroups.map_validators
|
62
|
+
options = options.merge('aria-invalid' => 'true') if errors.any?
|
60
63
|
|
61
|
-
|
62
|
-
|
64
|
+
options
|
65
|
+
end
|
63
66
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
+
def validated_options options
|
68
|
+
object_options.merge html_options(options)
|
69
|
+
end
|
67
70
|
|
68
|
-
|
69
|
-
|
71
|
+
def validations
|
72
|
+
validations = validators.map do |validator|
|
73
|
+
result = {}
|
70
74
|
|
71
|
-
|
72
|
-
|
75
|
+
mapping = FormGroups.validator_mapping.select { |v| validator.is_a? v }
|
76
|
+
mapping.each_value { |m| m.call validator, result }
|
73
77
|
|
74
|
-
|
75
|
-
|
78
|
+
result
|
79
|
+
end
|
76
80
|
|
77
|
-
|
78
|
-
|
79
|
-
|
81
|
+
Hash[*validations.collect{ |h| h.to_a }.flatten]
|
82
|
+
end
|
83
|
+
|
84
|
+
def validators
|
85
|
+
@object.class.validators.select do |validator|
|
86
|
+
validator.attributes.include? @method
|
87
|
+
end
|
80
88
|
end
|
81
|
-
end
|
82
89
|
end
|
83
90
|
end
|
@@ -9,8 +9,15 @@ module FormGroups
|
|
9
9
|
FieldTag.new(object_name, method, object, @template, self, options).render(&block)
|
10
10
|
end
|
11
11
|
|
12
|
-
|
12
|
+
def id method
|
13
|
+
@sanitized_id ||= object_name.to_s.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "")
|
14
|
+
end
|
13
15
|
|
16
|
+
def value method
|
17
|
+
object[method]
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
14
21
|
def objectify options
|
15
22
|
options.merge(object: object)
|
16
23
|
end
|
data/lib/form_groups/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: formgroups-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexey Chernetsov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-05-
|
11
|
+
date: 2015-05-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|