acts_as_api_sequel 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (206) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +14 -0
  3. data/.travis.yml +5 -0
  4. data/Gemfile +13 -0
  5. data/History.txt +108 -0
  6. data/README.md +126 -0
  7. data/Rakefile +48 -0
  8. data/acts_as_api.gemspec +28 -0
  9. data/examples/introduction/docco.css +186 -0
  10. data/examples/introduction/index.html +338 -0
  11. data/examples/introduction/index.rb +144 -0
  12. data/examples/introduction/layout.mustache +67 -0
  13. data/lib/acts_as_api.rb +46 -0
  14. data/lib/acts_as_api/adapters.rb +5 -0
  15. data/lib/acts_as_api/adapters/mongoid.rb +11 -0
  16. data/lib/acts_as_api/api_template.rb +139 -0
  17. data/lib/acts_as_api/array.rb +21 -0
  18. data/lib/acts_as_api/base.rb +88 -0
  19. data/lib/acts_as_api/config.rb +58 -0
  20. data/lib/acts_as_api/exceptions.rb +4 -0
  21. data/lib/acts_as_api/rails_renderer.rb +17 -0
  22. data/lib/acts_as_api/rendering.rb +95 -0
  23. data/lib/acts_as_api/responder.rb +39 -0
  24. data/lib/acts_as_api/version.rb +3 -0
  25. data/spec/README.md +55 -0
  26. data/spec/active_record_dummy/.gitignore +15 -0
  27. data/spec/active_record_dummy/Gemfile +16 -0
  28. data/spec/active_record_dummy/README.rdoc +261 -0
  29. data/spec/active_record_dummy/Rakefile +7 -0
  30. data/spec/active_record_dummy/app/assets/images/rails.png +0 -0
  31. data/spec/active_record_dummy/app/assets/javascripts/application.js +15 -0
  32. data/spec/active_record_dummy/app/assets/stylesheets/application.css +13 -0
  33. data/spec/active_record_dummy/app/controllers/application_controller.rb +3 -0
  34. data/spec/active_record_dummy/app/helpers/application_helper.rb +2 -0
  35. data/spec/active_record_dummy/app/mailers/.gitkeep +0 -0
  36. data/spec/active_record_dummy/app/models/.gitkeep +0 -0
  37. data/spec/active_record_dummy/app/models/profile.rb +4 -0
  38. data/spec/active_record_dummy/app/models/task.rb +4 -0
  39. data/spec/active_record_dummy/app/models/untouched.rb +2 -0
  40. data/spec/active_record_dummy/app/models/user.rb +186 -0
  41. data/spec/active_record_dummy/app/views/layouts/application.html.erb +14 -0
  42. data/spec/active_record_dummy/config.ru +4 -0
  43. data/spec/active_record_dummy/config/application.rb +59 -0
  44. data/spec/active_record_dummy/config/boot.rb +6 -0
  45. data/spec/active_record_dummy/config/database.yml +25 -0
  46. data/spec/active_record_dummy/config/environment.rb +5 -0
  47. data/spec/active_record_dummy/config/environments/development.rb +37 -0
  48. data/spec/active_record_dummy/config/environments/production.rb +67 -0
  49. data/spec/active_record_dummy/config/environments/test.rb +37 -0
  50. data/spec/active_record_dummy/config/initializers/backtrace_silencers.rb +7 -0
  51. data/spec/active_record_dummy/config/initializers/generators.rb +2 -0
  52. data/spec/active_record_dummy/config/initializers/inflections.rb +15 -0
  53. data/spec/active_record_dummy/config/initializers/mime_types.rb +5 -0
  54. data/spec/active_record_dummy/config/initializers/secret_token.rb +7 -0
  55. data/spec/active_record_dummy/config/initializers/session_store.rb +8 -0
  56. data/spec/active_record_dummy/config/initializers/wrap_parameters.rb +14 -0
  57. data/spec/active_record_dummy/config/locales/en.yml +5 -0
  58. data/spec/active_record_dummy/config/routes.rb +59 -0
  59. data/spec/active_record_dummy/db/migrate/20110214201640_create_tables.rb +44 -0
  60. data/spec/active_record_dummy/db/schema.rb +47 -0
  61. data/spec/active_record_dummy/db/seeds.rb +7 -0
  62. data/spec/active_record_dummy/doc/README_FOR_APP +2 -0
  63. data/spec/active_record_dummy/lib/assets/.gitkeep +0 -0
  64. data/spec/active_record_dummy/lib/tasks/.gitkeep +0 -0
  65. data/spec/active_record_dummy/log/.gitkeep +0 -0
  66. data/spec/active_record_dummy/public/404.html +26 -0
  67. data/spec/active_record_dummy/public/422.html +26 -0
  68. data/spec/active_record_dummy/public/500.html +25 -0
  69. data/spec/active_record_dummy/public/favicon.ico +0 -0
  70. data/spec/active_record_dummy/public/index.html +241 -0
  71. data/spec/active_record_dummy/public/robots.txt +5 -0
  72. data/spec/active_record_dummy/script/rails +6 -0
  73. data/spec/active_record_dummy/vendor/assets/javascripts/.gitkeep +0 -0
  74. data/spec/active_record_dummy/vendor/assets/stylesheets/.gitkeep +0 -0
  75. data/spec/active_record_dummy/vendor/plugins/.gitkeep +0 -0
  76. data/spec/controllers/plain_objects_controller_spec.rb +36 -0
  77. data/spec/controllers/respond_with_users_controller_spec.rb +142 -0
  78. data/spec/controllers/users_controller_spec.rb +15 -0
  79. data/spec/models/model_spec.rb +31 -0
  80. data/spec/mongoid_dummy/.gitignore +15 -0
  81. data/spec/mongoid_dummy/Gemfile +18 -0
  82. data/spec/mongoid_dummy/README.rdoc +261 -0
  83. data/spec/mongoid_dummy/Rakefile +7 -0
  84. data/spec/mongoid_dummy/app/assets/images/rails.png +0 -0
  85. data/spec/mongoid_dummy/app/assets/javascripts/application.js +15 -0
  86. data/spec/mongoid_dummy/app/assets/stylesheets/application.css +13 -0
  87. data/spec/mongoid_dummy/app/controllers/application_controller.rb +3 -0
  88. data/spec/mongoid_dummy/app/helpers/application_helper.rb +2 -0
  89. data/spec/mongoid_dummy/app/mailers/.gitkeep +0 -0
  90. data/spec/mongoid_dummy/app/models/.gitkeep +0 -0
  91. data/spec/mongoid_dummy/app/models/profile.rb +10 -0
  92. data/spec/mongoid_dummy/app/models/task.rb +12 -0
  93. data/spec/mongoid_dummy/app/models/untouched.rb +7 -0
  94. data/spec/mongoid_dummy/app/models/user.rb +48 -0
  95. data/spec/mongoid_dummy/app/views/layouts/application.html.erb +14 -0
  96. data/spec/mongoid_dummy/config.ru +4 -0
  97. data/spec/mongoid_dummy/config/application.rb +65 -0
  98. data/spec/mongoid_dummy/config/boot.rb +6 -0
  99. data/spec/mongoid_dummy/config/environment.rb +5 -0
  100. data/spec/mongoid_dummy/config/environments/development.rb +31 -0
  101. data/spec/mongoid_dummy/config/environments/production.rb +64 -0
  102. data/spec/mongoid_dummy/config/environments/test.rb +35 -0
  103. data/spec/mongoid_dummy/config/initializers/backtrace_silencers.rb +7 -0
  104. data/spec/mongoid_dummy/config/initializers/generators.rb +2 -0
  105. data/spec/mongoid_dummy/config/initializers/include_acts_as_api.rb +3 -0
  106. data/spec/mongoid_dummy/config/initializers/inflections.rb +15 -0
  107. data/spec/mongoid_dummy/config/initializers/mime_types.rb +5 -0
  108. data/spec/mongoid_dummy/config/initializers/secret_token.rb +7 -0
  109. data/spec/mongoid_dummy/config/initializers/session_store.rb +8 -0
  110. data/spec/mongoid_dummy/config/initializers/wrap_parameters.rb +10 -0
  111. data/spec/mongoid_dummy/config/locales/en.yml +5 -0
  112. data/spec/mongoid_dummy/config/mongoid.yml +120 -0
  113. data/spec/mongoid_dummy/config/routes.rb +59 -0
  114. data/spec/mongoid_dummy/db/seeds.rb +7 -0
  115. data/spec/mongoid_dummy/doc/README_FOR_APP +2 -0
  116. data/spec/mongoid_dummy/lib/assets/.gitkeep +0 -0
  117. data/spec/mongoid_dummy/lib/tasks/.gitkeep +0 -0
  118. data/spec/mongoid_dummy/log/.gitkeep +0 -0
  119. data/spec/mongoid_dummy/public/404.html +26 -0
  120. data/spec/mongoid_dummy/public/422.html +26 -0
  121. data/spec/mongoid_dummy/public/500.html +25 -0
  122. data/spec/mongoid_dummy/public/favicon.ico +0 -0
  123. data/spec/mongoid_dummy/public/index.html +241 -0
  124. data/spec/mongoid_dummy/public/robots.txt +5 -0
  125. data/spec/mongoid_dummy/script/rails +6 -0
  126. data/spec/mongoid_dummy/vendor/assets/javascripts/.gitkeep +0 -0
  127. data/spec/mongoid_dummy/vendor/assets/stylesheets/.gitkeep +0 -0
  128. data/spec/mongoid_dummy/vendor/plugins/.gitkeep +0 -0
  129. data/spec/shared_engine/.gitignore +7 -0
  130. data/spec/shared_engine/Gemfile +14 -0
  131. data/spec/shared_engine/MIT-LICENSE +20 -0
  132. data/spec/shared_engine/README.rdoc +3 -0
  133. data/spec/shared_engine/Rakefile +29 -0
  134. data/spec/shared_engine/app/assets/images/shared_engine/.gitkeep +0 -0
  135. data/spec/shared_engine/app/assets/javascripts/shared_engine/application.js +15 -0
  136. data/spec/shared_engine/app/assets/stylesheets/shared_engine/application.css +13 -0
  137. data/spec/shared_engine/app/controllers/shared_engine/application_controller.rb +4 -0
  138. data/spec/shared_engine/app/controllers/shared_engine/plain_objects_controller.rb +14 -0
  139. data/spec/shared_engine/app/controllers/shared_engine/respond_with_users_controller.rb +64 -0
  140. data/spec/shared_engine/app/controllers/shared_engine/users_controller.rb +71 -0
  141. data/spec/shared_engine/app/helpers/shared_engine/application_helper.rb +4 -0
  142. data/spec/shared_engine/app/models/plain_object.rb +17 -0
  143. data/spec/shared_engine/app/models/user_template.rb +154 -0
  144. data/spec/shared_engine/app/views/layouts/shared_engine/application.html.erb +14 -0
  145. data/spec/shared_engine/config/routes.rb +28 -0
  146. data/spec/shared_engine/dummy/README.rdoc +261 -0
  147. data/spec/shared_engine/dummy/Rakefile +7 -0
  148. data/spec/shared_engine/dummy/app/assets/javascripts/application.js +15 -0
  149. data/spec/shared_engine/dummy/app/assets/stylesheets/application.css +13 -0
  150. data/spec/shared_engine/dummy/app/controllers/application_controller.rb +3 -0
  151. data/spec/shared_engine/dummy/app/helpers/application_helper.rb +2 -0
  152. data/spec/shared_engine/dummy/app/mailers/.gitkeep +0 -0
  153. data/spec/shared_engine/dummy/app/models/.gitkeep +0 -0
  154. data/spec/shared_engine/dummy/app/views/layouts/application.html.erb +14 -0
  155. data/spec/shared_engine/dummy/config.ru +4 -0
  156. data/spec/shared_engine/dummy/config/application.rb +62 -0
  157. data/spec/shared_engine/dummy/config/boot.rb +10 -0
  158. data/spec/shared_engine/dummy/config/database.yml +25 -0
  159. data/spec/shared_engine/dummy/config/environment.rb +5 -0
  160. data/spec/shared_engine/dummy/config/environments/development.rb +37 -0
  161. data/spec/shared_engine/dummy/config/environments/production.rb +67 -0
  162. data/spec/shared_engine/dummy/config/environments/test.rb +37 -0
  163. data/spec/shared_engine/dummy/config/initializers/backtrace_silencers.rb +7 -0
  164. data/spec/shared_engine/dummy/config/initializers/inflections.rb +15 -0
  165. data/spec/shared_engine/dummy/config/initializers/mime_types.rb +5 -0
  166. data/spec/shared_engine/dummy/config/initializers/secret_token.rb +7 -0
  167. data/spec/shared_engine/dummy/config/initializers/session_store.rb +8 -0
  168. data/spec/shared_engine/dummy/config/initializers/wrap_parameters.rb +14 -0
  169. data/spec/shared_engine/dummy/config/locales/en.yml +5 -0
  170. data/spec/shared_engine/dummy/config/routes.rb +4 -0
  171. data/spec/shared_engine/dummy/lib/assets/.gitkeep +0 -0
  172. data/spec/shared_engine/dummy/log/.gitkeep +0 -0
  173. data/spec/shared_engine/dummy/public/404.html +26 -0
  174. data/spec/shared_engine/dummy/public/422.html +26 -0
  175. data/spec/shared_engine/dummy/public/500.html +25 -0
  176. data/spec/shared_engine/dummy/public/favicon.ico +0 -0
  177. data/spec/shared_engine/dummy/script/rails +6 -0
  178. data/spec/shared_engine/lib/magic/rails/engine.rb +69 -0
  179. data/spec/shared_engine/lib/shared_engine.rb +4 -0
  180. data/spec/shared_engine/lib/shared_engine/engine.rb +5 -0
  181. data/spec/shared_engine/lib/shared_engine/version.rb +3 -0
  182. data/spec/shared_engine/lib/tasks/shared_engine_tasks.rake +4 -0
  183. data/spec/shared_engine/script/rails +8 -0
  184. data/spec/shared_engine/shared_engine.gemspec +22 -0
  185. data/spec/spec.opts +1 -0
  186. data/spec/spec_helper.rb +22 -0
  187. data/spec/support/api_test_helpers.rb +23 -0
  188. data/spec/support/controller_examples.rb +458 -0
  189. data/spec/support/it_supports.rb +3 -0
  190. data/spec/support/model_examples/associations.rb +272 -0
  191. data/spec/support/model_examples/callbacks.rb +38 -0
  192. data/spec/support/model_examples/closures.rb +49 -0
  193. data/spec/support/model_examples/conditional_if.rb +165 -0
  194. data/spec/support/model_examples/conditional_unless.rb +165 -0
  195. data/spec/support/model_examples/enabled.rb +10 -0
  196. data/spec/support/model_examples/extending.rb +112 -0
  197. data/spec/support/model_examples/methods.rb +23 -0
  198. data/spec/support/model_examples/options.rb +53 -0
  199. data/spec/support/model_examples/renaming.rb +50 -0
  200. data/spec/support/model_examples/simple.rb +23 -0
  201. data/spec/support/model_examples/sub_nodes.rb +105 -0
  202. data/spec/support/model_examples/undefined.rb +7 -0
  203. data/spec/support/model_examples/untouched.rb +13 -0
  204. data/spec/support/routing.rb +4 -0
  205. data/spec/support/simple_fixtures.rb +59 -0
  206. metadata +499 -0
@@ -0,0 +1,165 @@
1
+ shared_examples_for "conditional unless statements" do
2
+
3
+ describe "using the :unless 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(:unless_under_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(:unless_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(2).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 include(:last_name)
49
+ end
50
+
51
+ it "the other specified fields have the correct value" do
52
+ @response.values.should include(@luke.first_name, @luke.last_name)
53
+ end
54
+
55
+ end
56
+
57
+ describe "that returns true" do
58
+
59
+ before(:each) do
60
+ @response = @han.as_api_response(:unless_under_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(:unless_under_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(:unless_under_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
+
165
+ end
@@ -0,0 +1,10 @@
1
+ shared_examples_for "acts_as_api is enabled" do
2
+
3
+ it "indicates that acts_as_api is enabled" do
4
+ User.acts_as_api?.should be_true
5
+ end
6
+
7
+ it "does respond to api_accessible" do
8
+ User.should respond_to :api_accessible
9
+ end
10
+ end
@@ -0,0 +1,112 @@
1
+ shared_examples_for "extending a given api template" do
2
+
3
+ describe "multiple times" do
4
+
5
+ before(:each) do
6
+ User.api_accessible :public do |t|
7
+ t.add :first_name
8
+ end
9
+
10
+ User.api_accessible :for_buddies, :extend => :public do |t|
11
+ t.add :age
12
+ end
13
+
14
+ User.api_accessible :private, :extend => :for_buddies do |t|
15
+ t.add :last_name
16
+ end
17
+ @response = @luke.as_api_response(:private)
18
+ end
19
+
20
+ it "returns a hash" do
21
+ @response.should be_kind_of(Hash)
22
+ end
23
+
24
+ it "returns the correct number of fields" do
25
+ @response.should have(3).keys
26
+ end
27
+
28
+ it "returns all specified fields" do
29
+ @response.keys.sort_by(&:to_s).should eql([:age, :first_name, :last_name])
30
+ end
31
+
32
+ it "returns the correct values for the specified fields" do
33
+ @response.values.sort_by(&:to_s).should eql([@luke.age, @luke.first_name, @luke.last_name].sort_by(&:to_s))
34
+ end
35
+
36
+ end
37
+
38
+ describe "and removing a former added value" do
39
+
40
+ before(:each) do
41
+ @response = @luke.as_api_response(:age_and_first_name)
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(2).keys
50
+ end
51
+
52
+ it "returns all specified fields" do
53
+ @response.keys.sort_by(&:to_s).should eql([:first_name, :age].sort_by(&:to_s))
54
+ end
55
+
56
+ it "returns the correct values for the specified fields" do
57
+ @response.values.sort_by(&:to_s).should eql([@luke.first_name, @luke.age].sort_by(&:to_s))
58
+ end
59
+
60
+ end
61
+
62
+ describe "and inherit a field using another template name", :meow => true do
63
+
64
+ before(:each) do
65
+ Task.acts_as_api
66
+ Task.api_accessible :other_template do |t|
67
+ t.add :description
68
+ t.add :time_spent
69
+ end
70
+ User.api_accessible :extending_other_template, :extend => :other_sub_template
71
+ @response = @luke.as_api_response(:extending_other_template)
72
+ end
73
+
74
+ it "returns a hash" do
75
+ @response.should be_kind_of(Hash)
76
+ end
77
+
78
+ it "returns the correct number of fields" do
79
+ @response.should have(2).keys
80
+ end
81
+
82
+ it "returns all specified fields" do
83
+ @response.keys.should include(:first_name)
84
+ end
85
+
86
+ it "returns the correct values for the specified fields" do
87
+ @response.values.should include(@luke.first_name)
88
+ end
89
+
90
+ it "returns all specified fields" do
91
+ @response.keys.should include(:tasks)
92
+ end
93
+
94
+ it "returns the correct values for the specified fields" do
95
+ @response[:tasks].should be_an Array
96
+ @response[:tasks].should have(3).tasks
97
+ end
98
+
99
+ it "contains the associated child models with the determined api template" do
100
+ @response[:tasks].each do |task|
101
+ task.keys.should include(:description, :time_spent)
102
+ task.keys.should have(2).attributes
103
+ end
104
+ end
105
+
106
+ it "contains the correct data of the child models" do
107
+ task_hash = [ @destroy_deathstar, @study_with_yoda, @win_rebellion ].collect{|t| { :description => t.description, :time_spent => t.time_spent } }
108
+ @response[:tasks].should eql task_hash
109
+ end
110
+ end
111
+
112
+ end
@@ -0,0 +1,23 @@
1
+ shared_examples_for "calling a method in the api template" do
2
+
3
+ before(:each) do
4
+ @response = @luke.as_api_response(:only_full_name)
5
+ end
6
+
7
+ it "returns a hash" do
8
+ @response.should be_kind_of(Hash)
9
+ end
10
+
11
+ it "returns the correct number of fields" do
12
+ @response.should have(1).keys
13
+ end
14
+
15
+ it "returns all specified fields by name" do
16
+ @response.keys.should include(:full_name)
17
+ end
18
+
19
+ it "returns the correct values for the specified fields" do
20
+ @response.values.should include(@luke.full_name)
21
+ end
22
+
23
+ end
@@ -0,0 +1,53 @@
1
+ shared_examples_for "options" do
2
+
3
+ describe "options in the api template" do
4
+
5
+ before :each do
6
+ User.api_accessible :with_options do |t|
7
+ t.add lambda{|model,options| options }, :as => :options
8
+ t.add :profile
9
+ t.add :first_name, :if => lambda{|m,options| options[:with_name] }
10
+ end
11
+
12
+ Profile.acts_as_api
13
+ Profile.api_accessible :with_options do |t|
14
+ t.add lambda{|model,options| options }, :as => :options
15
+ end
16
+
17
+ Task.acts_as_api
18
+ Task.api_accessible :other_template do |t|
19
+ t.add :description
20
+ t.add :time_spent
21
+ t.add lambda{|model,options| options }, :as => :options
22
+ end
23
+ end
24
+
25
+ context "as_api_response accept options" do
26
+ before :each do
27
+ @response = @luke.as_api_response(:with_options, :loc => [12, 13])
28
+ end
29
+
30
+ it "returns the options field as specified" do
31
+ @response[:options][:loc].should == [12, 13]
32
+ end
33
+
34
+ it "returns the option for the associations " do
35
+ @response[:profile][:options][:loc].should == [12, 13]
36
+ end
37
+
38
+ end
39
+
40
+ context "allowed_to_render accept options" do
41
+ it "should not contains first_name when options[:with_name] is false" do
42
+ @response = @luke.as_api_response(:with_options, :with_name => false)
43
+ @response.should_not include(:first_name)
44
+ end
45
+
46
+ it "should contains first_name when options[:with_name] is true" do
47
+ @response = @luke.as_api_response(:with_options, :with_name => true)
48
+ @response[:first_name].should == 'Luke'
49
+ end
50
+ end
51
+
52
+ end
53
+ end
@@ -0,0 +1,50 @@
1
+ shared_examples_for "renaming" do
2
+
3
+ describe "an attribute in the api template" do
4
+
5
+ before(:each) do
6
+ @response = @luke.as_api_response(:rename_last_name)
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(:family_name)
19
+ end
20
+
21
+ it "returns the correct values for the specified fields" do
22
+ @response.values.should include(@luke.last_name)
23
+ end
24
+
25
+ end
26
+
27
+ describe "the node/key of a method in the api template" do
28
+
29
+ before(:each) do
30
+ @response = @luke.as_api_response(:rename_full_name)
31
+ end
32
+
33
+ it "returns a hash" do
34
+ @response.should be_kind_of(Hash)
35
+ end
36
+
37
+ it "returns the correct number of fields" do
38
+ @response.should have(1).keys
39
+ end
40
+
41
+ it "returns all specified fields" do
42
+ @response.keys.should include(:other_full_name)
43
+ end
44
+
45
+ it "returns the correct values for the specified fields" do
46
+ @response.values.should include(@luke.full_name)
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,23 @@
1
+ shared_examples_for "listing attributes in the api template" do
2
+
3
+ before(:each) do
4
+ @response = @luke.as_api_response(:name_only)
5
+ end
6
+
7
+ it "returns a hash" do
8
+ @response.should be_kind_of(Hash)
9
+ end
10
+
11
+ it "returns the correct number of fields" do
12
+ @response.should have(2).keys
13
+ end
14
+
15
+ it "returns the specified fields only" do
16
+ @response.keys.should include(:first_name, :last_name)
17
+ end
18
+
19
+ it "the specified fields have the correct value" do
20
+ @response.values.should include(@luke.first_name, @luke.last_name)
21
+ end
22
+
23
+ end
@@ -0,0 +1,105 @@
1
+ shared_examples_for "creating a sub hash in the api template" do
2
+
3
+ describe "and putting an attribute in it" do
4
+
5
+ before(:each) do
6
+ @response = @luke.as_api_response(:sub_node)
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(:sub_nodes)
19
+ end
20
+
21
+ it "returns the correct values for the specified fields" do
22
+ @response[:sub_nodes].should be_a Hash
23
+ end
24
+
25
+ it "provides the correct number of sub nodes" do
26
+ @response[:sub_nodes].should have(1).keys
27
+ end
28
+
29
+ it "provides the correct sub nodes values" do
30
+ @response[:sub_nodes][:foo].should eql("something")
31
+ end
32
+ end
33
+
34
+ describe "multiple times and putting an attribute in it" do
35
+
36
+ before(:each) do
37
+ @response = @luke.as_api_response(:nested_sub_node)
38
+ end
39
+
40
+ it "returns a hash" do
41
+ @response.should be_kind_of(Hash)
42
+ end
43
+
44
+ it "returns the correct number of fields" do
45
+ @response.should have(1).keys
46
+ end
47
+
48
+ it "returns all specified fields" do
49
+ @response.keys.should include(:sub_nodes)
50
+ end
51
+
52
+ it "returns the correct values for the specified fields" do
53
+ @response[:sub_nodes].should be_a Hash
54
+ end
55
+
56
+ it "provides the correct number of sub nodes" do
57
+ @response[:sub_nodes].should have(1).keys
58
+ end
59
+
60
+ it "provides the correct number of sub nodes in the second level" do
61
+ @response[:sub_nodes][:foo].should have(1).keys
62
+ end
63
+
64
+ it "provides the correct sub nodes values" do
65
+ @response[:sub_nodes][:foo].tap do |foo|
66
+ foo[:bar].tap do |bar|
67
+ bar.should eql(@luke.last_name)
68
+ end
69
+ end
70
+ end
71
+
72
+ end
73
+
74
+ describe "using a method" do
75
+
76
+ before(:each) do
77
+ @response = @luke.as_api_response(:nested_sub_hash)
78
+ end
79
+
80
+ it "returns a hash" do
81
+ @response.should be_kind_of(Hash)
82
+ end
83
+
84
+ it "returns the correct number of fields" do
85
+ @response.should have(1).keys
86
+ end
87
+
88
+ it "returns all specified fields" do
89
+ @response.keys.should include(:sub_hash)
90
+ end
91
+
92
+ it "provides the correct number of sub nodes" do
93
+ @response[:sub_hash].should have(2).keys
94
+ end
95
+
96
+ it "provides the correct sub nodes" do
97
+ @response[:sub_hash].keys.should include(:foo, :hello)
98
+ end
99
+
100
+ it "provides the correct values in its sub nodes" do
101
+ @response[:sub_hash].values.should include("bar", "world")
102
+ end
103
+
104
+ end
105
+ end