client_side_validations-rails_2 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. data/.gitignore +4 -0
  2. data/Gemfile +4 -0
  3. data/HISTORY +5 -0
  4. data/README.markdown +45 -0
  5. data/Rakefile +21 -0
  6. data/client_side_validations-rails_2.gemspec +35 -0
  7. data/generators/client_side_validations/client_side_validations_generator.rb +14 -0
  8. data/lib/client_side_validations/rails_2.rb +5 -0
  9. data/lib/client_side_validations/rails_2/action_view.rb +7 -0
  10. data/lib/client_side_validations/rails_2/action_view/form_helper.rb +11 -0
  11. data/lib/client_side_validations/rails_2/action_view/form_tag_helper.rb +6 -0
  12. data/lib/client_side_validations/rails_2/active_record.rb +14 -0
  13. data/lib/client_side_validations/rails_2/active_record/active_model.rb +12 -0
  14. data/lib/client_side_validations/rails_2/active_record/active_model/validations.rb +51 -0
  15. data/lib/client_side_validations/rails_2/active_record/active_model/validations/base.rb +25 -0
  16. data/lib/client_side_validations/rails_2/active_record/active_model/validations/numericality.rb +5 -0
  17. data/lib/client_side_validations/rails_2/active_record/middleware.rb +42 -0
  18. data/lib/client_side_validations/rails_2/active_record/validations.rb +23 -0
  19. data/lib/client_side_validations/rails_2/middleware.rb +22 -0
  20. data/lib/client_side_validations/rails_2/version.rb +5 -0
  21. data/test/action_view/cases/helper.rb +155 -0
  22. data/test/action_view/cases/test_helpers.rb +516 -0
  23. data/test/action_view/cases/test_legacy_helpers.rb +140 -0
  24. data/test/action_view/models.rb +2 -0
  25. data/test/action_view/models/comment.rb +26 -0
  26. data/test/action_view/models/post.rb +31 -0
  27. data/test/active_record/cases/helper.rb +13 -0
  28. data/test/active_record/cases/test_base.rb +10 -0
  29. data/test/active_record/cases/test_middleware.rb +157 -0
  30. data/test/active_record/cases/test_validation_reflection.rb +144 -0
  31. data/test/active_record/cases/test_validations.rb +27 -0
  32. data/test/active_record/models/guid.rb +7 -0
  33. data/test/active_record/models/user.rb +11 -0
  34. data/test/base_helper.rb +10 -0
  35. data/test/middleware/cases/helper.rb +6 -0
  36. metadata +237 -0
@@ -0,0 +1,140 @@
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]" size=\"30\" 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_check_box
51
+ form_for(@post) do |f|
52
+ concat f.check_box(:cost)
53
+ end
54
+
55
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", "put") do
56
+ %{<input name="post[cost]" type="hidden" value="0" />} +
57
+ %{<input id="post_cost" name="post[cost]" type="checkbox" value="1" />}
58
+ end
59
+ assert_equal expected, output_buffer
60
+ end
61
+
62
+ def test_radio_button
63
+ form_for(@post) do |f|
64
+ concat f.radio_button(:cost, "10")
65
+ end
66
+
67
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", "put") do
68
+ %{<input id="post_cost_10" name="post[cost]" type="radio" value="10" />}
69
+ end
70
+ assert_equal expected, output_buffer
71
+ end
72
+
73
+ def test_fields_for
74
+ result = fields_for(@comment) do |c|
75
+ c.text_field(:title)
76
+ end
77
+
78
+ expected = %{<input id="comment_title" name="comment[title]" size="30" type="text" />}
79
+
80
+ assert_equal expected, result
81
+ end
82
+
83
+ def test_select
84
+ form_for(@post) do |f|
85
+ concat f.select(:cost, [])
86
+ end
87
+
88
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", "put") do
89
+ %{<select id="post_cost" name="post[cost]"></select>}
90
+ end
91
+ assert_equal expected, output_buffer
92
+ end
93
+
94
+ def test_select_multiple
95
+ form_for(@post) do |f|
96
+ concat f.select(:cost, [], {}, :multiple => true)
97
+ end
98
+
99
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", "put") do
100
+ %{<select id="post_cost" multiple="multiple" name="post[cost][]"></select>}
101
+ end
102
+ assert_equal expected, output_buffer
103
+ end
104
+
105
+ def test_collection_select
106
+ form_for(@post) do |f|
107
+ concat f.collection_select(:cost, [], :id, :name)
108
+ end
109
+
110
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", "put") do
111
+ %{<select id="post_cost" name="post[cost]"></select>}
112
+ end
113
+ assert_equal expected, output_buffer
114
+ end
115
+
116
+ def test_grouped_collection_select
117
+ form_for(@post) do |f|
118
+ concat f.grouped_collection_select(:cost, [], :group_method, :group_label_method, :id, :name)
119
+ end
120
+
121
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", "put") do
122
+ %{<select id="post_cost" name="post[cost]"></select>}
123
+ end
124
+ assert_equal expected, output_buffer
125
+ end
126
+
127
+ def test_time_zone_select
128
+ zones = mock('TimeZones')
129
+ zones.stubs(:all).returns([])
130
+ form_for(@post) do |f|
131
+ concat f.time_zone_select(:cost, nil, :model => zones)
132
+ end
133
+
134
+ expected = whole_form("/posts/123", "edit_post_123", "edit_post", "put") do
135
+ %{<select id="post_cost" name="post[cost]"></select>}
136
+ end
137
+ assert_equal expected, output_buffer
138
+ end
139
+ end
140
+
@@ -0,0 +1,2 @@
1
+ require 'action_view/models/comment'
2
+ require 'action_view/models/post'
@@ -0,0 +1,26 @@
1
+ class Comment
2
+ attr_reader :id
3
+ attr_reader :post_id
4
+ attr_reader :title
5
+ def initialize(id = nil, post_id = nil); @id, @post_id = id, post_id end
6
+ def save; @id = 1; @post_id = 1 end
7
+ def new_record?; @id.nil? end
8
+ def to_param; @id; end
9
+ def name
10
+ @id.nil? ? "new #{self.class.name.downcase}" : "#{self.class.name.downcase} ##{@id}"
11
+ end
12
+
13
+ attr_accessor :relevances
14
+ def relevances_attributes=(attributes); end
15
+
16
+ def client_side_validation_hash
17
+ {
18
+ :title => {
19
+ :presence => {
20
+ :message => "can't be blank"
21
+ }
22
+ }
23
+ }
24
+ end
25
+ end
26
+
@@ -0,0 +1,31 @@
1
+ class Post < Struct.new(:title, :author_name, :body, :secret, :written_on, :cost)
2
+ alias_method :secret?, :secret
3
+
4
+ def persisted=(boolean)
5
+ @persisted = boolean
6
+ end
7
+
8
+ def persisted?
9
+ @persisted
10
+ end
11
+
12
+ def client_side_validation_hash
13
+ {
14
+ :cost => {
15
+ :presence => {
16
+ :message => "can't be blank"
17
+ }
18
+ }
19
+ }
20
+ end
21
+
22
+ attr_accessor :author
23
+ def author_attributes=(attributes); end
24
+
25
+ attr_accessor :comments, :comment_ids
26
+ def comments_attributes=(attributes); end
27
+
28
+ attr_accessor :tags
29
+ def tags_attributes=(attributes); end
30
+ end
31
+
@@ -0,0 +1,13 @@
1
+ require 'base_helper'
2
+ require 'active_record'
3
+
4
+ # Connection must be establised before anything else
5
+ ActiveRecord::Base.establish_connection(
6
+ :adapter => defined?(JRUBY_VERSION) ? 'jdbcsqlite3' : 'sqlite3',
7
+ :database => ':memory:'
8
+ )
9
+
10
+ require 'client_side_validations/rails_2/active_record'
11
+ require 'active_record/models/user'
12
+ require 'active_record/models/guid'
13
+
@@ -0,0 +1,10 @@
1
+ require 'active_record/cases/helper'
2
+
3
+ class ClientSideValidations::ActiveRecordTestBase < ActiveRecord::TestCase
4
+
5
+ def setup
6
+ @user = User.new
7
+ end
8
+
9
+ end
10
+
@@ -0,0 +1,157 @@
1
+ # encoding: utf-8
2
+
3
+ require 'middleware/cases/helper'
4
+ require 'active_record/cases/helper'
5
+
6
+ class ClientSideValidationsActiveRecordMiddlewareTest < Test::Unit::TestCase
7
+ include Rack::Test::Methods
8
+
9
+ def teardown
10
+ User.delete_all
11
+ end
12
+
13
+ def with_kcode(kcode)
14
+ if RUBY_VERSION < '1.9'
15
+ orig_kcode, $KCODE = $KCODE, kcode
16
+ begin
17
+ yield
18
+ ensure
19
+ $KCODE = orig_kcode
20
+ end
21
+ else
22
+ yield
23
+ end
24
+ end
25
+
26
+ def app
27
+ app = Proc.new { |env| [200, {}, ['success']] }
28
+ ClientSideValidations::Middleware::Validators.new(app)
29
+ end
30
+
31
+ def test_uniqueness_when_resource_exists
32
+ User.create(:email => 'user@test.com')
33
+ get '/validators/uniqueness.json', { 'user[email]' => 'user@test.com', 'case_sensitive' => true }
34
+
35
+ assert_equal 'false', last_response.body
36
+ assert last_response.ok?
37
+ end
38
+
39
+ def test_uniqueness_when_resource_exists_and_param_order_is_backwards
40
+ User.create(:email => 'user@test.com')
41
+ get '/validators/uniqueness.json', { 'case_sensitive' => true, 'user[email]' => 'user@test.com' }
42
+
43
+ assert_equal 'false', last_response.body
44
+ assert last_response.ok?
45
+ end
46
+
47
+ def test_uniqueness_when_resource_does_not_exist
48
+ get '/validators/uniqueness.json', { 'user[email]' => 'user@test.com', 'case_sensitive' => true }
49
+
50
+ assert_equal 'true', last_response.body
51
+ assert last_response.not_found?
52
+ end
53
+
54
+ def test_uniqueness_when_id_is_given
55
+ user = User.create(:email => 'user@test.com')
56
+ get '/validators/uniqueness.json', { 'user[email]' => 'user@test.com', 'id' => user.id, 'case_sensitive' => true }
57
+
58
+ assert_equal 'true', last_response.body
59
+ assert last_response.not_found?
60
+ end
61
+
62
+ def test_uniqueness_when_scope_is_given
63
+ User.create(:email => 'user@test.com', :age => 25)
64
+ get '/validators/uniqueness.json', { 'user[email]' => 'user@test.com', 'scope' => { 'age' => 30 }, 'case_sensitive' => true }
65
+
66
+ assert_equal 'true', last_response.body
67
+ assert last_response.not_found?
68
+ end
69
+
70
+ def test_uniqueness_when_multiple_scopes_are_given
71
+ User.create(:email => 'user@test.com', :age => 30, :name => 'Brian')
72
+ get '/validators/uniqueness.json', { 'user[email]' => 'user@test.com', 'scope' => { 'age' => 30, 'name' => 'Robert' }, 'case_sensitive' => true }
73
+
74
+ assert_equal 'true', last_response.body
75
+ assert last_response.not_found?
76
+ end
77
+
78
+ def test_uniqueness_when_case_insensitive
79
+ User.create(:name => 'Brian')
80
+ get '/validators/uniqueness.json', { 'user[name]' => 'BRIAN', 'case_sensitive' => false }
81
+
82
+ assert_equal 'false', last_response.body
83
+ assert last_response.ok?
84
+ end
85
+
86
+ def test_uniqueness_when_attribute_passes_as_an_integer
87
+ User.create(:name => 123)
88
+ get '/validators/uniqueness.json', { 'user[name]' => 123, 'case_sensitive' => true }
89
+
90
+ assert_equal 'false', last_response.body
91
+ assert last_response.ok?
92
+ end
93
+
94
+ def test_uniqueness_when_attribute_passes_as_an_integer
95
+ User.create(:name => 123)
96
+ get '/validators/uniqueness.json', { 'user[name]' => 123, 'case_sensitive' => true }
97
+
98
+ assert_equal 'false', last_response.body
99
+ assert last_response.ok?
100
+ end
101
+
102
+ def test_uniqueness_with_columns_which_are_sql_keywords
103
+ Guid.validates_uniqueness_of :key
104
+ assert_nothing_raised do
105
+ get '/validators/uniqueness.json', { 'guid[key]' => 'test', 'case_sensitive' => true }
106
+ end
107
+ end
108
+
109
+ def test_uniqueness_with_limit
110
+ # User.title is limited to 5 characters
111
+ User.create(:title => "abcde")
112
+ get '/validators/uniqueness.json', { 'user[title]' => 'abcdefgh', 'case_sensitive' => true }
113
+
114
+ assert_equal 'false', last_response.body
115
+ assert last_response.ok?
116
+ end
117
+
118
+ # def test_uniqueness_with_limit_and_utf8
119
+ # with_kcode('UTF8') do
120
+ # # User.title is limited to 5 characters
121
+ # User.create(:title => "一二三四五")
122
+ # get '/validators/uniqueness.json', { 'user[title]' => '一二三四五六七八', 'case_sensitive' => true }
123
+
124
+ # assert_equal 'false', last_response.body
125
+ # assert last_response.ok?
126
+ # end
127
+ # end
128
+
129
+ def test_validate_straight_inheritance_uniqueness
130
+ get '/validators/uniqueness.json', { 'inept_wizard[name]' => 'Rincewind', 'case_sensitive' => true }
131
+ assert_equal 'true', last_response.body
132
+ assert last_response.not_found?
133
+
134
+ IneptWizard.create(:name => 'Rincewind')
135
+ get '/validators/uniqueness.json', { 'inept_wizard[name]' => 'Rincewind', 'case_sensitive' => true }
136
+ assert_equal 'false', last_response.body
137
+ assert last_response.ok?
138
+
139
+ get '/validators/uniqueness.json', { 'conjurer[name]' => 'Rincewind', 'case_sensitive' => true }
140
+ assert_equal 'false', last_response.body
141
+ assert last_response.ok?
142
+
143
+ Conjurer.create(:name => 'The Amazing Bonko')
144
+ get '/validators/uniqueness.json', { 'thaumaturgist[name]' => 'The Amazing Bonko', 'case_sensitive' => true }
145
+ assert_equal 'false', last_response.body
146
+ assert last_response.ok?
147
+ end
148
+
149
+ def test_uniqueness_when_resource_is_a_nested_module
150
+ ActiveRecordTestModule::User2.create(:email => 'user@test.com')
151
+ get '/validators/uniqueness.json', { 'active_record_test_module/user2[email]' => 'user@test.com', 'case_sensitive' => true }
152
+
153
+ assert_equal 'false', last_response.body
154
+ assert last_response.ok?
155
+ end
156
+ end
157
+
@@ -0,0 +1,144 @@
1
+ require 'active_record/cases/test_base'
2
+
3
+ class ValidationReflectionTest < ClientSideValidations::ActiveRecordTestBase
4
+
5
+ def new_user
6
+ user = Class.new(User)
7
+ def user.name
8
+ 'User'
9
+ end
10
+ yield(user)
11
+ user.new
12
+ end
13
+
14
+ def test_acceptance_validator
15
+ user = new_user do |u|
16
+ u.validates_acceptance_of :flag
17
+ end
18
+ validator = ActiveModel::Validations::AcceptanceValidator.new(:flag)
19
+ expected = { :flag => [validator] }
20
+ assert_equal expected, user._validators
21
+ assert_equal :acceptance, validator.kind
22
+ user.flag = false
23
+ user.valid?
24
+ assert_equal 'must be accepted', user.errors[:flag]
25
+ end
26
+
27
+ def test_confirmation_validator
28
+ user = new_user do |u|
29
+ u.validates_confirmation_of :name
30
+ end
31
+ validator = ActiveModel::Validations::ConfirmationValidator.new(:name)
32
+ expected = { :name => [validator] }
33
+ assert_equal expected, user._validators
34
+ assert_equal :confirmation, validator.kind
35
+ user.name_confirmation = 'Brian'
36
+ user.valid?
37
+ assert_equal "doesn't match confirmation", user.errors[:name]
38
+ end
39
+
40
+ def test_exclusion_validator
41
+ user = new_user do |u|
42
+ u.validates_exclusion_of :name, :in => ['Brian']
43
+ end
44
+ validator = ActiveModel::Validations::ExclusionValidator.new(:name, :in => ['Brian'])
45
+ expected = { :name => [validator] }
46
+ assert_equal expected, user._validators
47
+ assert_equal :exclusion, validator.kind
48
+ user.name = 'Brian'
49
+ user.valid?
50
+ assert_equal "is reserved", user.errors[:name]
51
+ end
52
+
53
+ def test_format_validator
54
+ user = new_user do |u|
55
+ u.validates_format_of :name, :with => /\w+/
56
+ end
57
+ validator = ActiveModel::Validations::FormatValidator.new(:name, :with => /\w+/)
58
+ expected = { :name => [validator] }
59
+ assert_equal expected, user._validators
60
+ assert_equal :format, validator.kind
61
+ user.valid?
62
+ assert_equal "is invalid", user.errors[:name]
63
+ end
64
+
65
+ def test_inclusion_validator
66
+ user = new_user do |u|
67
+ u.validates_inclusion_of :name, :in => ['Brian']
68
+ end
69
+ validator = ActiveModel::Validations::InclusionValidator.new(:name, :in => ['Brian'])
70
+ expected = { :name => [validator] }
71
+ assert_equal expected, user._validators
72
+ assert_equal :inclusion, validator.kind
73
+ user.name = 'Steph'
74
+ user.valid?
75
+ assert_equal "is not included in the list", user.errors[:name]
76
+ end
77
+
78
+ def test_length_validator
79
+ user = new_user do |u|
80
+ u.validates_length_of :name, :is => 10
81
+ end
82
+ validator = ActiveModel::Validations::LengthValidator.new(:name, :is => 10)
83
+ expected = { :name => [validator] }
84
+ assert_equal expected, user._validators
85
+ assert_equal :length, validator.kind
86
+ user.name = 'Brian'
87
+ user.valid?
88
+ assert_equal "is the wrong length (should be 10 characters)", user.errors[:name]
89
+ end
90
+
91
+ def test_numericality_validator
92
+ user = new_user do |u|
93
+ u.validates_numericality_of :age
94
+ end
95
+ validator = ActiveModel::Validations::NumericalityValidator.new(:age)
96
+ expected = { :age => [validator] }
97
+ assert_equal expected, user._validators
98
+ assert_equal :numericality, validator.kind
99
+ user.age = 'Brian'
100
+ user.valid?
101
+ assert_equal "is not a number", user.errors[:age]
102
+ end
103
+
104
+ def test_presence_validator
105
+ user = new_user do |u|
106
+ u.validates_presence_of :name
107
+ end
108
+ validator = ActiveModel::Validations::PresenceValidator.new(:name)
109
+ expected = { :name => [validator] }
110
+ assert_equal expected, user._validators
111
+ assert_equal :presence, validator.kind
112
+ user.valid?
113
+ assert_equal "can't be blank", user.errors[:name]
114
+ end
115
+
116
+ def test_size_validator
117
+ user = new_user do |u|
118
+ u.validates_size_of :name, :is => 10
119
+ end
120
+ validator = ActiveModel::Validations::LengthValidator.new(:name, :is => 10)
121
+ expected = { :name => [validator] }
122
+ assert_equal expected, user._validators
123
+ assert_equal :length, validator.kind
124
+ user.name = 'brian'
125
+ user.valid?
126
+ assert_equal "is the wrong length (should be 10 characters)", user.errors[:name]
127
+ end
128
+
129
+ def test_uniqueness_validator
130
+ user = new_user do |u|
131
+ u.validates_uniqueness_of :name
132
+ end
133
+ validator = ActiveRecord::Validations::UniquenessValidator.new(:name)
134
+ expected = { :name => [validator] }
135
+ assert_equal expected, user._validators
136
+ assert_equal :uniqueness, validator.kind
137
+ User.create(:name => 'Brian')
138
+ user.name = 'Brian'
139
+ user.valid?
140
+ assert_equal "has already been taken", user.errors[:name]
141
+ User.destroy_all
142
+ end
143
+
144
+ end