acts_as_api 0.3.5 → 0.3.6
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.
- data/Gemfile +1 -1
- data/History.txt +5 -0
- data/README.rdoc +9 -2
- data/Rakefile +15 -1
- data/acts_as_api.gemspec +1 -0
- data/examples/introduction/index.html +7 -6
- data/examples/introduction/index.rb +9 -8
- data/lib/acts_as_api.rb +12 -5
- data/lib/acts_as_api/adapters.rb +5 -0
- data/lib/acts_as_api/adapters/mongoid.rb +11 -0
- data/lib/acts_as_api/array.rb +4 -8
- data/lib/acts_as_api/responder.rb +40 -0
- data/lib/acts_as_api/version.rb +1 -1
- data/spec/controllers/respond_with_users_controller_spec.rb +30 -2
- data/spec/controllers/users_controller_spec.rb +17 -235
- data/spec/models/active_record_spec.rb +28 -0
- data/spec/models/mongoid_spec.rb +28 -0
- data/spec/rails_app/app/controllers/respond_with_users_controller.rb +19 -8
- data/spec/rails_app/app/controllers/users_controller.rb +13 -4
- data/spec/rails_app/app/models/mongo_profile.rb +10 -0
- data/spec/rails_app/app/models/mongo_task.rb +12 -0
- data/spec/rails_app/app/models/mongo_untouched.rb +7 -0
- data/spec/rails_app/app/models/mongo_user.rb +149 -0
- data/spec/rails_app/app/models/user.rb +6 -6
- data/spec/rails_app/config/initializers/acts_as_api_mongoid.rb +6 -0
- data/spec/rails_app/config/mongoid.yml +23 -0
- data/spec/support/controller_examples.rb +246 -0
- data/spec/support/it_supports.rb +3 -0
- data/spec/support/model_examples/associations.rb +272 -0
- data/spec/support/model_examples/closures.rb +49 -0
- data/spec/support/model_examples/conditional_if.rb +165 -0
- data/spec/support/model_examples/conditional_unless.rb +165 -0
- data/spec/support/model_examples/enabled.rb +10 -0
- data/spec/support/model_examples/extending.rb +112 -0
- data/spec/support/model_examples/methods.rb +23 -0
- data/spec/support/model_examples/renaming.rb +50 -0
- data/spec/support/model_examples/simple.rb +23 -0
- data/spec/support/model_examples/sub_nodes.rb +105 -0
- data/spec/support/model_examples/undefined.rb +7 -0
- data/spec/support/model_examples/untouched.rb +13 -0
- data/spec/support/simple_fixtures.rb +39 -8
- metadata +67 -28
- data/spec/models/base/associations_spec.rb +0 -284
- data/spec/models/base/closures_spec.rb +0 -62
- data/spec/models/base/conditional_if_spec.rb +0 -178
- data/spec/models/base/conditional_unless_spec.rb +0 -178
- data/spec/models/base/enabled_spec.rb +0 -15
- data/spec/models/base/extending_spec.rb +0 -125
- data/spec/models/base/methods_spec.rb +0 -33
- data/spec/models/base/renaming_spec.rb +0 -63
- data/spec/models/base/simple_spec.rb +0 -33
- data/spec/models/base/sub_nodes_spec.rb +0 -118
- data/spec/models/base/undefined_spec.rb +0 -20
- data/spec/models/base/untouched_spec.rb +0 -18
@@ -0,0 +1,272 @@
|
|
1
|
+
shared_examples_for "including an association in the api template" do
|
2
|
+
|
3
|
+
describe "which doesn't acts_as_api" do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@response = @luke.as_api_response(:include_tasks)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "returns a hash" do
|
10
|
+
@response.should be_kind_of(Hash)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "returns the correct number of fields" do
|
14
|
+
@response.should have(1).keys
|
15
|
+
end
|
16
|
+
|
17
|
+
it "returns all specified fields" do
|
18
|
+
@response.keys.should include(:tasks)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "returns the correct values for the specified fields" do
|
22
|
+
@response[:tasks].should be_an Array
|
23
|
+
@response[:tasks].should have(3).tasks
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should contain the associated sub models" do
|
27
|
+
@response[:tasks].should include(@destroy_deathstar, @study_with_yoda, @win_rebellion)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "which does acts_as_api" do
|
32
|
+
|
33
|
+
context "has_many" do
|
34
|
+
|
35
|
+
before(:each) do
|
36
|
+
@task_model.acts_as_api
|
37
|
+
@task_model.api_accessible :include_tasks do |t|
|
38
|
+
t.add :heading
|
39
|
+
t.add :done
|
40
|
+
end
|
41
|
+
@response = @luke.as_api_response(:include_tasks)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "returns a hash" do
|
45
|
+
@response.should be_kind_of(Hash)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "returns the correct number of fields" do
|
49
|
+
@response.should have(1).keys
|
50
|
+
end
|
51
|
+
|
52
|
+
it "returns all specified fields" do
|
53
|
+
@response.keys.should include(:tasks)
|
54
|
+
end
|
55
|
+
|
56
|
+
it "returns the correct values for the specified fields" do
|
57
|
+
@response[:tasks].should be_an Array
|
58
|
+
@response[:tasks].should have(3).tasks
|
59
|
+
end
|
60
|
+
|
61
|
+
it "contains the associated child models with the determined api template" do
|
62
|
+
@response[:tasks].each do |task|
|
63
|
+
task.keys.should include(:heading, :done)
|
64
|
+
task.keys.should have(2).attributes
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
it "contains the correct data of the child models" do
|
69
|
+
task_hash = [ @destroy_deathstar, @study_with_yoda, @win_rebellion ].collect{|t| { :done => t.done, :heading => t.heading } }
|
70
|
+
@response[:tasks].should eql task_hash
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
context "has_one" do
|
75
|
+
|
76
|
+
before(:each) do
|
77
|
+
@profile_model.acts_as_api
|
78
|
+
@profile_model.api_accessible :include_profile do |t|
|
79
|
+
t.add :avatar
|
80
|
+
t.add :homepage
|
81
|
+
end
|
82
|
+
@response = @luke.as_api_response(:include_profile)
|
83
|
+
end
|
84
|
+
|
85
|
+
it "returns a hash" do
|
86
|
+
@response.should be_kind_of(Hash)
|
87
|
+
end
|
88
|
+
|
89
|
+
it "returns the correct number of fields" do
|
90
|
+
@response.should have(1).keys
|
91
|
+
end
|
92
|
+
|
93
|
+
it "returns all specified fields" do
|
94
|
+
@response.keys.should include(:profile)
|
95
|
+
end
|
96
|
+
|
97
|
+
it "returns the correct values for the specified fields" do
|
98
|
+
@response[:profile].should be_a Hash
|
99
|
+
@response[:profile].should have(2).attributes
|
100
|
+
end
|
101
|
+
|
102
|
+
it "contains the associated child models with the determined api template" do
|
103
|
+
@response[:profile].keys.should include(:avatar, :homepage)
|
104
|
+
end
|
105
|
+
|
106
|
+
it "contains the correct data of the child models" do
|
107
|
+
profile_hash = { :avatar => @luke.profile.avatar, :homepage => @luke.profile.homepage }
|
108
|
+
@response[:profile].should eql profile_hash
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
describe "which does acts_as_api, but with using another template name" do
|
115
|
+
|
116
|
+
before(:each) do
|
117
|
+
@task_model.acts_as_api
|
118
|
+
@task_model.api_accessible :other_template do |t|
|
119
|
+
t.add :description
|
120
|
+
t.add :time_spent
|
121
|
+
end
|
122
|
+
@response = @luke.as_api_response(:other_sub_template)
|
123
|
+
end
|
124
|
+
|
125
|
+
it "returns a hash" do
|
126
|
+
@response.should be_kind_of(Hash)
|
127
|
+
end
|
128
|
+
|
129
|
+
it "returns the correct number of fields" do
|
130
|
+
@response.should have(2).keys
|
131
|
+
end
|
132
|
+
|
133
|
+
it "returns all specified fields" do
|
134
|
+
@response.keys.should include(:first_name)
|
135
|
+
end
|
136
|
+
|
137
|
+
it "returns the correct values for the specified fields" do
|
138
|
+
@response.values.should include(@luke.first_name)
|
139
|
+
end
|
140
|
+
|
141
|
+
it "returns all specified fields" do
|
142
|
+
@response.keys.should include(:tasks)
|
143
|
+
end
|
144
|
+
|
145
|
+
it "returns the correct values for the specified fields" do
|
146
|
+
@response[:tasks].should be_an Array
|
147
|
+
@response[:tasks].should have(3).tasks
|
148
|
+
end
|
149
|
+
|
150
|
+
it "contains the associated child models with the determined api template" do
|
151
|
+
@response[:tasks].each do |task|
|
152
|
+
task.keys.should include(:description, :time_spent)
|
153
|
+
task.keys.should have(2).attributes
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
it "contains the correct data of the child models" do
|
158
|
+
task_hash = [ @destroy_deathstar, @study_with_yoda, @win_rebellion ].collect{|t| { :description => t.description, :time_spent => t.time_spent } }
|
159
|
+
@response[:tasks].should eql task_hash
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
describe "that is scoped" do
|
164
|
+
|
165
|
+
before(:each) do
|
166
|
+
# extend task model with scope
|
167
|
+
#class Task < ActiveRecord::Base
|
168
|
+
@task_model.class_eval do
|
169
|
+
scope :completed, where(:done => true)
|
170
|
+
end
|
171
|
+
@task_model.acts_as_api
|
172
|
+
@task_model.api_accessible :include_completed_tasks do |t|
|
173
|
+
t.add :heading
|
174
|
+
t.add :done
|
175
|
+
end
|
176
|
+
|
177
|
+
@response = @luke.as_api_response(:include_completed_tasks)
|
178
|
+
end
|
179
|
+
|
180
|
+
it "returns a hash" do
|
181
|
+
@response.should be_kind_of(Hash)
|
182
|
+
end
|
183
|
+
|
184
|
+
it "returns the correct number of fields" do
|
185
|
+
@response.should have(1).keys
|
186
|
+
end
|
187
|
+
|
188
|
+
it "returns all specified fields" do
|
189
|
+
@response.keys.should include(:completed_tasks)
|
190
|
+
end
|
191
|
+
|
192
|
+
it "returns the correct values for the specified fields" do
|
193
|
+
@response[:completed_tasks].should be_an Array
|
194
|
+
@response[:completed_tasks].should have(2).tasks
|
195
|
+
end
|
196
|
+
|
197
|
+
it "contains the associated child models with the determined api template" do
|
198
|
+
@response[:completed_tasks].each do |task|
|
199
|
+
task.keys.should include(:heading, :done)
|
200
|
+
task.keys.should have(2).attributes
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
it "contains the correct data of the child models" do
|
205
|
+
task_hash = [ @destroy_deathstar, @study_with_yoda ].collect{|t| { :done => t.done, :heading => t.heading } }
|
206
|
+
@response[:completed_tasks].should eql task_hash
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
describe "handling nil values" do
|
211
|
+
|
212
|
+
context "has_many" do
|
213
|
+
|
214
|
+
before(:each) do
|
215
|
+
@task_model.acts_as_api
|
216
|
+
@task_model.api_accessible :include_tasks do |t|
|
217
|
+
t.add :heading
|
218
|
+
t.add :done
|
219
|
+
end
|
220
|
+
@response = @han.as_api_response(:include_tasks)
|
221
|
+
end
|
222
|
+
|
223
|
+
it "returns a hash" do
|
224
|
+
@response.should be_kind_of(Hash)
|
225
|
+
end
|
226
|
+
|
227
|
+
it "returns the correct number of fields" do
|
228
|
+
@response.should have(1).keys
|
229
|
+
end
|
230
|
+
|
231
|
+
it "returns all specified fields" do
|
232
|
+
@response.keys.should include(:tasks)
|
233
|
+
end
|
234
|
+
|
235
|
+
it "returns the correct values for the specified fields" do
|
236
|
+
@response[:tasks].should be_kind_of(Array)
|
237
|
+
end
|
238
|
+
|
239
|
+
it "contains no associated child models" do
|
240
|
+
@response[:tasks].should have(0).items
|
241
|
+
end
|
242
|
+
|
243
|
+
end
|
244
|
+
|
245
|
+
context "has one" do
|
246
|
+
before(:each) do
|
247
|
+
@profile_model.acts_as_api
|
248
|
+
@profile_model.api_accessible :include_profile do |t|
|
249
|
+
t.add :avatar
|
250
|
+
t.add :homepage
|
251
|
+
end
|
252
|
+
@response = @han.as_api_response(:include_profile)
|
253
|
+
end
|
254
|
+
|
255
|
+
it "returns a hash" do
|
256
|
+
@response.should be_kind_of(Hash)
|
257
|
+
end
|
258
|
+
|
259
|
+
it "returns the correct number of fields" do
|
260
|
+
@response.should have(1).keys
|
261
|
+
end
|
262
|
+
|
263
|
+
it "returns all specified fields" do
|
264
|
+
@response.keys.should include(:profile)
|
265
|
+
end
|
266
|
+
|
267
|
+
it "returns nil for the association" do
|
268
|
+
@response[:profile].should be_nil
|
269
|
+
end
|
270
|
+
end
|
271
|
+
end
|
272
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
shared_examples_for "calling a closure in the api template" do
|
2
|
+
|
3
|
+
describe "i.e. a proc (the record is passed as only parameter)" do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@response = @luke.as_api_response(:calling_a_proc)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "returns a hash" do
|
10
|
+
@response.should be_kind_of(Hash)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "returns the correct number of fields" do
|
14
|
+
@response.should have(2).keys
|
15
|
+
end
|
16
|
+
|
17
|
+
it "returns all specified fields" do
|
18
|
+
@response.keys.sort_by(&:to_s).should eql([:all_caps_name, :without_param])
|
19
|
+
end
|
20
|
+
|
21
|
+
it "returns the correct values for the specified fields" do
|
22
|
+
@response.values.sort.should eql(["LUKE SKYWALKER", "Time"])
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "i.e. a lambda (the record is passed as only parameter)" do
|
27
|
+
|
28
|
+
before(:each) do
|
29
|
+
@response = @luke.as_api_response(:calling_a_lambda)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "returns a hash" do
|
33
|
+
@response.should be_kind_of(Hash)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "returns the correct number of fields" do
|
37
|
+
@response.should have(2).keys
|
38
|
+
end
|
39
|
+
|
40
|
+
it "returns all specified fields" do
|
41
|
+
@response.keys.sort_by(&:to_s).should eql([:all_caps_name, :without_param])
|
42
|
+
end
|
43
|
+
|
44
|
+
it "returns the correct values for the specified fields" do
|
45
|
+
@response.values.sort.should eql(["LUKE SKYWALKER", "Time"])
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,165 @@
|
|
1
|
+
shared_examples_for "conditional if statements" do
|
2
|
+
|
3
|
+
describe "using the :if option" do
|
4
|
+
|
5
|
+
describe "passing a symbol" do
|
6
|
+
|
7
|
+
describe "that returns false" do
|
8
|
+
|
9
|
+
before(:each) do
|
10
|
+
@response = @luke.as_api_response(:if_over_thirty)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "returns a hash" do
|
14
|
+
@response.should be_kind_of(Hash)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "returns the correct number of fields" do
|
18
|
+
@response.should have(1).keys
|
19
|
+
end
|
20
|
+
|
21
|
+
it "won't add the conditional field but all others" do
|
22
|
+
@response.keys.should include(:first_name)
|
23
|
+
@response.keys.should_not include(:full_name)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "the other specified fields have the correct value" do
|
27
|
+
@response.values.should include(@luke.first_name)
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "that returns nil" do
|
33
|
+
|
34
|
+
before(:each) do
|
35
|
+
@response = @luke.as_api_response(:if_returns_nil)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "returns a hash" do
|
39
|
+
@response.should be_kind_of(Hash)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "returns the correct number of fields" do
|
43
|
+
@response.should have(1).keys
|
44
|
+
end
|
45
|
+
|
46
|
+
it "won't add the conditional field but all others" do
|
47
|
+
@response.keys.should include(:first_name)
|
48
|
+
@response.keys.should_not include(:full_name)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "the other specified fields have the correct value" do
|
52
|
+
@response.values.should include(@luke.first_name)
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "that returns true" do
|
58
|
+
|
59
|
+
before(:each) do
|
60
|
+
@response = @han.as_api_response(:if_over_thirty)
|
61
|
+
end
|
62
|
+
|
63
|
+
it "returns a hash" do
|
64
|
+
@response.should be_kind_of(Hash)
|
65
|
+
end
|
66
|
+
|
67
|
+
it "returns the correct number of fields" do
|
68
|
+
@response.should have(2).keys
|
69
|
+
end
|
70
|
+
|
71
|
+
it "won't add the conditional field but all others" do
|
72
|
+
@response.keys.should include(:first_name)
|
73
|
+
@response.keys.should include(:last_name)
|
74
|
+
end
|
75
|
+
|
76
|
+
it "the other specified fields have the correct value" do
|
77
|
+
@response.values.should include(@han.first_name, @han.last_name)
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
|
86
|
+
describe "passing a proc" do
|
87
|
+
|
88
|
+
describe "that returns false" do
|
89
|
+
|
90
|
+
before(:each) do
|
91
|
+
@response = @luke.as_api_response(:if_over_thirty_proc)
|
92
|
+
end
|
93
|
+
|
94
|
+
it "returns a hash" do
|
95
|
+
@response.should be_kind_of(Hash)
|
96
|
+
end
|
97
|
+
|
98
|
+
it "returns the correct number of fields" do
|
99
|
+
@response.should have(1).keys
|
100
|
+
end
|
101
|
+
|
102
|
+
it "won't add the conditional field but all others" do
|
103
|
+
@response.keys.should include(:first_name)
|
104
|
+
@response.keys.should_not include(:full_name)
|
105
|
+
end
|
106
|
+
|
107
|
+
it "the other specified fields have the correct value" do
|
108
|
+
@response.values.should include(@luke.first_name)
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
describe "that returns nil" do
|
114
|
+
|
115
|
+
before(:each) do
|
116
|
+
@response = @luke.as_api_response(:if_returns_nil_proc)
|
117
|
+
end
|
118
|
+
|
119
|
+
it "returns a hash" do
|
120
|
+
@response.should be_kind_of(Hash)
|
121
|
+
end
|
122
|
+
|
123
|
+
it "returns the correct number of fields" do
|
124
|
+
@response.should have(1).keys
|
125
|
+
end
|
126
|
+
|
127
|
+
it "won't add the conditional field but all others" do
|
128
|
+
@response.keys.should include(:first_name)
|
129
|
+
@response.keys.should_not include(:full_name)
|
130
|
+
end
|
131
|
+
|
132
|
+
it "the other specified fields have the correct value" do
|
133
|
+
@response.values.should include(@luke.first_name)
|
134
|
+
end
|
135
|
+
|
136
|
+
end
|
137
|
+
|
138
|
+
describe "that returns true" do
|
139
|
+
|
140
|
+
before(:each) do
|
141
|
+
@response = @han.as_api_response(:if_over_thirty_proc)
|
142
|
+
end
|
143
|
+
|
144
|
+
it "returns a hash" do
|
145
|
+
@response.should be_kind_of(Hash)
|
146
|
+
end
|
147
|
+
|
148
|
+
it "returns the correct number of fields" do
|
149
|
+
@response.should have(2).keys
|
150
|
+
end
|
151
|
+
|
152
|
+
it "won't add the conditional field but all others" do
|
153
|
+
@response.keys.should include(:first_name)
|
154
|
+
@response.keys.should include(:last_name)
|
155
|
+
end
|
156
|
+
|
157
|
+
it "the other specified fields have the correct value" do
|
158
|
+
@response.values.should include(@han.first_name, @han.last_name)
|
159
|
+
end
|
160
|
+
|
161
|
+
end
|
162
|
+
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|