acts_as_api_sequel 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.travis.yml +5 -0
- data/Gemfile +13 -0
- data/History.txt +108 -0
- data/README.md +126 -0
- data/Rakefile +48 -0
- data/acts_as_api.gemspec +28 -0
- data/examples/introduction/docco.css +186 -0
- data/examples/introduction/index.html +338 -0
- data/examples/introduction/index.rb +144 -0
- data/examples/introduction/layout.mustache +67 -0
- data/lib/acts_as_api.rb +46 -0
- data/lib/acts_as_api/adapters.rb +5 -0
- data/lib/acts_as_api/adapters/mongoid.rb +11 -0
- data/lib/acts_as_api/api_template.rb +139 -0
- data/lib/acts_as_api/array.rb +21 -0
- data/lib/acts_as_api/base.rb +88 -0
- data/lib/acts_as_api/config.rb +58 -0
- data/lib/acts_as_api/exceptions.rb +4 -0
- data/lib/acts_as_api/rails_renderer.rb +17 -0
- data/lib/acts_as_api/rendering.rb +95 -0
- data/lib/acts_as_api/responder.rb +39 -0
- data/lib/acts_as_api/version.rb +3 -0
- data/spec/README.md +55 -0
- data/spec/active_record_dummy/.gitignore +15 -0
- data/spec/active_record_dummy/Gemfile +16 -0
- data/spec/active_record_dummy/README.rdoc +261 -0
- data/spec/active_record_dummy/Rakefile +7 -0
- data/spec/active_record_dummy/app/assets/images/rails.png +0 -0
- data/spec/active_record_dummy/app/assets/javascripts/application.js +15 -0
- data/spec/active_record_dummy/app/assets/stylesheets/application.css +13 -0
- data/spec/active_record_dummy/app/controllers/application_controller.rb +3 -0
- data/spec/active_record_dummy/app/helpers/application_helper.rb +2 -0
- data/spec/active_record_dummy/app/mailers/.gitkeep +0 -0
- data/spec/active_record_dummy/app/models/.gitkeep +0 -0
- data/spec/active_record_dummy/app/models/profile.rb +4 -0
- data/spec/active_record_dummy/app/models/task.rb +4 -0
- data/spec/active_record_dummy/app/models/untouched.rb +2 -0
- data/spec/active_record_dummy/app/models/user.rb +186 -0
- data/spec/active_record_dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/active_record_dummy/config.ru +4 -0
- data/spec/active_record_dummy/config/application.rb +59 -0
- data/spec/active_record_dummy/config/boot.rb +6 -0
- data/spec/active_record_dummy/config/database.yml +25 -0
- data/spec/active_record_dummy/config/environment.rb +5 -0
- data/spec/active_record_dummy/config/environments/development.rb +37 -0
- data/spec/active_record_dummy/config/environments/production.rb +67 -0
- data/spec/active_record_dummy/config/environments/test.rb +37 -0
- data/spec/active_record_dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/active_record_dummy/config/initializers/generators.rb +2 -0
- data/spec/active_record_dummy/config/initializers/inflections.rb +15 -0
- data/spec/active_record_dummy/config/initializers/mime_types.rb +5 -0
- data/spec/active_record_dummy/config/initializers/secret_token.rb +7 -0
- data/spec/active_record_dummy/config/initializers/session_store.rb +8 -0
- data/spec/active_record_dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/active_record_dummy/config/locales/en.yml +5 -0
- data/spec/active_record_dummy/config/routes.rb +59 -0
- data/spec/active_record_dummy/db/migrate/20110214201640_create_tables.rb +44 -0
- data/spec/active_record_dummy/db/schema.rb +47 -0
- data/spec/active_record_dummy/db/seeds.rb +7 -0
- data/spec/active_record_dummy/doc/README_FOR_APP +2 -0
- data/spec/active_record_dummy/lib/assets/.gitkeep +0 -0
- data/spec/active_record_dummy/lib/tasks/.gitkeep +0 -0
- data/spec/active_record_dummy/log/.gitkeep +0 -0
- data/spec/active_record_dummy/public/404.html +26 -0
- data/spec/active_record_dummy/public/422.html +26 -0
- data/spec/active_record_dummy/public/500.html +25 -0
- data/spec/active_record_dummy/public/favicon.ico +0 -0
- data/spec/active_record_dummy/public/index.html +241 -0
- data/spec/active_record_dummy/public/robots.txt +5 -0
- data/spec/active_record_dummy/script/rails +6 -0
- data/spec/active_record_dummy/vendor/assets/javascripts/.gitkeep +0 -0
- data/spec/active_record_dummy/vendor/assets/stylesheets/.gitkeep +0 -0
- data/spec/active_record_dummy/vendor/plugins/.gitkeep +0 -0
- data/spec/controllers/plain_objects_controller_spec.rb +36 -0
- data/spec/controllers/respond_with_users_controller_spec.rb +142 -0
- data/spec/controllers/users_controller_spec.rb +15 -0
- data/spec/models/model_spec.rb +31 -0
- data/spec/mongoid_dummy/.gitignore +15 -0
- data/spec/mongoid_dummy/Gemfile +18 -0
- data/spec/mongoid_dummy/README.rdoc +261 -0
- data/spec/mongoid_dummy/Rakefile +7 -0
- data/spec/mongoid_dummy/app/assets/images/rails.png +0 -0
- data/spec/mongoid_dummy/app/assets/javascripts/application.js +15 -0
- data/spec/mongoid_dummy/app/assets/stylesheets/application.css +13 -0
- data/spec/mongoid_dummy/app/controllers/application_controller.rb +3 -0
- data/spec/mongoid_dummy/app/helpers/application_helper.rb +2 -0
- data/spec/mongoid_dummy/app/mailers/.gitkeep +0 -0
- data/spec/mongoid_dummy/app/models/.gitkeep +0 -0
- data/spec/mongoid_dummy/app/models/profile.rb +10 -0
- data/spec/mongoid_dummy/app/models/task.rb +12 -0
- data/spec/mongoid_dummy/app/models/untouched.rb +7 -0
- data/spec/mongoid_dummy/app/models/user.rb +48 -0
- data/spec/mongoid_dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/mongoid_dummy/config.ru +4 -0
- data/spec/mongoid_dummy/config/application.rb +65 -0
- data/spec/mongoid_dummy/config/boot.rb +6 -0
- data/spec/mongoid_dummy/config/environment.rb +5 -0
- data/spec/mongoid_dummy/config/environments/development.rb +31 -0
- data/spec/mongoid_dummy/config/environments/production.rb +64 -0
- data/spec/mongoid_dummy/config/environments/test.rb +35 -0
- data/spec/mongoid_dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/mongoid_dummy/config/initializers/generators.rb +2 -0
- data/spec/mongoid_dummy/config/initializers/include_acts_as_api.rb +3 -0
- data/spec/mongoid_dummy/config/initializers/inflections.rb +15 -0
- data/spec/mongoid_dummy/config/initializers/mime_types.rb +5 -0
- data/spec/mongoid_dummy/config/initializers/secret_token.rb +7 -0
- data/spec/mongoid_dummy/config/initializers/session_store.rb +8 -0
- data/spec/mongoid_dummy/config/initializers/wrap_parameters.rb +10 -0
- data/spec/mongoid_dummy/config/locales/en.yml +5 -0
- data/spec/mongoid_dummy/config/mongoid.yml +120 -0
- data/spec/mongoid_dummy/config/routes.rb +59 -0
- data/spec/mongoid_dummy/db/seeds.rb +7 -0
- data/spec/mongoid_dummy/doc/README_FOR_APP +2 -0
- data/spec/mongoid_dummy/lib/assets/.gitkeep +0 -0
- data/spec/mongoid_dummy/lib/tasks/.gitkeep +0 -0
- data/spec/mongoid_dummy/log/.gitkeep +0 -0
- data/spec/mongoid_dummy/public/404.html +26 -0
- data/spec/mongoid_dummy/public/422.html +26 -0
- data/spec/mongoid_dummy/public/500.html +25 -0
- data/spec/mongoid_dummy/public/favicon.ico +0 -0
- data/spec/mongoid_dummy/public/index.html +241 -0
- data/spec/mongoid_dummy/public/robots.txt +5 -0
- data/spec/mongoid_dummy/script/rails +6 -0
- data/spec/mongoid_dummy/vendor/assets/javascripts/.gitkeep +0 -0
- data/spec/mongoid_dummy/vendor/assets/stylesheets/.gitkeep +0 -0
- data/spec/mongoid_dummy/vendor/plugins/.gitkeep +0 -0
- data/spec/shared_engine/.gitignore +7 -0
- data/spec/shared_engine/Gemfile +14 -0
- data/spec/shared_engine/MIT-LICENSE +20 -0
- data/spec/shared_engine/README.rdoc +3 -0
- data/spec/shared_engine/Rakefile +29 -0
- data/spec/shared_engine/app/assets/images/shared_engine/.gitkeep +0 -0
- data/spec/shared_engine/app/assets/javascripts/shared_engine/application.js +15 -0
- data/spec/shared_engine/app/assets/stylesheets/shared_engine/application.css +13 -0
- data/spec/shared_engine/app/controllers/shared_engine/application_controller.rb +4 -0
- data/spec/shared_engine/app/controllers/shared_engine/plain_objects_controller.rb +14 -0
- data/spec/shared_engine/app/controllers/shared_engine/respond_with_users_controller.rb +64 -0
- data/spec/shared_engine/app/controllers/shared_engine/users_controller.rb +71 -0
- data/spec/shared_engine/app/helpers/shared_engine/application_helper.rb +4 -0
- data/spec/shared_engine/app/models/plain_object.rb +17 -0
- data/spec/shared_engine/app/models/user_template.rb +154 -0
- data/spec/shared_engine/app/views/layouts/shared_engine/application.html.erb +14 -0
- data/spec/shared_engine/config/routes.rb +28 -0
- data/spec/shared_engine/dummy/README.rdoc +261 -0
- data/spec/shared_engine/dummy/Rakefile +7 -0
- data/spec/shared_engine/dummy/app/assets/javascripts/application.js +15 -0
- data/spec/shared_engine/dummy/app/assets/stylesheets/application.css +13 -0
- data/spec/shared_engine/dummy/app/controllers/application_controller.rb +3 -0
- data/spec/shared_engine/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/shared_engine/dummy/app/mailers/.gitkeep +0 -0
- data/spec/shared_engine/dummy/app/models/.gitkeep +0 -0
- data/spec/shared_engine/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/shared_engine/dummy/config.ru +4 -0
- data/spec/shared_engine/dummy/config/application.rb +62 -0
- data/spec/shared_engine/dummy/config/boot.rb +10 -0
- data/spec/shared_engine/dummy/config/database.yml +25 -0
- data/spec/shared_engine/dummy/config/environment.rb +5 -0
- data/spec/shared_engine/dummy/config/environments/development.rb +37 -0
- data/spec/shared_engine/dummy/config/environments/production.rb +67 -0
- data/spec/shared_engine/dummy/config/environments/test.rb +37 -0
- data/spec/shared_engine/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/shared_engine/dummy/config/initializers/inflections.rb +15 -0
- data/spec/shared_engine/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/shared_engine/dummy/config/initializers/secret_token.rb +7 -0
- data/spec/shared_engine/dummy/config/initializers/session_store.rb +8 -0
- data/spec/shared_engine/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/shared_engine/dummy/config/locales/en.yml +5 -0
- data/spec/shared_engine/dummy/config/routes.rb +4 -0
- data/spec/shared_engine/dummy/lib/assets/.gitkeep +0 -0
- data/spec/shared_engine/dummy/log/.gitkeep +0 -0
- data/spec/shared_engine/dummy/public/404.html +26 -0
- data/spec/shared_engine/dummy/public/422.html +26 -0
- data/spec/shared_engine/dummy/public/500.html +25 -0
- data/spec/shared_engine/dummy/public/favicon.ico +0 -0
- data/spec/shared_engine/dummy/script/rails +6 -0
- data/spec/shared_engine/lib/magic/rails/engine.rb +69 -0
- data/spec/shared_engine/lib/shared_engine.rb +4 -0
- data/spec/shared_engine/lib/shared_engine/engine.rb +5 -0
- data/spec/shared_engine/lib/shared_engine/version.rb +3 -0
- data/spec/shared_engine/lib/tasks/shared_engine_tasks.rake +4 -0
- data/spec/shared_engine/script/rails +8 -0
- data/spec/shared_engine/shared_engine.gemspec +22 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +22 -0
- data/spec/support/api_test_helpers.rb +23 -0
- data/spec/support/controller_examples.rb +458 -0
- data/spec/support/it_supports.rb +3 -0
- data/spec/support/model_examples/associations.rb +272 -0
- data/spec/support/model_examples/callbacks.rb +38 -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/options.rb +53 -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/routing.rb +4 -0
- data/spec/support/simple_fixtures.rb +59 -0
- metadata +499 -0
@@ -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.acts_as_api
|
37
|
+
Task.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.acts_as_api
|
78
|
+
Profile.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.acts_as_api
|
118
|
+
Task.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.class_eval do
|
169
|
+
scope :completed, where(:done => true)
|
170
|
+
end
|
171
|
+
Task.acts_as_api
|
172
|
+
Task.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.acts_as_api
|
216
|
+
Task.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.acts_as_api
|
248
|
+
Profile.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,38 @@
|
|
1
|
+
shared_examples_for "defining a model callback" do
|
2
|
+
|
3
|
+
describe "for a" do
|
4
|
+
|
5
|
+
describe "around_api_response" do
|
6
|
+
|
7
|
+
it "skips rendering if not yielded" do
|
8
|
+
@luke.skip_api_response = true
|
9
|
+
@luke.as_api_response(:name_only).keys.should include(:skipped)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "renders if yielded" do
|
13
|
+
@luke.as_api_response(:name_only).keys.should_not include(:skipped)
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "before_api_response" do
|
19
|
+
|
20
|
+
it "is called properly" do
|
21
|
+
@luke.as_api_response(:name_only)
|
22
|
+
@luke.before_api_response_called?.should eql(true)
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "after_api_response" do
|
28
|
+
|
29
|
+
it "is called properly" do
|
30
|
+
@luke.as_api_response(:name_only)
|
31
|
+
@luke.after_api_response_called?.should eql(true)
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
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
|
+
|