parsley_simple_form 0.0.4 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. checksums.yaml +6 -14
  2. data/README.md +2 -0
  3. data/lib/parsley_simple_form/action_view_extensions/form_helper.rb +4 -2
  4. data/lib/parsley_simple_form/concerns/range_concern.rb +29 -0
  5. data/lib/parsley_simple_form/concerns/type_sensitive_concern.rb +28 -0
  6. data/lib/parsley_simple_form/constraints/basics/equalto_constraint.rb +2 -2
  7. data/lib/parsley_simple_form/constraints/basics/max_constraint.rb +23 -0
  8. data/lib/parsley_simple_form/constraints/basics/maxcheck_constraint.rb +23 -0
  9. data/lib/parsley_simple_form/constraints/basics/maxlength_constraint.rb +20 -0
  10. data/lib/parsley_simple_form/constraints/basics/min_constraint.rb +22 -0
  11. data/lib/parsley_simple_form/constraints/basics/mincheck_constraint.rb +23 -0
  12. data/lib/parsley_simple_form/constraints/basics/minlength_constraint.rb +20 -0
  13. data/lib/parsley_simple_form/constraints/basics/notblank_constraint.rb +1 -1
  14. data/lib/parsley_simple_form/constraints/basics/range_constraint.rb +21 -0
  15. data/lib/parsley_simple_form/constraints/basics/rangecheck_constraint.rb +25 -0
  16. data/lib/parsley_simple_form/constraints/basics/rangelength_constraint.rb +21 -0
  17. data/lib/parsley_simple_form/constraints/basics/regexp_constraint.rb +23 -0
  18. data/lib/parsley_simple_form/constraints/basics/required_constraint.rb +1 -1
  19. data/lib/parsley_simple_form/constraints/basics/type_constraint.rb +5 -14
  20. data/lib/parsley_simple_form/form_builder.rb +16 -27
  21. data/lib/parsley_simple_form/railtie.rb +1 -0
  22. data/lib/parsley_simple_form/version.rb +1 -1
  23. data/test/dummy/log/test.log +4129 -0
  24. data/test/form_builder/constraints_test.rb +139 -0
  25. data/test/form_builder/type_test.rb +60 -0
  26. data/test/form_helper_test.rb +1 -1
  27. data/test/support/i18n_helper.rb +7 -0
  28. data/test/support/models.rb +1 -0
  29. data/test/test_helper.rb +1 -2
  30. metadata +33 -17
  31. data/test/form_builder_test.rb +0 -57
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- YzQ3ZTU3YjJiYmM2Yzc5NmMyMmMwYzYxNDQzZDBjZDgyN2E3Mzk0MA==
5
- data.tar.gz: !binary |-
6
- ODRmYmUwZDhjZGYwY2RlOTM3MDk2NDNhNmM0NWRjYzllODBiOGQ0MA==
7
- !binary "U0hBNTEy":
8
- metadata.gz: !binary |-
9
- OTQxZWNiZDI2NTVhN2JmZjMyMGJkMWZmMzQ3YjRiY2U4NDZiNjc1NzNlZGE2
10
- NmJhY2E1ODY2YjQ2NmFkZTY0ZDU1Njk4MDhhYTY5NTgwYTZjNDU4YWExNjBj
11
- MTcyZDYxYzI4YjU1MDhmMmUyM2YxNWFlZDE1ZTU0NGQ3MTdmNGQ=
12
- data.tar.gz: !binary |-
13
- NTk3ODhiYjU0NjMxZjc3OTcwYjJhMWFlY2FlNjljOTNkN2MyYzRiZmNmNzI0
14
- NDQ0OGViMzNlZDBmMjdhOGViMmRmM2FhMDI5MDdhZDJhNGY3ODBjNTJjY2Nk
15
- NWM2MDM5MWZkNzQ3ZjEyMjAwYzRhNjNmOGU3OThlNTM3ZWNmZWE=
2
+ SHA1:
3
+ metadata.gz: 498d675fb0132664462eebbacbf3212f36fddebd
4
+ data.tar.gz: b32c8da052886cd4cea4d805fb56770280c73afa
5
+ SHA512:
6
+ metadata.gz: da7808982c5475eb2b84378fcbb207c837457efcfb6c341513c37ab361fd9798a854016829ab57ccc6445180a183bc5c22164750aeba6a67e5ed6fc6edf00b25
7
+ data.tar.gz: 73d7e41f7d44efc54dc1c7a7975967f35624f9fab5fa5c8b2da5faccc4fb4ede7ae0b6a4b1b0cc2d8e6c07163b2d5aafa877ce9060c80f2b3e6d9a51f6aecaab
data/README.md CHANGED
@@ -21,6 +21,8 @@ This code:
21
21
 
22
22
  Automatically adds required constraint to inputs which are required attributes, and validates equality of `password` and `password_confirmation`.
23
23
 
24
+ Don't forget to add parsley.js to your project. You can do it adding the gem [parsley-rails](https://github.com/mekishizufu/parsley-rails) to project's Gemfile (we don't recommend this approach) or use a package manager like [Bower](https://github.com/bower/bower).
25
+
24
26
  Contributions
25
27
  -------------
26
28
 
@@ -5,8 +5,10 @@ module ParsleySimpleForm
5
5
  options = args.extract_options!
6
6
  options[:builder] = ParsleySimpleForm::FormBuilder
7
7
  options[:html] = {} if options[:html].nil?
8
- options[:html][:"data-validate"] = "parsley"
9
- options[:html][:novalidate] = true
8
+ #options[:html][:"data-validate"] = "parsley"
9
+ #options[:html][:novalidate] = true
10
+ options[:html][:'data-parsley-validate'] = true
11
+ options[:html][:'data-parsley-namespace'] = "data-parsley-" #to HTML5 valid
10
12
  simple_form_for(object, *(args << options), &block)
11
13
  end
12
14
  end
@@ -0,0 +1,29 @@
1
+ module ParsleySimpleForm
2
+ module Concerns
3
+ module RangeConcern
4
+ private
5
+ def parse_range(range)
6
+ {
7
+ :string => range_to_string(range),
8
+ :min => min_range(range),
9
+ :max => max_range(range)
10
+ }
11
+ end
12
+
13
+ def range_to_string(range)
14
+ return "[#{range.min},#{range.max}]" if range.is_a?(Range)
15
+ range
16
+ end
17
+
18
+ def min_range(range)
19
+ return range.min if range.is_a?(Range)
20
+ range.match(/\[(\d),(\d)\]/)[1]
21
+ end
22
+
23
+ def max_range(range)
24
+ return range.max if range.is_a?(Range)
25
+ range.match(/\[(\d),(\d)\]/)[2]
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,28 @@
1
+ module ParsleySimpleForm
2
+ module Concerns
3
+ module TypeSensitiveConcern
4
+ TYPES_CONSTRAINTS = %w(email url urlstrict digits number alphanum dateIso)
5
+ HTML_NATIVE_TYPES = %w(email url number tel)
6
+
7
+ TYPE_MAP = {
8
+ 'decimal' => 'number',
9
+ 'integer' => 'digits'
10
+ }
11
+
12
+
13
+
14
+ private
15
+ def mapped_type
16
+ if @mapped_type.nil?
17
+ @mapped_type = type
18
+ @mapped_type = TYPE_MAP[type] unless TYPE_MAP[type].nil?
19
+ end
20
+ @mapped_type
21
+ end
22
+
23
+ def type
24
+ @type ||= @form_builder.find_input(@form_builder.attribute_name, @options, &@block).input_type.to_s
25
+ end
26
+ end
27
+ end
28
+ end
@@ -14,8 +14,8 @@ module ParsleySimpleForm
14
14
  attr_equalto = @options[:equalto]
15
15
  input = @form_builder.find_input(attr_equalto, @options, &@block)
16
16
  {
17
- :'data-equalto' => '#' + input_id(attr_equalto, @options, &@block),
18
- :'data-equalto-message' =>
17
+ :'data-parsley-equalto' => '#' + input_id(attr_equalto, @options, &@block),
18
+ :'data-parsley-equalto-message' =>
19
19
  I18n::translate('form_validation.message.equalto', field_name: input.object.class.human_attribute_name(input.attribute_name))
20
20
  }
21
21
  end
@@ -0,0 +1,23 @@
1
+ module ParsleySimpleForm
2
+ module Constraints
3
+ module Basics
4
+ class MaxConstraint < Constraints::BaseConstraint
5
+ include Concerns::TypeSensitiveConcern
6
+
7
+ def match?
8
+ return false if @options.nil?
9
+ @input_html = @options[:input_html]
10
+ @max = @options[:max] || @input_html[:max]
11
+ !@options[:max].nil? || (!@input_html[:max].nil? && mapped_type.to_s == 'number')
12
+ end
13
+
14
+ def html_attributes
15
+ attributes = {:'data-parsley-max-message' => I18n::translate('form_validation.message.max', max: @max)}
16
+ attributes.merge!({:type => 'number', :max => @max}) if (@input_html[:max].nil? || !mapped_type.to_s == 'number')
17
+ attributes
18
+ end
19
+
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ module ParsleySimpleForm
2
+ module Constraints
3
+ module Basics
4
+ class MaxcheckConstraint < Constraints::BaseConstraint
5
+
6
+ def match?
7
+ return false if @options.nil?
8
+ !@options[:maxcheck].nil?
9
+ end
10
+
11
+ def html_attributes
12
+ attributes = {
13
+ :'data-parsley-maxcheck' => @options[:maxcheck],
14
+ :'data-parsley-maxcheck-message' => I18n::translate('form_validation.message.maxcheck', maxcheck: @options[:maxcheck])
15
+ }
16
+ attributes.merge!({:'data-parsley-group' => @options[:check_group]}) unless @options[:check_group].nil?
17
+ attributes
18
+ end
19
+
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,20 @@
1
+ module ParsleySimpleForm
2
+ module Constraints
3
+ module Basics
4
+ class MaxLengthConstraint < Constraints::BaseConstraint
5
+
6
+ def match?
7
+ !@options[:maxlength].nil?
8
+ end
9
+
10
+ def html_attributes
11
+ {
12
+ :'data-parsley-maxlength' => @options[:maxlength],
13
+ :'data-parsley-maxlength-message' => I18n::translate('form_validation.message.maxlength', maxlength: @options[:maxlength])
14
+ }
15
+ end
16
+
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,22 @@
1
+ module ParsleySimpleForm
2
+ module Constraints
3
+ module Basics
4
+ class MinConstraint < Constraints::BaseConstraint
5
+ include Concerns::TypeSensitiveConcern
6
+
7
+ def match?
8
+ return false if @options.nil?
9
+ @min = @options[:min]
10
+
11
+ @input_html = @options[:input_html]
12
+ !@options[:min].nil? || (!@input_html[:min].nil? && mapped_type == 'number')
13
+ end
14
+
15
+ def html_attributes
16
+ attributes = {:'data-parsley-min-message' => I18n::translate('form_validation.message.min', min: @min)}
17
+ attributes.merge({:type => 'number', :min => @min}) if (@input_html[:min].nil? || !mapped_type == 'number')
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,23 @@
1
+ module ParsleySimpleForm
2
+ module Constraints
3
+ module Basics
4
+ class MincheckConstraint < Constraints::BaseConstraint
5
+
6
+ def match?
7
+ return false if @options.nil?
8
+ !@options[:mincheck].nil?
9
+ end
10
+
11
+ def html_attributes
12
+ attributes = {
13
+ :'data-parsley-mincheck' => @options[:mincheck],
14
+ :'data-parsley-mincheck-message' => I18n::translate('form_validation.message.mincheck', mincheck: @options[:mincheck])
15
+ }
16
+ attributes.merge!({:'data-parsley-group' => @options[:check_group]}) unless @options[:check_group].nil?
17
+ attributes
18
+ end
19
+
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,20 @@
1
+ module ParsleySimpleForm
2
+ module Constraints
3
+ module Basics
4
+ class MinLengthConstraint < Constraints::BaseConstraint
5
+
6
+ def match?
7
+ !@options[:minlength].nil?
8
+ end
9
+
10
+ def html_attributes
11
+ {
12
+ :'data-parsley-minlength' => @options[:minlength],
13
+ :'data-parsley-minlength-message' => I18n::translate('form_validation.message.minlength', minlength: @options[:minlength])
14
+ }
15
+ end
16
+
17
+ end
18
+ end
19
+ end
20
+ end
@@ -8,7 +8,7 @@ module ParsleySimpleForm
8
8
  end
9
9
 
10
10
  def html_attributes
11
- {:'data-notblank' => true}
11
+ {:'data-parsley-notblank' => true}
12
12
  end
13
13
 
14
14
  end
@@ -0,0 +1,21 @@
1
+ module ParsleySimpleForm
2
+ module Constraints
3
+ module Basics
4
+ class RangeConstraint < Constraints::BaseConstraint
5
+ include Concerns::RangeConcern
6
+
7
+ def match?
8
+ !@options[:range].nil?
9
+ end
10
+
11
+ def html_attributes
12
+ range = parse_range(@options[:range])
13
+ {
14
+ :'data-parsley-range' => range[:string],
15
+ :'data-parsley-range-message' => I18n::translate('form_validation.message.range', min: range[:min], max: range[:max])
16
+ }
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,25 @@
1
+ module ParsleySimpleForm
2
+ module Constraints
3
+ module Basics
4
+ class RangecheckConstraint < Constraints::BaseConstraint
5
+ include Concerns::RangeConcern
6
+
7
+ def match?
8
+ return false if @options.nil?
9
+ !@options[:rangecheck].nil?
10
+ end
11
+
12
+ def html_attributes
13
+ range = parse_range(@options[:rangecheck])
14
+ attributes = {
15
+ :'data-parsley-rangecheck' => range[:string],
16
+ :'data-parsley-rangecheck-message' => I18n::translate('form_validation.message.ragecheck', min: range[:min], max: range[:max])
17
+ }
18
+ attributes.merge!({:'data-parsley-group' => @options[:check_group]}) unless @options[:check_group].nil?
19
+ attributes
20
+ end
21
+
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,21 @@
1
+ module ParsleySimpleForm
2
+ module Constraints
3
+ module Basics
4
+ class RangeLengthConstraint < Constraints::BaseConstraint
5
+ include Concerns::RangeConcern
6
+
7
+ def match?
8
+ !@options[:rangelength].nil?
9
+ end
10
+
11
+ def html_attributes
12
+ range = parse_range(@options[:rangelength])
13
+ {
14
+ :'data-parsley-rangelength' => range[:string],
15
+ :'data-parsley-rangelength-message' => I18n::translate('form_validation.message.rangelength', min: range[:min], max: range[:max])
16
+ }
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ module ParsleySimpleForm
2
+ module Constraints
3
+ module Basics
4
+ class RegexpConstraint < Constraints::BaseConstraint
5
+ def match?
6
+ return false if @options.nil?
7
+
8
+ if @options[:regexp].nil?
9
+ @regexp = @options[:input_html][:regexp] unless @options[:input_html].nil?
10
+ else
11
+ @regexp = @options[:regexp]
12
+ end
13
+ !@regexp.nil?
14
+ end
15
+
16
+ def html_attributes
17
+ {:'data-parsley-regexp-message' => I18n::translate('form_validation.message.regexp'),
18
+ :pattern => @regexp}
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -15,7 +15,7 @@ module ParsleySimpleForm
15
15
  end
16
16
 
17
17
  def html_attributes
18
- {"data-required-message".to_sym => I18n::translate('form_validation.message.required')}
18
+ {"data-parsley-required-message".to_sym => I18n::translate('form_validation.message.required')}
19
19
  end
20
20
 
21
21
  end
@@ -2,26 +2,17 @@ module ParsleySimpleForm
2
2
  module Constraints
3
3
  module Basics
4
4
  class TypeConstraint < Constraints::BaseConstraint
5
- TYPES_CONSTRAINTS = %w(email url urlstrict digits number alphanum dateIso phone)
6
-
7
- TYPE_MAP = {
8
- 'decimal' => 'number'
9
- }
5
+ include Concerns::TypeSensitiveConcern
10
6
 
11
7
  def match?
12
- TYPES_CONSTRAINTS.include?(type)
8
+ TYPES_CONSTRAINTS.include?(type) or !TYPE_MAP[type].nil?
13
9
  end
14
10
 
15
11
  def html_attributes
16
- type = TYPE_MAP[type] unless TYPE_MAP[type].nil?
17
- {"data-type-#{type}-message".to_sym => I18n::translate("form_validation.message.#{type}")}
18
- end
19
-
20
- private
21
- def type
22
- @type ||= @form_builder.find_input(@form_builder.attribute_name, @options, &@block).input_type.to_s
12
+ attributes = {"data-parsley-type-#{mapped_type}-message".to_sym => I18n::translate("form_validation.message.#{mapped_type}")}
13
+ attributes.merge!({"data-parsley-type" => mapped_type}) unless HTML_NATIVE_TYPES.include?(mapped_type)
14
+ attributes
23
15
  end
24
-
25
16
  end
26
17
  end
27
18
  end
@@ -2,14 +2,14 @@ require 'simple_form'
2
2
 
3
3
  module ParsleySimpleForm
4
4
  class FormBuilder < SimpleForm::FormBuilder
5
- attr_reader :attribute_name
5
+ attr_reader :attribute_name
6
6
 
7
7
  def input(attribute_name, options = {}, &block)
8
8
  @attribute_name = attribute_name
9
9
  @options = options
10
10
  @block = block
11
11
  @options[:input_html] ||= {}
12
- @options[:input_html].merge! i18n_messages
12
+ @options[:input_html].merge! parsley_html
13
13
  super(@attribute_name, @options, &@block)
14
14
  end
15
15
 
@@ -18,38 +18,27 @@ module ParsleySimpleForm
18
18
  end
19
19
 
20
20
  private
21
- def i18n_messages
22
- message = {}
23
- message.merge! message_by_type
24
- message.merge! message_required
25
- message.merge! message_equalto
26
- message.merge! message_notblank
21
+ def parsley_html
22
+ message = {}
23
+
24
+ basic_constraints.each do |constraint|
25
+ message.merge! check_constraint(constraint)
26
+ end
27
27
  message
28
28
  end
29
29
 
30
- def message_by_type
31
- type = Constraints::Basics::TypeConstraint.new(self,@options,&@block)
32
- return type.html_attributes if type.match?
30
+ def check_constraint(constraint)
31
+ constraint_checker = constraint.new(self,@options,&@block)
32
+ return constraint_checker.html_attributes if constraint_checker.match?
33
33
  {}
34
34
  end
35
35
 
36
- def message_required
37
- required = Constraints::Basics::RequiredConstraint.new(self,@options,&@block)
38
- return required.html_attributes if required.match?
39
- {}
36
+ def basic_constraints
37
+ # dinamically load all classes of Constraints::Basics module
38
+ Constraints::Basics.constants
39
+ .select { |c| Constraints::Basics.const_get(c).is_a?(Class) }
40
+ .map { |c| Constraints::Basics.const_get(c) }
40
41
  end
41
42
 
42
- def message_notblank
43
- notblank = Constraints::Basics::NotBlankConstraint.new(self,@options,&@block)
44
- return notblank.html_attributes if notblank.match?
45
- {}
46
- end
47
-
48
- #f.input :password_confirm, :equalto => :password
49
- def message_equalto
50
- equalto = Constraints::Basics::EqualtoConstraint.new(self,@options,&@block)
51
- return equalto.html_attributes if equalto.match?
52
- {}
53
- end
54
43
  end
55
44
  end