sequel 2.0.1 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +38 -0
- data/README +3 -4
- data/Rakefile +4 -4
- data/lib/sequel_model.rb +22 -2
- data/lib/sequel_model/association_reflection.rb +2 -9
- data/lib/sequel_model/associations.rb +184 -91
- data/lib/sequel_model/base.rb +117 -22
- data/lib/sequel_model/caching.rb +1 -1
- data/lib/sequel_model/dataset_methods.rb +26 -0
- data/lib/sequel_model/eager_loading.rb +16 -20
- data/lib/sequel_model/hooks.rb +1 -1
- data/lib/sequel_model/plugins.rb +1 -1
- data/lib/sequel_model/record.rb +125 -39
- data/lib/sequel_model/validations.rb +101 -115
- data/spec/association_reflection_spec.rb +6 -6
- data/spec/associations_spec.rb +205 -37
- data/spec/base_spec.rb +161 -1
- data/spec/dataset_methods_spec.rb +66 -0
- data/spec/eager_loading_spec.rb +36 -25
- data/spec/model_spec.rb +51 -6
- data/spec/record_spec.rb +172 -62
- data/spec/schema_spec.rb +7 -0
- data/spec/validations_spec.rb +152 -51
- metadata +5 -3
data/spec/schema_spec.rb
CHANGED
@@ -33,6 +33,13 @@ describe Sequel::Model, "create_table" do
|
|
33
33
|
MODEL_DB.sqls.should == ['CREATE TABLE items (name text, price float NOT NULL)']
|
34
34
|
end
|
35
35
|
|
36
|
+
it "should reload the schema from the database" do
|
37
|
+
schem = {:name=>{:type=>:string}, :price=>{:type=>:float}}
|
38
|
+
@model.db.should_receive(:schema).with(:items, :reload=>true).and_return(schem.to_a.sort_by{|x| x[0].to_s})
|
39
|
+
@model.create_table
|
40
|
+
@model.db_schema.should == schem
|
41
|
+
@model.instance_variable_get(:@columns).should == [:name, :price]
|
42
|
+
end
|
36
43
|
end
|
37
44
|
|
38
45
|
describe Sequel::Model, "drop_table" do
|
data/spec/validations_spec.rb
CHANGED
@@ -1,17 +1,15 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), "spec_helper")
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe Sequel::Model::Validation::Errors do
|
4
4
|
setup do
|
5
|
-
@errors = Validation::Errors.new
|
6
|
-
class Validation::Errors
|
7
|
-
attr_accessor :errors
|
8
|
-
end
|
5
|
+
@errors = Sequel::Model::Validation::Errors.new
|
9
6
|
end
|
10
7
|
|
11
8
|
specify "should be clearable using #clear" do
|
12
|
-
@errors.
|
9
|
+
@errors.add(:a, 'b')
|
10
|
+
@errors.should == {:a=>['b']}
|
13
11
|
@errors.clear
|
14
|
-
@errors.
|
12
|
+
@errors.should == {}
|
15
13
|
end
|
16
14
|
|
17
15
|
specify "should be empty if no errors are added" do
|
@@ -52,22 +50,13 @@ describe "Validation::Errors" do
|
|
52
50
|
end
|
53
51
|
end
|
54
52
|
|
55
|
-
describe
|
53
|
+
describe Sequel::Model do
|
56
54
|
setup do
|
57
|
-
@c = Class.new do
|
58
|
-
include Validation
|
59
|
-
attr_reader :before_validation, :after_validation
|
60
|
-
|
55
|
+
@c = Class.new(Sequel::Model) do
|
61
56
|
def self.validates_coolness_of(attr)
|
62
57
|
validates_each(attr) {|o, a, v| o.errors[a] << 'is not cool' if v != :cool}
|
63
58
|
end
|
64
59
|
end
|
65
|
-
|
66
|
-
@d = Class.new do
|
67
|
-
attr_accessor :errors
|
68
|
-
attr_reader :before_validation, :after_validation
|
69
|
-
def initialize; @errors = Validation::Errors.new; end
|
70
|
-
end
|
71
60
|
end
|
72
61
|
|
73
62
|
specify "should respond to validates, validations, has_validations?" do
|
@@ -81,7 +70,7 @@ describe Validation do
|
|
81
70
|
@c.validations[:xx].size.should == 1
|
82
71
|
@c.validations[:yy].size.should == 1
|
83
72
|
|
84
|
-
o = @
|
73
|
+
o = @c.new
|
85
74
|
@c.validations[:xx].first.call(o, :aa, 40)
|
86
75
|
@c.validations[:yy].first.call(o, :bb, 60)
|
87
76
|
|
@@ -100,7 +89,7 @@ describe Validation do
|
|
100
89
|
end
|
101
90
|
@c.validations[:blah].should_not be_empty
|
102
91
|
|
103
|
-
o = @
|
92
|
+
o = @c.new
|
104
93
|
@c.validations[:blah].first.call(o, :ttt, 40)
|
105
94
|
o.errors.full_messages.should == ['ttt is not cool']
|
106
95
|
o.errors.clear
|
@@ -109,14 +98,10 @@ describe Validation do
|
|
109
98
|
end
|
110
99
|
end
|
111
100
|
|
112
|
-
describe
|
101
|
+
describe Sequel::Model do
|
113
102
|
setup do
|
114
|
-
@c = Class.new do
|
115
|
-
|
116
|
-
attr_reader :before_validation, :after_validation
|
117
|
-
|
118
|
-
include Validation
|
119
|
-
|
103
|
+
@c = Class.new(Sequel::Model) do
|
104
|
+
columns :score
|
120
105
|
validates_each :score do |o, a, v|
|
121
106
|
o.errors[a] << 'too low' if v < 87
|
122
107
|
end
|
@@ -144,14 +129,11 @@ describe "A Validation instance" do
|
|
144
129
|
end
|
145
130
|
end
|
146
131
|
|
147
|
-
describe Validation::Generator do
|
132
|
+
describe Sequel::Model::Validation::Generator do
|
148
133
|
setup do
|
149
134
|
$testit = nil
|
150
135
|
|
151
|
-
@c = Class.new do
|
152
|
-
include Validation
|
153
|
-
attr_reader :before_validation, :after_validation
|
154
|
-
|
136
|
+
@c = Class.new(Sequel::Model) do
|
155
137
|
def self.validates_blah
|
156
138
|
$testit = 1324
|
157
139
|
end
|
@@ -159,19 +141,26 @@ describe Validation::Generator do
|
|
159
141
|
end
|
160
142
|
|
161
143
|
specify "should instance_eval the block, sending everything to its receiver" do
|
162
|
-
Validation::Generator.new(@c) do
|
144
|
+
Sequel::Model::Validation::Generator.new(@c) do
|
163
145
|
blah
|
164
146
|
end
|
165
147
|
$testit.should == 1324
|
166
148
|
end
|
167
149
|
end
|
168
150
|
|
169
|
-
describe
|
151
|
+
describe Sequel::Model do
|
170
152
|
setup do
|
171
|
-
@c = Class.new do
|
172
|
-
|
173
|
-
|
174
|
-
|
153
|
+
@c = Class.new(Sequel::Model) do
|
154
|
+
columns :value
|
155
|
+
|
156
|
+
def self.filter(*args)
|
157
|
+
o = Object.new
|
158
|
+
def o.count; 2; end
|
159
|
+
o
|
160
|
+
end
|
161
|
+
|
162
|
+
def skip; false; end
|
163
|
+
def dont_skip; true; end
|
175
164
|
end
|
176
165
|
@m = @c.new
|
177
166
|
end
|
@@ -196,6 +185,36 @@ describe "Validations" do
|
|
196
185
|
@m.should_not be_valid
|
197
186
|
end
|
198
187
|
|
188
|
+
specify "should validate acceptance_of with if => true" do
|
189
|
+
@c.validates_acceptance_of :value, :if => :dont_skip
|
190
|
+
@m.value = '0'
|
191
|
+
@m.should_not be_valid
|
192
|
+
end
|
193
|
+
|
194
|
+
specify "should validate acceptance_of with if => false" do
|
195
|
+
@c.validates_acceptance_of :value, :if => :skip
|
196
|
+
@m.value = '0'
|
197
|
+
@m.should be_valid
|
198
|
+
end
|
199
|
+
|
200
|
+
specify "should validate acceptance_of with if proc that evaluates to true" do
|
201
|
+
@c.validates_acceptance_of :value, :if => proc{true}
|
202
|
+
@m.value = '0'
|
203
|
+
@m.should_not be_valid
|
204
|
+
end
|
205
|
+
|
206
|
+
specify "should validate acceptance_of with if proc that evaluates to false" do
|
207
|
+
@c.validates_acceptance_of :value, :if => proc{false}
|
208
|
+
@m.value = '0'
|
209
|
+
@m.should be_valid
|
210
|
+
end
|
211
|
+
|
212
|
+
specify "should raise an error if :if option is not a Symbol, Proc, or nil" do
|
213
|
+
@c.validates_acceptance_of :value, :if => 1
|
214
|
+
@m.value = '0'
|
215
|
+
proc{@m.valid?}.should raise_error(Sequel::Error)
|
216
|
+
end
|
217
|
+
|
199
218
|
specify "should validate confirmation_of" do
|
200
219
|
@c.send(:attr_accessor, :value_confirmation)
|
201
220
|
@c.validates_confirmation_of :value
|
@@ -206,6 +225,22 @@ describe "Validations" do
|
|
206
225
|
@m.value_confirmation = 'blah'
|
207
226
|
@m.should be_valid
|
208
227
|
end
|
228
|
+
|
229
|
+
specify "should validate confirmation_of with if => true" do
|
230
|
+
@c.send(:attr_accessor, :value_confirmation)
|
231
|
+
@c.validates_confirmation_of :value, :if => :dont_skip
|
232
|
+
|
233
|
+
@m.value = 'blah'
|
234
|
+
@m.should_not be_valid
|
235
|
+
end
|
236
|
+
|
237
|
+
specify "should validate confirmation_of with if => false" do
|
238
|
+
@c.send(:attr_accessor, :value_confirmation)
|
239
|
+
@c.validates_confirmation_of :value, :if => :skip
|
240
|
+
|
241
|
+
@m.value = 'blah'
|
242
|
+
@m.should be_valid
|
243
|
+
end
|
209
244
|
|
210
245
|
specify "should validate format_of" do
|
211
246
|
@c.validates_format_of :value, :with => /.+_.+/
|
@@ -220,6 +255,20 @@ describe "Validations" do
|
|
220
255
|
proc {@c.validates_format_of :value, :with => :blah}.should raise_error(ArgumentError)
|
221
256
|
end
|
222
257
|
|
258
|
+
specify "should validate format_of with if => true" do
|
259
|
+
@c.validates_format_of :value, :with => /_/, :if => :dont_skip
|
260
|
+
|
261
|
+
@m.value = 'a'
|
262
|
+
@m.should_not be_valid
|
263
|
+
end
|
264
|
+
|
265
|
+
specify "should validate format_of with if => false" do
|
266
|
+
@c.validates_format_of :value, :with => /_/, :if => :skip
|
267
|
+
|
268
|
+
@m.value = 'a'
|
269
|
+
@m.should be_valid
|
270
|
+
end
|
271
|
+
|
223
272
|
specify "should validate length_of with maximum" do
|
224
273
|
@c.validates_length_of :value, :maximum => 5
|
225
274
|
@m.should_not be_valid
|
@@ -265,6 +314,20 @@ describe "Validations" do
|
|
265
314
|
@m.should be_valid
|
266
315
|
end
|
267
316
|
|
317
|
+
specify "should validate length_of with if => true" do
|
318
|
+
@c.validates_length_of :value, :is => 3, :if => :dont_skip
|
319
|
+
|
320
|
+
@m.value = 'a'
|
321
|
+
@m.should_not be_valid
|
322
|
+
end
|
323
|
+
|
324
|
+
specify "should validate length_of with if => false" do
|
325
|
+
@c.validates_length_of :value, :is => 3, :if => :skip
|
326
|
+
|
327
|
+
@m.value = 'a'
|
328
|
+
@m.should be_valid
|
329
|
+
end
|
330
|
+
|
268
331
|
specify "should validate numericality_of" do
|
269
332
|
@c.validates_numericality_of :value
|
270
333
|
@m.value = 'blah'
|
@@ -301,6 +364,20 @@ describe "Validations" do
|
|
301
364
|
@m.should_not be_valid
|
302
365
|
end
|
303
366
|
|
367
|
+
specify "should validate numericality_of with if => true" do
|
368
|
+
@c.validates_numericality_of :value, :if => :dont_skip
|
369
|
+
|
370
|
+
@m.value = 'a'
|
371
|
+
@m.should_not be_valid
|
372
|
+
end
|
373
|
+
|
374
|
+
specify "should validate numericality_of with if => false" do
|
375
|
+
@c.validates_numericality_of :value, :if => :skip
|
376
|
+
|
377
|
+
@m.value = 'a'
|
378
|
+
@m.should be_valid
|
379
|
+
end
|
380
|
+
|
304
381
|
specify "should validate presence_of" do
|
305
382
|
@c.validates_presence_of :value
|
306
383
|
@m.should_not be_valid
|
@@ -309,18 +386,47 @@ describe "Validations" do
|
|
309
386
|
@m.value = 1234
|
310
387
|
@m.should be_valid
|
311
388
|
end
|
389
|
+
|
390
|
+
specify "should validate presence_of with if => true" do
|
391
|
+
@c.validates_presence_of :value, :if => :dont_skip
|
392
|
+
@m.should_not be_valid
|
393
|
+
end
|
394
|
+
|
395
|
+
specify "should validate presence_of with if => false" do
|
396
|
+
@c.validates_presence_of :value, :if => :skip
|
397
|
+
@m.should be_valid
|
398
|
+
end
|
399
|
+
|
400
|
+
specify "should validate uniqueness_of with if => true" do
|
401
|
+
@c.validates_uniqueness_of :value, :if => :dont_skip
|
402
|
+
|
403
|
+
@m.value = 'a'
|
404
|
+
@m.should_not be_valid
|
405
|
+
end
|
406
|
+
|
407
|
+
specify "should validate uniqueness_of with if => false" do
|
408
|
+
@c.validates_uniqueness_of :value, :if => :skip
|
409
|
+
|
410
|
+
@m.value = 'a'
|
411
|
+
@m.should be_valid
|
412
|
+
end
|
413
|
+
|
414
|
+
specify "should validate with :if => block" do
|
415
|
+
@c.validates_presence_of :value, :if => proc {false}
|
416
|
+
|
417
|
+
@m.should be_valid
|
418
|
+
end
|
312
419
|
end
|
313
420
|
|
314
421
|
context "Superclass validations" do
|
315
422
|
setup do
|
316
|
-
@c1 = Class.new do
|
317
|
-
|
318
|
-
attr_reader :before_validation, :after_validation
|
319
|
-
attr_accessor :value
|
423
|
+
@c1 = Class.new(Sequel::Model) do
|
424
|
+
columns :value
|
320
425
|
validates_length_of :value, :minimum => 5
|
321
426
|
end
|
322
427
|
|
323
428
|
@c2 = Class.new(@c1) do
|
429
|
+
columns :value
|
324
430
|
validates_format_of :value, :with => /^[a-z]+$/
|
325
431
|
end
|
326
432
|
end
|
@@ -364,11 +470,8 @@ end
|
|
364
470
|
|
365
471
|
context ".validates with block" do
|
366
472
|
specify "should support calling .each" do
|
367
|
-
@c = Class.new do
|
368
|
-
|
369
|
-
attr_reader :before_validation, :after_validation
|
370
|
-
|
371
|
-
include Validation
|
473
|
+
@c = Class.new(Sequel::Model) do
|
474
|
+
columns :vvv
|
372
475
|
validates do
|
373
476
|
each :vvv do |o, a, v|
|
374
477
|
o.errors[a] << "is less than zero" if v.to_i < 0
|
@@ -545,13 +648,11 @@ describe Sequel::Model, "Validations" do
|
|
545
648
|
@user.should_not be_valid
|
546
649
|
@user.errors.full_messages.should == ['username is already taken']
|
547
650
|
|
548
|
-
@user = User.
|
549
|
-
@user.instance_variable_set(:@new, false)
|
651
|
+
@user = User.load(:id=>4, :username => "1record", :password => "anothertest")
|
550
652
|
@user.should_not be_valid
|
551
653
|
@user.errors.full_messages.should == ['username is already taken']
|
552
654
|
|
553
|
-
@user = User.
|
554
|
-
@user.instance_variable_set(:@new, false)
|
655
|
+
@user = User.load(:id=>3, :username => "1record", :password => "anothertest")
|
555
656
|
@user.should be_valid
|
556
657
|
@user.errors.full_messages.should == []
|
557
658
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sequel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremy Evans
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-06-
|
12
|
+
date: 2008-06-17 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -19,7 +19,7 @@ dependencies:
|
|
19
19
|
requirements:
|
20
20
|
- - "="
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: 2.0
|
22
|
+
version: 2.1.0
|
23
23
|
version:
|
24
24
|
description: "The Database Toolkit for Ruby: Model Classes"
|
25
25
|
email: code@jeremyevans.net
|
@@ -49,6 +49,7 @@ files:
|
|
49
49
|
- spec/spec_helper.rb
|
50
50
|
- spec/validations_spec.rb
|
51
51
|
- spec/inflector_spec.rb
|
52
|
+
- spec/dataset_methods_spec.rb
|
52
53
|
- spec/association_reflection_spec.rb
|
53
54
|
- lib/sequel.rb
|
54
55
|
- lib/sequel_model.rb
|
@@ -64,6 +65,7 @@ files:
|
|
64
65
|
- lib/sequel_model/validations.rb
|
65
66
|
- lib/sequel_model/inflector.rb
|
66
67
|
- lib/sequel_model/association_reflection.rb
|
68
|
+
- lib/sequel_model/dataset_methods.rb
|
67
69
|
- CHANGELOG
|
68
70
|
has_rdoc: true
|
69
71
|
homepage: http://sequel.rubyforge.org
|