af-client_side_validations 3.1.4.af1
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.
- data/client_side_validations.gemspec +34 -0
- data/lib/client_side_validations/action_view/form_builder.rb +160 -0
- data/lib/client_side_validations/action_view/form_helper.rb +92 -0
- data/lib/client_side_validations/action_view/form_tag_helper.rb +12 -0
- data/lib/client_side_validations/action_view.rb +14 -0
- data/lib/client_side_validations/active_model/acceptance.rb +10 -0
- data/lib/client_side_validations/active_model/exclusion.rb +15 -0
- data/lib/client_side_validations/active_model/format.rb +10 -0
- data/lib/client_side_validations/active_model/inclusion.rb +15 -0
- data/lib/client_side_validations/active_model/length.rb +24 -0
- data/lib/client_side_validations/active_model/numericality.rb +31 -0
- data/lib/client_side_validations/active_model/presence.rb +10 -0
- data/lib/client_side_validations/active_model.rb +60 -0
- data/lib/client_side_validations/active_record/middleware.rb +33 -0
- data/lib/client_side_validations/active_record/uniqueness.rb +28 -0
- data/lib/client_side_validations/active_record.rb +11 -0
- data/lib/client_side_validations/core_ext/range.rb +10 -0
- data/lib/client_side_validations/core_ext/regexp.rb +14 -0
- data/lib/client_side_validations/core_ext.rb +3 -0
- data/lib/client_side_validations/engine.rb +6 -0
- data/lib/client_side_validations/files.rb +8 -0
- data/lib/client_side_validations/formtastic.rb +21 -0
- data/lib/client_side_validations/middleware.rb +81 -0
- data/lib/client_side_validations/mongo_mapper/middleware.rb +20 -0
- data/lib/client_side_validations/mongo_mapper/uniqueness.rb +28 -0
- data/lib/client_side_validations/mongo_mapper.rb +9 -0
- data/lib/client_side_validations/mongoid/middleware.rb +20 -0
- data/lib/client_side_validations/mongoid/uniqueness.rb +28 -0
- data/lib/client_side_validations/mongoid.rb +9 -0
- data/lib/client_side_validations/simple_form.rb +24 -0
- data/lib/client_side_validations/version.rb +3 -0
- data/lib/client_side_validations.rb +13 -0
- data/lib/generators/client_side_validations/copy_asset_generator.rb +36 -0
- data/lib/generators/client_side_validations/install_generator.rb +45 -0
- data/lib/generators/templates/client_side_validations/README.rails.3.0 +6 -0
- data/lib/generators/templates/client_side_validations/README.rails.3.1 +7 -0
- data/lib/generators/templates/client_side_validations/initializer.rb +14 -0
- data/test/action_view/cases/helper.rb +176 -0
- data/test/action_view/cases/test_helpers.rb +666 -0
- data/test/action_view/cases/test_legacy_helpers.rb +217 -0
- data/test/action_view/models/comment.rb +35 -0
- data/test/action_view/models/post.rb +35 -0
- data/test/action_view/models.rb +3 -0
- data/test/active_model/cases/helper.rb +4 -0
- data/test/active_model/cases/test_acceptance_validator.rb +16 -0
- data/test/active_model/cases/test_base.rb +11 -0
- data/test/active_model/cases/test_confirmation_validator.rb +16 -0
- data/test/active_model/cases/test_exclusion_validator.rb +20 -0
- data/test/active_model/cases/test_format_validator.rb +21 -0
- data/test/active_model/cases/test_inclusion_validator.rb +21 -0
- data/test/active_model/cases/test_length_validator.rb +61 -0
- data/test/active_model/cases/test_numericality_validator.rb +46 -0
- data/test/active_model/cases/test_presence_validator.rb +16 -0
- data/test/active_model/cases/test_validations.rb +175 -0
- data/test/active_model/models/person.rb +17 -0
- data/test/active_record/cases/helper.rb +12 -0
- data/test/active_record/cases/test_base.rb +11 -0
- data/test/active_record/cases/test_middleware.rb +175 -0
- data/test/active_record/cases/test_uniqueness_validator.rb +50 -0
- data/test/active_record/models/guid.rb +7 -0
- data/test/active_record/models/user.rb +14 -0
- data/test/base_helper.rb +7 -0
- data/test/core_ext/cases/test_core_ext.rb +46 -0
- data/test/formtastic/cases/helper.rb +7 -0
- data/test/formtastic/cases/test_form_builder.rb +11 -0
- data/test/formtastic/cases/test_form_helper.rb +21 -0
- data/test/generators/cases/test_generators.rb +70 -0
- data/test/javascript/config.ru +3 -0
- data/test/javascript/public/test/callbacks/elementAfter.js +54 -0
- data/test/javascript/public/test/callbacks/elementBefore.js +54 -0
- data/test/javascript/public/test/callbacks/elementFail.js +70 -0
- data/test/javascript/public/test/callbacks/elementPass.js +70 -0
- data/test/javascript/public/test/callbacks/formAfter.js +45 -0
- data/test/javascript/public/test/callbacks/formBefore.js +45 -0
- data/test/javascript/public/test/callbacks/formFail.js +51 -0
- data/test/javascript/public/test/callbacks/formPass.js +50 -0
- data/test/javascript/public/test/form_builders/validateForm.js +66 -0
- data/test/javascript/public/test/form_builders/validateFormtastic.js +54 -0
- data/test/javascript/public/test/form_builders/validateNestedForm.js +66 -0
- data/test/javascript/public/test/form_builders/validateSimpleForm.js +57 -0
- data/test/javascript/public/test/settings.js +15 -0
- data/test/javascript/public/test/validateElement.js +209 -0
- data/test/javascript/public/test/validators/acceptance.js +42 -0
- data/test/javascript/public/test/validators/confirmation.js +25 -0
- data/test/javascript/public/test/validators/exclusion.js +41 -0
- data/test/javascript/public/test/validators/format.js +27 -0
- data/test/javascript/public/test/validators/inclusion.js +42 -0
- data/test/javascript/public/test/validators/length.js +76 -0
- data/test/javascript/public/test/validators/numericality.js +158 -0
- data/test/javascript/public/test/validators/presence.js +21 -0
- data/test/javascript/public/test/validators/uniqueness.js +107 -0
- data/test/javascript/public/vendor/jquery.metadata.js +122 -0
- data/test/javascript/public/vendor/qunit.css +196 -0
- data/test/javascript/public/vendor/qunit.js +1374 -0
- data/test/javascript/server.rb +84 -0
- data/test/javascript/views/index.erb +20 -0
- data/test/javascript/views/layout.erb +21 -0
- data/test/middleware/cases/helper.rb +18 -0
- data/test/middleware/cases/test_middleware.rb +8 -0
- data/test/mongo_mapper/cases/helper.rb +9 -0
- data/test/mongo_mapper/cases/test_base.rb +15 -0
- data/test/mongo_mapper/cases/test_middleware.rb +77 -0
- data/test/mongo_mapper/cases/test_uniqueness_validator.rb +50 -0
- data/test/mongo_mapper/models/magazine.rb +11 -0
- data/test/mongoid/cases/helper.rb +16 -0
- data/test/mongoid/cases/test_base.rb +15 -0
- data/test/mongoid/cases/test_middleware.rb +77 -0
- data/test/mongoid/cases/test_uniqueness_validator.rb +49 -0
- data/test/mongoid/models/book.rb +12 -0
- data/test/simple_form/cases/helper.rb +5 -0
- data/test/simple_form/cases/test_form_builder.rb +14 -0
- data/test/simple_form/cases/test_form_helper.rb +24 -0
- data/test/test_loader.rb +6 -0
- data/vendor/assets/javascripts/rails.validations.js +418 -0
- metadata +436 -0
|
@@ -0,0 +1,84 @@
|
|
|
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('<', '<').gsub('>', '>')
|
|
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
|
+
|
|
@@ -0,0 +1,20 @@
|
|
|
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>
|
|
@@ -0,0 +1,21 @@
|
|
|
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>
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
require 'base_helper'
|
|
2
|
+
require 'action_controller'
|
|
3
|
+
require 'action_controller/railtie'
|
|
4
|
+
require 'rails'
|
|
5
|
+
|
|
6
|
+
# Pulled from railties/test/abstract_unit in Rails 3.1
|
|
7
|
+
module TestApp
|
|
8
|
+
class Application < Rails::Application
|
|
9
|
+
config.root = File.dirname(__FILE__)
|
|
10
|
+
config.active_support.deprecation = :log
|
|
11
|
+
config.logger = Logger.new(STDOUT)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
require 'client_side_validations/middleware'
|
|
16
|
+
require 'client_side_validations/engine'
|
|
17
|
+
|
|
18
|
+
TestApp::Application.initialize!
|
|
@@ -0,0 +1,9 @@
|
|
|
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
|
+
|
|
@@ -0,0 +1,15 @@
|
|
|
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
|
+
|
|
@@ -0,0 +1,77 @@
|
|
|
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
|
+
|
|
@@ -0,0 +1,50 @@
|
|
|
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" }
|
|
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
|
|
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
|
+
end
|
|
50
|
+
|
|
@@ -0,0 +1,16 @@
|
|
|
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
|
+
config.slaves = [
|
|
10
|
+
Mongo::Connection.new(host).db(name)
|
|
11
|
+
]
|
|
12
|
+
config.persist_in_safe_mode = false
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
require 'mongoid/models/book'
|
|
16
|
+
|
|
@@ -0,0 +1,15 @@
|
|
|
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
|
+
|
|
@@ -0,0 +1,77 @@
|
|
|
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
|
+
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
require 'mongoid/cases/test_base'
|
|
2
|
+
|
|
3
|
+
class Mongoid::UniqunessValidatorTest < ClientSideValidations::MongoidTestBase
|
|
4
|
+
|
|
5
|
+
def test_uniqueness_client_side_hash
|
|
6
|
+
expected_hash = { :message => "is already taken" }
|
|
7
|
+
assert_equal expected_hash, UniquenessValidator.new(:attributes => [:name]).client_side_hash(@book, :age)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def test_uniqueness_client_side_hash_with_custom_message
|
|
11
|
+
expected_hash = { :message => "is not available" }
|
|
12
|
+
assert_equal expected_hash, UniquenessValidator.new(:attributes => [:name], :message => "is not available").client_side_hash(@book, :age)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def test_uniqueness_client_side_hash
|
|
16
|
+
@book.stubs(:new_record?).returns(false)
|
|
17
|
+
@book.stubs(:id).returns(1)
|
|
18
|
+
expected_hash = { :message => "is already taken", :id => 1 }
|
|
19
|
+
assert_equal expected_hash, UniquenessValidator.new(:attributes => [:name]).client_side_hash(@book, :age)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def test_uniqueness_client_side_hash_with_single_scope_item
|
|
23
|
+
@book.stubs(:author_email).returns("test@test.com")
|
|
24
|
+
expected_hash = { :message => "is already taken", :scope => {:author_email => "test@test.com"} }
|
|
25
|
+
result_hash = UniquenessValidator.new(:attributes => [:author_name], :scope => :author_email).client_side_hash(@book, :author_name)
|
|
26
|
+
assert_equal expected_hash, result_hash
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def test_uniqueness_client_side_hash_with_multiple_scope_items
|
|
30
|
+
@book.stubs(:age).returns(30)
|
|
31
|
+
@book.stubs(:author_email).returns("test@test.com")
|
|
32
|
+
expected_hash = { :message => "is already taken", :scope => {:age => 30, :author_email => "test@test.com"} }
|
|
33
|
+
result_hash = UniquenessValidator.new(:attributes => [:author_name], :scope => [:age, :author_email]).client_side_hash(@book, :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 => "is already taken" }
|
|
39
|
+
result_hash = UniquenessValidator.new(:attributes => [:author_name], :scope => []).client_side_hash(@book, :author_name)
|
|
40
|
+
assert_equal expected_hash, result_hash
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def test_uniqueness_client_side_hash_when_nested_module
|
|
44
|
+
@book = MongoidTestModule::Book2.new
|
|
45
|
+
expected_hash = { :message => "is already taken", :class => 'mongoid_test_module/book2' }
|
|
46
|
+
assert_equal expected_hash, UniquenessValidator.new(:attributes => [:name]).client_side_hash(@book, :age)
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
require 'simple_form/cases/helper'
|
|
2
|
+
|
|
3
|
+
class ClientSideValidations::SimpleForm::FormBuilderTest < Test::Unit::TestCase
|
|
4
|
+
def test_client_side_form_js_hash
|
|
5
|
+
expected = {
|
|
6
|
+
:type => 'SimpleForm::FormBuilder',
|
|
7
|
+
:error_class => :error,
|
|
8
|
+
:error_tag => :span,
|
|
9
|
+
:wrapper_error_class => :field_with_errors,
|
|
10
|
+
:wrapper_tag => :div
|
|
11
|
+
}
|
|
12
|
+
assert_equal expected, SimpleForm::FormBuilder.client_side_form_settings(nil, nil)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
require 'action_view/cases/helper'
|
|
2
|
+
require 'middleware/cases/helper'
|
|
3
|
+
require 'simple_form/cases/helper'
|
|
4
|
+
|
|
5
|
+
class ClientSideValidations::SimpleForm::FormHelperTest < ActionView::TestCase
|
|
6
|
+
include ActionViewTestSetup
|
|
7
|
+
include SimpleForm::ActionViewExtensions::FormHelper
|
|
8
|
+
|
|
9
|
+
def client_side_form_settings_helper
|
|
10
|
+
""
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def test_simple_form_for
|
|
14
|
+
ActionView::TestCase::TestController.any_instance.stubs(:action_name).returns('edit')
|
|
15
|
+
simple_form_for(@post, :validate => true) do |f|
|
|
16
|
+
concat f.input(:cost)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
expected = %{<form accept-charset="UTF-8" action="/posts/123" class="simple_form post" data-validate="true" id="edit_post_123" method="post" novalidate="novalidate"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="_method" type="hidden" value="put" /></div><div class="input string required"><label class="string required" for="post_cost"><abbr title="required">*</abbr> Cost</label><input class="string required" data-validate="true" id="post_cost" name="post[cost]" required="required" size="50" type="text" /></div></form><script>window['edit_post_123'] = {"type":"SimpleForm::FormBuilder","error_class":"error","error_tag":"span","wrapper_error_class":"field_with_errors","wrapper_tag":"div","validators":{"post[cost]":{"presence":{"message":"can't be blank"}}}};</script>}
|
|
20
|
+
assert_equal expected, output_buffer, "\n\n *** If you're running Ruby 1.8 and this test fails is is most likely due to 1.8's lack of insertion order persistence with Hashes ***\n"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
end
|
|
24
|
+
|