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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 294fa8ac75fd2c282df2c5a438fee7d7bf82b20b
4
- data.tar.gz: 576085dc9ede62d2509edb986a834c211d05bf68
3
+ metadata.gz: 9f9479def9f703406cbe035f829f43440db7ae07
4
+ data.tar.gz: 6c10c93e4056cb5a69fc84092b4f7d3a0dc7aa8f
5
5
  SHA512:
6
- metadata.gz: debba5e7b956cd1fbd1355b670bdc290f71adfa859ed5f2d28aaf5bb2a09c96c229e0af17dcc192adae10ed439fcb48e275dfaa2f1b1fc010aa84d76114fec14
7
- data.tar.gz: 1631d4029713fbe661be3f3410e3cd97fa651a9c226cdd696ccd901138e99600371ca8cb744f1c617b491d51db6798d3f9e8850471f2012e9670435ea4338f17
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['ActiveModel::Validations::LengthValidator'] = Proc.new do |validator, result|
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['ActiveModel::Validations::PresenceValidator'] = Proc.new do |validator, result|
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, options = {}
35
- @template.label @method, text, unvalidated_options(options)
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[:placeholder ] = placeholder
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, {}, validated_options(options), &block
51
+ @template.select @method, nil, object_options, html_options(options), &block
49
52
  end
50
53
 
51
54
  private
52
55
 
53
- def unvalidated_options options
54
- @default_options.merge(options).merge(object: @object)
55
- end
56
+ def object_options
57
+ @default_options.merge(object: @object)
58
+ end
56
59
 
57
- def validated_options options
58
- options = unvalidated_options(options).merge(validations) if FormGroups.map_validators
59
- options = options.merge('aria-invalid' => 'true') if errors.any?
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
- options
62
- end
64
+ options
65
+ end
63
66
 
64
- def validations
65
- validations = validators.map do |validator|
66
- result = {}
67
+ def validated_options options
68
+ object_options.merge html_options(options)
69
+ end
67
70
 
68
- mapping = FormGroups.validator_mapping[validator.class.name]
69
- mapping.call(validator, result) if mapping
71
+ def validations
72
+ validations = validators.map do |validator|
73
+ result = {}
70
74
 
71
- result
72
- end
75
+ mapping = FormGroups.validator_mapping.select { |v| validator.is_a? v }
76
+ mapping.each_value { |m| m.call validator, result }
73
77
 
74
- Hash[*validations.collect{ |h| h.to_a }.flatten]
75
- end
78
+ result
79
+ end
76
80
 
77
- def validators
78
- @object.class.validators.select do |validator|
79
- validator.attributes.include? @method
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
- private
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
@@ -1,3 +1,3 @@
1
1
  module FormGroups
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
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.3
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-05 00:00:00.000000000 Z
11
+ date: 2015-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails