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