active_hash 1.2.3 → 1.3.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.
@@ -1,1051 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe ActiveHash, "Base" do
4
-
5
- before do
6
- class Country < ActiveHash::Base
7
- end
8
- end
9
-
10
- after do
11
- Object.send :remove_const, :Country
12
- end
13
-
14
- it "passes LocalJumpError through in .transaction when no block is given" do
15
- expect { Country.transaction }.to raise_error(LocalJumpError)
16
- end
17
-
18
- # This require must be after the above example so that ActiveRecord
19
- # is available in its full glory for the test classes that use it,
20
- # but doesn't create a false positive for the above example.
21
- require "active_record"
22
-
23
- describe ".fields" do
24
- before do
25
- Country.fields :name, :iso_name
26
- end
27
-
28
- it "defines a reader for each field" do
29
- Country.new.should respond_to(:name)
30
- Country.new.should respond_to(:iso_name)
31
- end
32
-
33
- it "defines interrogator methods for each field" do
34
- Country.new.should respond_to(:name?)
35
- Country.new.should respond_to(:iso_name?)
36
- end
37
-
38
- it "defines single finder methods for each field" do
39
- Country.should respond_to(:find_by_name)
40
- Country.should respond_to(:find_by_iso_name)
41
- end
42
-
43
- it "defines banged single finder methods for each field" do
44
- Country.should respond_to(:find_by_name!)
45
- Country.should respond_to(:find_by_iso_name!)
46
- end
47
-
48
- it "defines array finder methods for each field" do
49
- Country.should respond_to(:find_all_by_name)
50
- Country.should respond_to(:find_all_by_iso_name)
51
- end
52
-
53
- it "does not define banged array finder methods for each field" do
54
- Country.should_not respond_to(:find_all_by_name!)
55
- Country.should_not respond_to(:find_all_by_iso_name!)
56
- end
57
-
58
- it "defines single finder methods for all combinations of fields" do
59
- Country.should respond_to(:find_by_name_and_iso_name)
60
- Country.should respond_to(:find_by_iso_name_and_name)
61
- end
62
-
63
- it "defines banged single finder methods for all combinations of fields" do
64
- Country.should respond_to(:find_by_name_and_iso_name!)
65
- Country.should respond_to(:find_by_iso_name_and_name!)
66
- end
67
-
68
- it "defines array finder methods for all combinations of fields" do
69
- Country.should respond_to(:find_all_by_name_and_iso_name)
70
- Country.should respond_to(:find_all_by_iso_name_and_name)
71
- end
72
-
73
- it "does not define banged array finder methods for all combinations of fields" do
74
- Country.should_not respond_to(:find_all_by_name_and_iso_name!)
75
- Country.should_not respond_to(:find_all_by_iso_name_and_name!)
76
- end
77
-
78
- it "allows you to pass options to the built-in find_by_* methods (but ignores the hash for now)" do
79
- Country.find_by_name("Canada", :select => nil).should be_nil
80
- Country.find_all_by_name("Canada", :select => nil).should == []
81
- end
82
-
83
- it "allows you to pass options to the custom find_by_* methods (but ignores the hash for now)" do
84
- Country.find_by_name_and_iso_name("Canada", "CA", :select => nil).should be_nil
85
- Country.find_all_by_name_and_iso_name("Canada", "CA", :select => nil).should == []
86
- end
87
-
88
- it "blows up if you try to overwrite :attributes" do
89
- proc do
90
- Country.field :attributes
91
- end.should raise_error(ActiveHash::ReservedFieldError)
92
- end
93
- end
94
-
95
- describe ".data=" do
96
- before do
97
- class Region < ActiveHash::Base
98
- field :description
99
- end
100
- end
101
-
102
- it "populates the object with data and auto-assigns keys" do
103
- Country.data = [{:name => "US"}, {:name => "Canada"}]
104
- Country.data.should == [{:name => "US", :id => 1}, {:name => "Canada", :id => 2}]
105
- end
106
-
107
- it "allows each of it's subclasses to have it's own data" do
108
- Country.data = [{:name => "US"}, {:name => "Canada"}]
109
- Region.data = [{:description => "A big region"}, {:description => "A remote region"}]
110
-
111
- Country.data.should == [{:name => "US", :id => 1}, {:name => "Canada", :id => 2}]
112
- Region.data.should == [{:description => "A big region", :id => 1}, {:description => "A remote region", :id => 2}]
113
- end
114
-
115
- it "marks the class as dirty" do
116
- Country.dirty.should be_false
117
- Country.data = []
118
- Country.dirty.should be_true
119
- end
120
- end
121
-
122
- describe ".add" do
123
- before do
124
- Country.fields :name
125
- end
126
-
127
- it "adds a record" do
128
- proc {
129
- Country.add :name => "Russia"
130
- }.should change { Country.count }
131
- end
132
-
133
- it "marks the class as dirty" do
134
- Country.dirty.should be_false
135
- Country.add :name => "Russia"
136
- Country.dirty.should be_true
137
- end
138
-
139
- it "returns the record" do
140
- record = Country.add :name => "Russia"
141
- record.name.should == "Russia"
142
- end
143
-
144
- it "should populate the id" do
145
- record = Country.add :name => "Russia"
146
- record.id.should_not be_nil
147
- end
148
- end
149
-
150
- describe ".all" do
151
- before do
152
- Country.field :name
153
- Country.data = [
154
- {:id => 1, :name => "US"},
155
- {:id => 2, :name => "Canada"}
156
- ]
157
- end
158
-
159
- it "returns an empty array if data is nil" do
160
- Country.data = nil
161
- Country.all.should be_empty
162
- end
163
-
164
- it "returns all data as inflated objects" do
165
- Country.all.all? { |country| country.should be_kind_of(Country) }
166
- end
167
-
168
- it "populates the data correctly" do
169
- records = Country.all
170
- records.first.id.should == 1
171
- records.first.name.should == "US"
172
- records.last.id.should == 2
173
- records.last.name.should == "Canada"
174
- end
175
-
176
- it "re-populates the records after data= is called" do
177
- Country.data = [
178
- {:id => 45, :name => "Canada"}
179
- ]
180
- records = Country.all
181
- records.first.id.should == 45
182
- records.first.name.should == "Canada"
183
- records.length.should == 1
184
- end
185
-
186
- it "filters the records from a AR-like conditions hash" do
187
- record = Country.all(:conditions => {:name => 'US'})
188
- record.count.should == 1
189
- record.first.id.should == 1
190
- record.first.name.should == 'US'
191
- end
192
- end
193
-
194
- describe ".where" do
195
- before do
196
- Country.field :name
197
- Country.field :language
198
- Country.data = [
199
- {:id => 1, :name => "US", :language => 'English'},
200
- {:id => 2, :name => "Canada", :language => 'English'},
201
- {:id => 3, :name => "Mexico", :language => 'Spanish'}
202
- ]
203
- end
204
-
205
- it "raises ArgumentError if no conditions are provided" do
206
- lambda{
207
- Country.where
208
- }.should raise_error(ArgumentError)
209
- end
210
-
211
- it "returns all records when passed nil" do
212
- Country.where(nil).should == Country.all
213
- end
214
-
215
- it "returns all data as inflated objects" do
216
- Country.where(:language => 'English').all? { |country| country.should be_kind_of(Country) }
217
- end
218
-
219
- it "populates the data correctly" do
220
- records = Country.where(:language => 'English')
221
- records.first.id.should == 1
222
- records.first.name.should == "US"
223
- records.last.id.should == 2
224
- records.last.name.should == "Canada"
225
- end
226
-
227
- it "re-populates the records after data= is called" do
228
- Country.data = [
229
- {:id => 45, :name => "Canada"}
230
- ]
231
- records = Country.where(:name => 'Canada')
232
- records.first.id.should == 45
233
- records.first.name.should == "Canada"
234
- records.length.should == 1
235
- end
236
-
237
- it "filters the records from a AR-like conditions hash" do
238
- record = Country.where(:name => 'US')
239
- record.count.should == 1
240
- record.first.id.should == 1
241
- record.first.name.should == 'US'
242
- end
243
-
244
- it "raises an error if ids aren't unique" do
245
- proc do
246
- Country.data = [
247
- {:id => 1, :name => "US", :language => 'English'},
248
- {:id => 2, :name => "Canada", :language => 'English'},
249
- {:id => 2, :name => "Mexico", :language => 'Spanish'}
250
- ]
251
- end.should raise_error(ActiveHash::IdError)
252
- end
253
- end
254
-
255
- describe ".count" do
256
- before do
257
- Country.data = [
258
- {:id => 1, :name => "US"},
259
- {:id => 2, :name => "Canada"}
260
- ]
261
- end
262
-
263
- it "returns the number of elements in the array" do
264
- Country.count.should == 2
265
- end
266
- end
267
-
268
- describe ".first" do
269
- before do
270
- Country.data = [
271
- {:id => 1, :name => "US"},
272
- {:id => 2, :name => "Canada"}
273
- ]
274
- end
275
-
276
- it "returns the first object" do
277
- Country.first.should == Country.new(:id => 1)
278
- end
279
- end
280
-
281
- describe ".last" do
282
- before do
283
- Country.data = [
284
- {:id => 1, :name => "US"},
285
- {:id => 2, :name => "Canada"}
286
- ]
287
- end
288
-
289
- it "returns the last object" do
290
- Country.last.should == Country.new(:id => 2)
291
- end
292
- end
293
-
294
- describe ".find" do
295
- before do
296
- Country.data = [
297
- {:id => 1, :name => "US"},
298
- {:id => 2, :name => "Canada"}
299
- ]
300
- end
301
-
302
- context "with an id" do
303
- it "finds the record with the specified id" do
304
- Country.find(2).id.should == 2
305
- end
306
-
307
- it "finds the record with the specified id as a string" do
308
- Country.find("2").id.should == 2
309
- end
310
-
311
- it "raises ActiveHash::RecordNotFound when id not found" do
312
- proc do
313
- Country.find(0)
314
- end.should raise_error(ActiveHash::RecordNotFound, /Couldn't find Country with ID=0/)
315
- end
316
- end
317
-
318
- context "with :all" do
319
- it "returns all records" do
320
- Country.find(:all).should == [Country.new(:id => 1), Country.new(:id => 2)]
321
- end
322
- end
323
-
324
- context "with 2 arguments" do
325
- it "returns the record with the given id and ignores the conditions" do
326
- Country.find(1, :conditions => "foo=bar").should == Country.new(:id => 1)
327
- Country.find(:all, :conditions => "foo=bar").length.should == 2
328
- end
329
- end
330
-
331
- context "with an array of ids" do
332
- before do
333
- Country.data = [
334
- {:id => 1},
335
- {:id => 2},
336
- {:id => 3}
337
- ]
338
- end
339
-
340
- it "returns all matching ids" do
341
- Country.find([1, 3]).should == [Country.new(:id => 1), Country.new(:id => 3)]
342
- end
343
-
344
- it "raises ActiveHash::RecordNotFound when id not found" do
345
- proc do
346
- Country.find([0, 3])
347
- end.should raise_error(ActiveHash::RecordNotFound, /Couldn't find Country with ID=0/)
348
- end
349
- end
350
- end
351
-
352
- describe ".find_by_id" do
353
- before do
354
- Country.data = [
355
- {:id => 1, :name => "US"},
356
- {:id => 2, :name => "Canada"}
357
- ]
358
- end
359
-
360
- context "with an id" do
361
- it "finds the record with the specified id" do
362
- Country.find_by_id(2).id.should == 2
363
- end
364
-
365
- it "finds the record with the specified id as a string" do
366
- Country.find_by_id("2").id.should == 2
367
- end
368
- end
369
-
370
- context "with string ids" do
371
- before do
372
- Country.data = [
373
- {:id => "abc", :name => "US"},
374
- {:id => "def", :name => "Canada"}
375
- ]
376
- end
377
-
378
- it "finds the record with the specified id" do
379
- Country.find_by_id("abc").id.should == "abc"
380
- end
381
- end
382
-
383
- context "with nil" do
384
- it "returns nil" do
385
- Country.find_by_id(nil).should be_nil
386
- end
387
- end
388
-
389
- context "with an id not present" do
390
- it "returns nil" do
391
- Country.find_by_id(4567).should be_nil
392
- end
393
- end
394
- end
395
-
396
- describe "custom finders" do
397
- before do
398
- Country.fields :name, :monarch, :language
399
-
400
- # Start ids above 4 lest we get nil and think it's an AH::Base model with id=4.
401
- Country.data = [
402
- {:id => 11, :name => nil, :monarch => nil, :language => "Latin"},
403
- {:id => 12, :name => "US", :monarch => nil, :language => "English"},
404
- {:id => 13, :name => "Canada", :monarch => "The Crown of England", :language => "English"},
405
- {:id => 14, :name => "UK", :monarch => "The Crown of England", :language => "English"}
406
- ]
407
- end
408
-
409
- describe "find_by_<field_name>" do
410
- describe "with a match" do
411
- context "for a non-nil argument" do
412
- it "returns the first matching record" do
413
- Country.find_by_name("US").id.should == 12
414
- end
415
- end
416
-
417
- context "for a nil argument" do
418
- it "returns the first matching record" do
419
- Country.find_by_name(nil).id.should == 11
420
- end
421
- end
422
- end
423
-
424
- describe "without a match" do
425
- before do
426
- Country.data = []
427
- end
428
-
429
- context "for a non-nil argument" do
430
- it "returns nil" do
431
- Country.find_by_name("Mexico").should be_nil
432
- end
433
- end
434
-
435
- context "for a nil argument" do
436
- it "returns nil" do
437
- Country.find_by_name(nil).should be_nil
438
- end
439
- end
440
- end
441
- end
442
-
443
- describe "find_by_<field_name>!" do
444
- describe "with a match" do
445
- context "for a non-nil argument" do
446
- it "returns the first matching record" do
447
- Country.find_by_name!("US").id.should == 12
448
- end
449
- end
450
-
451
- context "for a nil argument" do
452
- it "returns the first matching record" do
453
- Country.find_by_name!(nil).id.should == 11
454
- end
455
- end
456
- end
457
-
458
- describe "without a match" do
459
- before do
460
- Country.data = []
461
- end
462
-
463
- context "for a non-nil argument" do
464
- it "raises ActiveHash::RecordNotFound" do
465
- lambda { Country.find_by_name!("Mexico") }.should raise_error(ActiveHash::RecordNotFound, /Couldn't find Country with name = Mexico/)
466
- end
467
- end
468
-
469
- context "for a nil argument" do
470
- it "raises ActiveHash::RecordNotFound" do
471
- lambda { Country.find_by_name!(nil) }.should raise_error(ActiveHash::RecordNotFound, /Couldn't find Country with name = /)
472
- end
473
- end
474
- end
475
- end
476
-
477
- describe "find_all_by_<field_name>" do
478
- describe "with matches" do
479
- it "returns all matching records" do
480
- countries = Country.find_all_by_monarch("The Crown of England")
481
- countries.length.should == 2
482
- countries.first.name.should == "Canada"
483
- countries.last.name.should == "UK"
484
- end
485
- end
486
-
487
- describe "without matches" do
488
- it "returns an empty array" do
489
- Country.find_all_by_name("Mexico").should be_empty
490
- end
491
- end
492
- end
493
-
494
- describe "find_by_<field_one>_and_<field_two>" do
495
- describe "with a match" do
496
- it "returns the first matching record" do
497
- Country.find_by_name_and_monarch("Canada", "The Crown of England").id.should == 13
498
- Country.find_by_monarch_and_name("The Crown of England", "Canada").id.should == 13
499
- end
500
- end
501
-
502
- describe "with a match based on to_s" do
503
- it "returns the first matching record" do
504
- Country.find_by_name_and_id("Canada", "13").id.should == 13
505
- end
506
- end
507
-
508
- describe "without a match" do
509
- it "returns nil" do
510
- Country.find_by_name_and_monarch("US", "The Crown of England").should be_nil
511
- end
512
- end
513
-
514
- describe "for fields the class doesn't have" do
515
- it "raises a NoMethodError" do
516
- lambda {
517
- Country.find_by_name_and_shoe_size("US", 10)
518
- }.should raise_error(NoMethodError, /undefined method `find_by_name_and_shoe_size' (?:for|on) Country/)
519
- end
520
- end
521
- end
522
-
523
- describe "find_by_<field_one>_and_<field_two>!" do
524
- describe "with a match" do
525
- it "returns the first matching record" do
526
- Country.find_by_name_and_monarch!("Canada", "The Crown of England").id.should == 13
527
- Country.find_by_monarch_and_name!("The Crown of England", "Canada").id.should == 13
528
- end
529
- end
530
-
531
- describe "with a match based on to_s" do
532
- it "returns the first matching record" do
533
- Country.find_by_name_and_id!("Canada", "13").id.should == 13
534
- end
535
- end
536
-
537
- describe "without a match" do
538
- it "raises ActiveHash::RecordNotFound" do
539
- lambda { Country.find_by_name_and_monarch!("US", "The Crown of England") }.should raise_error(ActiveHash::RecordNotFound, /Couldn't find Country with name = US, monarch = The Crown of England/)
540
- end
541
- end
542
-
543
- describe "for fields the class doesn't have" do
544
- it "raises a NoMethodError" do
545
- lambda {
546
- Country.find_by_name_and_shoe_size!("US", 10)
547
- }.should raise_error(NoMethodError, /undefined method `find_by_name_and_shoe_size!' (?:for|on) Country/)
548
- end
549
- end
550
- end
551
-
552
- describe "find_all_by_<field_one>_and_<field_two>" do
553
- describe "with matches" do
554
- it "returns all matching records" do
555
- countries = Country.find_all_by_monarch_and_language("The Crown of England", "English")
556
- countries.length.should == 2
557
- countries.first.name.should == "Canada"
558
- countries.last.name.should == "UK"
559
- end
560
- end
561
-
562
- describe "without matches" do
563
- it "returns an empty array" do
564
- Country.find_all_by_monarch_and_language("Shaka Zulu", "Zulu").should be_empty
565
- end
566
- end
567
- end
568
- end
569
-
570
- describe "#method_missing" do
571
- it "doesn't blow up if you call a missing dynamic finder when fields haven't been set" do
572
- proc do
573
- Country.find_by_name("Foo")
574
- end.should raise_error(NoMethodError, /undefined method `find_by_name' (?:for|on) Country/)
575
- end
576
- end
577
-
578
- describe "#attributes" do
579
- it "returns the hash passed in the initializer" do
580
- Country.field :foo
581
- country = Country.new(:foo => :bar)
582
- country.attributes.should == {:foo => :bar}
583
- end
584
-
585
- it "symbolizes keys" do
586
- Country.field :foo
587
- country = Country.new("foo" => :bar)
588
- country.attributes.should == {:foo => :bar}
589
- end
590
-
591
- it "is works with #[]" do
592
- Country.field :foo
593
- country = Country.new(:foo => :bar)
594
- country[:foo].should == :bar
595
- end
596
-
597
- it "is works with #[]=" do
598
- Country.field :foo
599
- country = Country.new
600
- country[:foo] = :bar
601
- country.foo.should == :bar
602
- end
603
- end
604
-
605
- describe "reader methods" do
606
- context "for regular fields" do
607
- before do
608
- Country.fields :name, :iso_name
609
- end
610
-
611
- it "returns the given attribute when present" do
612
- country = Country.new(:name => "Spain")
613
- country.name.should == "Spain"
614
- end
615
-
616
- it "returns nil when not present" do
617
- country = Country.new
618
- country.name.should be_nil
619
- end
620
- end
621
-
622
- context "for fields with default values" do
623
- before do
624
- Country.field :name, :default => "foobar"
625
- end
626
-
627
- it "returns the given attribute when present" do
628
- country = Country.new(:name => "Spain")
629
- country.name.should == "Spain"
630
- end
631
-
632
- it "returns the default value when not present" do
633
- country = Country.new
634
- country.name.should == "foobar"
635
- end
636
- end
637
- end
638
-
639
- describe "interrogator methods" do
640
- before do
641
- Country.fields :name, :iso_name
642
- end
643
-
644
- it "returns true if the given attribute is non-blank" do
645
- country = Country.new(:name => "Spain")
646
- country.should be_name
647
- end
648
-
649
- it "returns false if the given attribute is blank" do
650
- country = Country.new(:name => " ")
651
- country.name?.should == false
652
- end
653
-
654
- it "returns false if the given attribute was not passed" do
655
- country = Country.new
656
- country.should_not be_name
657
- end
658
- end
659
-
660
- describe "#id" do
661
- context "when not passed an id" do
662
- it "returns nil" do
663
- country = Country.new
664
- country.id.should be_nil
665
- end
666
- end
667
- end
668
-
669
- describe "#quoted_id" do
670
- it "should return id" do
671
- Country.new(:id => 2).quoted_id.should == 2
672
- end
673
- end
674
-
675
- describe "#to_param" do
676
- it "should return id as a string" do
677
- Country.create(:id => 2).to_param.should == "2"
678
- end
679
- end
680
-
681
- describe "#persisted" do
682
- it "should return true if the object has been saved" do
683
- Country.create(:id => 2).should be_persisted
684
- end
685
-
686
- it "should return false if the object has not been saved" do
687
- Country.new(:id => 2).should_not be_persisted
688
- end
689
- end
690
-
691
- describe "#persisted" do
692
- it "should return true if the object has been saved" do
693
- Country.create(:id => 2).should be_persisted
694
- end
695
-
696
- it "should return false if the object has not been saved" do
697
- Country.new(:id => 2).should_not be_persisted
698
- end
699
- end
700
-
701
- describe "#eql?" do
702
- before do
703
- class Region < ActiveHash::Base
704
- end
705
- end
706
-
707
- it "should return true with the same class and id" do
708
- Country.new(:id => 23).eql?(Country.new(:id => 23)).should be_true
709
- end
710
-
711
- it "should return false with the same class and different ids" do
712
- Country.new(:id => 24).eql?(Country.new(:id => 23)).should be_false
713
- end
714
-
715
- it "should return false with the different classes and the same id" do
716
- Country.new(:id => 23).eql?(Region.new(:id => 23)).should be_false
717
- end
718
-
719
- it "returns false when id is nil" do
720
- Country.new.eql?(Country.new).should be_false
721
- end
722
- end
723
-
724
- describe "#==" do
725
- before do
726
- class Region < ActiveHash::Base
727
- end
728
- end
729
-
730
- it "should return true with the same class and id" do
731
- Country.new(:id => 23).should == Country.new(:id => 23)
732
- end
733
-
734
- it "should return false with the same class and different ids" do
735
- Country.new(:id => 24).should_not == Country.new(:id => 23)
736
- end
737
-
738
- it "should return false with the different classes and the same id" do
739
- Country.new(:id => 23).should_not == Region.new(:id => 23)
740
- end
741
-
742
- it "returns false when id is nil" do
743
- Country.new.should_not == Country.new
744
- end
745
- end
746
-
747
- describe "#hash" do
748
- it "returns id for hash" do
749
- Country.new(:id => 45).hash.should == 45.hash
750
- Country.new.hash.should == nil.hash
751
- end
752
-
753
- it "is hashable" do
754
- {Country.new(:id => 4) => "bar"}.should == {Country.new(:id => 4) => "bar"}
755
- {Country.new(:id => 3) => "bar"}.should_not == {Country.new(:id => 4) => "bar"}
756
- end
757
- end
758
-
759
- describe "#readonly?" do
760
- it "returns true" do
761
- Country.new.should be_readonly
762
- end
763
- end
764
-
765
- describe "auto-discovery of fields" do
766
- it "dynamically creates fields for all keys in the hash" do
767
- Country.data = [
768
- {:field1 => "foo"},
769
- {:field2 => "bar"},
770
- {:field3 => "biz"}
771
- ]
772
-
773
- [:field1, :field2, :field3].each do |field|
774
- Country.should respond_to("find_by_#{field}")
775
- Country.should respond_to("find_all_by_#{field}")
776
- Country.new.should respond_to(field)
777
- Country.new.should respond_to("#{field}?")
778
- end
779
- end
780
-
781
- it "doesn't override methods already defined" do
782
- Country.class_eval do
783
- class << self
784
- def find_by_name(name)
785
- "find_by_name defined manually"
786
- end
787
-
788
- def find_all_by_name(name)
789
- "find_all_by_name defined manually"
790
- end
791
- end
792
-
793
- def name
794
- "name defined manually"
795
- end
796
-
797
- def name?
798
- "name? defined manually"
799
- end
800
- end
801
-
802
- Country.find_by_name("foo").should == "find_by_name defined manually"
803
- Country.find_all_by_name("foo").should == "find_all_by_name defined manually"
804
- Country.new.name.should == "name defined manually"
805
- Country.new.name?.should == "name? defined manually"
806
-
807
- Country.data = [
808
- {:name => "foo"}
809
- ]
810
-
811
- Country.all
812
- Country.find_by_name("foo").should == "find_by_name defined manually"
813
- Country.find_all_by_name("foo").should == "find_all_by_name defined manually"
814
- Country.new.name.should == "name defined manually"
815
- Country.new.name?.should == "name? defined manually"
816
- end
817
- end
818
-
819
- describe "using with belongs_to in ActiveRecord" do
820
- before do
821
- Country.data = [
822
- {:id => 1, :name => "foo"}
823
- ]
824
-
825
- class Book < ActiveRecord::Base
826
- establish_connection :adapter => "sqlite3", :database => ":memory:"
827
- connection.create_table(:books, :force => true) do |t|
828
- t.text :subject_type
829
- t.integer :subject_id
830
- t.integer :country_id
831
- end
832
- belongs_to :subject, :polymorphic => true
833
- belongs_to :country
834
- end
835
- end
836
-
837
- after do
838
- Object.send :remove_const, :Book
839
- end
840
-
841
- it "should be possible to use it as a parent" do
842
- book = Book.new
843
- book.country = Country.first
844
- book.country.should == Country.first
845
- end
846
-
847
- it "should be possible to use it as a polymorphic parent" do
848
- book = Book.new
849
- book.subject = Country.first
850
- book.subject.should == Country.first
851
- end
852
-
853
- end
854
-
855
- describe "#cache_key" do
856
- it 'should use the class\'s cache_key and id' do
857
- Country.data = [
858
- {:id => 1, :name => "foo"}
859
- ]
860
-
861
- Country.first.cache_key.should == 'countries/1'
862
- end
863
-
864
- it 'should use the record\'s updated_at if present' do
865
- timestamp = Time.now
866
-
867
- Country.data = [
868
- {:id => 1, :name => "foo", :updated_at => timestamp}
869
- ]
870
-
871
- Country.first.cache_key.should == "countries/1-#{timestamp.to_s(:number)}"
872
- end
873
-
874
- it 'should use "new" instead of the id for a new record' do
875
- Country.new(:id => 1).cache_key.should == 'countries/new'
876
- end
877
- end
878
-
879
- describe "#save" do
880
-
881
- before do
882
- Country.field :name
883
- end
884
-
885
- it "adds the new object to the data collection" do
886
- Country.all.should be_empty
887
- country = Country.new :id => 1, :name => "foo"
888
- country.save.should be_true
889
- Country.all.should == [country]
890
- end
891
-
892
- it "adds the new object to the data collection" do
893
- Country.all.should be_empty
894
- country = Country.new :id => 1, :name => "foo"
895
- country.save!.should be_true
896
- Country.all.should == [country]
897
- end
898
-
899
- it "marks the class as dirty" do
900
- Country.dirty.should be_false
901
- Country.new(:id => 1, :name => "foo").save
902
- Country.dirty.should be_true
903
- end
904
-
905
- it "it is a no-op if the object has already been added to the collection" do
906
- Country.all.should be_empty
907
- country = Country.new :id => 1, :name => "foo"
908
- country.save
909
- country.name = "bar"
910
- country.save
911
- country.save!
912
- Country.all.should == [country]
913
- end
914
-
915
- end
916
-
917
- describe ".create" do
918
-
919
- before do
920
- Country.field :name
921
- end
922
-
923
- it "works with no args" do
924
- Country.all.should be_empty
925
- country = Country.create
926
- country.id.should == 1
927
- end
928
-
929
- it "adds the new object to the data collection" do
930
- Country.all.should be_empty
931
- country = Country.create :id => 1, :name => "foo"
932
- country.id.should == 1
933
- country.name.should == "foo"
934
- Country.all.should == [country]
935
- end
936
-
937
- it "adds an auto-incrementing id if the id is nil" do
938
- country1 = Country.new :name => "foo"
939
- country1.save
940
- country1.id.should == 1
941
-
942
- country2 = Country.new :name => "bar"
943
- country2.save
944
- country2.id.should == 2
945
- end
946
-
947
- it "does not add auto-incrementing id if the id is present" do
948
- country1 = Country.new :id => 456, :name => "foo"
949
- country1.save
950
- country1.id.should == 456
951
- end
952
-
953
- it "does not blow up with strings" do
954
- country1 = Country.new :id => "foo", :name => "foo"
955
- country1.save
956
- country1.id.should == "foo"
957
-
958
- country2 = Country.new :name => "foo"
959
- country2.save
960
- country2.id.should be_nil
961
- end
962
-
963
- it "adds the new object to the data collection" do
964
- Country.all.should be_empty
965
- country = Country.create! :id => 1, :name => "foo"
966
- country.id.should == 1
967
- country.name.should == "foo"
968
- Country.all.should == [country]
969
- end
970
-
971
- it "marks the class as dirty" do
972
- Country.dirty.should be_false
973
- Country.create! :id => 1, :name => "foo"
974
- Country.dirty.should be_true
975
- end
976
-
977
- end
978
-
979
- describe "#valid?" do
980
-
981
- it "should return true" do
982
- Country.new.should be_valid
983
- end
984
-
985
- end
986
-
987
- describe "#new_record?" do
988
- before do
989
- Country.field :name
990
- Country.data = [
991
- :id => 1, :name => "foo"
992
- ]
993
- end
994
-
995
- it "returns false when the object is already part of the collection" do
996
- Country.new(:id => 1).should_not be_new_record
997
- end
998
-
999
- it "returns true when the object is not part of the collection" do
1000
- Country.new(:id => 2).should be_new_record
1001
- end
1002
-
1003
- end
1004
-
1005
- describe ".transaction" do
1006
-
1007
- it "execute the block given to it" do
1008
- foo = Object.new
1009
- foo.should_receive(:bar)
1010
- Country.transaction do
1011
- foo.bar
1012
- end
1013
- end
1014
-
1015
- it "swallows ActiveRecord::Rollback errors" do
1016
- proc do
1017
- Country.transaction do
1018
- raise ActiveRecord::Rollback
1019
- end
1020
- end.should_not raise_error
1021
- end
1022
-
1023
- it "passes other errors through" do
1024
- proc do
1025
- Country.transaction do
1026
- raise "hell"
1027
- end
1028
- end.should raise_error("hell")
1029
- end
1030
-
1031
- end
1032
-
1033
- describe ".delete_all" do
1034
-
1035
- it "clears out all record" do
1036
- country1 = Country.create
1037
- country2 = Country.create
1038
- Country.all.should == [country1, country2]
1039
- Country.delete_all
1040
- Country.all.should be_empty
1041
- end
1042
-
1043
- it "marks the class as dirty" do
1044
- Country.dirty.should be_false
1045
- Country.delete_all
1046
- Country.dirty.should be_true
1047
- end
1048
-
1049
- end
1050
-
1051
- end