client_side_validations 0.6.0
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/.document +5 -0
- data/.gitignore +22 -0
- data/LICENSE +24 -0
- data/README.rdoc +17 -0
- data/Rakefile +47 -0
- data/TODO +2 -0
- data/VERSION +1 -0
- data/client_side_validations.gemspec +79 -0
- data/javascript/jspec/commands/example_command.rb +19 -0
- data/javascript/jspec/rhino.js +10 -0
- data/javascript/jspec/unit/jquery.validate.spec.js +146 -0
- data/javascript/jspec/unit/spec.helper.js +0 -0
- data/javascript/lib/client_side_validations.js +92 -0
- data/javascript/vendor/jspec.js +1889 -0
- data/javascript/vendor/jspec.xhr.js +210 -0
- data/lib/adapters/action_view.rb +40 -0
- data/lib/adapters/active_model.rb +90 -0
- data/lib/adapters/active_record_2.rb +90 -0
- data/lib/client_side_validations.rb +47 -0
- data/spec/action_view_2_spec.rb +74 -0
- data/spec/action_view_3_spec.rb +74 -0
- data/spec/active_model_3_spec.rb +271 -0
- data/spec/active_record_2_spec.rb +286 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/support/required_gems.rb +2 -0
- data/tasks/spec.rake +31 -0
- metadata +122 -0
@@ -0,0 +1,74 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
gem 'actionpack', '~> 2.0'
|
3
|
+
require 'action_pack'
|
4
|
+
require 'action_view'
|
5
|
+
require 'client_side_validations'
|
6
|
+
|
7
|
+
describe 'ActionView 2.x Form Helper' do
|
8
|
+
context 'ActionView::Base' do
|
9
|
+
subject { ActionView::Base.new }
|
10
|
+
|
11
|
+
context 'only the url' do
|
12
|
+
before do
|
13
|
+
@object_name = 'object'
|
14
|
+
@url = '/objects/new.json'
|
15
|
+
@expected_javascript = <<-JS
|
16
|
+
<script type="text/javascript">
|
17
|
+
$(document).ready(function() {
|
18
|
+
$('##{@object_name}').clientSideValidations('#{@url}', 'jquery.validate');
|
19
|
+
})
|
20
|
+
</script>
|
21
|
+
JS
|
22
|
+
subject.should_receive(:dom_id).and_return(@object_name)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should generate the proper javascript' do
|
26
|
+
subject.client_side_validations(@object_name,
|
27
|
+
:url => @url).should == @expected_javascript
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'a different adapter' do
|
32
|
+
before do
|
33
|
+
@object_name = 'object'
|
34
|
+
@url = '/objects/new.json'
|
35
|
+
@adapter = 'some.other.adapter'
|
36
|
+
@expected_javascript = <<-JS
|
37
|
+
<script type="text/javascript">
|
38
|
+
$(document).ready(function() {
|
39
|
+
$('##{@object_name}').clientSideValidations('#{@url}', '#{@adapter}');
|
40
|
+
})
|
41
|
+
</script>
|
42
|
+
JS
|
43
|
+
subject.should_receive(:dom_id).and_return(@object_name)
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should generate the proper javascript' do
|
47
|
+
subject.client_side_validations(@object_name,
|
48
|
+
:url => @url, :adapter => @adapter).should == @expected_javascript
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'not including the :url' do
|
53
|
+
before do
|
54
|
+
@object_name = 'object'
|
55
|
+
@adapter = 'some.other.adapter'
|
56
|
+
@expected_javascript = <<-JS
|
57
|
+
<script type="text/javascript">
|
58
|
+
$(document).ready(function() {
|
59
|
+
$('##{@object_name}').clientSideValidations('#{@url}', '#{@some_other_adapter}');
|
60
|
+
})
|
61
|
+
</script>
|
62
|
+
JS
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'should raise an error' do
|
66
|
+
expect {
|
67
|
+
subject.client_side_validations(@object_name,
|
68
|
+
:adapter => @some_other_adapter)
|
69
|
+
}.to raise_error
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
gem 'actionpack', '~> 3.0'
|
3
|
+
require 'action_pack'
|
4
|
+
require 'action_view'
|
5
|
+
require 'client_side_validations'
|
6
|
+
|
7
|
+
describe 'ActionView 2.x Form Helper' do
|
8
|
+
context 'ActionView::Base' do
|
9
|
+
subject { ActionView::Base.new }
|
10
|
+
|
11
|
+
context 'only the url' do
|
12
|
+
before do
|
13
|
+
@object_name = 'object'
|
14
|
+
@url = '/objects/new.json'
|
15
|
+
@expected_javascript = <<-JS
|
16
|
+
<script type="text/javascript">
|
17
|
+
$(document).ready(function() {
|
18
|
+
$('##{@object_name}').clientSideValidations('#{@url}', 'jquery.validate');
|
19
|
+
})
|
20
|
+
</script>
|
21
|
+
JS
|
22
|
+
subject.should_receive(:dom_id).and_return(@object_name)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should generate the proper javascript' do
|
26
|
+
subject.client_side_validations(@object_name,
|
27
|
+
:url => @url).should == @expected_javascript
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'a different adapter' do
|
32
|
+
before do
|
33
|
+
@object_name = 'object'
|
34
|
+
@url = '/objects/new.json'
|
35
|
+
@adapter = 'some.other.adapter'
|
36
|
+
@expected_javascript = <<-JS
|
37
|
+
<script type="text/javascript">
|
38
|
+
$(document).ready(function() {
|
39
|
+
$('##{@object_name}').clientSideValidations('#{@url}', '#{@adapter}');
|
40
|
+
})
|
41
|
+
</script>
|
42
|
+
JS
|
43
|
+
subject.should_receive(:dom_id).and_return(@object_name)
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should generate the proper javascript' do
|
47
|
+
subject.client_side_validations(@object_name,
|
48
|
+
:url => @url, :adapter => @adapter).should == @expected_javascript
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'not including the :url' do
|
53
|
+
before do
|
54
|
+
@object_name = 'object'
|
55
|
+
@adapter = 'some.other.adapter'
|
56
|
+
@expected_javascript = <<-JS
|
57
|
+
<script type="text/javascript">
|
58
|
+
$(document).ready(function() {
|
59
|
+
$('##{@object_name}').clientSideValidations('#{@url}', '#{@some_other_adapter}');
|
60
|
+
})
|
61
|
+
</script>
|
62
|
+
JS
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'should raise an error' do
|
66
|
+
expect {
|
67
|
+
subject.client_side_validations(@object_name,
|
68
|
+
:adapter => @some_other_adapter)
|
69
|
+
}.to raise_error
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,271 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
require 'active_model'
|
3
|
+
require 'client_side_validations'
|
4
|
+
|
5
|
+
describe "Validation to hash" do
|
6
|
+
|
7
|
+
before do
|
8
|
+
class Klass
|
9
|
+
include ActiveModel::Validations
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
after do
|
14
|
+
Object.send(:remove_const, :Klass)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should support validate_presence_of" do
|
18
|
+
Klass.class_eval { validates_presence_of :string }
|
19
|
+
instance = Klass.new
|
20
|
+
expected_hash = { "presence" => { "message" => "can't be blank"} }
|
21
|
+
result_hash = instance.validation_to_hash(:string)
|
22
|
+
result_hash.should == expected_hash
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should support validates_format_of of" do
|
26
|
+
Klass.class_eval { validates_format_of :string, :with => /\A\d\Z/i }
|
27
|
+
instance = Klass.new
|
28
|
+
expected_hash = { "format" => { "message" => "is invalid", "with" => "^\\d$" } }
|
29
|
+
result_hash = instance.validation_to_hash(:string)
|
30
|
+
result_hash.should == expected_hash
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should support different ways to write regex" do
|
34
|
+
Klass.class_eval do
|
35
|
+
validates_format_of :string, :with => /^\d$/i
|
36
|
+
validates_format_of :string_2, :with => /\d/
|
37
|
+
end
|
38
|
+
instance = Klass.new
|
39
|
+
expected_hash_1 = { "format" => { "message" => "is invalid", "with" => "^\\d$" } }
|
40
|
+
expected_hash_2 = { "format" => { "message" => "is invalid", "with" => "\\d" } }
|
41
|
+
result_hash_1 = instance.validation_to_hash(:string)
|
42
|
+
result_hash_2 = instance.validation_to_hash(:string_2)
|
43
|
+
result_hash_1.should == expected_hash_1
|
44
|
+
result_hash_2.should == expected_hash_2
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should support minimum validates_length_of of" do
|
48
|
+
Klass.class_eval { validates_length_of :string, :minimum => 10 }
|
49
|
+
instance = Klass.new
|
50
|
+
expected_hash = { "length" => { "message" => "is too short (minimum is 10 characters)", "minimum" => 10 } }
|
51
|
+
result_hash = instance.validation_to_hash(:string)
|
52
|
+
result_hash.should == expected_hash
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should support maximum validates_length_of of" do
|
56
|
+
Klass.class_eval { validates_length_of :string, :maximum => 10 }
|
57
|
+
instance = Klass.new
|
58
|
+
expected_hash = { "length" => { "message" => "is too long (maximum is 10 characters)", "maximum" => 10 } }
|
59
|
+
result_hash = instance.validation_to_hash(:string)
|
60
|
+
result_hash.should == expected_hash
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should support validations with conditionals" do
|
64
|
+
Klass.class_eval do
|
65
|
+
validates_presence_of :string, :if => :need_string?
|
66
|
+
validates_presence_of :string_2, :unless => :need_string?
|
67
|
+
validates_presence_of :integer, :if => :need_integer?
|
68
|
+
|
69
|
+
def need_string?
|
70
|
+
true
|
71
|
+
end
|
72
|
+
|
73
|
+
def need_integer?
|
74
|
+
false
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
instance = Klass.new
|
79
|
+
expected_hash_1 = { "presence" => { "message" => "can't be blank" } }
|
80
|
+
result_hash_1 = instance.validation_to_hash(:string)
|
81
|
+
result_hash_1.should == expected_hash_1
|
82
|
+
|
83
|
+
expected_hash_2 = { }
|
84
|
+
result_hash_2 = instance.validation_to_hash(:string_2)
|
85
|
+
result_hash_2.should == expected_hash_2
|
86
|
+
|
87
|
+
expected_hash_3 = { }
|
88
|
+
result_hash_3 = instance.validation_to_hash(:integer)
|
89
|
+
result_hash_3.should == expected_hash_3
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should support validating the validates_numericality_of of" do
|
93
|
+
Klass.class_eval { validates_numericality_of :integer }
|
94
|
+
instance = Klass.new
|
95
|
+
expected_hash = { "numericality" => { "message" => "is not a number" } }
|
96
|
+
result_hash = instance.validation_to_hash(:integer)
|
97
|
+
result_hash.should == expected_hash
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should strip out the AR callback options" do
|
101
|
+
Klass.class_eval { validates_presence_of :string, :on => :create }
|
102
|
+
instance = Klass.new
|
103
|
+
expected_hash = { "presence" => { "message" => "can't be blank"} }
|
104
|
+
result_hash = instance.validation_to_hash(:string)
|
105
|
+
result_hash.should == expected_hash
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should support multiple validations for the same method" do
|
109
|
+
Klass.class_eval do
|
110
|
+
validates_presence_of :integer
|
111
|
+
validates_numericality_of :integer
|
112
|
+
end
|
113
|
+
|
114
|
+
instance = Klass.new
|
115
|
+
expected_hash = { "presence" => { "message" => "can't be blank" },
|
116
|
+
"numericality" => { "message" => "is not a number" } }
|
117
|
+
result_hash = instance.validation_to_hash(:integer)
|
118
|
+
result_hash.should == expected_hash
|
119
|
+
end
|
120
|
+
|
121
|
+
context 'with custom validation messages' do
|
122
|
+
before do
|
123
|
+
add_translation(:en, :klass => { :attributes => { :string_2 => { :presence => "String_2" } } })
|
124
|
+
|
125
|
+
Klass.class_eval do
|
126
|
+
validates_presence_of :string, :message => "String"
|
127
|
+
validates_presence_of :string_2, :message => :presence
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
after do
|
132
|
+
remove_translation(:en, :klass)
|
133
|
+
end
|
134
|
+
|
135
|
+
it 'should have a message of "String" for #string' do
|
136
|
+
instance = Klass.new
|
137
|
+
expected_hash = { "presence" => { "message" => "String" } }
|
138
|
+
result_hash = instance.validation_to_hash(:string)
|
139
|
+
result_hash.should == expected_hash
|
140
|
+
end
|
141
|
+
|
142
|
+
it 'should have a message of "String_2" for #string_2' do
|
143
|
+
instance = Klass.new
|
144
|
+
expected_hash = { "presence" => { "message" => "String_2" } }
|
145
|
+
result_hash = instance.validation_to_hash(:string_2)
|
146
|
+
result_hash.should == expected_hash
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
context 'Other languages' do
|
151
|
+
before do
|
152
|
+
add_translation(:es, :klass => { :attributes => { :string => { :presence => "String-es" } } })
|
153
|
+
|
154
|
+
Klass.class_eval do
|
155
|
+
validates_presence_of :string, :message => :presence
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
after do
|
160
|
+
remove_translation(:es, :klass)
|
161
|
+
end
|
162
|
+
|
163
|
+
it 'should result in "String-es" for Spanish translations' do
|
164
|
+
instance = Klass.new
|
165
|
+
expected_hash = { "presence" => { "message" => "String-es" } }
|
166
|
+
result_hash = instance.validation_to_hash(:string, :locale => :es)
|
167
|
+
result_hash.should == expected_hash
|
168
|
+
end
|
169
|
+
|
170
|
+
it 'should result in "String-es" for Spanish translations when passed string "es" instead of symbol' do
|
171
|
+
instance = Klass.new
|
172
|
+
expected_hash = { "presence" => { "message" => "String-es" } }
|
173
|
+
result_hash = instance.validation_to_hash(:string, :locale => "es")
|
174
|
+
result_hash.should == expected_hash
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
xit "should support the regular validate method" do
|
179
|
+
Klass.class_eval do
|
180
|
+
validate :do_something
|
181
|
+
|
182
|
+
def do_something
|
183
|
+
errors.add(:string, "test this out")
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
instance = Klass.new
|
188
|
+
# TODO: Unsupported right now
|
189
|
+
end
|
190
|
+
|
191
|
+
def add_translation(lang, hash)
|
192
|
+
validations = {
|
193
|
+
:errors => {
|
194
|
+
:models => hash
|
195
|
+
}
|
196
|
+
}
|
197
|
+
I18n.backend.store_translations(lang, validations)
|
198
|
+
end
|
199
|
+
|
200
|
+
def remove_translation(lang, key)
|
201
|
+
model_validations = I18n.translate('errors.models')
|
202
|
+
model_validations.delete(key)
|
203
|
+
validations = {
|
204
|
+
:errors => model_validations
|
205
|
+
}
|
206
|
+
I18n.backend.store_translations(lang, validations)
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
describe "Validations to JSON" do
|
211
|
+
before do
|
212
|
+
class Klass
|
213
|
+
include ActiveModel::Validations
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
after do
|
218
|
+
Object.send(:remove_const, :Klass)
|
219
|
+
end
|
220
|
+
|
221
|
+
it "should support a sinlgle validation" do
|
222
|
+
Klass.class_eval do
|
223
|
+
validates_presence_of :string
|
224
|
+
end
|
225
|
+
|
226
|
+
instance = Klass.new
|
227
|
+
expected_json = {:string => { "presence" => { "message" => "can't be blank" } } }.to_json
|
228
|
+
result_json = instance.validations_to_json(:string)
|
229
|
+
result_json.should == expected_json
|
230
|
+
end
|
231
|
+
|
232
|
+
it "should support multiple validations on the same field" do
|
233
|
+
Klass.class_eval do
|
234
|
+
validates_presence_of :number
|
235
|
+
validates_numericality_of :number
|
236
|
+
end
|
237
|
+
|
238
|
+
instance = Klass.new
|
239
|
+
expected_json = {:number => { "presence" => { "message" => "can't be blank" }, "numericality" => { "message" => "is not a number" } } }.to_json
|
240
|
+
result_json = instance.validations_to_json(:number)
|
241
|
+
result_json.should == expected_json
|
242
|
+
end
|
243
|
+
|
244
|
+
it "should support single validations on different fields" do
|
245
|
+
Klass.class_eval do
|
246
|
+
validates_presence_of :string
|
247
|
+
validates_presence_of :string_2
|
248
|
+
end
|
249
|
+
|
250
|
+
instance = Klass.new
|
251
|
+
expected_json = {:string => { "presence" => { "message" => "can't be blank" } },
|
252
|
+
:string_2 => { "presence" => { "message" => "can't be blank" } } }.to_json
|
253
|
+
result_json = instance.validations_to_json(:string, :string_2)
|
254
|
+
result_json.should == expected_json
|
255
|
+
end
|
256
|
+
|
257
|
+
it "should support multiple validations on different fields" do
|
258
|
+
Klass.class_eval do
|
259
|
+
validates_presence_of :number_1
|
260
|
+
validates_numericality_of :number_1
|
261
|
+
validates_presence_of :number_2
|
262
|
+
validates_numericality_of :number_2
|
263
|
+
end
|
264
|
+
|
265
|
+
instance = Klass.new
|
266
|
+
expected_json = {:number_1 => { "presence" => { "message" => "can't be blank" }, "numericality" => { "message" => "is not a number" } },
|
267
|
+
:number_2 => { "presence" => { "message" => "can't be blank" }, "numericality" => { "message" => "is not a number" } } }.to_json
|
268
|
+
result_json = instance.validations_to_json(:number_1, :number_2)
|
269
|
+
result_json.should == expected_json
|
270
|
+
end
|
271
|
+
end
|
@@ -0,0 +1,286 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
gem 'activerecord', '~> 2.0'
|
3
|
+
require 'active_record'
|
4
|
+
require 'client_side_validations'
|
5
|
+
|
6
|
+
describe 'Validations' do
|
7
|
+
describe 'to hash' do
|
8
|
+
|
9
|
+
before do
|
10
|
+
class Klass < ActiveRecord::Base
|
11
|
+
def self.columns() @columns ||= []; end
|
12
|
+
|
13
|
+
def self.column(name, sql_type = nil, default = nil, null = true)
|
14
|
+
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
after do
|
20
|
+
Object.send(:remove_const, :Klass)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should support validate_presence_of" do
|
24
|
+
Klass.class_eval { validates_presence_of :string }
|
25
|
+
instance = Klass.new
|
26
|
+
expected_hash = { "presence" => { "message" => "can't be blank"} }
|
27
|
+
result_hash = instance.validation_to_hash(:string)
|
28
|
+
result_hash.should == expected_hash
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should support validates_format_of of" do
|
32
|
+
Klass.class_eval { validates_format_of :string, :with => /\A\d\Z/i }
|
33
|
+
instance = Klass.new
|
34
|
+
expected_hash = { "format" => { "message" => "is invalid", "with" => "^\\d$" } }
|
35
|
+
result_hash = instance.validation_to_hash(:string)
|
36
|
+
result_hash.should == expected_hash
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should support different ways to write regex" do
|
40
|
+
Klass.class_eval do
|
41
|
+
validates_format_of :string, :with => /^\d$/i
|
42
|
+
validates_format_of :string_2, :with => /\d/
|
43
|
+
end
|
44
|
+
instance = Klass.new
|
45
|
+
expected_hash_1 = { "format" => { "message" => "is invalid", "with" => "^\\d$" } }
|
46
|
+
expected_hash_2 = { "format" => { "message" => "is invalid", "with" => "\\d" } }
|
47
|
+
result_hash_1 = instance.validation_to_hash(:string)
|
48
|
+
result_hash_2 = instance.validation_to_hash(:string_2)
|
49
|
+
result_hash_1.should == expected_hash_1
|
50
|
+
result_hash_2.should == expected_hash_2
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should support minimum validates_length_of of" do
|
54
|
+
Klass.class_eval { validates_length_of :string, :minimum => 10 }
|
55
|
+
instance = Klass.new
|
56
|
+
expected_hash = { "length" => { "message" => "is too short (minimum is 10 characters)", "minimum" => 10 } }
|
57
|
+
result_hash = instance.validation_to_hash(:string)
|
58
|
+
result_hash.should == expected_hash
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should support maximum validates_length_of of" do
|
62
|
+
Klass.class_eval { validates_length_of :string, :maximum => 10 }
|
63
|
+
instance = Klass.new
|
64
|
+
expected_hash = { "length" => { "message" => "is too long (maximum is 10 characters)", "maximum" => 10 } }
|
65
|
+
result_hash = instance.validation_to_hash(:string)
|
66
|
+
result_hash.should == expected_hash
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should support validations with conditionals" do
|
70
|
+
Klass.class_eval do
|
71
|
+
validates_presence_of :string, :if => :need_string?
|
72
|
+
validates_presence_of :string_2, :unless => :need_string?
|
73
|
+
validates_presence_of :integer, :if => :need_integer?
|
74
|
+
|
75
|
+
def need_string?
|
76
|
+
true
|
77
|
+
end
|
78
|
+
|
79
|
+
def need_integer?
|
80
|
+
false
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
instance = Klass.new
|
85
|
+
expected_hash_1 = { "presence" => { "message" => "can't be blank" } }
|
86
|
+
result_hash_1 = instance.validation_to_hash(:string)
|
87
|
+
result_hash_1.should == expected_hash_1
|
88
|
+
|
89
|
+
expected_hash_2 = { }
|
90
|
+
result_hash_2 = instance.validation_to_hash(:string_2)
|
91
|
+
result_hash_2.should == expected_hash_2
|
92
|
+
|
93
|
+
expected_hash_3 = { }
|
94
|
+
result_hash_3 = instance.validation_to_hash(:integer)
|
95
|
+
result_hash_3.should == expected_hash_3
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should support validating the validates_numericality_of of" do
|
99
|
+
Klass.class_eval { validates_numericality_of :integer }
|
100
|
+
instance = Klass.new
|
101
|
+
expected_hash = { "numericality" => { "message" => "is not a number" } }
|
102
|
+
result_hash = instance.validation_to_hash(:integer)
|
103
|
+
result_hash.should == expected_hash
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should strip out the AR callback options" do
|
107
|
+
Klass.class_eval { validates_presence_of :string, :on => :create }
|
108
|
+
instance = Klass.new
|
109
|
+
expected_hash = { "presence" => { "message" => "can't be blank"} }
|
110
|
+
result_hash = instance.validation_to_hash(:string)
|
111
|
+
result_hash.should == expected_hash
|
112
|
+
end
|
113
|
+
|
114
|
+
it "should support multiple validations for the same method" do
|
115
|
+
Klass.class_eval do
|
116
|
+
validates_presence_of :integer
|
117
|
+
validates_numericality_of :integer
|
118
|
+
end
|
119
|
+
|
120
|
+
instance = Klass.new
|
121
|
+
expected_hash = { "presence" => { "message" => "can't be blank" },
|
122
|
+
"numericality" => { "message" => "is not a number" } }
|
123
|
+
result_hash = instance.validation_to_hash(:integer)
|
124
|
+
result_hash.should == expected_hash
|
125
|
+
end
|
126
|
+
|
127
|
+
context 'with custom validation messages' do
|
128
|
+
before do
|
129
|
+
add_translation(:en, :klass => { :attributes => { :string_2 => { :presence => "String_2" } } })
|
130
|
+
|
131
|
+
Klass.class_eval do
|
132
|
+
validates_presence_of :string, :message => "String"
|
133
|
+
validates_presence_of :string_2, :message => :presence
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
after do
|
138
|
+
remove_translation(:en, :klass)
|
139
|
+
end
|
140
|
+
|
141
|
+
it 'should have a message of "String" for #string' do
|
142
|
+
instance = Klass.new
|
143
|
+
expected_hash = { "presence" => { "message" => "String" } }
|
144
|
+
result_hash = instance.validation_to_hash(:string)
|
145
|
+
result_hash.should == expected_hash
|
146
|
+
end
|
147
|
+
|
148
|
+
it 'should have a message of "String_2" for #string_2' do
|
149
|
+
instance = Klass.new
|
150
|
+
expected_hash = { "presence" => { "message" => "String_2" } }
|
151
|
+
result_hash = instance.validation_to_hash(:string_2)
|
152
|
+
result_hash.should == expected_hash
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
context 'Other languages' do
|
157
|
+
before do
|
158
|
+
add_translation(:es, :klass => { :attributes => { :string => { :presence => "String-es" } } })
|
159
|
+
|
160
|
+
Klass.class_eval do
|
161
|
+
validates_presence_of :string, :message => :presence
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
after do
|
166
|
+
remove_translation(:es, :klass)
|
167
|
+
end
|
168
|
+
|
169
|
+
it 'should result in "String-es" for Spanish translations' do
|
170
|
+
instance = Klass.new
|
171
|
+
expected_hash = { "presence" => { "message" => "String-es" } }
|
172
|
+
result_hash = instance.validation_to_hash(:string, :locale => :es)
|
173
|
+
result_hash.should == expected_hash
|
174
|
+
end
|
175
|
+
|
176
|
+
it 'should result in "String-es" for Spanish translations when passed string "es" instead of symbol' do
|
177
|
+
instance = Klass.new
|
178
|
+
expected_hash = { "presence" => { "message" => "String-es" } }
|
179
|
+
result_hash = instance.validation_to_hash(:string, :locale => "es")
|
180
|
+
result_hash.should == expected_hash
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
xit "should support the regular validate method" do
|
185
|
+
Klass.class_eval do
|
186
|
+
validate :do_something
|
187
|
+
|
188
|
+
def do_something
|
189
|
+
errors.add(:string, "test this out")
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
instance = Klass.new
|
194
|
+
# TODO: Unsupported right now
|
195
|
+
end
|
196
|
+
|
197
|
+
def add_translation(lang, hash)
|
198
|
+
validations = {
|
199
|
+
:activerecord => {
|
200
|
+
:errors => {
|
201
|
+
:models => hash
|
202
|
+
}
|
203
|
+
}
|
204
|
+
}
|
205
|
+
I18n.backend.store_translations(lang, validations)
|
206
|
+
end
|
207
|
+
|
208
|
+
def remove_translation(lang, key)
|
209
|
+
model_validations = I18n.translate('activerecord.errors.models')
|
210
|
+
model_validations.delete(key)
|
211
|
+
validations = {
|
212
|
+
:activerecord => {
|
213
|
+
:errors => model_validations
|
214
|
+
}
|
215
|
+
}
|
216
|
+
I18n.backend.store_translations(lang, validations)
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
describe 'to JSON' do
|
221
|
+
before do
|
222
|
+
class Klass < ActiveRecord::Base
|
223
|
+
def self.columns() @columns ||= []; end
|
224
|
+
|
225
|
+
def self.column(name, sql_type = nil, default = nil, null = true)
|
226
|
+
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
|
227
|
+
end
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
after do
|
232
|
+
Object.send(:remove_const, :Klass)
|
233
|
+
end
|
234
|
+
|
235
|
+
it "should support a sinlgle validation" do
|
236
|
+
Klass.class_eval do
|
237
|
+
validates_presence_of :string
|
238
|
+
end
|
239
|
+
|
240
|
+
instance = Klass.new
|
241
|
+
expected_json = {:string => { "presence" => { "message" => "can't be blank" } } }.to_json
|
242
|
+
result_json = instance.validations_to_json(:string)
|
243
|
+
result_json.should == expected_json
|
244
|
+
end
|
245
|
+
|
246
|
+
it "should support multiple validations on the same field" do
|
247
|
+
Klass.class_eval do
|
248
|
+
validates_presence_of :number
|
249
|
+
validates_numericality_of :number
|
250
|
+
end
|
251
|
+
|
252
|
+
instance = Klass.new
|
253
|
+
expected_json = {:number => { "presence" => { "message" => "can't be blank" }, "numericality" => { "message" => "is not a number" } } }.to_json
|
254
|
+
result_json = instance.validations_to_json(:number)
|
255
|
+
result_json.should == expected_json
|
256
|
+
end
|
257
|
+
|
258
|
+
it "should support single validations on different fields" do
|
259
|
+
Klass.class_eval do
|
260
|
+
validates_presence_of :string
|
261
|
+
validates_presence_of :string_2
|
262
|
+
end
|
263
|
+
|
264
|
+
instance = Klass.new
|
265
|
+
expected_json = {:string => { "presence" => { "message" => "can't be blank" } },
|
266
|
+
:string_2 => { "presence" => { "message" => "can't be blank" } } }.to_json
|
267
|
+
result_json = instance.validations_to_json(:string, :string_2)
|
268
|
+
result_json.should == expected_json
|
269
|
+
end
|
270
|
+
|
271
|
+
it "should support multiple validations on different fields" do
|
272
|
+
Klass.class_eval do
|
273
|
+
validates_presence_of :number_1
|
274
|
+
validates_numericality_of :number_1
|
275
|
+
validates_presence_of :number_2
|
276
|
+
validates_numericality_of :number_2
|
277
|
+
end
|
278
|
+
|
279
|
+
instance = Klass.new
|
280
|
+
expected_json = {:number_1 => { "presence" => { "message" => "can't be blank" }, "numericality" => { "message" => "is not a number" } },
|
281
|
+
:number_2 => { "presence" => { "message" => "can't be blank" }, "numericality" => { "message" => "is not a number" } } }.to_json
|
282
|
+
result_json = instance.validations_to_json(:number_1, :number_2)
|
283
|
+
result_json.should == expected_json
|
284
|
+
end
|
285
|
+
end
|
286
|
+
end
|