api_resource 0.4.3 → 0.5.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.
Files changed (89) hide show
  1. data/VERSION +1 -1
  2. data/api_resource.gemspec +4 -74
  3. data/coverage/assets/0.5.3/app.js +88 -0
  4. data/coverage/assets/0.5.3/fancybox/blank.gif +0 -0
  5. data/coverage/assets/0.5.3/fancybox/fancy_close.png +0 -0
  6. data/coverage/assets/0.5.3/fancybox/fancy_loading.png +0 -0
  7. data/coverage/assets/0.5.3/fancybox/fancy_nav_left.png +0 -0
  8. data/coverage/assets/0.5.3/fancybox/fancy_nav_right.png +0 -0
  9. data/coverage/assets/0.5.3/fancybox/fancy_shadow_e.png +0 -0
  10. data/coverage/assets/0.5.3/fancybox/fancy_shadow_n.png +0 -0
  11. data/coverage/assets/0.5.3/fancybox/fancy_shadow_ne.png +0 -0
  12. data/coverage/assets/0.5.3/fancybox/fancy_shadow_nw.png +0 -0
  13. data/coverage/assets/0.5.3/fancybox/fancy_shadow_s.png +0 -0
  14. data/coverage/assets/0.5.3/fancybox/fancy_shadow_se.png +0 -0
  15. data/coverage/assets/0.5.3/fancybox/fancy_shadow_sw.png +0 -0
  16. data/coverage/assets/0.5.3/fancybox/fancy_shadow_w.png +0 -0
  17. data/coverage/assets/0.5.3/fancybox/fancy_title_left.png +0 -0
  18. data/coverage/assets/0.5.3/fancybox/fancy_title_main.png +0 -0
  19. data/coverage/assets/0.5.3/fancybox/fancy_title_over.png +0 -0
  20. data/coverage/assets/0.5.3/fancybox/fancy_title_right.png +0 -0
  21. data/coverage/assets/0.5.3/fancybox/fancybox-x.png +0 -0
  22. data/coverage/assets/0.5.3/fancybox/fancybox-y.png +0 -0
  23. data/coverage/assets/0.5.3/fancybox/fancybox.png +0 -0
  24. data/coverage/assets/0.5.3/fancybox/jquery.fancybox-1.3.1.css +363 -0
  25. data/coverage/assets/0.5.3/fancybox/jquery.fancybox-1.3.1.pack.js +44 -0
  26. data/coverage/assets/0.5.3/favicon_green.png +0 -0
  27. data/coverage/assets/0.5.3/favicon_red.png +0 -0
  28. data/coverage/assets/0.5.3/favicon_yellow.png +0 -0
  29. data/coverage/assets/0.5.3/highlight.css +129 -0
  30. data/coverage/assets/0.5.3/highlight.pack.js +1 -0
  31. data/coverage/assets/0.5.3/jquery-1.6.2.min.js +18 -0
  32. data/coverage/assets/0.5.3/jquery.dataTables.min.js +152 -0
  33. data/coverage/assets/0.5.3/jquery.timeago.js +141 -0
  34. data/coverage/assets/0.5.3/jquery.url.js +174 -0
  35. data/coverage/assets/0.5.3/loading.gif +0 -0
  36. data/coverage/assets/0.5.3/magnify.png +0 -0
  37. data/coverage/assets/0.5.3/smoothness/images/ui-bg_flat_0_aaaaaa_40x100.png +0 -0
  38. data/coverage/assets/0.5.3/smoothness/images/ui-bg_flat_75_ffffff_40x100.png +0 -0
  39. data/coverage/assets/0.5.3/smoothness/images/ui-bg_glass_55_fbf9ee_1x400.png +0 -0
  40. data/coverage/assets/0.5.3/smoothness/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
  41. data/coverage/assets/0.5.3/smoothness/images/ui-bg_glass_75_dadada_1x400.png +0 -0
  42. data/coverage/assets/0.5.3/smoothness/images/ui-bg_glass_75_e6e6e6_1x400.png +0 -0
  43. data/coverage/assets/0.5.3/smoothness/images/ui-bg_glass_95_fef1ec_1x400.png +0 -0
  44. data/coverage/assets/0.5.3/smoothness/images/ui-bg_highlight-soft_75_cccccc_1x100.png +0 -0
  45. data/coverage/assets/0.5.3/smoothness/images/ui-icons_222222_256x240.png +0 -0
  46. data/coverage/assets/0.5.3/smoothness/images/ui-icons_2e83ff_256x240.png +0 -0
  47. data/coverage/assets/0.5.3/smoothness/images/ui-icons_454545_256x240.png +0 -0
  48. data/coverage/assets/0.5.3/smoothness/images/ui-icons_888888_256x240.png +0 -0
  49. data/coverage/assets/0.5.3/smoothness/images/ui-icons_cd0a0a_256x240.png +0 -0
  50. data/coverage/assets/0.5.3/smoothness/jquery-ui-1.8.4.custom.css +295 -0
  51. data/coverage/assets/0.5.3/stylesheet.css +383 -0
  52. data/coverage/index.html +3573 -0
  53. data/lib/api_resource/associations/abstract_scope.rb +191 -0
  54. data/lib/api_resource/associations/association_scope.rb +47 -0
  55. data/lib/api_resource/associations/belongs_to_remote_object_proxy.rb +5 -6
  56. data/lib/api_resource/associations/has_many_remote_object_proxy.rb +5 -8
  57. data/lib/api_resource/associations/has_one_remote_object_proxy.rb +12 -13
  58. data/lib/api_resource/associations/multi_object_proxy.rb +65 -39
  59. data/lib/api_resource/associations/resource_scope.rb +6 -17
  60. data/lib/api_resource/associations/scope.rb +23 -121
  61. data/lib/api_resource/associations/single_object_proxy.rb +41 -50
  62. data/lib/api_resource/associations.rb +32 -11
  63. data/lib/api_resource/attributes.rb +108 -69
  64. data/lib/api_resource/base.rb +114 -106
  65. data/lib/api_resource/local.rb +1 -1
  66. data/lib/api_resource/model_errors.rb +9 -6
  67. data/lib/api_resource/scopes.rb +53 -16
  68. data/lib/api_resource.rb +3 -1
  69. data/spec/lib/api_resource_spec.rb +3 -7
  70. data/spec/lib/associations/association_scope_spec.rb +19 -0
  71. data/spec/lib/associations_spec.rb +251 -162
  72. data/spec/lib/attributes_spec.rb +33 -15
  73. data/spec/lib/base_spec.rb +302 -64
  74. data/spec/lib/callbacks_spec.rb +4 -2
  75. data/spec/lib/local_spec.rb +5 -1
  76. data/spec/spec_helper.rb +2 -3
  77. data/spec/support/mocks/association_mocks.rb +9 -1
  78. data/spec/support/requests/association_requests.rb +5 -5
  79. data/spec/support/requests/test_resource_requests.rb +16 -4
  80. data/spec/tmp/api_resource_test_db.sqlite +0 -0
  81. metadata +68 -22
  82. data/.document +0 -5
  83. data/.rspec +0 -5
  84. data/.travis.yml +0 -4
  85. data/lib/api_resource/associations/association_proxy.rb +0 -121
  86. data/lib/api_resource/associations/dynamic_resource_scope.rb +0 -23
  87. data/lib/api_resource/associations/generic_scope.rb +0 -68
  88. data/lib/api_resource/associations/multi_argument_resource_scope.rb +0 -15
  89. data/lib/api_resource/associations/relation_scope.rb +0 -25
@@ -5,12 +5,112 @@ include ApiResource
5
5
 
6
6
  describe "Base" do
7
7
 
8
+ before(:all) do
9
+ TestResource.reload_resource_definition
10
+ end
11
+
8
12
  after(:all) do
9
- TestResource.reload_class_data
13
+ TestResource.reload_resource_definition
14
+ end
15
+
16
+ context ".new_element_path" do
17
+
18
+ before(:all) do
19
+
20
+ PrefixResource = Class.new(ApiResource::Base) do
21
+ self.prefix = "/path/to/project"
22
+ end
23
+
24
+ DynamicPrefixResource = Class.new(ApiResource::Base) do
25
+ self.prefix = "/path/to/nested/:id/"
26
+ end
27
+
28
+ end
29
+
30
+
31
+ it "should return a full path if there are no nested ids" do
32
+ PrefixResource.new_element_path.should eql(
33
+ "/path/to/project/prefix_resources/new.json"
34
+ )
35
+ end
36
+
37
+ it "should return a non-nested path if there are nested ids" do
38
+ DynamicPrefixResource.new_element_path.should eql(
39
+ "/dynamic_prefix_resources/new.json"
40
+ )
41
+ end
42
+
10
43
  end
44
+
45
+ context "Comparison" do
46
+
47
+ context "&group_by" do
48
+
49
+ it "should allow grouping by resources with the same id" do
50
+
51
+ test_resource_1 = TestResource.new
52
+ test_resource_1.stubs(:id => 1)
53
+
54
+ test_resource_2 = TestResource.new
55
+ test_resource_2.stubs(:id => 1)
56
+
57
+
58
+ ParentResource = Struct.new(:resource, :name)
59
+
60
+ data = [
61
+ ParentResource.new(test_resource_1, "Dan"),
62
+ ParentResource.new(test_resource_2, "Brian")
63
+ ]
64
+
65
+ data.group_by(&:resource).keys.length.should be 1
66
+
67
+ end
68
+
69
+ end
70
+
71
+ end
72
+
11
73
 
12
74
  describe "Loading data from a hash" do
13
75
 
76
+
77
+ context ".instantiate_record" do
78
+
79
+ it "should set boolean values" do
80
+
81
+ tr = TestResource.instantiate_record(:is_active => true)
82
+ tr.is_active.should eql(true)
83
+
84
+ end
85
+
86
+ it "should set boolean values" do
87
+
88
+ tr = TestResource.instantiate_record(:is_active => false)
89
+ tr.is_active.should eql(false)
90
+
91
+ end
92
+
93
+ end
94
+
95
+ context ".instantiate_collection" do
96
+
97
+ it "should set boolean values" do
98
+
99
+ data = [
100
+ {"id"=>96229, "name"=>"Mathew", "age"=>31544, "is_active"=>true},
101
+ {"id"=>82117, "name"=>"Rick", "age"=>14333, "is_active"=>true},
102
+ {"id"=>92922, "name"=>"Jimmie", "age"=>89153, "is_active"=>true},
103
+ {"id"=>67548, "name"=>"Forest", "age"=>35062, "is_active"=>true},
104
+ {"id"=>6993, "name"=>"Georgette", "age"=>84223, "is_active"=>true}
105
+ ]
106
+
107
+ tr = TestResource.instantiate_collection(data)
108
+ tr.first.is_active.should eql(true)
109
+
110
+ end
111
+
112
+ end
113
+
14
114
  context "Associations" do
15
115
  before(:all) do
16
116
  TestResource.has_many :has_many_objects
@@ -39,9 +139,14 @@ describe "Base" do
39
139
  end
40
140
 
41
141
  it "should properly load the data from the provided array or hash" do
42
- tst = TestResource.new({:has_many_objects => [{:service_uri => '/path'}]})
142
+ tst = TestResource.new({
143
+ :has_many_objects => [{:service_uri => '/path'}]
144
+ })
43
145
  tst.has_many_objects.remote_path.should eql('/path')
44
- tst = TestResource.new({:has_many_objects => {:service_uri => '/path'}})
146
+
147
+ tst = TestResource.new({
148
+ :has_many_objects => {:service_uri => '/path'}
149
+ })
45
150
  tst.has_many_objects.remote_path.should eql('/path')
46
151
  end
47
152
 
@@ -55,7 +160,8 @@ describe "Base" do
55
160
  tst.has_one_object.should be_a(Associations::SingleObjectProxy)
56
161
  end
57
162
 
58
- it "should throw an error if a belongs_to or has_many association is not a hash or nil" do
163
+ it "should throw an error if a belongs_to or
164
+ has_many association is not a hash or nil" do
59
165
  lambda {
60
166
  TestResource.new(:belongs_to_object => [])
61
167
  }.should raise_error
@@ -65,7 +171,11 @@ describe "Base" do
65
171
  end
66
172
 
67
173
  it "should properly load data from the provided hash" do
68
- tst = TestResource.new(:has_one_object => {:service_uri => "/path"})
174
+ tst = TestResource.new(
175
+ :has_one_object => {
176
+ :service_uri => "/path"
177
+ }
178
+ )
69
179
  tst.has_one_object.remote_path.should eql('/path')
70
180
  end
71
181
 
@@ -128,7 +238,7 @@ describe "Base" do
128
238
  describe "Serialization" do
129
239
 
130
240
  before(:all) do
131
- TestResource.reload_class_data
241
+ TestResource.reload_resource_definition
132
242
  TestResource.has_many :has_many_objects
133
243
  TestResource.define_attributes :attr1, :attr2
134
244
  TestResource.include_root_in_json = true
@@ -155,15 +265,28 @@ describe "Base" do
155
265
  hash["test_resource"].should_not be_nil
156
266
  end
157
267
 
158
- it "should not include associations by default if they have not changed" do
159
- tst = TestResource.new({:attr1 => "attr1", :attr2 => "attr2", :has_many_objects => []})
268
+ it "should not include associations by default if
269
+ they have not changed" do
270
+ tst = TestResource.new({
271
+ :attr1 => "attr1",
272
+ :attr2 => "attr2",
273
+ :has_many_objects => []
274
+ })
160
275
  hash = JSON.parse(tst.to_json)
161
276
  hash["has_many_objects"].should be_nil
162
277
  end
163
278
 
164
279
  it "should include associations passed given in the include_associations array" do
165
- tst = TestResource.new({:attr1 => "attr1", :attr2 => "attr2", :has_many_objects => []})
166
- hash = JSON.parse(tst.to_json(:include_associations => [:has_many_objects]))
280
+ tst = TestResource.new({
281
+ :attr1 => "attr1",
282
+ :attr2 => "attr2",
283
+ :has_many_objects => []
284
+ })
285
+ hash = JSON.parse(
286
+ tst.to_json(
287
+ :include_associations => [:has_many_objects]
288
+ )
289
+ )
167
290
  hash["has_many_objects"].should_not be_nil
168
291
  end
169
292
 
@@ -174,8 +297,19 @@ describe "Base" do
174
297
  hash["has_many_objects"].should_not be_nil
175
298
  end
176
299
 
177
- it "should not include unknown attributes unless they are passed in via the include_extras array" do
178
- tst = TestResource.new({:attr1 => "attr1", :attr2 => "attr2", :attr3 => "attr3"})
300
+ it "should not include unknown attributes unless they
301
+ are passed in via the include_extras array" do
302
+
303
+ TestResource.class_eval do
304
+ define_protected_attributes(:attr3)
305
+ end
306
+
307
+ tst = TestResource.instantiate_record({
308
+ :attr1 => "attr1",
309
+ :attr2 => "attr2",
310
+ :attr3 => "attr3"
311
+ })
312
+
179
313
  hash = JSON.parse(tst.to_json)
180
314
  hash["attr3"].should be_nil
181
315
  hash = JSON.parse(tst.to_json(:include_extras => [:attr3]))
@@ -183,7 +317,11 @@ describe "Base" do
183
317
  end
184
318
 
185
319
  it "should ignore fields set under the except option" do
186
- tst = TestResource.new({:attr1 => "attr1", :attr2 => "attr2", :attr3 => "attr3"})
320
+ tst = TestResource.instantiate_record({
321
+ :attr1 => "attr1",
322
+ :attr2 => "attr2",
323
+ :attr3 => "attr3"
324
+ })
187
325
  hash = JSON.parse(tst.to_json(:except => [:attr1]))
188
326
  hash["attr1"].should be_nil
189
327
  end
@@ -193,16 +331,32 @@ describe "Base" do
193
331
  TestResource.has_many(:has_many_objects)
194
332
  end
195
333
  after(:all) do
196
- TestResource.reload_class_data
334
+ TestResource.reload_resource_definition
197
335
  end
198
336
 
199
337
  it "should include the id of nested objects in the serialization" do
200
- tst = TestResource.new({:attr1 => "attr1", :attr2 => "attr2", :has_many_objects => [{:name => "123", :id => "1"}]})
201
- hash = JSON.parse(tst.to_json(:include_associations => [:has_many_objects]))
202
- hash["has_many_objects"].first["id"].should_not be nil
338
+ tst = TestResource.new({
339
+ :attr1 => "attr1",
340
+ :attr2 => "attr2",
341
+ :has_many_objects => [
342
+ {:name => "123", :id => "1"}
343
+ ]
344
+ })
345
+ tst.has_many_objects.first.id
346
+ hash = JSON.parse(
347
+ tst.to_json(:include_associations => [:has_many_objects])
348
+ )
349
+ hash["has_many_objects"].first["id"].should_not be_nil
203
350
  end
351
+
204
352
  it "should include the id of nested objects in the serialization" do
205
- tst = TestResource.new({:attr1 => "attr1", :attr2 => "attr2", :has_many_objects => [{:name => "123"}]})
353
+ tst = TestResource.new({
354
+ :attr1 => "attr1",
355
+ :attr2 => "attr2",
356
+ :has_many_objects => [
357
+ {:name => "123"}
358
+ ]
359
+ })
206
360
  hash = JSON.parse(tst.to_json(:include_associations => [:has_many_objects]))
207
361
  hash["has_many_objects"].first.keys.should_not include "id"
208
362
  end
@@ -218,7 +372,9 @@ describe "Base" do
218
372
  end
219
373
 
220
374
  it "should properly serialize associations if they are included" do
221
- tst = TestResource.new({:attr1 => "attr1", :attr2 => "attr2", :has_many_objects => []})
375
+ tst = TestResource.new({
376
+ :has_many_objects => []
377
+ })
222
378
  hash = Hash.from_xml(tst.to_xml(:include_associations => [:has_many_objects]))
223
379
  hash["test_resource"]["has_many_objects"].should eql([])
224
380
  end
@@ -229,7 +385,7 @@ describe "Base" do
229
385
  describe "Finding Data" do
230
386
 
231
387
  before(:all) do
232
- TestResource.reload_class_data
388
+ TestResource.reload_resource_definition
233
389
  end
234
390
 
235
391
  it "should be able to find all" do
@@ -262,7 +418,7 @@ describe "Base" do
262
418
 
263
419
  before(:all) do
264
420
  TestResource.include_root_in_json = true
265
- TestResource.reload_class_data
421
+ TestResource.reload_resource_definition
266
422
  end
267
423
 
268
424
  context "Creating new records" do
@@ -271,11 +427,6 @@ describe "Base" do
271
427
  TestResource.has_many :has_many_objects
272
428
  end
273
429
 
274
- it "should be able to post new data via the create method" do
275
- tr = TestResource.create({:name => "Ethan", :age => 20})
276
- tr.id.should_not be_blank
277
- end
278
-
279
430
  it "should be able to post new data via the save method" do
280
431
  tr = TestResource.build({:name => "Ethan", :age => 20})
281
432
  tr.save.should be_true
@@ -367,7 +518,7 @@ describe "Base" do
367
518
 
368
519
  context "Updating old records" do
369
520
  before(:all) do
370
- TestResource.reload_class_data
521
+ TestResource.reload_resource_definition
371
522
  TestResource.has_many :has_many_objects
372
523
  RestClient::Payload.stubs(:has_file? => false)
373
524
  end
@@ -380,11 +531,17 @@ describe "Base" do
380
531
 
381
532
  ApiResource::Connection.any_instance.expects(:put).with(
382
533
  "/test_resources/1.json",
383
- "{\"test_resource\":{\"age\":6}}",
534
+ JSON.unparse({
535
+ :test_resource => {
536
+ :name => "Ethan",
537
+ :age => 6
538
+ }
539
+ }),
384
540
  TestResource.headers
385
541
  )
386
542
 
387
- tr = TestResource.new(:id => 1, :name => "Ethan")
543
+ tr = TestResource.new(:name => "Ethan")
544
+ tr.stubs(:id => 1)
388
545
  tr.should_not be_new
389
546
 
390
547
  # Thus we know we are calling update
@@ -396,11 +553,18 @@ describe "Base" do
396
553
  it "should include changed associations without specification" do
397
554
  ApiResource::Connection.any_instance.expects(:put).with(
398
555
  "/test_resources/1.json",
399
- "{\"test_resource\":{\"has_many_objects\":[{}]}}",
556
+ JSON.unparse({
557
+ :test_resource => {
558
+ :name => "Ethan",
559
+ :has_many_objects => [{}]
560
+ }
561
+ }),
400
562
  TestResource.headers
401
563
  )
402
564
 
403
- tr = TestResource.new(:id => 1, :name => "Ethan")
565
+ tr = TestResource.new(:name => "Ethan")
566
+ tr.stubs(:id => 1)
567
+
404
568
  tr.has_many_objects = [HasManyObject.new]
405
569
  tr.save
406
570
  end
@@ -409,11 +573,17 @@ describe "Base" do
409
573
  it "should include unchanged associations if they are specified" do
410
574
  ApiResource::Connection.any_instance.expects(:put).with(
411
575
  "/test_resources/1.json",
412
- "{\"test_resource\":{\"has_many_objects\":[]}}",
576
+ JSON.unparse({
577
+ :test_resource => {
578
+ :name => "Ethan",
579
+ :has_many_objects => []
580
+ }
581
+ }),
413
582
  TestResource.headers
414
583
  )
415
584
 
416
- tr = TestResource.new(:id => 1, :name => "Ethan")
585
+ tr = TestResource.new(:name => "Ethan")
586
+ tr.stubs(:id => 1)
417
587
  tr.save(:has_many_objects)
418
588
  end
419
589
 
@@ -425,18 +595,32 @@ describe "Base" do
425
595
 
426
596
  ApiResource::Connection.any_instance.expects(:put).with(
427
597
  "/test_resources/1.json",
428
- "{\"test_resource\":{\"has_one_object\":{\"size\":\"large\"}}}",
598
+ JSON.unparse({
599
+ :test_resource => {
600
+ :name => "Ethan",
601
+ :has_one_object => {
602
+ :size => "large"
603
+ }
604
+ }
605
+ }),
429
606
  TestResource.headers
430
607
  ).in_sequence(correct_order)
431
608
 
432
- tr = TestResource.new(:id => 1, :name => "Ethan")
609
+ tr = TestResource.new(:name => "Ethan")
610
+ tr.stubs(:id => 1)
433
611
  tr.has_one_object = HasOneObject.new(:size => "large", :color => nil)
434
612
  tr.save(:include_associations => [:has_one_object])
435
613
 
436
614
 
437
615
  ApiResource::Connection.any_instance.expects(:put).with(
438
616
  "/test_resources/1.json",
439
- "{\"test_resource\":{\"has_one_object\":{\"size\":null}}}",
617
+ JSON.unparse({
618
+ :test_resource => {
619
+ :has_one_object => {
620
+ :size => nil
621
+ }
622
+ }
623
+ }),
440
624
  TestResource.headers
441
625
  ).in_sequence(correct_order)
442
626
 
@@ -452,11 +636,19 @@ describe "Base" do
452
636
 
453
637
  ApiResource::Connection.any_instance.expects(:put).with(
454
638
  "/test_resources/1.json",
455
- "{\"test_resource\":{\"has_one_object\":{\"size\":\"large\"}}}",
639
+ JSON.unparse({
640
+ :test_resource => {
641
+ :name => "Ethan",
642
+ :has_one_object => {
643
+ :size => "large"
644
+ }
645
+ }
646
+ }),
456
647
  TestResource.headers
457
648
  ).in_sequence(correct_order)
458
649
 
459
- tr = TestResource.new(:id => 1, :name => "Ethan")
650
+ tr = TestResource.new(:name => "Ethan")
651
+ tr.stubs(:id => 1)
460
652
  tr.has_one_object = HasOneObject.new(:size => "large", :color => nil)
461
653
  tr.save(:include_associations => [:has_one_object])
462
654
 
@@ -473,26 +665,41 @@ describe "Base" do
473
665
 
474
666
 
475
667
  it "should include all attributes if include_all_attributes_on_update is true" do
668
+
476
669
  ApiResource::Connection.any_instance.expects(:put).with(
477
670
  "/test_resources/1.json",
478
671
  "{\"test_resource\":{\"name\":\"Ethan\",\"age\":null,\"is_active\":null,\"bday\":null,\"roles\":[]}}",
479
672
  TestResource.headers
480
673
  )
481
-
482
- TestResource.include_all_attributes_on_update = true
483
- tr = TestResource.new(:id => 1, :name => "Ethan")
484
- tr.save
485
- TestResource.include_all_attributes_on_update = false
674
+ begin
675
+ TestResource.include_all_attributes_on_update = true
676
+ tr = TestResource.new(:name => "Ethan")
677
+ tr.stubs(:id => 1)
678
+ tr.save
679
+ ensure
680
+ TestResource.include_all_attributes_on_update = false
681
+ end
486
682
  end
487
683
 
488
684
  it "should provide an update_attributes method to set attrs and save" do
489
- ApiResource::Connection.any_instance.expects(:put).with(
490
- "/test_resources/1.json",
491
- "{\"test_resource\":{\"name\":\"Dan\"}}",
492
- TestResource.headers
493
- )
685
+
686
+ correct_order = sequence("ordering")
687
+
688
+ # initial save
689
+ ApiResource::Connection.any_instance.expects(:put)
690
+ .in_sequence(correct_order)
691
+
692
+ ApiResource::Connection.any_instance.expects(:put)
693
+ .with(
694
+ "/test_resources/1.json",
695
+ {:test_resource => {:name => "Dan"}}.to_json,
696
+ TestResource.headers
697
+ ).in_sequence(correct_order)
698
+
699
+ tr = TestResource.new(:name => "Ethan")
700
+ tr.stubs(:id => 1)
701
+ tr.save
494
702
 
495
- tr = TestResource.new(:id => 1, :name => "Ethan")
496
703
  tr.update_attributes(:name => "Dan")
497
704
  end
498
705
 
@@ -500,28 +707,50 @@ describe "Base" do
500
707
  it "should include nil attributes when updating if they have
501
708
  changed by default" do
502
709
 
503
- ApiResource::Connection.any_instance.expects(:put).with(
504
- "/test_resources/1.json",
505
- "{\"test_resource\":{\"is_active\":null}}",
506
- TestResource.headers
507
- )
710
+ correct_order = sequence("ordering")
711
+
712
+ # initial save
713
+ ApiResource::Connection.any_instance.expects(:put)
714
+ .in_sequence(correct_order)
715
+
716
+ ApiResource::Connection.any_instance.expects(:put)
717
+ .with(
718
+ "/test_resources/1.json",
719
+ {:test_resource => {:is_active => nil}}.to_json,
720
+ TestResource.headers
721
+ )
722
+ .in_sequence(correct_order)
508
723
 
509
724
  tr = TestResource.new(
510
- :id => "1", :name => "Ethan", :is_active => false
725
+ :name => "Ethan", :is_active => false
511
726
  )
727
+ tr.stubs(:id => 1)
728
+ tr.save
729
+
512
730
  tr.update_attributes(:is_active => nil)
513
731
  end
514
732
 
515
733
  it "should include attributes that have changed to false by default" do
516
- ApiResource::Connection.any_instance.expects(:put).with(
517
- "/test_resources/1.json",
518
- "{\"test_resource\":{\"is_active\":false}}",
519
- TestResource.headers
520
- )
734
+ correct_order = sequence("ordering")
735
+
736
+ # initial save
737
+ ApiResource::Connection.any_instance.expects(:put)
738
+ .in_sequence(correct_order)
739
+
740
+ # update
741
+ ApiResource::Connection.any_instance.expects(:put)
742
+ .with(
743
+ "/test_resources/1.json",
744
+ {:test_resource => {:is_active => false}}.to_json,
745
+ TestResource.headers
746
+ ).in_sequence(correct_order)
521
747
 
522
748
  tr = TestResource.new(
523
- :id => "1", :name => "Ethan", :is_active => true
749
+ :name => "Ethan", :is_active => true
524
750
  )
751
+ tr.stubs(:id => 1)
752
+ tr.save
753
+
525
754
  tr.update_attributes(:is_active => false)
526
755
 
527
756
  end
@@ -536,7 +765,9 @@ describe "Base" do
536
765
  end
537
766
 
538
767
  it "should be able to destroy itself as an instance" do
539
- tr = TestResource.new(:id => 1, :name => "Ethan")
768
+ tr = TestResource.new(:name => "Ethan")
769
+ tr.stubs(:id => 1)
770
+
540
771
  tr.destroy.should be_true
541
772
  end
542
773
  end
@@ -544,8 +775,11 @@ describe "Base" do
544
775
  describe "Random methods" do
545
776
 
546
777
  it "should know if it is persisted" do
547
- tr = TestResource.new(:id => 1, :name => "Ethan")
778
+ tr = TestResource.new(:name => "Ethan")
779
+ tr.stubs(:id => 1)
780
+
548
781
  tr.persisted?.should be_true
782
+
549
783
  tr = TestResource.new(:name => "Ethan")
550
784
  tr.persisted?.should be_false
551
785
  end
@@ -609,6 +843,9 @@ describe "Base" do
609
843
  begin
610
844
  initial = ApiResource::Base.ttl
611
845
  ApiResource::Base.ttl = 1
846
+ if defined?(Rails)
847
+ Object.send(:remove_const, :Rails)
848
+ end
612
849
  example.run
613
850
  ensure
614
851
  ApiResource::Base.ttl = initial
@@ -640,13 +877,14 @@ describe "Base" do
640
877
 
641
878
  it "should load the resouce definition when respond_to? is called" do
642
879
  # remove our attribute that denotes that the definition was loaded
643
- TestResource.send(:remove_instance_variable, :@class_data)
880
+ TestResource.send(:remove_instance_variable, :@resource_definition)
644
881
  TestResource.expects(:set_class_attributes_upon_load)
645
882
  TestResource.respond_to?(:test)
646
883
  end
647
884
 
648
885
  it "should not load the resource definition when respond_to? is called
649
886
  if the definition has already been loaded" do
887
+ TestResource.send(:respond_to?, :some_method)
650
888
  TestResource.expects(:set_class_attributes_upon_load).never
651
889
  TestResource.send(:respond_to?, :some_method)
652
890
  end
@@ -49,7 +49,8 @@ describe "Should put callbacks around save, create, update, and destroy by defau
49
49
  end
50
50
 
51
51
  it "should fire save and update callbacks when updating a record" do
52
- tr = TestResource.new(:id => 1, :name => "Ethan", :age => 20)
52
+ tr = TestResource.new(:name => "Ethan", :age => 20)
53
+ tr.stubs(:id => 1)
53
54
  tr.name = "Test"
54
55
  tr.age = 21
55
56
  tr.save.should be_true
@@ -59,7 +60,8 @@ describe "Should put callbacks around save, create, update, and destroy by defau
59
60
  end
60
61
 
61
62
  it "should only fire destroy callbacks when destroying a record" do
62
- tr = TestResource.new(:id => 1, :name => "Ethan", :age => 20)
63
+ tr = TestResource.new(:name => "Ethan", :age => 20)
64
+ tr.stubs(:id => 1)
63
65
  tr.destroy.should be_true
64
66
  tr.d_val.should eql(2)
65
67
  tr.s_val.should be_nil
@@ -12,7 +12,11 @@ describe "Local" do
12
12
  end
13
13
  mtr = MyTestResource.new
14
14
  # should still have scopes
15
- MyTestResource.reload_class_attributes
15
+
16
+ MyTestResource.expects(:clear_attributes).never
17
+ MyTestResource.expects(:clear_related_objects).never
18
+
19
+ MyTestResource.reload_resource_definition
16
20
  mtr.scopes.should_not be_blank
17
21
 
18
22
  end
data/spec/spec_helper.rb CHANGED
@@ -35,12 +35,11 @@ Spork.prefork do
35
35
  ApiResource.format = :json
36
36
  ApiResource.load_mocks_and_factories
37
37
 
38
- ApiResource.logger.level = Log4r::INFO
38
+ ApiResource.logger.level = Log4r::DEBUG
39
39
 
40
40
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
41
41
 
42
-
43
-
42
+
44
43
  RSpec.configure do |config|
45
44
  config.mock_with :mocha
46
45
  config.treat_symbols_as_metadata_keys_with_true_values = true
@@ -21,7 +21,15 @@ Mocks.define do
21
21
  get((0..4).to_a.collect{HashDealer.roll(:test_association_resource)}, :params => {})
22
22
  get((0..4).to_a.collect{HashDealer.roll(:active_test_association_resource)}, :params => {:active => true})
23
23
  get((0..4).to_a.collect{HashDealer.roll(:active_test_association_resource)}, :params => {:active => false})
24
- get((0..4).to_a.collect{HashDealer.roll(:active_birthday_test_association_resource)}, :params => {:active => true, :birthday => true})
24
+ get(
25
+ (0..4).to_a.collect{
26
+ HashDealer.roll(:active_birthday_test_association_resource)
27
+ },
28
+ :params => {
29
+ :active => true,
30
+ :birthday => {:date => Date.today}
31
+ }.matcher
32
+ )
25
33
  end
26
34
 
27
35
  endpoint("/has_one_objects/new") do