acts_as_api 0.2.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +10 -0
- data/Gemfile +12 -0
- data/History.txt +8 -0
- data/README.rdoc +29 -225
- data/Rakefile +20 -28
- data/acts_as_api.gemspec +31 -0
- data/examples/introduction/docco.css +186 -0
- data/examples/introduction/index.html +340 -0
- data/examples/introduction/index.rb +132 -0
- data/examples/introduction/layout.mustache +64 -0
- data/lib/acts_as_api.rb +11 -25
- data/lib/acts_as_api/api_template.rb +14 -0
- data/lib/acts_as_api/base.rb +61 -56
- data/lib/acts_as_api/config.rb +34 -0
- data/lib/acts_as_api/rails_renderer.rb +15 -0
- data/lib/acts_as_api/rendering.rb +11 -8
- data/lib/acts_as_api/version.rb +4 -0
- data/spec/controllers/respond_with_users_controller_spec.rb +5 -0
- data/spec/controllers/users_controller_spec.rb +161 -0
- data/spec/models/base_spec.rb +437 -0
- data/spec/rails_app/.gitignore +4 -0
- data/spec/rails_app/Rakefile +7 -0
- data/spec/rails_app/app/controllers/application_controller.rb +3 -0
- data/spec/rails_app/app/controllers/respond_with_users_controller.rb +15 -0
- data/spec/rails_app/app/controllers/users_controller.rb +21 -0
- data/spec/rails_app/app/helpers/application_helper.rb +2 -0
- data/spec/rails_app/app/models/task.rb +3 -0
- data/spec/rails_app/app/models/untouched.rb +2 -0
- data/spec/rails_app/app/models/user.rb +69 -0
- data/spec/rails_app/app/views/layouts/application.html.erb +14 -0
- data/spec/rails_app/config.ru +4 -0
- data/spec/rails_app/config/application.rb +42 -0
- data/spec/rails_app/config/boot.rb +6 -0
- data/spec/rails_app/config/database.yml +23 -0
- data/spec/rails_app/config/environment.rb +5 -0
- data/spec/rails_app/config/environments/development.rb +26 -0
- data/spec/rails_app/config/environments/production.rb +49 -0
- data/spec/rails_app/config/environments/test.rb +35 -0
- data/spec/rails_app/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/rails_app/config/initializers/inflections.rb +10 -0
- data/spec/rails_app/config/initializers/mime_types.rb +5 -0
- data/spec/rails_app/config/initializers/secret_token.rb +7 -0
- data/spec/rails_app/config/initializers/session_store.rb +8 -0
- data/spec/rails_app/config/locales/en.yml +5 -0
- data/spec/rails_app/config/routes.rb +7 -0
- data/spec/rails_app/db/migrate/20110214201640_create_tables.rb +35 -0
- data/spec/rails_app/db/schema.rb +34 -0
- data/spec/rails_app/db/seeds.rb +7 -0
- data/spec/rails_app/lib/tasks/.gitkeep +0 -0
- data/spec/rails_app/public/404.html +26 -0
- data/spec/rails_app/public/422.html +26 -0
- data/spec/rails_app/public/500.html +26 -0
- data/spec/rails_app/public/favicon.ico +0 -0
- data/spec/rails_app/public/images/rails.png +0 -0
- data/spec/rails_app/public/index.html +239 -0
- data/spec/rails_app/public/javascripts/application.js +2 -0
- data/spec/rails_app/public/javascripts/controls.js +965 -0
- data/spec/rails_app/public/javascripts/dragdrop.js +974 -0
- data/spec/rails_app/public/javascripts/effects.js +1123 -0
- data/spec/rails_app/public/javascripts/prototype.js +6001 -0
- data/spec/rails_app/public/javascripts/rails.js +191 -0
- data/spec/rails_app/public/robots.txt +5 -0
- data/spec/rails_app/public/stylesheets/.gitkeep +0 -0
- data/spec/rails_app/script/rails +6 -0
- data/spec/spec_helper.rb +12 -13
- data/spec/support/api_test_helpers.rb +23 -0
- metadata +137 -35
- data/Manifest.txt +0 -15
- data/script/console +0 -10
- data/script/destroy +0 -14
- data/script/generate +0 -14
- data/spec/acts_as_api_spec.rb +0 -87
- data/tasks/rspec.rake +0 -21
@@ -0,0 +1,437 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper.rb'
|
2
|
+
|
3
|
+
describe "acts_as_api" do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@luke = User.create({ :first_name => 'Luke', :last_name => 'Skywalker', :age => 25, :active => true })
|
7
|
+
@han = User.create({ :first_name => 'Han', :last_name => 'Solo', :age => 35, :active => true })
|
8
|
+
@leia = User.create({ :first_name => 'Princess', :last_name => 'Leia', :age => 25, :active => false })
|
9
|
+
|
10
|
+
@destroy_deathstar = @luke.tasks.create({ :heading => "Destroy Deathstar", :description => "XWing, Shoot, BlowUp", :time_spent => 30, :done => true })
|
11
|
+
@study_with_yoda = @luke.tasks.create({ :heading => "Study with Yoda", :description => "Jedi Stuff, ya know", :time_spent => 60, :done => true })
|
12
|
+
@win_rebellion = @luke.tasks.create({ :heading => "Win Rebellion", :description => "no idea yet...", :time_spent => 180, :done => false })
|
13
|
+
end
|
14
|
+
|
15
|
+
after(:each) do
|
16
|
+
User.delete_all
|
17
|
+
Task.delete_all
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "is disabled by default" do
|
21
|
+
it "should indicate that acts_as_api is disabled" do
|
22
|
+
Untouched.acts_as_api?.should be_false
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should not respond to api_accessible" do
|
26
|
+
Untouched.should_not respond_to :api_accessible
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "is enabled" do
|
31
|
+
|
32
|
+
before(:each) do
|
33
|
+
User.acts_as_api
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should indicate that acts_as_api is enabled" do
|
37
|
+
User.acts_as_api?.should be_true
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should not respond to api_accessible" do
|
41
|
+
User.should respond_to :api_accessible
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "listing attributes in the api template" do
|
45
|
+
|
46
|
+
before(:each) do
|
47
|
+
@response = @luke.as_api_response(:name_only)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should return a hash" do
|
51
|
+
@response.should be_kind_of(Hash)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should return the correct number of keys" do
|
55
|
+
@response.should have(2).keys
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should return all specified fields" do
|
59
|
+
@response.keys.should include(:first_name, :last_name)
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should return the correct values for the specified fields" do
|
63
|
+
@response.values.should include(@luke.first_name, @luke.last_name)
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
describe "calling a method in the api template" do
|
69
|
+
|
70
|
+
before(:each) do
|
71
|
+
@response = @luke.as_api_response(:only_full_name)
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should return a hash" do
|
75
|
+
@response.should be_kind_of(Hash)
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should return the correct number of keys" do
|
79
|
+
@response.should have(1).key
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should return all specified fields" do
|
83
|
+
@response.keys.should include(:full_name)
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should return the correct values for the specified fields" do
|
87
|
+
@response.values.should include(@luke.full_name)
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
describe "renaming an attribute in the api template" do
|
93
|
+
|
94
|
+
before(:each) do
|
95
|
+
@response = @luke.as_api_response(:rename_last_name)
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should return a hash" do
|
99
|
+
@response.should be_kind_of(Hash)
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should return the correct number of keys" do
|
103
|
+
@response.should have(1).key
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should return all specified fields" do
|
107
|
+
@response.keys.should include(:family_name)
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should return the correct values for the specified fields" do
|
111
|
+
@response.values.should include(@luke.last_name)
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
115
|
+
|
116
|
+
describe "renaming the node/key of a method in the api template" do
|
117
|
+
|
118
|
+
before(:each) do
|
119
|
+
@response = @luke.as_api_response(:rename_full_name)
|
120
|
+
end
|
121
|
+
|
122
|
+
it "should return a hash" do
|
123
|
+
@response.should be_kind_of(Hash)
|
124
|
+
end
|
125
|
+
|
126
|
+
it "should return the correct number of keys" do
|
127
|
+
@response.should have(1).key
|
128
|
+
end
|
129
|
+
|
130
|
+
it "should return all specified fields" do
|
131
|
+
@response.keys.should include(:other_full_name)
|
132
|
+
end
|
133
|
+
|
134
|
+
it "should return the correct values for the specified fields" do
|
135
|
+
@response.values.should include(@luke.full_name)
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|
139
|
+
|
140
|
+
describe "extending a given api template (multiple times)" do
|
141
|
+
|
142
|
+
before(:each) do
|
143
|
+
User.api_accessible :public do |t|
|
144
|
+
t.add :first_name
|
145
|
+
end
|
146
|
+
|
147
|
+
User.api_accessible :for_buddies, :extend => :public do |t|
|
148
|
+
t.add :age
|
149
|
+
end
|
150
|
+
|
151
|
+
User.api_accessible :private, :extend => :for_buddies do |t|
|
152
|
+
t.add :last_name
|
153
|
+
end
|
154
|
+
@response = @luke.as_api_response(:private)
|
155
|
+
end
|
156
|
+
|
157
|
+
it "should return a hash" do
|
158
|
+
@response.should be_kind_of(Hash)
|
159
|
+
end
|
160
|
+
|
161
|
+
it "should return the correct number of keys" do
|
162
|
+
@response.should have(3).keys
|
163
|
+
end
|
164
|
+
|
165
|
+
it "should return all specified fields" do
|
166
|
+
@response.keys.sort_by(&:to_s).should eql([:age, :first_name, :last_name])
|
167
|
+
end
|
168
|
+
|
169
|
+
it "should return the correct values for the specified fields" do
|
170
|
+
@response.values.sort_by(&:to_s).should eql([@luke.age, @luke.first_name, @luke.last_name].sort_by(&:to_s))
|
171
|
+
end
|
172
|
+
|
173
|
+
end
|
174
|
+
|
175
|
+
describe "extending a given api template and removing a former added value" do
|
176
|
+
|
177
|
+
before(:each) do
|
178
|
+
@response = @luke.as_api_response(:age_and_first_name)
|
179
|
+
end
|
180
|
+
|
181
|
+
it "should return a hash" do
|
182
|
+
@response.should be_kind_of(Hash)
|
183
|
+
end
|
184
|
+
|
185
|
+
it "should return the correct number of keys" do
|
186
|
+
@response.should have(2).keys
|
187
|
+
end
|
188
|
+
|
189
|
+
it "should return all specified fields" do
|
190
|
+
@response.keys.sort_by(&:to_s).should eql([:first_name, :age].sort_by(&:to_s))
|
191
|
+
end
|
192
|
+
|
193
|
+
it "should return the correct values for the specified fields" do
|
194
|
+
@response.values.sort_by(&:to_s).should eql([@luke.first_name, @luke.age].sort_by(&:to_s))
|
195
|
+
end
|
196
|
+
|
197
|
+
end
|
198
|
+
|
199
|
+
describe "calling a proc in the api (the record is passed as only parameter)" do
|
200
|
+
|
201
|
+
before(:each) do
|
202
|
+
@response = @luke.as_api_response(:calling_a_proc)
|
203
|
+
end
|
204
|
+
|
205
|
+
it "should return a hash" do
|
206
|
+
@response.should be_kind_of(Hash)
|
207
|
+
end
|
208
|
+
|
209
|
+
it "should return the correct number of keys" do
|
210
|
+
@response.should have(2).keys
|
211
|
+
end
|
212
|
+
|
213
|
+
it "should return all specified fields" do
|
214
|
+
@response.keys.sort_by(&:to_s).should eql([:all_caps_name, :without_param])
|
215
|
+
end
|
216
|
+
|
217
|
+
it "should return the correct values for the specified fields" do
|
218
|
+
@response.values.sort.should eql(["LUKE SKYWALKER", "Time"])
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
describe "calling a lambda in the api (the record is passed as only parameter)" do
|
223
|
+
|
224
|
+
before(:each) do
|
225
|
+
@response = @luke.as_api_response(:calling_a_lambda)
|
226
|
+
end
|
227
|
+
|
228
|
+
it "should return a hash" do
|
229
|
+
@response.should be_kind_of(Hash)
|
230
|
+
end
|
231
|
+
|
232
|
+
it "should return the correct number of keys" do
|
233
|
+
@response.should have(2).keys
|
234
|
+
end
|
235
|
+
|
236
|
+
it "should return all specified fields" do
|
237
|
+
@response.keys.sort_by(&:to_s).should eql([:all_caps_name, :without_param])
|
238
|
+
end
|
239
|
+
|
240
|
+
it "should return the correct values for the specified fields" do
|
241
|
+
@response.values.sort.should eql(["LUKE SKYWALKER", "Time"])
|
242
|
+
end
|
243
|
+
|
244
|
+
end
|
245
|
+
|
246
|
+
describe "including an association (which doesn't acts_as_api) in the api template" do
|
247
|
+
|
248
|
+
before(:each) do
|
249
|
+
@response = @luke.as_api_response(:include_tasks)
|
250
|
+
end
|
251
|
+
|
252
|
+
it "should return a hash" do
|
253
|
+
@response.should be_kind_of(Hash)
|
254
|
+
end
|
255
|
+
|
256
|
+
it "should return the correct number of keys" do
|
257
|
+
@response.should have(1).key
|
258
|
+
end
|
259
|
+
|
260
|
+
it "should return all specified fields" do
|
261
|
+
@response.keys.should include(:tasks)
|
262
|
+
end
|
263
|
+
|
264
|
+
it "should return the correct values for the specified fields" do
|
265
|
+
@response[:tasks].should be_an Array
|
266
|
+
@response[:tasks].should have(3).tasks
|
267
|
+
end
|
268
|
+
|
269
|
+
it "should contain the associated sub models" do
|
270
|
+
@response[:tasks].should include(@destroy_deathstar, @study_with_yoda, @win_rebellion)
|
271
|
+
end
|
272
|
+
|
273
|
+
end
|
274
|
+
|
275
|
+
describe "including an association (which does acts_as_api) in the api template" do
|
276
|
+
|
277
|
+
before(:each) do
|
278
|
+
Task.acts_as_api
|
279
|
+
Task.api_accessible :include_tasks do |t|
|
280
|
+
t.add :heading
|
281
|
+
t.add :done
|
282
|
+
end
|
283
|
+
@response = @luke.as_api_response(:include_tasks)
|
284
|
+
end
|
285
|
+
|
286
|
+
it "should return a hash" do
|
287
|
+
@response.should be_kind_of(Hash)
|
288
|
+
end
|
289
|
+
|
290
|
+
it "should return the correct number of keys" do
|
291
|
+
@response.should have(1).key
|
292
|
+
end
|
293
|
+
|
294
|
+
it "should return all specified fields" do
|
295
|
+
@response.keys.should include(:tasks)
|
296
|
+
end
|
297
|
+
|
298
|
+
it "should return the correct values for the specified fields" do
|
299
|
+
@response[:tasks].should be_an Array
|
300
|
+
@response[:tasks].should have(3).tasks
|
301
|
+
end
|
302
|
+
|
303
|
+
it "should contain the associated child models with the determined api template" do
|
304
|
+
@response[:tasks].each do |task|
|
305
|
+
task.keys.should include(:heading, :done)
|
306
|
+
task.keys.should have(2).attributes
|
307
|
+
end
|
308
|
+
end
|
309
|
+
|
310
|
+
it "should contain the correct data of the child models" do
|
311
|
+
task_hash = [ @destroy_deathstar, @study_with_yoda, @win_rebellion ].collect{|t| { :done => t.done, :heading => t.heading } }
|
312
|
+
@response[:tasks].should eql task_hash
|
313
|
+
end
|
314
|
+
|
315
|
+
end
|
316
|
+
|
317
|
+
describe "including a scoped association in the api template" do
|
318
|
+
|
319
|
+
before(:each) do
|
320
|
+
# extend task model with scope
|
321
|
+
class Task < ActiveRecord::Base
|
322
|
+
scope :completed, where(:done => true)
|
323
|
+
end
|
324
|
+
Task.acts_as_api
|
325
|
+
Task.api_accessible :include_completed_tasks do |t|
|
326
|
+
t.add :heading
|
327
|
+
t.add :done
|
328
|
+
end
|
329
|
+
|
330
|
+
@response = @luke.as_api_response(:include_completed_tasks)
|
331
|
+
end
|
332
|
+
|
333
|
+
it "should return a hash" do
|
334
|
+
@response.should be_kind_of(Hash)
|
335
|
+
end
|
336
|
+
|
337
|
+
it "should return the correct number of keys" do
|
338
|
+
@response.should have(1).key
|
339
|
+
end
|
340
|
+
|
341
|
+
it "should return all specified fields" do
|
342
|
+
@response.keys.should include(:completed_tasks)
|
343
|
+
end
|
344
|
+
|
345
|
+
it "should return the correct values for the specified fields" do
|
346
|
+
@response[:completed_tasks].should be_an Array
|
347
|
+
@response[:completed_tasks].should have(2).tasks
|
348
|
+
end
|
349
|
+
|
350
|
+
it "should contain the associated child models with the determined api template" do
|
351
|
+
@response[:completed_tasks].each do |task|
|
352
|
+
task.keys.should include(:heading, :done)
|
353
|
+
task.keys.should have(2).attributes
|
354
|
+
end
|
355
|
+
end
|
356
|
+
|
357
|
+
it "should contain the correct data of the child models" do
|
358
|
+
task_hash = [ @destroy_deathstar, @study_with_yoda ].collect{|t| { :done => t.done, :heading => t.heading } }
|
359
|
+
@response[:completed_tasks].should eql task_hash
|
360
|
+
end
|
361
|
+
|
362
|
+
end
|
363
|
+
|
364
|
+
describe "creating a sub node in the api template and putting an attribute in it" do
|
365
|
+
|
366
|
+
before(:each) do
|
367
|
+
@response = @luke.as_api_response(:sub_node)
|
368
|
+
end
|
369
|
+
|
370
|
+
it "should return a hash" do
|
371
|
+
@response.should be_kind_of(Hash)
|
372
|
+
end
|
373
|
+
|
374
|
+
it "should return the correct number of keys" do
|
375
|
+
@response.should have(1).key
|
376
|
+
end
|
377
|
+
|
378
|
+
it "should return all specified fields" do
|
379
|
+
@response.keys.should include(:sub_nodes)
|
380
|
+
end
|
381
|
+
|
382
|
+
it "should return the correct values for the specified fields" do
|
383
|
+
@response[:sub_nodes].should be_a Hash
|
384
|
+
end
|
385
|
+
|
386
|
+
it "should provide the correct number of sub nodes" do
|
387
|
+
@response[:sub_nodes].should have(1).keys
|
388
|
+
end
|
389
|
+
|
390
|
+
it "should provide the correct sub nodes values" do
|
391
|
+
@response[:sub_nodes][:foo].should eql("something")
|
392
|
+
end
|
393
|
+
end
|
394
|
+
|
395
|
+
describe "creating multiple sub nodes in the api template and putting an attribute in it" do
|
396
|
+
|
397
|
+
before(:each) do
|
398
|
+
@response = @luke.as_api_response(:nested_sub_node)
|
399
|
+
end
|
400
|
+
|
401
|
+
it "should return a hash" do
|
402
|
+
@response.should be_kind_of(Hash)
|
403
|
+
end
|
404
|
+
|
405
|
+
it "should return the correct number of keys" do
|
406
|
+
@response.should have(1).key
|
407
|
+
end
|
408
|
+
|
409
|
+
it "should return all specified fields" do
|
410
|
+
@response.keys.should include(:sub_nodes)
|
411
|
+
end
|
412
|
+
|
413
|
+
it "should return the correct values for the specified fields" do
|
414
|
+
@response[:sub_nodes].should be_a Hash
|
415
|
+
end
|
416
|
+
|
417
|
+
it "should provide the correct number of sub nodes" do
|
418
|
+
@response[:sub_nodes].should have(1).keys
|
419
|
+
end
|
420
|
+
|
421
|
+
it "should provide the correct number of sub nodes in the second level" do
|
422
|
+
@response[:sub_nodes][:foo].should have(1).keys
|
423
|
+
end
|
424
|
+
|
425
|
+
it "should provide the correct sub nodes values" do
|
426
|
+
@response[:sub_nodes][:foo].tap do |foo|
|
427
|
+
foo[:bar].tap do |bar|
|
428
|
+
bar.should eql(@luke.last_name)
|
429
|
+
end
|
430
|
+
end
|
431
|
+
end
|
432
|
+
|
433
|
+
end
|
434
|
+
|
435
|
+
end
|
436
|
+
|
437
|
+
end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
# Add your own tasks in files placed in lib/tasks ending in .rake,
|
2
|
+
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
|
3
|
+
|
4
|
+
require File.expand_path('../config/application', __FILE__)
|
5
|
+
require 'rake'
|
6
|
+
|
7
|
+
RailsApp::Application.load_tasks
|