sequel_model 0.4.2 → 0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +26 -0
- data/README +3 -3
- data/Rakefile +4 -4
- data/lib/sequel_model.rb +17 -16
- data/lib/sequel_model/associations.rb +317 -0
- data/lib/sequel_model/record.rb +2 -2
- data/spec/associations_spec.rb +616 -0
- data/spec/deprecated_relations_spec.rb +153 -0
- data/spec/spec_helper.rb +4 -0
- metadata +8 -7
- data/lib/sequel_model/relations.rb +0 -154
- data/spec/relations_spec.rb +0 -149
data/lib/sequel_model/record.rb
CHANGED
@@ -2,15 +2,15 @@ module Sequel
|
|
2
2
|
class Model
|
3
3
|
attr_reader :values
|
4
4
|
attr_reader :changed_columns
|
5
|
-
|
5
|
+
|
6
6
|
# Returns value of attribute.
|
7
7
|
def [](column)
|
8
8
|
@values[column]
|
9
9
|
end
|
10
10
|
# Sets value of attribute and marks the column as changed.
|
11
11
|
def []=(column, value)
|
12
|
-
@values[column] = value
|
13
12
|
@changed_columns << column unless @changed_columns.include?(column)
|
13
|
+
@values[column] = value
|
14
14
|
end
|
15
15
|
|
16
16
|
# Enumerates through all attributes.
|
@@ -0,0 +1,616 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "spec_helper")
|
2
|
+
|
3
|
+
describe Sequel::Model, "many_to_one" do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
MODEL_DB.reset
|
7
|
+
|
8
|
+
@c2 = Class.new(Sequel::Model(:nodes)) do
|
9
|
+
def columns; [:id, :parent_id]; end
|
10
|
+
end
|
11
|
+
|
12
|
+
@dataset = @c2.dataset
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should use implicit key if omitted" do
|
16
|
+
@c2.many_to_one :parent, :class => @c2
|
17
|
+
|
18
|
+
d = @c2.new(:id => 1, :parent_id => 234)
|
19
|
+
p = d.parent
|
20
|
+
p.class.should == @c2
|
21
|
+
p.values.should == {:x => 1, :id => 1}
|
22
|
+
|
23
|
+
MODEL_DB.sqls.should == ["SELECT * FROM nodes WHERE (id = 234) LIMIT 1"]
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should use implicit class if omitted" do
|
27
|
+
class ParParent < Sequel::Model
|
28
|
+
end
|
29
|
+
|
30
|
+
@c2.many_to_one :par_parent
|
31
|
+
|
32
|
+
d = @c2.new(:id => 1, :par_parent_id => 234)
|
33
|
+
p = d.par_parent
|
34
|
+
p.class.should == ParParent
|
35
|
+
|
36
|
+
MODEL_DB.sqls.should == ["SELECT * FROM par_parents WHERE (id = 234) LIMIT 1"]
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should use explicit key if given" do
|
40
|
+
@c2.many_to_one :parent, :class => @c2, :key => :blah
|
41
|
+
|
42
|
+
d = @c2.new(:id => 1, :blah => 567)
|
43
|
+
p = d.parent
|
44
|
+
p.class.should == @c2
|
45
|
+
p.values.should == {:x => 1, :id => 1}
|
46
|
+
|
47
|
+
MODEL_DB.sqls.should == ["SELECT * FROM nodes WHERE (id = 567) LIMIT 1"]
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should return nil if key value is nil" do
|
51
|
+
@c2.many_to_one :parent, :class => @c2
|
52
|
+
|
53
|
+
d = @c2.new(:id => 1)
|
54
|
+
d.parent.should == nil
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should define a setter method" do
|
58
|
+
@c2.many_to_one :parent, :class => @c2
|
59
|
+
|
60
|
+
d = @c2.new(:id => 1)
|
61
|
+
d.parent = @c2.new(:id => 4321)
|
62
|
+
d.values.should == {:id => 1, :parent_id => 4321}
|
63
|
+
|
64
|
+
d.parent = nil
|
65
|
+
d.values.should == {:id => 1, :parent_id => nil}
|
66
|
+
|
67
|
+
e = @c2.new(:id => 6677)
|
68
|
+
d.parent = e
|
69
|
+
d.values.should == {:id => 1, :parent_id => 6677}
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should not persist changes until saved" do
|
73
|
+
@c2.many_to_one :parent, :class => @c2
|
74
|
+
|
75
|
+
d = @c2.create(:id => 1)
|
76
|
+
MODEL_DB.reset
|
77
|
+
d.parent = @c2.new(:id => 345)
|
78
|
+
MODEL_DB.sqls.should == []
|
79
|
+
d.save_changes
|
80
|
+
MODEL_DB.sqls.should == ['UPDATE nodes SET parent_id = 345 WHERE (id = 1)']
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should set cached instance variable when accessed" do
|
84
|
+
@c2.many_to_one :parent, :class => @c2
|
85
|
+
|
86
|
+
d = @c2.create(:id => 1)
|
87
|
+
MODEL_DB.reset
|
88
|
+
d.parent_id = 234
|
89
|
+
d.instance_variables.include?("@parent").should == false
|
90
|
+
e = d.parent
|
91
|
+
MODEL_DB.sqls.should == ["SELECT * FROM nodes WHERE (id = 234) LIMIT 1"]
|
92
|
+
d.instance_variable_get("@parent").should == e
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should set cached instance variable when assigned" do
|
96
|
+
@c2.many_to_one :parent, :class => @c2
|
97
|
+
|
98
|
+
d = @c2.create(:id => 1)
|
99
|
+
MODEL_DB.reset
|
100
|
+
d.instance_variables.include?("@parent").should == false
|
101
|
+
d.parent = @c2.new(:id => 234)
|
102
|
+
e = d.parent
|
103
|
+
d.instance_variable_get("@parent").should == e
|
104
|
+
MODEL_DB.sqls.should == []
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should use cached instance variable if available" do
|
108
|
+
@c2.many_to_one :parent, :class => @c2
|
109
|
+
|
110
|
+
d = @c2.create(:id => 1, :parent_id => 234)
|
111
|
+
MODEL_DB.reset
|
112
|
+
d.instance_variable_set(:@parent, 42)
|
113
|
+
d.parent.should == 42
|
114
|
+
MODEL_DB.sqls.should == []
|
115
|
+
end
|
116
|
+
|
117
|
+
it "should not use cached instance variable if asked to reload" do
|
118
|
+
@c2.many_to_one :parent, :class => @c2
|
119
|
+
|
120
|
+
d = @c2.create(:id => 1)
|
121
|
+
MODEL_DB.reset
|
122
|
+
d.parent_id = 234
|
123
|
+
d.instance_variable_set(:@parent, 42)
|
124
|
+
d.parent(true).should_not == 42
|
125
|
+
MODEL_DB.sqls.should == ["SELECT * FROM nodes WHERE (id = 234) LIMIT 1"]
|
126
|
+
end
|
127
|
+
|
128
|
+
it "should have belongs_to alias" do
|
129
|
+
@c2.belongs_to :parent, :class => @c2
|
130
|
+
|
131
|
+
d = @c2.create(:id => 1)
|
132
|
+
MODEL_DB.reset
|
133
|
+
d.parent_id = 234
|
134
|
+
d.instance_variables.include?("@parent").should == false
|
135
|
+
e = d.parent
|
136
|
+
MODEL_DB.sqls.should == ["SELECT * FROM nodes WHERE (id = 234) LIMIT 1"]
|
137
|
+
d.instance_variable_get("@parent").should == e
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
describe Sequel::Model, "one_to_many" do
|
142
|
+
|
143
|
+
before(:each) do
|
144
|
+
MODEL_DB.reset
|
145
|
+
|
146
|
+
@c1 = Class.new(Sequel::Model(:attributes)) do
|
147
|
+
def columns; [:id, :node_id]; end
|
148
|
+
end
|
149
|
+
|
150
|
+
@c2 = Class.new(Sequel::Model(:nodes)) do
|
151
|
+
attr_accessor :xxx
|
152
|
+
|
153
|
+
def self.name
|
154
|
+
'Node'
|
155
|
+
end
|
156
|
+
end
|
157
|
+
@dataset = @c2.dataset
|
158
|
+
|
159
|
+
@c2.dataset.extend(Module.new {
|
160
|
+
def fetch_rows(sql)
|
161
|
+
@db << sql
|
162
|
+
yield Hash.new
|
163
|
+
end
|
164
|
+
})
|
165
|
+
|
166
|
+
@c1.dataset.extend(Module.new {
|
167
|
+
def fetch_rows(sql)
|
168
|
+
@db << sql
|
169
|
+
yield Hash.new
|
170
|
+
end
|
171
|
+
})
|
172
|
+
end
|
173
|
+
|
174
|
+
it "should use implicit key if omitted" do
|
175
|
+
@c2.one_to_many :attributes, :class => @c1
|
176
|
+
|
177
|
+
n = @c2.new(:id => 1234)
|
178
|
+
a = n.attributes
|
179
|
+
a.should be_a_kind_of(Sequel::Dataset)
|
180
|
+
a.sql.should == 'SELECT * FROM attributes WHERE (node_id = 1234)'
|
181
|
+
end
|
182
|
+
|
183
|
+
it "should use implicit class if omitted" do
|
184
|
+
class HistoricalValue < Sequel::Model
|
185
|
+
end
|
186
|
+
|
187
|
+
@c2.one_to_many :historical_values
|
188
|
+
|
189
|
+
n = @c2.new(:id => 1234)
|
190
|
+
v = n.historical_values
|
191
|
+
v.should be_a_kind_of(Sequel::Dataset)
|
192
|
+
v.sql.should == 'SELECT * FROM historical_values WHERE (node_id = 1234)'
|
193
|
+
v.model_classes.should == {nil => HistoricalValue}
|
194
|
+
end
|
195
|
+
|
196
|
+
it "should use explicit key if given" do
|
197
|
+
@c2.one_to_many :attributes, :class => @c1, :key => :nodeid
|
198
|
+
|
199
|
+
n = @c2.new(:id => 1234)
|
200
|
+
a = n.attributes
|
201
|
+
a.should be_a_kind_of(Sequel::Dataset)
|
202
|
+
a.sql.should == 'SELECT * FROM attributes WHERE (nodeid = 1234)'
|
203
|
+
end
|
204
|
+
|
205
|
+
it "should define an add_ method" do
|
206
|
+
@c2.one_to_many :attributes, :class => @c1
|
207
|
+
|
208
|
+
n = @c2.new(:id => 1234)
|
209
|
+
a = @c1.new(:id => 2345)
|
210
|
+
a.save!
|
211
|
+
MODEL_DB.reset
|
212
|
+
a.should == n.add_attribute(a)
|
213
|
+
MODEL_DB.sqls.should == ['UPDATE attributes SET node_id = 1234 WHERE (id = 2345)']
|
214
|
+
end
|
215
|
+
|
216
|
+
it "should define a remove_ method" do
|
217
|
+
@c2.one_to_many :attributes, :class => @c1
|
218
|
+
|
219
|
+
n = @c2.new(:id => 1234)
|
220
|
+
a = @c1.new(:id => 2345)
|
221
|
+
a.save!
|
222
|
+
MODEL_DB.reset
|
223
|
+
a.should == n.remove_attribute(a)
|
224
|
+
MODEL_DB.sqls.should == ['UPDATE attributes SET node_id = NULL WHERE (id = 2345)']
|
225
|
+
end
|
226
|
+
|
227
|
+
it "should accept a block" do
|
228
|
+
@c2.one_to_many :attributes, :class => @c1 do |ds|
|
229
|
+
ds.filter(:xxx => @xxx)
|
230
|
+
end
|
231
|
+
|
232
|
+
n = @c2.new(:id => 1234)
|
233
|
+
n.xxx = 'blah'
|
234
|
+
n.attributes.sql.should == "SELECT * FROM attributes WHERE (node_id = 1234) AND (xxx = 'blah')"
|
235
|
+
end
|
236
|
+
|
237
|
+
it "should support an order option" do
|
238
|
+
@c2.one_to_many :attributes, :class => @c1, :order => :kind
|
239
|
+
|
240
|
+
n = @c2.new(:id => 1234)
|
241
|
+
n.attributes.sql.should == "SELECT * FROM attributes WHERE (node_id = 1234) ORDER BY kind"
|
242
|
+
end
|
243
|
+
|
244
|
+
it "should support order option with block" do
|
245
|
+
@c2.one_to_many :attributes, :class => @c1, :order => :kind do |ds|
|
246
|
+
ds.filter(:xxx => @xxx)
|
247
|
+
end
|
248
|
+
|
249
|
+
n = @c2.new(:id => 1234)
|
250
|
+
n.attributes.sql.should == "SELECT * FROM attributes WHERE (node_id = 1234) AND (xxx IS NULL) ORDER BY kind"
|
251
|
+
end
|
252
|
+
|
253
|
+
it "should support :cache option for returning array with all members of the association" do
|
254
|
+
@c2.one_to_many :attributes, :class => @c1, :cache => true
|
255
|
+
|
256
|
+
n = @c2.new(:id => 1234)
|
257
|
+
atts = n.attributes
|
258
|
+
atts.should be_a_kind_of(Array)
|
259
|
+
atts.size.should == 1
|
260
|
+
atts.first.should be_a_kind_of(@c1)
|
261
|
+
atts.first.values.should == {}
|
262
|
+
|
263
|
+
MODEL_DB.sqls.should == ['SELECT * FROM attributes WHERE (node_id = 1234)']
|
264
|
+
end
|
265
|
+
|
266
|
+
it "should support :cache option with a block" do
|
267
|
+
@c2.one_to_many :attributes, :class => @c1, :cache => true do |ds|
|
268
|
+
ds.filter(:xxx => @xxx)
|
269
|
+
end
|
270
|
+
|
271
|
+
n = @c2.new(:id => 1234)
|
272
|
+
atts = n.attributes
|
273
|
+
atts.should be_a_kind_of(Array)
|
274
|
+
atts.size.should == 1
|
275
|
+
atts.first.should be_a_kind_of(@c1)
|
276
|
+
atts.first.values.should == {}
|
277
|
+
|
278
|
+
MODEL_DB.sqls.should == ['SELECT * FROM attributes WHERE (node_id = 1234) AND (xxx IS NULL)']
|
279
|
+
end
|
280
|
+
|
281
|
+
it "should set cached instance variable when accessed" do
|
282
|
+
@c2.one_to_many :attributes, :class => @c1, :cache => true
|
283
|
+
|
284
|
+
n = @c2.new(:id => 1234)
|
285
|
+
MODEL_DB.reset
|
286
|
+
n.instance_variables.include?("@attributes").should == false
|
287
|
+
atts = n.attributes
|
288
|
+
atts.should == n.instance_variable_get("@attributes")
|
289
|
+
MODEL_DB.sqls.should == ['SELECT * FROM attributes WHERE (node_id = 1234)']
|
290
|
+
end
|
291
|
+
|
292
|
+
it "should use cached instance variable if available" do
|
293
|
+
@c2.one_to_many :attributes, :class => @c1, :cache => true
|
294
|
+
|
295
|
+
n = @c2.new(:id => 1234)
|
296
|
+
MODEL_DB.reset
|
297
|
+
n.instance_variable_set(:@attributes, 42)
|
298
|
+
n.attributes.should == 42
|
299
|
+
MODEL_DB.sqls.should == []
|
300
|
+
end
|
301
|
+
|
302
|
+
it "should not use cached instance variable if asked to reload" do
|
303
|
+
@c2.one_to_many :attributes, :class => @c1, :cache => true
|
304
|
+
|
305
|
+
n = @c2.new(:id => 1234)
|
306
|
+
MODEL_DB.reset
|
307
|
+
n.instance_variable_set(:@attributes, 42)
|
308
|
+
n.attributes(true).should_not == 42
|
309
|
+
MODEL_DB.sqls.should == ['SELECT * FROM attributes WHERE (node_id = 1234)']
|
310
|
+
end
|
311
|
+
|
312
|
+
it "should add item to cached instance variable if it exists when calling add_" do
|
313
|
+
@c2.one_to_many :attributes, :class => @c1, :cache => true
|
314
|
+
|
315
|
+
n = @c2.new(:id => 1234)
|
316
|
+
att = @c1.new(:id => 345)
|
317
|
+
MODEL_DB.reset
|
318
|
+
a = []
|
319
|
+
n.instance_variable_set(:@attributes, a)
|
320
|
+
n.add_attribute(att)
|
321
|
+
a.should == [att]
|
322
|
+
end
|
323
|
+
|
324
|
+
it "should remove item from cached instance variable if it exists when calling remove_" do
|
325
|
+
@c2.one_to_many :attributes, :class => @c1, :cache => true
|
326
|
+
|
327
|
+
n = @c2.new(:id => 1234)
|
328
|
+
att = @c1.new(:id => 345)
|
329
|
+
MODEL_DB.reset
|
330
|
+
a = [att]
|
331
|
+
n.instance_variable_set(:@attributes, a)
|
332
|
+
n.remove_attribute(att)
|
333
|
+
a.should == []
|
334
|
+
end
|
335
|
+
|
336
|
+
it "should have has_many alias" do
|
337
|
+
@c2.has_many :attributes, :class => @c1, :cache => true
|
338
|
+
|
339
|
+
n = @c2.new(:id => 1234)
|
340
|
+
atts = n.attributes
|
341
|
+
atts.should be_a_kind_of(Array)
|
342
|
+
atts.size.should == 1
|
343
|
+
atts.first.should be_a_kind_of(@c1)
|
344
|
+
atts.first.values.should == {}
|
345
|
+
|
346
|
+
MODEL_DB.sqls.should == ['SELECT * FROM attributes WHERE (node_id = 1234)']
|
347
|
+
end
|
348
|
+
end
|
349
|
+
|
350
|
+
describe Sequel::Model, "many_to_many" do
|
351
|
+
|
352
|
+
before(:each) do
|
353
|
+
MODEL_DB.reset
|
354
|
+
|
355
|
+
@c1 = Class.new(Sequel::Model(:attributes)) do
|
356
|
+
def self.name
|
357
|
+
'Attribute'
|
358
|
+
end
|
359
|
+
end
|
360
|
+
|
361
|
+
@c2 = Class.new(Sequel::Model(:nodes)) do
|
362
|
+
attr_accessor :xxx
|
363
|
+
|
364
|
+
def self.name
|
365
|
+
'Node'
|
366
|
+
end
|
367
|
+
end
|
368
|
+
@dataset = @c2.dataset
|
369
|
+
|
370
|
+
[@c1, @c2].each do |c|
|
371
|
+
c.dataset.extend(Module.new {
|
372
|
+
def fetch_rows(sql)
|
373
|
+
@db << sql
|
374
|
+
yield Hash.new
|
375
|
+
end
|
376
|
+
})
|
377
|
+
end
|
378
|
+
end
|
379
|
+
|
380
|
+
it "should use implicit key values and join table if omitted" do
|
381
|
+
@c2.many_to_many :attributes, :class => @c1
|
382
|
+
|
383
|
+
n = @c2.new(:id => 1234)
|
384
|
+
a = n.attributes
|
385
|
+
a.should be_a_kind_of(Sequel::Dataset)
|
386
|
+
['SELECT * FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) AND (attributes_nodes.node_id = 1234)',
|
387
|
+
'SELECT * FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.node_id = 1234) AND (attributes_nodes.attribute_id = attributes.id)'
|
388
|
+
].should(include(a.sql))
|
389
|
+
end
|
390
|
+
|
391
|
+
it "should use implicit class if omitted" do
|
392
|
+
class Tag < Sequel::Model
|
393
|
+
end
|
394
|
+
|
395
|
+
@c2.many_to_many :tags
|
396
|
+
|
397
|
+
n = @c2.new(:id => 1234)
|
398
|
+
a = n.tags
|
399
|
+
a.should be_a_kind_of(Sequel::Dataset)
|
400
|
+
['SELECT * FROM tags INNER JOIN nodes_tags ON (nodes_tags.tag_id = tags.id) AND (nodes_tags.node_id = 1234)',
|
401
|
+
'SELECT * FROM tags INNER JOIN nodes_tags ON (nodes_tags.node_id = 1234) AND (nodes_tags.tag_id = tags.id)'
|
402
|
+
].should(include(a.sql))
|
403
|
+
end
|
404
|
+
|
405
|
+
it "should use explicit key values and join table if given" do
|
406
|
+
@c2.many_to_many :attributes, :class => @c1, :left_key => :nodeid, :right_key => :attributeid, :join_table => :attribute2node
|
407
|
+
|
408
|
+
n = @c2.new(:id => 1234)
|
409
|
+
a = n.attributes
|
410
|
+
a.should be_a_kind_of(Sequel::Dataset)
|
411
|
+
['SELECT * FROM attributes INNER JOIN attribute2node ON (attribute2node.nodeid = 1234) AND (attribute2node.attributeid = attributes.id)',
|
412
|
+
'SELECT * FROM attributes INNER JOIN attribute2node ON (attribute2node.attributeid = attributes.id) AND (attribute2node.nodeid = 1234)'
|
413
|
+
].should(include(a.sql))
|
414
|
+
end
|
415
|
+
|
416
|
+
it "should support order option" do
|
417
|
+
@c2.many_to_many :attributes, :class => @c1, :order => :blah
|
418
|
+
|
419
|
+
n = @c2.new(:id => 1234)
|
420
|
+
a = n.attributes
|
421
|
+
a.should be_a_kind_of(Sequel::Dataset)
|
422
|
+
['SELECT * FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) AND (attributes_nodes.node_id = 1234) ORDER BY blah',
|
423
|
+
'SELECT * FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.node_id = 1234) AND (attributes_nodes.attribute_id = attributes.id) ORDER BY blah'
|
424
|
+
].should(include(a.sql))
|
425
|
+
end
|
426
|
+
|
427
|
+
it "should support optional dataset block" do
|
428
|
+
@c2.many_to_many :attributes, :class => @c1 do |ds|
|
429
|
+
ds.filter(:xxx => @xxx)
|
430
|
+
end
|
431
|
+
|
432
|
+
n = @c2.new(:id => 1234)
|
433
|
+
n.xxx = 555
|
434
|
+
a = n.attributes
|
435
|
+
a.should be_a_kind_of(Sequel::Dataset)
|
436
|
+
['SELECT * FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) AND (attributes_nodes.node_id = 1234) WHERE (xxx = 555)',
|
437
|
+
'SELECT * FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.node_id = 1234) AND (attributes_nodes.attribute_id = attributes.id) WHERE (xxx = 555)'
|
438
|
+
].should(include(a.sql))
|
439
|
+
end
|
440
|
+
|
441
|
+
it "should define an add_ method" do
|
442
|
+
@c2.many_to_many :attributes, :class => @c1
|
443
|
+
|
444
|
+
n = @c2.new(:id => 1234)
|
445
|
+
a = @c1.new(:id => 2345)
|
446
|
+
a.should == n.add_attribute(a)
|
447
|
+
['INSERT INTO attributes_nodes (node_id, attribute_id) VALUES (1234, 2345)',
|
448
|
+
'INSERT INTO attributes_nodes (attribute_id, node_id) VALUES (2345, 1234)'
|
449
|
+
].should(include(MODEL_DB.sqls.first))
|
450
|
+
end
|
451
|
+
|
452
|
+
it "should define a remove_ method" do
|
453
|
+
@c2.many_to_many :attributes, :class => @c1
|
454
|
+
|
455
|
+
n = @c2.new(:id => 1234)
|
456
|
+
a = @c1.new(:id => 2345)
|
457
|
+
a.should == n.remove_attribute(a)
|
458
|
+
['DELETE FROM attributes_nodes WHERE (node_id = 1234) AND (attribute_id = 2345)',
|
459
|
+
'DELETE FROM attributes_nodes WHERE (attribute_id = 2345) AND (node_id = 1234)'
|
460
|
+
].should(include(MODEL_DB.sqls.first))
|
461
|
+
end
|
462
|
+
|
463
|
+
it "should provide an array with all members of the association (if cache option is specified)" do
|
464
|
+
@c2.many_to_many :attributes, :class => @c1, :cache => true
|
465
|
+
|
466
|
+
n = @c2.new(:id => 1234)
|
467
|
+
atts = n.attributes
|
468
|
+
atts.should be_a_kind_of(Array)
|
469
|
+
atts.size.should == 1
|
470
|
+
atts.first.should be_a_kind_of(@c1)
|
471
|
+
|
472
|
+
['SELECT * FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) AND (attributes_nodes.node_id = 1234)',
|
473
|
+
'SELECT * FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.node_id = 1234) AND (attributes_nodes.attribute_id = attributes.id)'
|
474
|
+
].should(include(MODEL_DB.sqls.first))
|
475
|
+
end
|
476
|
+
|
477
|
+
it "should set cached instance variable when accessed" do
|
478
|
+
@c2.many_to_many :attributes, :class => @c1, :cache => true
|
479
|
+
|
480
|
+
n = @c2.new(:id => 1234)
|
481
|
+
MODEL_DB.reset
|
482
|
+
n.instance_variables.include?("@attributes").should == false
|
483
|
+
atts = n.attributes
|
484
|
+
atts.should == n.instance_variable_get("@attributes")
|
485
|
+
MODEL_DB.sqls.length.should == 1
|
486
|
+
end
|
487
|
+
|
488
|
+
it "should use cached instance variable if available" do
|
489
|
+
@c2.many_to_many :attributes, :class => @c1, :cache => true
|
490
|
+
|
491
|
+
n = @c2.new(:id => 1234)
|
492
|
+
MODEL_DB.reset
|
493
|
+
n.instance_variable_set(:@attributes, 42)
|
494
|
+
n.attributes.should == 42
|
495
|
+
MODEL_DB.sqls.should == []
|
496
|
+
end
|
497
|
+
|
498
|
+
it "should not use cached instance variable if asked to reload" do
|
499
|
+
@c2.many_to_many :attributes, :class => @c1, :cache => true
|
500
|
+
|
501
|
+
n = @c2.new(:id => 1234)
|
502
|
+
MODEL_DB.reset
|
503
|
+
n.instance_variable_set(:@attributes, 42)
|
504
|
+
n.attributes(true).should_not == 42
|
505
|
+
MODEL_DB.sqls.length.should == 1
|
506
|
+
end
|
507
|
+
|
508
|
+
it "should add item to cached instance variable if it exists when calling add_" do
|
509
|
+
@c2.many_to_many :attributes, :class => @c1, :cache => true
|
510
|
+
|
511
|
+
n = @c2.new(:id => 1234)
|
512
|
+
att = @c1.new(:id => 345)
|
513
|
+
MODEL_DB.reset
|
514
|
+
a = []
|
515
|
+
n.instance_variable_set(:@attributes, a)
|
516
|
+
n.add_attribute(att)
|
517
|
+
a.should == [att]
|
518
|
+
end
|
519
|
+
|
520
|
+
it "should remove item from cached instance variable if it exists when calling remove_" do
|
521
|
+
@c2.many_to_many :attributes, :class => @c1, :cache => true
|
522
|
+
|
523
|
+
n = @c2.new(:id => 1234)
|
524
|
+
att = @c1.new(:id => 345)
|
525
|
+
MODEL_DB.reset
|
526
|
+
a = [att]
|
527
|
+
n.instance_variable_set(:@attributes, a)
|
528
|
+
n.remove_attribute(att)
|
529
|
+
a.should == []
|
530
|
+
end
|
531
|
+
|
532
|
+
it "should have has_and_belongs_to_many alias" do
|
533
|
+
@c2.has_and_belongs_to_many :attributes, :class => @c1
|
534
|
+
|
535
|
+
n = @c2.new(:id => 1234)
|
536
|
+
a = n.attributes
|
537
|
+
a.should be_a_kind_of(Sequel::Dataset)
|
538
|
+
['SELECT * FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.attribute_id = attributes.id) AND (attributes_nodes.node_id = 1234)',
|
539
|
+
'SELECT * FROM attributes INNER JOIN attributes_nodes ON (attributes_nodes.node_id = 1234) AND (attributes_nodes.attribute_id = attributes.id)'
|
540
|
+
].should(include(a.sql))
|
541
|
+
end
|
542
|
+
|
543
|
+
end
|
544
|
+
|
545
|
+
describe Sequel::Model, "all_association_reflections" do
|
546
|
+
before(:each) do
|
547
|
+
MODEL_DB.reset
|
548
|
+
@c1 = Class.new(Sequel::Model(:nodes)) do
|
549
|
+
def self.name
|
550
|
+
'Node'
|
551
|
+
end
|
552
|
+
end
|
553
|
+
end
|
554
|
+
|
555
|
+
it "should include all association reflection hashes" do
|
556
|
+
@c1.all_association_reflections.should == []
|
557
|
+
@c1.associate :many_to_one, :parent, :class => @c1
|
558
|
+
@c1.all_association_reflections.should == [{
|
559
|
+
:type => :many_to_one, :name => :parent, :class_name => 'Node',
|
560
|
+
:class => @c1, :key => :parent_id, :block => nil
|
561
|
+
}]
|
562
|
+
@c1.associate :one_to_many, :children, :class => @c1
|
563
|
+
@c1.all_association_reflections.sort_by{|x|x[:name].to_s}.should == [{
|
564
|
+
:type => :one_to_many, :name => :children, :class_name => 'Node',
|
565
|
+
:class => @c1, :key => :node_id, :block => nil}, {
|
566
|
+
:type => :many_to_one, :name => :parent, :class_name => 'Node',
|
567
|
+
:class => @c1, :key => :parent_id, :block => nil}]
|
568
|
+
end
|
569
|
+
end
|
570
|
+
|
571
|
+
describe Sequel::Model, "association_reflection" do
|
572
|
+
before(:each) do
|
573
|
+
MODEL_DB.reset
|
574
|
+
@c1 = Class.new(Sequel::Model(:nodes)) do
|
575
|
+
def self.name
|
576
|
+
'Node'
|
577
|
+
end
|
578
|
+
end
|
579
|
+
end
|
580
|
+
|
581
|
+
it "should return nil for nonexistent association" do
|
582
|
+
@c1.association_reflection(:blah).should == nil
|
583
|
+
end
|
584
|
+
|
585
|
+
it "should return association reflection hash if association exists" do
|
586
|
+
@c1.associate :many_to_one, :parent, :class => @c1
|
587
|
+
@c1.association_reflection(:parent).should == {
|
588
|
+
:type => :many_to_one, :name => :parent, :class_name => 'Node',
|
589
|
+
:class => @c1, :key => :parent_id, :block => nil
|
590
|
+
}
|
591
|
+
@c1.associate :one_to_many, :children, :class => @c1
|
592
|
+
@c1.association_reflection(:children).should == {
|
593
|
+
:type => :one_to_many, :name => :children, :class_name => 'Node',
|
594
|
+
:class => @c1, :key => :node_id, :block => nil
|
595
|
+
}
|
596
|
+
end
|
597
|
+
end
|
598
|
+
|
599
|
+
describe Sequel::Model, "associations" do
|
600
|
+
before(:each) do
|
601
|
+
MODEL_DB.reset
|
602
|
+
@c1 = Class.new(Sequel::Model(:nodes)) do
|
603
|
+
def self.name
|
604
|
+
'Node'
|
605
|
+
end
|
606
|
+
end
|
607
|
+
end
|
608
|
+
|
609
|
+
it "should include all association names" do
|
610
|
+
@c1.associations.should == []
|
611
|
+
@c1.associate :many_to_one, :parent, :class => @c1
|
612
|
+
@c1.associations.should == [:parent]
|
613
|
+
@c1.associate :one_to_many, :children, :class => @c1
|
614
|
+
@c1.associations.sort_by{|x|x.to_s}.should == [:children, :parent]
|
615
|
+
end
|
616
|
+
end
|