client_side_validations 3.1.5 → 3.2.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (105) hide show
  1. data/client_side_validations.gemspec +1 -9
  2. data/lib/client_side_validations.rb +6 -2
  3. data/lib/client_side_validations/action_view/form_builder.rb +4 -2
  4. data/lib/client_side_validations/action_view/form_helper.rb +15 -7
  5. data/lib/client_side_validations/active_model.rb +5 -2
  6. data/lib/client_side_validations/active_record.rb +2 -1
  7. data/lib/client_side_validations/active_record/middleware.rb +15 -2
  8. data/lib/client_side_validations/active_record/uniqueness.rb +0 -1
  9. data/lib/client_side_validations/core_ext/regexp.rb +1 -1
  10. data/lib/client_side_validations/middleware.rb +40 -16
  11. data/lib/client_side_validations/version.rb +1 -1
  12. data/lib/generators/templates/client_side_validations/initializer.rb +0 -3
  13. data/vendor/assets/javascripts/rails.validations.js +135 -170
  14. metadata +22 -329
  15. data/lib/client_side_validations/formtastic.rb +0 -21
  16. data/lib/client_side_validations/mongo_mapper.rb +0 -9
  17. data/lib/client_side_validations/mongo_mapper/middleware.rb +0 -20
  18. data/lib/client_side_validations/mongo_mapper/uniqueness.rb +0 -29
  19. data/lib/client_side_validations/mongoid.rb +0 -9
  20. data/lib/client_side_validations/mongoid/format.rb +0 -10
  21. data/lib/client_side_validations/mongoid/length.rb +0 -24
  22. data/lib/client_side_validations/mongoid/middleware.rb +0 -20
  23. data/lib/client_side_validations/mongoid/presence.rb +0 -10
  24. data/lib/client_side_validations/mongoid/uniqueness.rb +0 -29
  25. data/lib/client_side_validations/simple_form.rb +0 -24
  26. data/lib/generators/templates/client_side_validations/README.rails.3.0 +0 -6
  27. data/test/action_view/cases/helper.rb +0 -176
  28. data/test/action_view/cases/test_helpers.rb +0 -635
  29. data/test/action_view/cases/test_legacy_helpers.rb +0 -217
  30. data/test/action_view/models.rb +0 -3
  31. data/test/action_view/models/comment.rb +0 -35
  32. data/test/action_view/models/post.rb +0 -35
  33. data/test/active_model/cases/helper.rb +0 -4
  34. data/test/active_model/cases/test_acceptance_validator.rb +0 -16
  35. data/test/active_model/cases/test_base.rb +0 -11
  36. data/test/active_model/cases/test_confirmation_validator.rb +0 -16
  37. data/test/active_model/cases/test_exclusion_validator.rb +0 -20
  38. data/test/active_model/cases/test_format_validator.rb +0 -21
  39. data/test/active_model/cases/test_inclusion_validator.rb +0 -21
  40. data/test/active_model/cases/test_length_validator.rb +0 -61
  41. data/test/active_model/cases/test_numericality_validator.rb +0 -46
  42. data/test/active_model/cases/test_presence_validator.rb +0 -16
  43. data/test/active_model/cases/test_validations.rb +0 -175
  44. data/test/active_model/models/person.rb +0 -17
  45. data/test/active_record/cases/helper.rb +0 -12
  46. data/test/active_record/cases/test_base.rb +0 -11
  47. data/test/active_record/cases/test_middleware.rb +0 -175
  48. data/test/active_record/cases/test_uniqueness_validator.rb +0 -56
  49. data/test/active_record/models/guid.rb +0 -7
  50. data/test/active_record/models/user.rb +0 -14
  51. data/test/base_helper.rb +0 -21
  52. data/test/core_ext/cases/test_core_ext.rb +0 -46
  53. data/test/formtastic/cases/helper.rb +0 -7
  54. data/test/formtastic/cases/test_form_builder.rb +0 -11
  55. data/test/formtastic/cases/test_form_helper.rb +0 -21
  56. data/test/generators/cases/test_generators.rb +0 -70
  57. data/test/javascript/config.ru +0 -3
  58. data/test/javascript/public/test/callbacks/elementAfter.js +0 -54
  59. data/test/javascript/public/test/callbacks/elementBefore.js +0 -54
  60. data/test/javascript/public/test/callbacks/elementFail.js +0 -70
  61. data/test/javascript/public/test/callbacks/elementPass.js +0 -70
  62. data/test/javascript/public/test/callbacks/formAfter.js +0 -45
  63. data/test/javascript/public/test/callbacks/formBefore.js +0 -45
  64. data/test/javascript/public/test/callbacks/formFail.js +0 -51
  65. data/test/javascript/public/test/callbacks/formPass.js +0 -50
  66. data/test/javascript/public/test/form_builders/validateForm.js +0 -66
  67. data/test/javascript/public/test/form_builders/validateFormtastic.js +0 -54
  68. data/test/javascript/public/test/form_builders/validateNestedForm.js +0 -66
  69. data/test/javascript/public/test/form_builders/validateSimpleForm.js +0 -57
  70. data/test/javascript/public/test/settings.js +0 -15
  71. data/test/javascript/public/test/validateElement.js +0 -179
  72. data/test/javascript/public/test/validators/acceptance.js +0 -42
  73. data/test/javascript/public/test/validators/confirmation.js +0 -25
  74. data/test/javascript/public/test/validators/exclusion.js +0 -41
  75. data/test/javascript/public/test/validators/format.js +0 -27
  76. data/test/javascript/public/test/validators/inclusion.js +0 -42
  77. data/test/javascript/public/test/validators/length.js +0 -76
  78. data/test/javascript/public/test/validators/numericality.js +0 -158
  79. data/test/javascript/public/test/validators/presence.js +0 -21
  80. data/test/javascript/public/test/validators/uniqueness.js +0 -107
  81. data/test/javascript/public/vendor/jquery.metadata.js +0 -122
  82. data/test/javascript/public/vendor/qunit.css +0 -196
  83. data/test/javascript/public/vendor/qunit.js +0 -1374
  84. data/test/javascript/server.rb +0 -84
  85. data/test/javascript/views/index.erb +0 -20
  86. data/test/javascript/views/layout.erb +0 -21
  87. data/test/middleware/cases/helper.rb +0 -7
  88. data/test/middleware/cases/test_middleware.rb +0 -8
  89. data/test/mongo_mapper/cases/helper.rb +0 -9
  90. data/test/mongo_mapper/cases/test_base.rb +0 -15
  91. data/test/mongo_mapper/cases/test_middleware.rb +0 -77
  92. data/test/mongo_mapper/cases/test_uniqueness_validator.rb +0 -55
  93. data/test/mongo_mapper/models/magazine.rb +0 -11
  94. data/test/mongoid/cases/helper.rb +0 -12
  95. data/test/mongoid/cases/test_base.rb +0 -15
  96. data/test/mongoid/cases/test_format_validator.rb +0 -21
  97. data/test/mongoid/cases/test_length_validator.rb +0 -60
  98. data/test/mongoid/cases/test_middleware.rb +0 -77
  99. data/test/mongoid/cases/test_presence_validator.rb +0 -16
  100. data/test/mongoid/cases/test_uniqueness_validator.rb +0 -54
  101. data/test/mongoid/models/book.rb +0 -12
  102. data/test/simple_form/cases/helper.rb +0 -5
  103. data/test/simple_form/cases/test_form_builder.rb +0 -14
  104. data/test/simple_form/cases/test_form_helper.rb +0 -24
  105. data/test/test_loader.rb +0 -6
@@ -1,84 +0,0 @@
1
- require 'sinatra'
2
- require 'json'
3
- require 'ruby-debug'
4
-
5
- use Rack::Static, :urls => ['/vendor/assets/javascripts'], :root => File.expand_path('../..', settings.root)
6
-
7
- helpers do
8
- def jquery_link version
9
- if params[:version] == version
10
- "[#{version}]"
11
- else
12
- "<a href='/?version=#{version}'>#{version}</a>"
13
- end
14
- end
15
-
16
- def jquery_src
17
- if params[:version] == 'edge' then '/vendor/jquery.js'
18
- else "http://code.jquery.com/jquery-#{params[:version]}.js"
19
- end
20
- end
21
-
22
- def test_base
23
- names = ['/vendor/qunit.js', 'settings']
24
- names.map { |name| script_tag name }.join("\n")
25
- end
26
-
27
- def test *types
28
- types.map do |type|
29
- Dir.glob(File.expand_path("public/test/#{type}", settings.root) + '/*.js').map { |file| File.basename(file) }.map do |file|
30
- script_tag "/test/#{type}/#{file}"
31
- end.join("\n")
32
- end.join("\n")
33
- end
34
-
35
- def script_tag src
36
- src = "/test/#{src}.js" unless src.index('/')
37
- %(<script src='#{src}' type='text/javascript'></script>)
38
- end
39
- end
40
-
41
- get '/' do
42
- params[:version] ||= '1.4.4'
43
- erb :index
44
- end
45
-
46
- get '/validators/uniqueness' do
47
- content_type 'application/json'
48
-
49
- if user = params[:user2]
50
- status 500
51
- 'error'
52
- elsif user = params['active_record_test_module/user2']
53
- status 200
54
- 'false'
55
- elsif scope = params[:scope]
56
- if scope[:name] == 'test name' || scope[:name] == 'taken name'
57
- status 200
58
- 'false'
59
- else
60
- status 404
61
- 'true'
62
- end
63
- elsif params[:case_sensitive] == 'false' && (params[:user][:email] || params[:users][:email]) == 'taken@test.com'
64
- status 200
65
- 'false'
66
- else
67
- status 404
68
- 'true'
69
- end
70
-
71
- end
72
-
73
- post '/users' do
74
- data = { :params => params }.update(request.env)
75
- payload = data.to_json.gsub('<', '&lt;').gsub('>', '&gt;')
76
- <<-HTML
77
- <script>
78
- if (window.top && window.top !== window)
79
- window.top.jQuery.event.trigger('iframe:loaded', #{payload})
80
- </script>
81
- <p id="response">Form submitted</p>
82
- HTML
83
- end
84
-
@@ -1,20 +0,0 @@
1
- <% @title = "client-side-validations test" %>
2
-
3
- <%= test_base %>
4
- <%= script_tag 'validateElement' %>
5
- <%= test :validators, :form_builders, :callbacks %>
6
-
7
- <h1 id="qunit-header"><%= @title %></h1>
8
- <div id="jquery-version">
9
- jQuery version:
10
- <% %w{1.4.1 1.4.2 1.4.3 1.4.4 1.5 1.5.1 1.6}.each do |version| %>
11
- <%= jquery_link version %> •
12
- <% end %>
13
- <%= jquery_link 'edge' if File.exist?(settings.root + '/public/vendor/jquery.js') %>
14
- </div>
15
- <h2 id="qunit-banner"></h2>
16
- <div id="qunit-testrunner-toolbar"></div>
17
- <h2 id="qunit-userAgent"></h2>
18
- <ol id="qunit-tests"></ol>
19
-
20
- <div id="qunit-fixture"></div>
@@ -1,21 +0,0 @@
1
- <!DOCTYPE html>
2
- <html id="html">
3
- <head>
4
- <title><%= @title %></title>
5
- <link href="/vendor/qunit.css" media="screen" rel="stylesheet" type="text/css" media="screen, projection" />
6
- <style>
7
- #jquery-version {
8
- color: #8699A4; text-align: right; font-family: sans-serif; line-height: 1;
9
- margin-top: -1.8em; padding: 0 2em .8em 0;
10
- }
11
- #jquery-version a { color: white; text-decoration: underline; }
12
- </style>
13
-
14
- <%= script_tag jquery_src %>
15
- <%= script_tag "/vendor/assets/javascripts/rails.validations.js" %>
16
- </head>
17
-
18
- <body id="body">
19
- <%= yield %>
20
- </body>
21
- </html>
@@ -1,7 +0,0 @@
1
- require 'base_helper'
2
- require 'action_controller'
3
- require 'action_controller/railtie'
4
- require 'client_side_validations/middleware'
5
- require 'client_side_validations/engine'
6
-
7
- TestApp::Application.initialize!
@@ -1,8 +0,0 @@
1
- require 'middleware/cases/helper'
2
-
3
- class ClientSideValidationsMiddleWareTest < Test::Unit::TestCase
4
- def test_if_middleware_is_auto_included
5
- assert Rails.configuration.middleware.include?(ClientSideValidations::Middleware::Validators)
6
- end
7
- end
8
-
@@ -1,9 +0,0 @@
1
- require 'base_helper'
2
- require 'mongo_mapper'
3
- require 'client_side_validations/mongo_mapper'
4
-
5
- MongoMapper.database = "client_side_validations_development"
6
- MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017)
7
-
8
- require 'mongo_mapper/models/magazine'
9
-
@@ -1,15 +0,0 @@
1
- require 'mongo_mapper/cases/helper'
2
-
3
- class ClientSideValidations::MongoMapperTestBase < Test::Unit::TestCase
4
- include MongoMapper::Plugins::Validations
5
-
6
- def setup
7
- @magazine = Magazine.new
8
- end
9
-
10
- def test_uniqueness_client_side_hash
11
- expected_hash = { :message => "has already been taken", :case_sensitive => true}
12
- assert_equal expected_hash, UniquenessValidator.new(:attributes => [:name]).client_side_hash(@magazine, :age)
13
- end
14
- end
15
-
@@ -1,77 +0,0 @@
1
- require 'middleware/cases/helper'
2
- require 'mongo_mapper/cases/helper'
3
-
4
- class ClientSideValidationsMongoMapperMiddlewareTest < Test::Unit::TestCase
5
- include Rack::Test::Methods
6
-
7
- def setup
8
- # I've been burned enough times with not having the db clear
9
- # I should probably use a db cleaner instead of this
10
- Magazine.delete_all
11
- end
12
-
13
- def teardown
14
- Magazine.delete_all
15
- end
16
-
17
- def app
18
- app = Proc.new { |env| [200, {}, ['success']] }
19
- ClientSideValidations::Middleware::Validators.new(app)
20
- end
21
-
22
- def test_uniqueness_when_resource_exists
23
- Magazine.create(:author_email => 'magazine@test.com')
24
- get '/validators/uniqueness', { 'magazine[author_email]' => 'magazine@test.com' }
25
-
26
- assert_equal 'false', last_response.body
27
- assert last_response.ok?
28
- end
29
-
30
- def test_uniqueness_when_resource_does_not_exist
31
- get '/validators/uniqueness', { 'magazine[author_email]' => 'magazine@test.com' }
32
-
33
- assert_equal 'true', last_response.body
34
- assert last_response.not_found?
35
- end
36
-
37
- def test_uniqueness_when_id_is_given
38
- magazine = Magazine.create(:author_email => 'magazine@test.com')
39
- get '/validators/uniqueness', { 'magazine[author_email]' => 'magazine@test.com', 'id' => magazine.id }
40
-
41
- assert_equal 'true', last_response.body
42
- assert last_response.not_found?
43
- end
44
-
45
- def test_uniqueness_when_scope_is_given
46
- Magazine.create(:author_email => 'magazine@test.com', :age => 25)
47
- get '/validators/uniqueness', { 'magazine[author_email]' => 'magazine@test.com', 'scope' => { 'age' => 30 } }
48
-
49
- assert_equal 'true', last_response.body
50
- assert last_response.not_found?
51
- end
52
-
53
- def test_uniqueness_when_multiple_scopes_are_given
54
- Magazine.create(:author_email => 'magazine@test.com', :age => 30, :author_name => 'Brian')
55
- get '/validators/uniqueness', { 'magazine[author_email]' => 'magazine@test.com', 'scope' => { 'age' => 30, 'author_name' => 'Robert' } }
56
-
57
- assert_equal 'true', last_response.body
58
- assert last_response.not_found?
59
- end
60
-
61
- def test_uniqueness_when_case_insensitive
62
- Magazine.create(:author_name => 'Brian')
63
- get '/validators/uniqueness', { 'magazine[author_name]' => 'BRIAN', 'case_sensitive' => false }
64
-
65
- assert_equal 'false', last_response.body
66
- assert last_response.ok?
67
- end
68
-
69
- def test_uniqueness_when_resource_exists
70
- MongoMapperTestModule::Magazine2.create(:author_email => 'magazine@test.com')
71
- get '/validators/uniqueness', { 'mongo_mapper_test_module/magazine2[author_email]' => 'magazine@test.com' }
72
-
73
- assert_equal 'false', last_response.body
74
- assert last_response.ok?
75
- end
76
- end
77
-
@@ -1,55 +0,0 @@
1
- require 'mongo_mapper/cases/test_base'
2
-
3
- class MongoMapper::UniqunessValidatorTest < ClientSideValidations::MongoMapperTestBase
4
-
5
- def test_uniqueness_client_side_hash
6
- expected_hash = { :message => "has already been taken", :case_sensitive => true }
7
- assert_equal expected_hash, UniquenessValidator.new(:attributes => [:name]).client_side_hash(@magazine, :age)
8
- end
9
-
10
- def test_uniqueness_client_side_hash_with_custom_message
11
- expected_hash = { :message => "is not available" , :case_sensitive => true }
12
- assert_equal expected_hash, UniquenessValidator.new(:attributes => [:name], :message => "is not available").client_side_hash(@magazine, :age)
13
- end
14
-
15
- def test_uniqueness_client_side_hash_with_id
16
- @magazine.stubs(:new_record?).returns(false)
17
- @magazine.stubs(:id).returns(1)
18
- expected_hash = { :message => "has already been taken", :case_sensitive => true , :id => 1 }
19
- assert_equal expected_hash, UniquenessValidator.new(:attributes => [:name]).client_side_hash(@magazine, :age)
20
- end
21
-
22
- def test_uniqueness_client_side_hash_with_single_scope_item
23
- @magazine.stubs(:author_email).returns("test@test.com")
24
- expected_hash = { :message => "has already been taken", :case_sensitive => true , :scope => {:author_email => "test@test.com"} }
25
- result_hash = UniquenessValidator.new(:attributes => [:author_name], :scope => :author_email).client_side_hash(@magazine, :author_name)
26
- assert_equal expected_hash, result_hash
27
- end
28
-
29
- def test_uniqueness_client_side_hash_with_multiple_scope_items
30
- @magazine.stubs(:age).returns(30)
31
- @magazine.stubs(:author_email).returns("test@test.com")
32
- expected_hash = { :message => "has already been taken", :case_sensitive => true , :scope => {:age => 30, :author_email => "test@test.com"} }
33
- result_hash = UniquenessValidator.new(:attributes => [:author_name], :scope => [:age, :author_email]).client_side_hash(@magazine, :author_name)
34
- assert_equal expected_hash, result_hash
35
- end
36
-
37
- def test_uniqueness_client_side_hash_with_empty_scope_array
38
- expected_hash = { :message => "has already been taken" , :case_sensitive => true }
39
- result_hash = UniquenessValidator.new(:attributes => [:author_name], :scope => []).client_side_hash(@magazine, :author_name)
40
- assert_equal expected_hash, result_hash
41
- end
42
-
43
- def test_uniqueness_client_side_hash_when_nested_module
44
- @magazine = MongoMapperTestModule::Magazine2.new
45
- expected_hash = { :message => "has already been taken", :case_sensitive => true, :class => 'mongo_mapper_test_module/magazine2' }
46
- assert_equal expected_hash, UniquenessValidator.new(:attributes => [:name]).client_side_hash(@magazine, :age)
47
- end
48
-
49
- def test_uniqueness_client_side_hash_with_custom_message
50
- expected_hash = { :message => "has already been taken", :case_sensitive => true, :allow_blank => true }
51
- assert_equal expected_hash, UniquenessValidator.new(:attributes => [:name], :allow_blank => true).client_side_hash(@magazine, :age)
52
- end
53
-
54
- end
55
-
@@ -1,11 +0,0 @@
1
- class Magazine
2
- include MongoMapper::Document
3
-
4
- key :age, Integer
5
- key :author_name, String
6
- key :author_email, String
7
- end
8
-
9
- module MongoMapperTestModule
10
- class Magazine2 < Magazine; end
11
- end
@@ -1,12 +0,0 @@
1
- require 'base_helper'
2
- require 'mongoid'
3
- require 'client_side_validations/mongoid'
4
-
5
- Mongoid.configure do |config|
6
- name = 'client_side_validations_development'
7
- host = 'localhost'
8
- config.master = Mongo::Connection.new.db(name)
9
- end
10
-
11
- require 'mongoid/models/book'
12
-
@@ -1,15 +0,0 @@
1
- require 'mongoid/cases/helper'
2
-
3
- class ClientSideValidations::MongoidTestBase < Test::Unit::TestCase
4
- include Mongoid::Validations
5
-
6
- def setup
7
- @book = Book.new
8
- end
9
-
10
- def test_uniqueness_client_side_hash
11
- expected_hash = { :message => "is already taken" }
12
- assert_equal expected_hash, UniquenessValidator.new(:attributes => [:name]).client_side_hash(@book, :age)
13
- end
14
- end
15
-
@@ -1,21 +0,0 @@
1
- require 'mongoid/cases/test_base'
2
-
3
- class Mongoid::UniqunessValidatorTest < ClientSideValidations::MongoidTestBase
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(@book, :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(@book, :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(@book, :age)
18
- end
19
-
20
- end
21
-
@@ -1,60 +0,0 @@
1
- require 'mongoid/cases/test_base'
2
-
3
- class Mongoid::UniqunessValidatorTest < ClientSideValidations::MongoidTestBase
4
- def test_length_client_side_hash
5
- expected_hash = {
6
- :messages => {
7
- :is => "is the wrong length (should be 10 characters)"
8
- },
9
- :is => 10
10
- }
11
- assert_equal expected_hash, LengthValidator.new(:attributes => [:age], :is => 10).client_side_hash(@book, :author_name)
12
- end
13
-
14
- def test_length_client_side_hash_with_custom_message
15
- expected_hash = {
16
- :messages => {
17
- :is => "is the wrong length (should be 10 words)"
18
- },
19
- :is => 10
20
- }
21
- assert_equal expected_hash, LengthValidator.new(:attributes => [:age], :is => 10, :wrong_length => "is the wrong length (should be %{count} words)").client_side_hash(@book, :author_name)
22
- end
23
-
24
- def test_length_client_side_hash_with_js_tokenizer
25
- expected_hash = {
26
- :messages => {
27
- :is => "is the wrong length (should be 10 characters)"
28
- },
29
- :is => 10,
30
- :js_tokenizer => %q{match(/\w+/g)}
31
- }
32
- 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(@book, :author_name)
33
- end
34
-
35
- def test_length_client_side_hash_with_minimum_and_maximum
36
- expected_hash = {
37
- :messages => {
38
- :minimum => "is too short (minimum is 5 characters)",
39
- :maximum => "is too long (maximum is 10 characters)"
40
- },
41
- :minimum => 5,
42
- :maximum => 10
43
- }
44
- assert_equal expected_hash, LengthValidator.new(:attributes => [:age], :minimum => 5, :maximum => 10).client_side_hash(@book, :author_name)
45
- end
46
-
47
- def test_length_client_side_hash_with_range
48
- expected_hash = {
49
- :messages => {
50
- :minimum => "is too short (minimum is 5 characters)",
51
- :maximum => "is too long (maximum is 10 characters)"
52
- },
53
- :minimum => 5,
54
- :maximum => 10
55
- }
56
- assert_equal expected_hash, LengthValidator.new(:attributes => [:age], :within => 5..10).client_side_hash(@book, :author_name)
57
- end
58
-
59
- end
60
-
@@ -1,77 +0,0 @@
1
- require 'middleware/cases/helper'
2
- require 'mongoid/cases/helper'
3
-
4
- class ClientSideValidationsMongoidMiddlewareTest < Test::Unit::TestCase
5
- include Rack::Test::Methods
6
-
7
- def setup
8
- # I've been burned enough times with not having the db clear
9
- # I should probably use a db cleaner instead of this
10
- Book.delete_all
11
- end
12
-
13
- def teardown
14
- Book.delete_all
15
- end
16
-
17
- def app
18
- app = Proc.new { |env| [200, {}, ['success']] }
19
- ClientSideValidations::Middleware::Validators.new(app)
20
- end
21
-
22
- def test_uniqueness_when_resource_exists
23
- Book.create(:author_email => 'book@test.com')
24
- get '/validators/uniqueness', { 'book[author_email]' => 'book@test.com' }
25
-
26
- assert_equal 'false', last_response.body
27
- assert last_response.ok?
28
- end
29
-
30
- def test_uniqueness_when_resource_does_not_exist
31
- get '/validators/uniqueness', { 'book[author_email]' => 'book@test.com' }
32
-
33
- assert_equal 'true', last_response.body
34
- assert last_response.not_found?
35
- end
36
-
37
- def test_uniqueness_when_id_is_given
38
- book = Book.create(:author_email => 'book@test.com')
39
- get '/validators/uniqueness', { 'book[author_email]' => 'book@test.com', 'id' => book.id }
40
-
41
- assert_equal 'true', last_response.body
42
- assert last_response.not_found?
43
- end
44
-
45
- def test_uniqueness_when_scope_is_given
46
- Book.create(:author_email => 'book@test.com', :age => 25)
47
- get '/validators/uniqueness', { 'book[author_email]' => 'book@test.com', 'scope' => { 'age' => 30 } }
48
-
49
- assert_equal 'true', last_response.body
50
- assert last_response.not_found?
51
- end
52
-
53
- def test_uniqueness_when_multiple_scopes_are_given
54
- Book.create(:author_email => 'book@test.com', :age => 30, :author_name => 'Brian')
55
- get '/validators/uniqueness', { 'book[author_email]' => 'book@test.com', 'scope' => { 'age' => 30, 'author_name' => 'Robert' } }
56
-
57
- assert_equal 'true', last_response.body
58
- assert last_response.not_found?
59
- end
60
-
61
- def test_uniqueness_when_case_insensitive
62
- Book.create(:author_name => 'Brian')
63
- get '/validators/uniqueness', { 'book[author_name]' => 'BRIAN', 'case_sensitive' => false }
64
-
65
- assert_equal 'false', last_response.body
66
- assert last_response.ok?
67
- end
68
-
69
- def test_uniqueness_when_resource_is_a_nested_module
70
- MongoidTestModule::Book2.create(:author_email => 'book@test.com')
71
- get '/validators/uniqueness', { 'mongoid_test_module/book2[author_email]' => 'book@test.com' }
72
-
73
- assert_equal 'false', last_response.body
74
- assert last_response.ok?
75
- end
76
- end
77
-