old_api_resource 0.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.
Files changed (48) hide show
  1. data/.document +5 -0
  2. data/.rspec +3 -0
  3. data/Gemfile +26 -0
  4. data/Guardfile +22 -0
  5. data/LICENSE.txt +20 -0
  6. data/README.rdoc +19 -0
  7. data/Rakefile +49 -0
  8. data/VERSION +1 -0
  9. data/lib/old_api_resource.rb +70 -0
  10. data/lib/old_api_resource/associations.rb +192 -0
  11. data/lib/old_api_resource/associations/association_proxy.rb +92 -0
  12. data/lib/old_api_resource/associations/multi_object_proxy.rb +74 -0
  13. data/lib/old_api_resource/associations/related_object_hash.rb +12 -0
  14. data/lib/old_api_resource/associations/relation_scope.rb +24 -0
  15. data/lib/old_api_resource/associations/resource_scope.rb +25 -0
  16. data/lib/old_api_resource/associations/scope.rb +88 -0
  17. data/lib/old_api_resource/associations/single_object_proxy.rb +64 -0
  18. data/lib/old_api_resource/attributes.rb +162 -0
  19. data/lib/old_api_resource/base.rb +548 -0
  20. data/lib/old_api_resource/callbacks.rb +49 -0
  21. data/lib/old_api_resource/connection.rb +167 -0
  22. data/lib/old_api_resource/core_extensions.rb +7 -0
  23. data/lib/old_api_resource/custom_methods.rb +119 -0
  24. data/lib/old_api_resource/exceptions.rb +85 -0
  25. data/lib/old_api_resource/formats.rb +14 -0
  26. data/lib/old_api_resource/formats/json_format.rb +25 -0
  27. data/lib/old_api_resource/formats/xml_format.rb +36 -0
  28. data/lib/old_api_resource/log_subscriber.rb +15 -0
  29. data/lib/old_api_resource/mocks.rb +260 -0
  30. data/lib/old_api_resource/model_errors.rb +86 -0
  31. data/lib/old_api_resource/observing.rb +29 -0
  32. data/lib/old_api_resource/railtie.rb +18 -0
  33. data/old_api_resource.gemspec +134 -0
  34. data/spec/lib/associations_spec.rb +519 -0
  35. data/spec/lib/attributes_spec.rb +121 -0
  36. data/spec/lib/base_spec.rb +499 -0
  37. data/spec/lib/callbacks_spec.rb +68 -0
  38. data/spec/lib/mocks_spec.rb +28 -0
  39. data/spec/lib/model_errors_spec.rb +29 -0
  40. data/spec/spec_helper.rb +36 -0
  41. data/spec/support/mocks/association_mocks.rb +46 -0
  42. data/spec/support/mocks/error_resource_mocks.rb +21 -0
  43. data/spec/support/mocks/test_resource_mocks.rb +43 -0
  44. data/spec/support/requests/association_requests.rb +14 -0
  45. data/spec/support/requests/error_resource_requests.rb +25 -0
  46. data/spec/support/requests/test_resource_requests.rb +31 -0
  47. data/spec/support/test_resource.rb +50 -0
  48. metadata +286 -0
@@ -0,0 +1,121 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ include OldApiResource
4
+
5
+ describe "Attributes" do
6
+
7
+ after(:all) do
8
+ TestResource.reload_class_attributes
9
+ end
10
+
11
+ context "Defining, getting, and setting attributes" do
12
+ it "should be able to define known attributes" do
13
+ TestResource.define_attributes :attr1, :attr2
14
+ TestResource.attribute?(:attr1).should be_true
15
+ TestResource.attribute?(:attr2).should be_true
16
+ end
17
+
18
+ it "should define methods for testing for reading and writing known attributes" do
19
+ TestResource.define_attributes :attr1, :attr2
20
+ tst = TestResource.new
21
+ tst.respond_to?(:attr1).should be_true
22
+ tst.respond_to?(:attr1=).should be_true
23
+ tst.respond_to?(:attr1?).should be_true
24
+ end
25
+
26
+ it "should be able to set and change attributes" do
27
+ TestResource.define_attributes :attr1, :attr2
28
+ tst = TestResource.new
29
+ tst.attr1.should be_nil
30
+ tst.attr1?.should be_false
31
+ tst.attr1 = "test"
32
+ tst.attr1.should eql("test")
33
+ tst.attr1?.should be_true
34
+ end
35
+
36
+ it "should be able to set multiple attributes at once" do
37
+ TestResource.define_attributes :attr1, :attr2, :attr3
38
+ tst = TestResource.new
39
+ tst.attr3 = "123"
40
+
41
+ tst.attributes = {:attr1 => "abc", :attr2 => "test"}
42
+ tst.attr1.should eql "abc"
43
+ tst.attr2.should eql "test"
44
+ tst.attr3.should eql "123"
45
+ end
46
+
47
+ end
48
+
49
+ context "Protected attributes" do
50
+ it "should allow protected attributes that cannot be changed" do
51
+ TestResource.define_protected_attributes :pattr3
52
+ lambda {
53
+ tst = TestResource.new
54
+ tst.pattr3 = "test"
55
+ }.should raise_error
56
+ end
57
+ end
58
+
59
+ context "Dirty tracking" do
60
+ context "Changes to attributes" do
61
+ it "should implement dirty tracking for attributes" do
62
+ TestResource.define_attributes :attr1, :attr2
63
+ tst = TestResource.new
64
+ tst.changed.should be_blank
65
+ tst.attr1 = "Hello"
66
+ tst.changed.include?("attr1").should be_true
67
+ tst.changes.should_not be_blank
68
+
69
+ # Set an attribute equal to itself
70
+ tst.attr2 = tst.attr2
71
+ tst.changes.include?("attr2").should be_false
72
+ end
73
+
74
+ end
75
+
76
+ context "Resetting and marking attributes current" do
77
+
78
+ before(:each) do
79
+ TestResource.define_attributes :attr1, :attr2
80
+ end
81
+
82
+ it "should be able to mark any list of attributes as current (unchanged)" do
83
+ tst = TestResource.new
84
+ tst.attr1 = "Hello"
85
+ tst.changed.should_not be_blank
86
+ tst.set_attributes_as_current :attr1, :attr2
87
+ tst.changed.should be_blank
88
+ end
89
+
90
+ it "should be able to mark all the attributes as current if none are given" do
91
+ tst = TestResource.new
92
+ tst.attr1 = "attr1"
93
+ tst.attr2 = "attr2"
94
+ tst.changed.should_not be_blank
95
+ tst.set_attributes_as_current
96
+ tst.changed.should be_blank
97
+ end
98
+
99
+ it "should be able to reset any list of attributes" do
100
+ tst = TestResource.new
101
+ tst.attr1 = "attr1"
102
+ tst.reset_attribute_changes :attr1
103
+ tst.attr1.should be_nil
104
+ tst.changed.should be_blank
105
+ end
106
+
107
+ it "should be able to reset all the attributes if none are given" do
108
+ tst = TestResource.new
109
+ tst.attr1 = "attr1"
110
+ tst.attr2 = "attr2"
111
+
112
+ tst.reset_attribute_changes
113
+ tst.attr1.should be_nil
114
+ tst.attr2.should be_nil
115
+ tst.changed.should be_blank
116
+ end
117
+ end
118
+
119
+ end
120
+
121
+ end
@@ -0,0 +1,499 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+ require 'json'
3
+
4
+ include OldApiResource
5
+
6
+ describe "Base" do
7
+
8
+ after(:all) do
9
+ TestResource.reload_class_attributes
10
+ end
11
+
12
+ describe "Loading data from a hash" do
13
+
14
+ describe "Determining Attributes, Scopes, and Associations from the server" do
15
+
16
+ it "should determine it's attributes when the class loads" do
17
+ tst = TestResource.new
18
+ tst.attribute?(:name).should be_true
19
+ tst.attribute?(:age).should be_true
20
+ end
21
+
22
+ it "should determine it's associations when the class loads" do
23
+ tst = TestResource.new
24
+ tst.association?(:has_many_objects).should be_true
25
+ tst.association?(:belongs_to_object).should be_true
26
+ end
27
+
28
+ it "should be able to determine scopes when the class loads" do
29
+ tst = TestResource.new
30
+ tst.scope?(:paginate).should be_true
31
+ tst.scope?(:active).should be_true
32
+ end
33
+
34
+ end
35
+ context "Attributes" do
36
+ before(:all) do
37
+ TestResource.define_attributes :attr1, :attr2
38
+ TestResource.define_protected_attributes :attr3
39
+ end
40
+
41
+ it "should set attributes for the data loaded from a hash" do
42
+ tst = TestResource.new({:attr1 => "attr1", :attr2 => "attr2"})
43
+ tst.attr1?.should be_true
44
+ tst.attr1.should eql("attr1")
45
+ tst.attr1 = "test"
46
+ tst.attr1.should eql("test")
47
+ end
48
+
49
+ it "should create protected attributes for unknown attributes trying to be loaded" do
50
+ tst = TestResource.new({:attr1 => "attr1", :attr3 => "attr3"})
51
+ tst.attr3?.should be_true
52
+ tst.attr3.should eql("attr3")
53
+ lambda {
54
+ tst.attr3 = "test"
55
+ }.should raise_error
56
+ end
57
+ end
58
+
59
+ context "Associations" do
60
+ before(:all) do
61
+ TestResource.has_many :has_many_objects
62
+ TestResource.has_one :has_one_object
63
+ TestResource.belongs_to :belongs_to_object
64
+ end
65
+
66
+ after(:all) do
67
+ TestResource.related_objects.each do |key,val|
68
+ val.clear
69
+ end
70
+ end
71
+
72
+ context "MultiObjectProxy" do
73
+
74
+ it "should create a MultiObjectProxy for has_many associations" do
75
+ tst = TestResource.new({:has_many_objects => []})
76
+ tst.has_many_objects.should be_a(Associations::MultiObjectProxy)
77
+ end
78
+
79
+ it "should throw an error if a has many association is not nil or an array or a hash" do
80
+ TestResource.new({:has_many_objects => nil})
81
+ lambda {
82
+ TestResource.new({:has_many_objects => "invalid"})
83
+ }.should raise_error
84
+ end
85
+
86
+ it "should properly load the data from the provided array or hash" do
87
+ tst = TestResource.new({:has_many_objects => [{:service_uri => '/path'}]})
88
+ tst.has_many_objects.remote_path.should eql('/path')
89
+ tst = TestResource.new({:has_many_objects => {:service_uri => '/path'}})
90
+ tst.has_many_objects.remote_path.should eql('/path')
91
+ end
92
+
93
+ end
94
+
95
+ context "SingleObjectProxy" do
96
+
97
+ it "should create a SingleObjectProxy for belongs to and has_one associations" do
98
+ tst = TestResource.new(:belongs_to_object => {}, :has_one_object => {})
99
+ tst.belongs_to_object.should be_a(Associations::SingleObjectProxy)
100
+ tst.has_one_object.should be_a(Associations::SingleObjectProxy)
101
+ end
102
+
103
+ it "should throw an error if a belongs_to or has_many association is not a hash or nil" do
104
+ lambda {
105
+ TestResource.new(:belongs_to_object => [])
106
+ }.should raise_error
107
+ lambda {
108
+ TestResource.new(:has_one_object => [])
109
+ }.should raise_error
110
+ end
111
+
112
+ it "should properly load data from the provided hash" do
113
+ tst = TestResource.new(:has_one_object => {:service_uri => "/path"})
114
+ tst.has_one_object.remote_path.should eql('/path')
115
+ end
116
+
117
+ end
118
+ end
119
+ end
120
+
121
+ describe "Request parameters and paths" do
122
+
123
+ after(:each) do
124
+ TestResource.element_name = TestResource.model_name.element
125
+ TestResource.collection_name = TestResource.element_name.to_s.pluralize
126
+ end
127
+
128
+ it "should set the element name and collection name by default" do
129
+ TestResource.element_name.should eql("test_resource")
130
+ TestResource.collection_name.should eql("test_resources")
131
+ end
132
+
133
+ it "should be able to set the element and collection names to anything" do
134
+ TestResource.element_name = "element"
135
+ TestResource.collection_name = "elements"
136
+ TestResource.element_name.should eql("element")
137
+ TestResource.collection_name.should eql("elements")
138
+ end
139
+
140
+ it "should propery generate collection paths and element paths with the new names and the default format json" do
141
+ TestResource.element_name = "element"
142
+ TestResource.collection_name = "elements"
143
+ TestResource.new_element_path.should eql("/elements/new.json")
144
+ TestResource.collection_path.should eql("/elements.json")
145
+ TestResource.element_path(1).should eql("/elements/1.json")
146
+ TestResource.element_path(1, :active => true).should eql("/elements/1.json?active=true")
147
+ end
148
+
149
+ it "should be able to set the format" do
150
+ TestResource.format.extension.to_sym.should eql(:json)
151
+ TestResource.format = :xml
152
+ TestResource.format.extension.to_sym.should eql(:xml)
153
+ TestResource.format = :json
154
+ end
155
+
156
+ it "should be able to set an http timeout" do
157
+ TestResource.timeout = 5
158
+ TestResource.timeout.should eql(5)
159
+ TestResource.connection.timeout.should eql(5)
160
+ end
161
+
162
+ end
163
+
164
+ describe "Serialization" do
165
+
166
+ before(:all) do
167
+ TestResource.reload_class_attributes
168
+ TestResource.has_many :has_many_objects
169
+ TestResource.define_attributes :attr1, :attr2
170
+ TestResource.include_root_in_json = true
171
+ end
172
+
173
+ before(:each) do
174
+ TestResource.include_root_in_json = false
175
+ end
176
+
177
+ context "JSON" do
178
+
179
+ it "should be able to serialize itself without the root" do
180
+ TestResource.include_root_in_json = false
181
+ tst = TestResource.new({:attr1 => "attr1", :attr2 => "attr2"})
182
+ hash = JSON.parse(tst.to_json)
183
+ hash["attr1"].should eql("attr1")
184
+ hash["attr2"].should eql("attr2")
185
+ end
186
+
187
+ it "should be able to serialize itself with the root" do
188
+ TestResource.include_root_in_json = true
189
+ tst = TestResource.new({:attr1 => "attr1", :attr2 => "attr2"})
190
+ hash = JSON.parse(tst.to_json)
191
+ hash["test_resource"].should_not be_nil
192
+ end
193
+
194
+ it "should not include associations by default" do
195
+ tst = TestResource.new({:attr1 => "attr1", :attr2 => "attr2", :has_many_objects => []})
196
+ hash = JSON.parse(tst.to_json)
197
+ hash["has_many_objects"].should be_nil
198
+ end
199
+
200
+ it "should include associations passed given in the include_associations array" do
201
+ tst = TestResource.new({:attr1 => "attr1", :attr2 => "attr2", :has_many_objects => []})
202
+ hash = JSON.parse(tst.to_json(:include_associations => [:has_many_objects]))
203
+ hash["has_many_objects"].should_not be_nil
204
+ end
205
+
206
+ it "should not include unknown attributes unless they are passed in via the include_extras array" do
207
+ tst = TestResource.new({:attr1 => "attr1", :attr2 => "attr2", :attr3 => "attr3"})
208
+ hash = JSON.parse(tst.to_json)
209
+ hash["attr3"].should be_nil
210
+ hash = JSON.parse(tst.to_json(:include_extras => [:attr3]))
211
+ hash["attr3"].should_not be_nil
212
+ end
213
+
214
+ it "should ignore fields set under the except option" do
215
+ tst = TestResource.new({:attr1 => "attr1", :attr2 => "attr2", :attr3 => "attr3"})
216
+ hash = JSON.parse(tst.to_json(:except => [:attr1]))
217
+ hash["attr1"].should be_nil
218
+ end
219
+
220
+ context "Nested Objects" do
221
+ before(:all) do
222
+ TestResource.has_many(:has_many_objects)
223
+ end
224
+ after(:all) do
225
+ TestResource.reload_class_attributes
226
+ end
227
+
228
+ it "should include the id of nested objects in the serialization" do
229
+ tst = TestResource.new({:attr1 => "attr1", :attr2 => "attr2", :has_many_objects => [{:name => "123", :id => "1"}]})
230
+ hash = JSON.parse(tst.to_json(:include_associations => [:has_many_objects]))
231
+ hash["has_many_objects"].first["id"].should_not be nil
232
+ end
233
+ it "should include the id of nested objects in the serialization" do
234
+ tst = TestResource.new({:attr1 => "attr1", :attr2 => "attr2", :has_many_objects => [{:name => "123"}]})
235
+ hash = JSON.parse(tst.to_json(:include_associations => [:has_many_objects]))
236
+ hash["has_many_objects"].first.keys.should_not include "id"
237
+ end
238
+ end
239
+ end
240
+
241
+ context "XML" do
242
+
243
+ it "should only be able to serialize itself with the root" do
244
+ tst = TestResource.new({:attr1 => "attr1", :attr2 => "attr2"})
245
+ hash = Hash.from_xml(tst.to_xml)
246
+ hash["test_resource"].should_not be_nil
247
+ end
248
+
249
+ it "should properly serialize associations if they are included" do
250
+ tst = TestResource.new({:attr1 => "attr1", :attr2 => "attr2", :has_many_objects => []})
251
+ hash = Hash.from_xml(tst.to_xml(:include_associations => [:has_many_objects]))
252
+ hash["test_resource"]["has_many_objects"].should eql([])
253
+ end
254
+ end
255
+
256
+ end
257
+
258
+ describe "Finding Data" do
259
+
260
+ before(:all) do
261
+ TestResource.reload_class_attributes
262
+ end
263
+
264
+ it "should be able to find all" do
265
+ resources = TestResource.find(:all)
266
+ resources.size.should eql(5)
267
+ resources.each{|r| r.should be_a TestResource}
268
+ end
269
+
270
+ it "should be able to find first or last" do
271
+ res = TestResource.first
272
+ res.should be_a TestResource
273
+ res.name.should_not be_blank
274
+ res.age.should_not be_blank
275
+
276
+ res = TestResource.last
277
+ res.should be_a TestResource
278
+ res.name.should_not be_blank
279
+ res.age.should_not be_blank
280
+ end
281
+
282
+ it "should be able to find by id" do
283
+ res = TestResource.find(2)
284
+ res.should be_a TestResource
285
+ res.id.to_i.should eql(2)
286
+ end
287
+
288
+ end
289
+
290
+ describe "Saving Data" do
291
+
292
+ before(:all) do
293
+ TestResource.include_root_in_json = true
294
+ TestResource.reload_class_attributes
295
+ end
296
+
297
+ context "Creating new records" do
298
+
299
+ before(:all) do
300
+ TestResource.has_many :has_many_objects
301
+ end
302
+
303
+ it "should be able to post new data via the create method" do
304
+ tr = TestResource.create({:name => "Ethan", :age => 20})
305
+ tr.id.should_not be_blank
306
+ end
307
+
308
+ it "should be able to post new data via the save method" do
309
+ tr = TestResource.build({:name => "Ethan", :age => 20})
310
+ tr.save.should be_true
311
+ tr.id.should_not be_blank
312
+ end
313
+
314
+ context("Override create to return the json") do
315
+
316
+ before(:all) do
317
+ TestResource.send(:alias_method, :old_create, :create)
318
+ TestResource.send(:alias_method, :old_save, :save)
319
+
320
+ TestResource.send(:define_method, :create) do |*args|
321
+ opts = args.extract_options!
322
+ # When we create we should not include any blank attributes unless they are associations
323
+ except = self.class.include_blank_attributes_on_create ? {} : self.attributes.select{|k,v| v.blank?}
324
+ opts[:except] = opts[:except] ? opts[:except].concat(except.keys).uniq.symbolize_array : except.keys.symbolize_array
325
+ opts[:include_associations] = opts[:include_associations] ? opts[:include_associations].concat(args) : []
326
+ opts[:include_extras] ||= []
327
+ encode(opts)
328
+ end
329
+ TestResource.send(:define_method, :save) do |*args|
330
+ new? ? create(*args) : update(*args)
331
+ end
332
+ end
333
+
334
+ after(:all) do
335
+ TestResource.send(:alias_method, :create, :old_create)
336
+ TestResource.send(:alias_method, :save, :old_save)
337
+ end
338
+
339
+ it "should be able to include associations when saving if they are specified" do
340
+ tr = TestResource.build(:name => "Ethan", :age => 20)
341
+ hash = JSON.parse(tr.save)
342
+ hash['test_resource']['has_many_objects'].should be_nil
343
+ hash = JSON.parse(tr.save(:include_associations => [:has_many_objects]))
344
+ hash['test_resource']['has_many_objects'].should eql([])
345
+ end
346
+
347
+ it "should not include nil attributes when creating by default" do
348
+ tr = TestResource.build(:name => "Ethan")
349
+ hash = JSON.parse(tr.save)
350
+ hash['test_resource']['age'].should be_nil
351
+ hash['test_resource']['name'].should eql("Ethan")
352
+ end
353
+
354
+ it "should include nil attributes if they are passed in through the include_extras" do
355
+ tr = TestResource.build(:name => "Ethan")
356
+ hash = JSON.parse(tr.save(:include_extras => [:age]))
357
+ hash['test_resource'].key?('age').should be_true
358
+ end
359
+
360
+ it "should include nil attributes when creating if include_nil_attributes_on_create is true" do
361
+ TestResource.include_blank_attributes_on_create = true
362
+ tr = TestResource.build(:name => "Ethan")
363
+ hash = JSON.parse(tr.save)
364
+ hash['test_resource'].key?('age').should be_true
365
+ TestResource.include_blank_attributes_on_create = false
366
+ end
367
+ end
368
+ end
369
+
370
+ context "Updating old records" do
371
+ before(:all) do
372
+ TestResource.reload_class_attributes
373
+ TestResource.has_many :has_many_objects
374
+
375
+ TestResource.send(:alias_method, :old_update, :update)
376
+ TestResource.send(:alias_method, :old_save, :save)
377
+
378
+ TestResource.send(:define_method, :update) do |*args|
379
+ opts = args.extract_options!
380
+ # When we create we should not include any blank attributes
381
+ except = self.class.attribute_names - self.changed.symbolize_array
382
+ changed_associations = self.changed.symbolize_array.select{|item| self.association?(item)}
383
+ opts[:except] = opts[:except] ? opts[:except].concat(except).uniq.symbolize_array : except.symbolize_array
384
+ opts[:include_associations] = opts[:include_associations] ? opts[:include_associations].concat(args).concat(changed_associations).uniq : changed_associations.concat(args)
385
+ opts[:include_extras] ||= []
386
+ opts[:except] = [:id] if self.class.include_all_attributes_on_update
387
+ encode(opts)
388
+ end
389
+ TestResource.send(:define_method, :save) do |*args|
390
+ new? ? create(*args) : update(*args)
391
+ end
392
+ end
393
+
394
+ after(:all) do
395
+ TestResource.send(:alias_method, :update, :old_update)
396
+ TestResource.send(:alias_method, :save, :old_save)
397
+ end
398
+
399
+ it "should be able to put updated data via the update method" do
400
+ tr = TestResource.new(:id => 1, :name => "Ethan")
401
+ tr.should_not be_new
402
+ # Thus we know we are calling update
403
+ tr.age = 6
404
+ hash = JSON.parse(tr.update)
405
+ hash['test_resource']['age'].should eql(6)
406
+
407
+ hash = JSON.parse(tr.save)
408
+ hash['test_resource']['age'].should eql(6)
409
+ end
410
+
411
+ it "should only include changed attributes when updating" do
412
+ tr = TestResource.new(:id => 1, :name => "Ethan")
413
+ tr.should_not be_new
414
+ # Thus we know we are calling update
415
+ tr.age = 6
416
+ hash = JSON.parse(tr.save)
417
+ hash['test_resource']['name'].should be_nil
418
+ end
419
+
420
+ it "should include changed associations without specification" do
421
+ tr = TestResource.new(:id => 1, :name => "Ethan")
422
+ tr.has_many_objects = [HasManyObject.new]
423
+ hash = JSON.parse(tr.save)
424
+ hash['test_resource']['has_many_objects'].should_not be_blank
425
+ end
426
+
427
+ it "should include unchanged associations if they are specified" do
428
+ tr = TestResource.new(:id => 1, :name => "Ethan")
429
+ hash = JSON.parse(tr.save(:has_many_objects))
430
+ hash['test_resource']['has_many_objects'].should eql([])
431
+ end
432
+
433
+ it "should include all attributes if include_all_attributes_on_update is true" do
434
+ TestResource.include_all_attributes_on_update = true
435
+ tr = TestResource.new(:id => 1, :name => "Ethan")
436
+ hash = JSON.parse(tr.save)
437
+ hash['test_resource']['name'].should eql("Ethan")
438
+ hash['test_resource'].key?('age').should be_true
439
+ TestResource.include_all_attributes_on_update = false
440
+ end
441
+
442
+ it "should provide an update_attributes method to set attrs and save" do
443
+
444
+ tr = TestResource.new(:id => 1, :name => "Ethan")
445
+ hash = JSON.parse(tr.update_attributes(:name => "Dan"))
446
+ hash['test_resource']['name'].should eql "Dan"
447
+
448
+ end
449
+
450
+
451
+ end
452
+
453
+ end
454
+
455
+ describe "Deleting data" do
456
+ it "should be able to delete an id from the class method" do
457
+ TestResource.delete(1).should be_true
458
+ end
459
+
460
+ it "should be able to destroy itself as an instance" do
461
+ tr = TestResource.new(:id => 1, :name => "Ethan")
462
+ tr.destroy.should be_true
463
+ end
464
+ end
465
+
466
+ describe "Random methods" do
467
+
468
+ it "should know if it is persisted" do
469
+ tr = TestResource.new(:id => 1, :name => "Ethan")
470
+ tr.persisted?.should be_true
471
+ tr = TestResource.new(:name => "Ethan")
472
+ tr.persisted?.should be_false
473
+ end
474
+
475
+ end
476
+
477
+ describe "Inheritable Accessors" do
478
+
479
+ it "should copy the default values down to any level of subclass" do
480
+
481
+ class Child < TestResource
482
+ end
483
+
484
+ Child.site.should eql(TestResource.site)
485
+ Child.site.should_not be_blank
486
+ end
487
+
488
+ end
489
+
490
+ describe "Inflections" do
491
+
492
+ it "should be able to singularize and pluralize words ending in ess" do
493
+ "address".singularize.should eql("address")
494
+ "address".pluralize.should eql("addresses")
495
+ end
496
+
497
+ end
498
+
499
+ end