nimboids-client_side_validations 3.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. data/client_side_validations.gemspec +45 -0
  2. data/javascript/rails.validations.js +393 -0
  3. data/lib/client_side_validations.rb +8 -0
  4. data/lib/client_side_validations/action_view.rb +13 -0
  5. data/lib/client_side_validations/action_view/form_builder.rb +64 -0
  6. data/lib/client_side_validations/action_view/form_helper.rb +69 -0
  7. data/lib/client_side_validations/action_view/form_tag_helper.rb +12 -0
  8. data/lib/client_side_validations/active_model.rb +52 -0
  9. data/lib/client_side_validations/active_model/acceptance.rb +10 -0
  10. data/lib/client_side_validations/active_model/exclusion.rb +15 -0
  11. data/lib/client_side_validations/active_model/format.rb +10 -0
  12. data/lib/client_side_validations/active_model/inclusion.rb +15 -0
  13. data/lib/client_side_validations/active_model/length.rb +22 -0
  14. data/lib/client_side_validations/active_model/numericality.rb +26 -0
  15. data/lib/client_side_validations/active_model/presence.rb +10 -0
  16. data/lib/client_side_validations/active_record.rb +11 -0
  17. data/lib/client_side_validations/active_record/middleware.rb +25 -0
  18. data/lib/client_side_validations/active_record/uniqueness.rb +26 -0
  19. data/lib/client_side_validations/core_ext.rb +3 -0
  20. data/lib/client_side_validations/core_ext/range.rb +10 -0
  21. data/lib/client_side_validations/core_ext/regexp.rb +14 -0
  22. data/lib/client_side_validations/formtastic.rb +21 -0
  23. data/lib/client_side_validations/middleware.rb +83 -0
  24. data/lib/client_side_validations/mongoid.rb +9 -0
  25. data/lib/client_side_validations/mongoid/middleware.rb +20 -0
  26. data/lib/client_side_validations/mongoid/uniqueness.rb +26 -0
  27. data/lib/client_side_validations/simple_form.rb +24 -0
  28. data/lib/client_side_validations/version.rb +3 -0
  29. data/lib/generators/client_side_validations/install_generator.rb +22 -0
  30. data/lib/generators/templates/README +7 -0
  31. data/lib/generators/templates/client_side_validations.rb +14 -0
  32. data/test/action_view/cases/helper.rb +152 -0
  33. data/test/action_view/cases/test_helpers.rb +237 -0
  34. data/test/action_view/cases/test_legacy_helpers.rb +150 -0
  35. data/test/action_view/models.rb +3 -0
  36. data/test/action_view/models/comment.rb +35 -0
  37. data/test/action_view/models/post.rb +35 -0
  38. data/test/active_model/cases/helper.rb +4 -0
  39. data/test/active_model/cases/test_acceptance_validator.rb +16 -0
  40. data/test/active_model/cases/test_base.rb +11 -0
  41. data/test/active_model/cases/test_confirmation_validator.rb +16 -0
  42. data/test/active_model/cases/test_exclusion_validator.rb +20 -0
  43. data/test/active_model/cases/test_format_validator.rb +21 -0
  44. data/test/active_model/cases/test_inclusion_validator.rb +21 -0
  45. data/test/active_model/cases/test_length_validator.rb +61 -0
  46. data/test/active_model/cases/test_numericality_validator.rb +46 -0
  47. data/test/active_model/cases/test_presence_validator.rb +16 -0
  48. data/test/active_model/cases/test_validations.rb +151 -0
  49. data/test/active_model/models/person.rb +17 -0
  50. data/test/active_record/cases/helper.rb +12 -0
  51. data/test/active_record/cases/test_base.rb +11 -0
  52. data/test/active_record/cases/test_middleware.rb +150 -0
  53. data/test/active_record/cases/test_uniqueness_validator.rb +45 -0
  54. data/test/active_record/models/guid.rb +7 -0
  55. data/test/active_record/models/user.rb +10 -0
  56. data/test/base_helper.rb +6 -0
  57. data/test/core_ext/cases/test_core_ext.rb +45 -0
  58. data/test/formtastic/cases/helper.rb +2 -0
  59. data/test/formtastic/cases/test_form_builder.rb +11 -0
  60. data/test/formtastic/cases/test_form_helper.rb +22 -0
  61. data/test/generators/cases/test_install_generator.rb +15 -0
  62. data/test/javascript/config.ru +3 -0
  63. data/test/javascript/public/test/callbacks/elementAfter.js +54 -0
  64. data/test/javascript/public/test/callbacks/elementBefore.js +54 -0
  65. data/test/javascript/public/test/callbacks/elementFail.js +70 -0
  66. data/test/javascript/public/test/callbacks/elementPass.js +70 -0
  67. data/test/javascript/public/test/callbacks/formAfter.js +45 -0
  68. data/test/javascript/public/test/callbacks/formBefore.js +45 -0
  69. data/test/javascript/public/test/callbacks/formFail.js +51 -0
  70. data/test/javascript/public/test/callbacks/formPass.js +50 -0
  71. data/test/javascript/public/test/form_builders/validateForm.js +66 -0
  72. data/test/javascript/public/test/form_builders/validateFormtastic.js +54 -0
  73. data/test/javascript/public/test/form_builders/validateSimpleForm.js +57 -0
  74. data/test/javascript/public/test/settings.js +15 -0
  75. data/test/javascript/public/test/validateElement.js +144 -0
  76. data/test/javascript/public/test/validators/acceptance.js +42 -0
  77. data/test/javascript/public/test/validators/confirmation.js +25 -0
  78. data/test/javascript/public/test/validators/exclusion.js +41 -0
  79. data/test/javascript/public/test/validators/format.js +27 -0
  80. data/test/javascript/public/test/validators/inclusion.js +42 -0
  81. data/test/javascript/public/test/validators/length.js +70 -0
  82. data/test/javascript/public/test/validators/numericality.js +140 -0
  83. data/test/javascript/public/test/validators/presence.js +15 -0
  84. data/test/javascript/public/test/validators/uniqueness.js +89 -0
  85. data/test/javascript/public/vendor/jquery.metadata.js +122 -0
  86. data/test/javascript/public/vendor/qunit.css +196 -0
  87. data/test/javascript/public/vendor/qunit.js +1374 -0
  88. data/test/javascript/server.rb +78 -0
  89. data/test/javascript/views/index.erb +20 -0
  90. data/test/javascript/views/layout.erb +21 -0
  91. data/test/middleware/cases/helper.rb +15 -0
  92. data/test/middleware/cases/test_middleware.rb +8 -0
  93. data/test/mongoid/cases/helper.rb +16 -0
  94. data/test/mongoid/cases/test_base.rb +15 -0
  95. data/test/mongoid/cases/test_middleware.rb +68 -0
  96. data/test/mongoid/cases/test_uniqueness_validator.rb +44 -0
  97. data/test/mongoid/models/book.rb +8 -0
  98. data/test/simple_form/cases/helper.rb +2 -0
  99. data/test/simple_form/cases/test_form_builder.rb +14 -0
  100. data/test/simple_form/cases/test_form_helper.rb +22 -0
  101. metadata +435 -0
@@ -0,0 +1,150 @@
1
+ require 'action_view/cases/helper'
2
+
3
+ class ClientSideValidations::LegacyActionViewHelpersTest < ActionView::TestCase
4
+ include ActionViewTestSetup
5
+
6
+ def test_text_field
7
+ form_for(@post) do |f|
8
+ concat f.text_field(:cost)
9
+ end
10
+
11
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", "put") do
12
+ %{<input id="post_cost" name="post[cost]" size="30" type="text" />}
13
+ end
14
+ assert_equal expected, output_buffer
15
+ end
16
+
17
+ def test_password_field
18
+ form_for(@post) do |f|
19
+ concat f.password_field(:cost)
20
+ end
21
+
22
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", "put") do
23
+ %{<input id="post_cost" name="post[cost]" size="30" type="password" />}
24
+ end
25
+ assert_equal expected, output_buffer
26
+ end
27
+
28
+ def test_file_field
29
+ form_for(@post) do |f|
30
+ concat f.file_field(:cost)
31
+ end
32
+
33
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", "put") do
34
+ %{<input id="post_cost" name="post[cost]" type="file" />}
35
+ end
36
+ assert_equal expected, output_buffer
37
+ end
38
+
39
+ def test_text_area
40
+ form_for(@post) do |f|
41
+ concat f.text_area(:cost)
42
+ end
43
+
44
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", "put") do
45
+ %{<textarea cols="40" id="post_cost" name="post[cost]" rows="20"></textarea>}
46
+ end
47
+ assert_equal expected, output_buffer
48
+ end
49
+
50
+ def test_search_field
51
+ form_for(@post) do |f|
52
+ concat f.search_field(:cost)
53
+ end
54
+
55
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", "put") do
56
+ %{<input id="post_cost" name="post[cost]" size="30" type="search" />}
57
+ end
58
+ assert_equal expected, output_buffer
59
+ end
60
+
61
+ def test_telephone_field
62
+ form_for(@post) do |f|
63
+ concat f.telephone_field(:cost)
64
+ end
65
+
66
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", "put") do
67
+ %{<input id="post_cost" name="post[cost]" size="30" type="tel" />}
68
+ end
69
+ assert_equal expected, output_buffer
70
+ end
71
+
72
+ def test_phone_field
73
+ form_for(@post) do |f|
74
+ concat f.phone_field(:cost)
75
+ end
76
+
77
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", "put") do
78
+ %{<input id="post_cost" name="post[cost]" size="30" type="tel" />}
79
+ end
80
+ assert_equal expected, output_buffer
81
+ end
82
+
83
+ def test_url_field
84
+ form_for(@post) do |f|
85
+ concat f.url_field(:cost)
86
+ end
87
+
88
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", "put") do
89
+ %{<input id="post_cost" name="post[cost]" size="30" type="url" />}
90
+ end
91
+ assert_equal expected, output_buffer
92
+ end
93
+
94
+ def test_email_field
95
+ form_for(@post) do |f|
96
+ concat f.email_field(:cost)
97
+ end
98
+
99
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", "put") do
100
+ %{<input id="post_cost" name="post[cost]" size="30" type="email" />}
101
+ end
102
+ assert_equal expected, output_buffer
103
+ end
104
+
105
+ def test_number_field
106
+ form_for(@post) do |f|
107
+ concat f.number_field(:cost)
108
+ end
109
+
110
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", "put") do
111
+ %{<input id="post_cost" name="post[cost]" size="30" type="number" />}
112
+ end
113
+ assert_equal expected, output_buffer
114
+ end
115
+
116
+ def test_range_field
117
+ form_for(@post) do |f|
118
+ concat f.range_field(:cost)
119
+ end
120
+
121
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", "put") do
122
+ %{<input id="post_cost" name="post[cost]" size="30" type="range" />}
123
+ end
124
+ assert_equal expected, output_buffer
125
+ end
126
+
127
+ def test_check_box
128
+ form_for(@post) do |f|
129
+ concat f.check_box(:cost)
130
+ end
131
+
132
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", "put") do
133
+ %{<input name="post[cost]" type="hidden" value="0" />} +
134
+ %{<input id="post_cost" name="post[cost]" type="checkbox" value="1" />}
135
+ end
136
+ assert_equal expected, output_buffer
137
+ end
138
+
139
+ def test_radio_button
140
+ form_for(@post) do |f|
141
+ concat f.radio_button(:cost, "10")
142
+ end
143
+
144
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", "put") do
145
+ %{<input id="post_cost_10" name="post[cost]" type="radio" value="10" />}
146
+ end
147
+ assert_equal expected, output_buffer
148
+ end
149
+
150
+ end
@@ -0,0 +1,3 @@
1
+ require 'active_model'
2
+ require 'action_view/models/comment'
3
+ require 'action_view/models/post'
@@ -0,0 +1,35 @@
1
+ class Comment
2
+ extend ActiveModel::Naming
3
+ include ActiveModel::Conversion
4
+ include ActiveModel::Validations
5
+
6
+ attr_reader :id
7
+ attr_reader :post_id
8
+ attr_reader :title
9
+
10
+ validates_presence_of :title
11
+
12
+ def initialize(id = nil, post_id = nil); @id, @post_id = id, post_id end
13
+ def to_key; id ? [id] : nil end
14
+ def save; @id = 1; @post_id = 1 end
15
+ def persisted?; @id.present? end
16
+ def to_param; @id; end
17
+ def name
18
+ @id.nil? ? "new #{self.class.name.downcase}" : "#{self.class.name.downcase} ##{@id}"
19
+ end
20
+
21
+ attr_accessor :relevances
22
+ def relevances_attributes=(attributes); end
23
+
24
+ def client_side_validation_hash
25
+ {
26
+ :title => {
27
+ :presence => {
28
+ :message => "can't be blank"
29
+ }
30
+ }
31
+ }
32
+ end
33
+
34
+ end
35
+
@@ -0,0 +1,35 @@
1
+ class Post < Struct.new(:title, :author_name, :body, :secret, :written_on, :cost)
2
+ extend ActiveModel::Naming
3
+ include ActiveModel::Conversion
4
+ extend ActiveModel::Translation
5
+
6
+ alias_method :secret?, :secret
7
+
8
+ def persisted=(boolean)
9
+ @persisted = boolean
10
+ end
11
+
12
+ def persisted?
13
+ @persisted
14
+ end
15
+
16
+ def client_side_validation_hash
17
+ {
18
+ :cost => {
19
+ :presence => {
20
+ :message => "can't be blank"
21
+ }
22
+ }
23
+ }
24
+ end
25
+
26
+ attr_accessor :author
27
+ def author_attributes=(attributes); end
28
+
29
+ attr_accessor :comments, :comment_ids
30
+ def comments_attributes=(attributes); end
31
+
32
+ attr_accessor :tags
33
+ def tags_attributes=(attributes); end
34
+ end
35
+
@@ -0,0 +1,4 @@
1
+ require 'base_helper'
2
+ require 'active_model'
3
+ require 'client_side_validations/active_model'
4
+ require 'active_model/models/person'
@@ -0,0 +1,16 @@
1
+ require 'active_model/cases/test_base'
2
+
3
+ class ActiveModel::AcceptanceValidatorTest < ClientSideValidations::ActiveModelTestBase
4
+
5
+ def test_acceptance_client_side_hash
6
+ expected_hash = { :message => "must be accepted", :accept => "1" }
7
+ assert_equal expected_hash, AcceptanceValidator.new(:attributes => [:name]).client_side_hash(@person, :age)
8
+ end
9
+
10
+ def test_acceptance_client_side_hash_with_custom_message
11
+ expected_hash = { :message => "you must accept", :accept => "1" }
12
+ assert_equal expected_hash, AcceptanceValidator.new(:attributes => [:name], :message => "you must accept").client_side_hash(@person, :age)
13
+ end
14
+
15
+ end
16
+
@@ -0,0 +1,11 @@
1
+ require 'active_model/cases/helper'
2
+
3
+ class ClientSideValidations::ActiveModelTestBase < ActiveModel::TestCase
4
+ include ActiveModel::Validations
5
+
6
+ def setup
7
+ @person = Person.new
8
+ end
9
+
10
+ end
11
+
@@ -0,0 +1,16 @@
1
+ require 'active_model/cases/test_base'
2
+
3
+ class ActiveModel::ConfirmationValidatorTest < ClientSideValidations::ActiveModelTestBase
4
+
5
+ def test_confirmation_client_side_hash
6
+ expected_hash = { :message => "doesn't match confirmation" }
7
+ assert_equal expected_hash, ConfirmationValidator.new(:attributes => [:name]).client_side_hash(@person, :age)
8
+ end
9
+
10
+ def test_confirmation_client_side_hash_with_custom_message
11
+ expected_hash = { :message => "you must confirm" }
12
+ assert_equal expected_hash, ConfirmationValidator.new(:attributes => [:name], :message => "you must confirm").client_side_hash(@person, :age)
13
+ end
14
+
15
+ end
16
+
@@ -0,0 +1,20 @@
1
+ require 'active_model/cases/test_base'
2
+
3
+ class ActiveModel::ExclusionValidatorTest < ClientSideValidations::ActiveModelTestBase
4
+
5
+ def test_exclusion_client_side_hash
6
+ expected_hash = { :message => "is reserved", :in => [1, 2] }
7
+ assert_equal expected_hash, ExclusionValidator.new(:attributes => [:name], :in => [1, 2]).client_side_hash(@person, :age)
8
+ end
9
+
10
+ def test_exclusion_client_side_hash_with_custom_message
11
+ expected_hash = { :message => "is exclusive", :in => [1, 2] }
12
+ assert_equal expected_hash, ExclusionValidator.new(:attributes => [:name], :in => [1, 2], :message => "is exclusive").client_side_hash(@person, :age)
13
+ end
14
+
15
+ def test_exclusion_client_side_hash_with_ranges
16
+ expected_hash = { :message => "is reserved", :range => 1..2 }
17
+ assert_equal expected_hash, ExclusionValidator.new(:attributes => [:name], :in => 1..2).client_side_hash(@person, :age)
18
+ end
19
+ end
20
+
@@ -0,0 +1,21 @@
1
+ require 'active_model/cases/test_base'
2
+
3
+ class ActiveModel::FormatValidatorTest < ClientSideValidations::ActiveModelTestBase
4
+
5
+ def test_format_client_side_hash
6
+ expected_hash = { :message => "is invalid", :with => /.+/ }
7
+ assert_equal expected_hash, FormatValidator.new(:attributes => [:name], :with => /.+/).client_side_hash(@person, :age)
8
+ end
9
+
10
+ def test_format_client_side_hash_without
11
+ expected_hash = { :message => "is invalid", :without => /.+/ }
12
+ assert_equal expected_hash, FormatValidator.new(:attributes => [:name], :without => /.+/).client_side_hash(@person, :age)
13
+ end
14
+
15
+ def test_format_client_side_hash_with_custom_message
16
+ expected_hash = { :message => "is wrong format", :with => /.+/ }
17
+ assert_equal expected_hash, FormatValidator.new(:attributes => [:name], :with => /.+/, :message => "is wrong format").client_side_hash(@person, :age)
18
+ end
19
+
20
+ end
21
+
@@ -0,0 +1,21 @@
1
+ require 'active_model/cases/test_base'
2
+
3
+ class ActiveModel::InclusionValidatorTest < ClientSideValidations::ActiveModelTestBase
4
+
5
+ def test_inclusion_client_side_hash
6
+ expected_hash = { :message => "is not included in the list", :in => [1, 2] }
7
+ assert_equal expected_hash, InclusionValidator.new(:attributes => [:name], :in => [1, 2]).client_side_hash(@person, :age)
8
+ end
9
+
10
+ def test_inclusion_client_side_hash_with_custom_message
11
+ expected_hash = { :message => "is not a choice", :in => [1, 2] }
12
+ assert_equal expected_hash, InclusionValidator.new(:attributes => [:name], :in => [1, 2], :message => "is not a choice").client_side_hash(@person, :age)
13
+ end
14
+
15
+ def test_inclusion_client_side_hash_with_range
16
+ expected_hash = { :message => "is not included in the list", :range => 1..2 }
17
+ assert_equal expected_hash, InclusionValidator.new(:attributes => [:name], :in => 1..2).client_side_hash(@person, :age)
18
+ end
19
+
20
+ end
21
+
@@ -0,0 +1,61 @@
1
+ require 'active_model/cases/test_base'
2
+
3
+ class ActiveModel::LengthValidatorTest < ClientSideValidations::ActiveModelTestBase
4
+
5
+ def test_length_client_side_hash
6
+ expected_hash = {
7
+ :messages => {
8
+ :is => "is the wrong length (should be 10 characters)"
9
+ },
10
+ :is => 10
11
+ }
12
+ assert_equal expected_hash, LengthValidator.new(:attributes => [:age], :is => 10).client_side_hash(@person, :first_name)
13
+ end
14
+
15
+ def test_length_client_side_hash_with_custom_message
16
+ expected_hash = {
17
+ :messages => {
18
+ :is => "is the wrong length (should be 10 words)"
19
+ },
20
+ :is => 10
21
+ }
22
+ assert_equal expected_hash, LengthValidator.new(:attributes => [:age], :is => 10, :wrong_length => "is the wrong length (should be %{count} words)").client_side_hash(@person, :first_name)
23
+ end
24
+
25
+ def test_length_client_side_hash_with_js_tokenizer
26
+ expected_hash = {
27
+ :messages => {
28
+ :is => "is the wrong length (should be 10 characters)"
29
+ },
30
+ :is => 10,
31
+ :js_tokenizer => %q{match(/\w+/g)}
32
+ }
33
+ assert_equal expected_hash, LengthValidator.new(:attributes => [:age], :is => 10, :tokenizer => proc { |value| value.split(/\w+/) }, :js_tokenizer => %q{match(/\w+/g)}).client_side_hash(@person, :first_name)
34
+ end
35
+
36
+ def test_length_client_side_hash_with_minimum_and_maximum
37
+ expected_hash = {
38
+ :messages => {
39
+ :minimum => "is too short (minimum is 5 characters)",
40
+ :maximum => "is too long (maximum is 10 characters)"
41
+ },
42
+ :minimum => 5,
43
+ :maximum => 10
44
+ }
45
+ assert_equal expected_hash, LengthValidator.new(:attributes => [:age], :minimum => 5, :maximum => 10).client_side_hash(@person, :first_name)
46
+ end
47
+
48
+ def test_length_client_side_hash_with_range
49
+ expected_hash = {
50
+ :messages => {
51
+ :minimum => "is too short (minimum is 5 characters)",
52
+ :maximum => "is too long (maximum is 10 characters)"
53
+ },
54
+ :minimum => 5,
55
+ :maximum => 10
56
+ }
57
+ assert_equal expected_hash, LengthValidator.new(:attributes => [:age], :within => 5..10).client_side_hash(@person, :first_name)
58
+ end
59
+
60
+ end
61
+
@@ -0,0 +1,46 @@
1
+ require 'active_model/cases/test_base'
2
+
3
+ class ActiveModel::NumericalityValidatorTest < ClientSideValidations::ActiveModelTestBase
4
+
5
+ def test_numericality_client_side_hash
6
+ expected_hash = { :messages => { :numericality => "is not a number" } }
7
+ assert_equal expected_hash, NumericalityValidator.new(:attributes => [:age]).client_side_hash(@person, :age)
8
+ end
9
+
10
+ def test_numericality_client_side_hash_with_custom_message
11
+ expected_hash = { :messages => { :numericality => "bad number" } }
12
+ assert_equal expected_hash, NumericalityValidator.new(:attributes => [:age], :message => "bad number").client_side_hash(@person, :age)
13
+ end
14
+
15
+ def test_numericality_client_side_hash_with_options
16
+ expected_hash = {
17
+ :messages => {
18
+ :numericality => "is not a number",
19
+ :only_integer => "must be an integer",
20
+ :greater_than => "must be greater than 10",
21
+ :greater_than_or_equal_to => "must be greater than or equal to 10",
22
+ :equal_to => "must be equal to 10",
23
+ :less_than => "must be less than 10",
24
+ :less_than_or_equal_to => "must be less than or equal to 10",
25
+ :odd => "must be odd",
26
+ :even => "must be even"
27
+ },
28
+ :only_integer => true,
29
+ :greater_than => 10,
30
+ :greater_than_or_equal_to => 10,
31
+ :equal_to => 10,
32
+ :less_than => 10,
33
+ :less_than_or_equal_to => 10,
34
+ :odd => true,
35
+ :even => true
36
+ }
37
+ test_hash = NumericalityValidator.new(:attributes => [:age],
38
+ :only_integer => true, :greater_than => 10, :greater_than_or_equal_to => 10,
39
+ :equal_to => 10, :less_than => 10, :less_than_or_equal_to => 10,
40
+ :odd => true, :even => true).client_side_hash(@person, :age)
41
+
42
+ assert_equal expected_hash, test_hash
43
+ end
44
+
45
+ end
46
+