client_side_validations 0.8.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,286 +0,0 @@
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
data/spec/spec.opts DELETED
@@ -1 +0,0 @@
1
- --color
data/spec/spec_helper.rb DELETED
@@ -1,9 +0,0 @@
1
- $LOAD_PATH.unshift(File.dirname(__FILE__))
2
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
- require 'spec'
4
- require 'spec/autorun'
5
- Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f}
6
-
7
- Spec::Runner.configure do |config|
8
-
9
- end
@@ -1,2 +0,0 @@
1
- require 'rubygems'
2
- require 'ruby-debug'
data/tasks/spec.rake DELETED
@@ -1,31 +0,0 @@
1
- begin
2
- require 'spec'
3
- rescue LoadError
4
- require 'rubygems'
5
- require 'spec'
6
- end
7
- begin
8
- require 'spec/rake/spectask'
9
- rescue LoadError
10
- puts <<-EOS
11
- To use rspec for testing you must install rspec gem:
12
- gem install rspec
13
- EOS
14
- exit(0)
15
- end
16
-
17
- namespace :spec do
18
- desc "Run ActiveRecord specs"
19
- Spec::Rake::SpecTask.new(:active_record) do |t|
20
- t.spec_opts = ['--options', "spec/spec.opts"]
21
- t.spec_files = FileList['spec/**/*_spec.rb']
22
- t.ruby_opts << "-r #{File.join(File.dirname(__FILE__), *%w[gem_loader load_active_support])}"
23
- end
24
-
25
- desc "Run ActiveModel specs"
26
- Spec::Rake::SpecTask.new(:active_model) do |t|
27
- t.spec_opts = ['--options', "spec/spec.opts"]
28
- t.spec_files = FileList['spec/**/*_spec.rb']
29
- t.ruby_opts << "-r #{File.join(File.dirname(__FILE__), *%w[gem_loader load_tzinfo_gem])}"
30
- end
31
- end