acts_as_api_sequel 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (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,338 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta http-equiv="content-type" content="text/html;charset=utf-8">
5
+ <title>acts_as_api</title>
6
+ <meta content="A Ruby/Rails gem to easily generate web api reponses!" name="description" />
7
+ <meta content="Christian Bäuerlein" name="author" />
8
+ <meta content="en" name="language" />
9
+ <link rel="stylesheet" href="./docco.css">
10
+ <link href="http://fonts.googleapis.com/css?family=Copse:regular" rel="stylesheet" type="text/css" >
11
+ <style>
12
+ h1, h2, h3, h4, h5 {
13
+ font-family: 'Copse', serif;
14
+ font-style: normal;
15
+ font-weight: 700;
16
+ text-shadow: none;
17
+ text-decoration: none;
18
+ text-transform: none;
19
+ letter-spacing: 0em;
20
+ word-spacing: 0em;
21
+ line-height: 1.2;
22
+ }
23
+ </style>
24
+ </head>
25
+ <body>
26
+ <div id='container'>
27
+ <div id="background"></div>
28
+ <table cellspacing=0 cellpadding=0>
29
+ <thead>
30
+ <tr>
31
+ <th class=docs>
32
+ <h1>acts_as_api</h1>
33
+ <p>Makes creating XML/JSON responses in Rails 3 easy and fun.</p>
34
+ </th>
35
+ <th class=code></th>
36
+ </tr>
37
+ </thead>
38
+ <tbody>
39
+ <tr id='section-1'>
40
+ <td class=docs>
41
+ <div class="pilwrap">
42
+ <a class="pilcrow" href="#section-1">&#182;</a>
43
+ </div>
44
+ <p>The built-in XML/JSON support of Rails is great but:
45
+ You surely don’t want to expose your models always with all attributes.</p>
46
+
47
+ <p>acts_as_api enriches the models and controllers of your app in a rails-like way so you can easily determine how your API responses should look like.</p>
48
+ </td>
49
+ <td class=code>
50
+ <div class='highlight'><pre></pre></div>
51
+ </td>
52
+ </tr>
53
+ <tr id='section-Features'>
54
+ <td class=docs>
55
+ <div class="pilwrap">
56
+ <a class="pilcrow" href="#section-Features">&#182;</a>
57
+ </div>
58
+ <h2>Features</h2>
59
+
60
+ <ul>
61
+ <li>DRY templates for your api responses</li>
62
+ <li>Ships with support for <strong>ActiveRecord</strong> and <strong>Mongoid</strong></li>
63
+ <li>Support for Rails 3 Responders</li>
64
+ <li>Plays very well together with client libs like <a href="http://documentcloud.github.com/backbone">Backbone.js</a> or <a href="http://restkit.org">RestKit</a> (iOS).</li>
65
+ <li>Easy but very flexible syntax for defining the templates</li>
66
+ <li>XML, JSON and JSON-P support out of the box, easy to extend</li>
67
+ <li>Support for meta data like pagination info, etc&hellip;</li>
68
+ <li>Minimal dependecies (you can also use it without Rails)</li>
69
+ <li>Supports multiple api rendering templates for a models. This is especially useful for API versioning or for example for private vs. public access points to a user’s profile.</li>
70
+ </ul>
71
+
72
+
73
+ <hr />
74
+ </td>
75
+ <td class=code>
76
+ <div class='highlight'><pre></pre></div>
77
+ </td>
78
+ </tr>
79
+ <tr id='section-Rails_3.x_Quickstart'>
80
+ <td class=docs>
81
+ <div class="pilwrap">
82
+ <a class="pilcrow" href="#section-Rails_3.x_Quickstart">&#182;</a>
83
+ </div>
84
+ <h2>Rails 3.x Quickstart</h2>
85
+ </td>
86
+ <td class=code>
87
+ <div class='highlight'><pre></pre></div>
88
+ </td>
89
+ </tr>
90
+ <tr id='section-4'>
91
+ <td class=docs>
92
+ <div class="pilwrap">
93
+ <a class="pilcrow" href="#section-4">&#182;</a>
94
+ </div>
95
+ <p>Add to gemfile</p>
96
+ </td>
97
+ <td class=code>
98
+ <div class='highlight'><pre><span class="n">gem</span> <span class="s1">&#39;acts_as_api&#39;</span></pre></div>
99
+ </td>
100
+ </tr>
101
+ <tr id='section-5'>
102
+ <td class=docs>
103
+ <div class="pilwrap">
104
+ <a class="pilcrow" href="#section-5">&#182;</a>
105
+ </div>
106
+ <p>Update your bundle</p>
107
+ </td>
108
+ <td class=code>
109
+ <div class='highlight'><pre><span class="n">bundle</span> <span class="n">install</span></pre></div>
110
+ </td>
111
+ </tr>
112
+ <tr id='section-Setting_up_your_Model'>
113
+ <td class=docs>
114
+ <div class="pilwrap">
115
+ <a class="pilcrow" href="#section-Setting_up_your_Model">&#182;</a>
116
+ </div>
117
+ <h3>Setting up your Model</h3>
118
+ </td>
119
+ <td class=code>
120
+ <div class='highlight'><pre></pre></div>
121
+ </td>
122
+ </tr>
123
+ <tr id='section-7'>
124
+ <td class=docs>
125
+ <div class="pilwrap">
126
+ <a class="pilcrow" href="#section-7">&#182;</a>
127
+ </div>
128
+ <p>Given you have a model <code>User</code>.
129
+ If you only want to expose the <code>first_name</code> and <code>last_name</code> attribute of a user via your api, you would do something like this:</p>
130
+ </td>
131
+ <td class=code>
132
+ <div class='highlight'><pre></pre></div>
133
+ </td>
134
+ </tr>
135
+ <tr id='section-8'>
136
+ <td class=docs>
137
+ <div class="pilwrap">
138
+ <a class="pilcrow" href="#section-8">&#182;</a>
139
+ </div>
140
+ <p>Within your model:</p>
141
+
142
+ <p>First you activate acts_as_api for your model by calling <code>acts_as_api</code>.</p>
143
+
144
+ <p>Then you define an api template to render the model with <code>api_accessible</code>.</p>
145
+ </td>
146
+ <td class=code>
147
+ <div class='highlight'><pre><span class="k">class</span> <span class="nc">User</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span>
148
+
149
+ <span class="n">acts_as_api</span>
150
+
151
+ <span class="n">api_accessible</span> <span class="ss">:name_only</span> <span class="k">do</span> <span class="o">|</span><span class="n">template</span><span class="o">|</span>
152
+ <span class="n">template</span><span class="o">.</span><span class="n">add</span> <span class="ss">:first_name</span>
153
+ <span class="n">template</span><span class="o">.</span><span class="n">add</span> <span class="ss">:last_name</span>
154
+ <span class="k">end</span>
155
+
156
+ <span class="k">end</span></pre></div>
157
+ </td>
158
+ </tr>
159
+ <tr id='section-9'>
160
+ <td class=docs>
161
+ <div class="pilwrap">
162
+ <a class="pilcrow" href="#section-9">&#182;</a>
163
+ </div>
164
+ <p>An API template with the name <code>:name_only</code> was created.</p>
165
+
166
+ <p>See below how to use it in the controller:</p>
167
+ </td>
168
+ <td class=code>
169
+ <div class='highlight'><pre></pre></div>
170
+ </td>
171
+ </tr>
172
+ <tr id='section-Setting_up_your_Controller'>
173
+ <td class=docs>
174
+ <div class="pilwrap">
175
+ <a class="pilcrow" href="#section-Setting_up_your_Controller">&#182;</a>
176
+ </div>
177
+ <h3>Setting up your Controller</h3>
178
+ </td>
179
+ <td class=code>
180
+ <div class='highlight'><pre></pre></div>
181
+ </td>
182
+ </tr>
183
+ <tr id='section-11'>
184
+ <td class=docs>
185
+ <div class="pilwrap">
186
+ <a class="pilcrow" href="#section-11">&#182;</a>
187
+ </div>
188
+ <p>Now you just have to exchange the <code>render</code> method in your controller for the <code>render_for_api</code> method.</p>
189
+ </td>
190
+ <td class=code>
191
+ <div class='highlight'><pre><span class="k">class</span> <span class="nc">UsersController</span> <span class="o">&lt;</span> <span class="no">ApplicationController</span>
192
+
193
+ <span class="k">def</span> <span class="nf">index</span>
194
+ <span class="vi">@users</span> <span class="o">=</span> <span class="no">User</span><span class="o">.</span><span class="n">all</span></pre></div>
195
+ </td>
196
+ </tr>
197
+ <tr id='section-12'>
198
+ <td class=docs>
199
+ <div class="pilwrap">
200
+ <a class="pilcrow" href="#section-12">&#182;</a>
201
+ </div>
202
+ <p>Note that it&rsquo;s wise to add a <code>root</code> param when rendering lists.</p>
203
+ </td>
204
+ <td class=code>
205
+ <div class='highlight'><pre> <span class="n">respond_to</span> <span class="k">do</span> <span class="o">|</span><span class="nb">format</span><span class="o">|</span>
206
+ <span class="nb">format</span><span class="o">.</span><span class="n">xml</span> <span class="p">{</span> <span class="n">render_for_api</span> <span class="ss">:name_only</span><span class="p">,</span> <span class="ss">:xml</span> <span class="o">=&gt;</span> <span class="vi">@users</span><span class="p">,</span> <span class="ss">:root</span> <span class="o">=&gt;</span> <span class="ss">:users</span> <span class="p">}</span>
207
+ <span class="nb">format</span><span class="o">.</span><span class="n">json</span> <span class="p">{</span> <span class="n">render_for_api</span> <span class="ss">:name_only</span><span class="p">,</span> <span class="ss">:json</span> <span class="o">=&gt;</span> <span class="vi">@users</span><span class="p">,</span> <span class="ss">:root</span> <span class="o">=&gt;</span> <span class="ss">:users</span> <span class="p">}</span>
208
+ <span class="k">end</span>
209
+ <span class="k">end</span>
210
+
211
+ <span class="k">def</span> <span class="nf">show</span>
212
+ <span class="vi">@user</span> <span class="o">=</span> <span class="no">User</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">params</span><span class="o">[</span><span class="ss">:id</span><span class="o">]</span><span class="p">)</span>
213
+
214
+ <span class="n">respond_to</span> <span class="k">do</span> <span class="o">|</span><span class="nb">format</span><span class="o">|</span>
215
+ <span class="nb">format</span><span class="o">.</span><span class="n">xml</span> <span class="p">{</span> <span class="n">render_for_api</span> <span class="ss">:name_only</span><span class="p">,</span> <span class="ss">:xml</span> <span class="o">=&gt;</span> <span class="vi">@user</span> <span class="p">}</span>
216
+ <span class="nb">format</span><span class="o">.</span><span class="n">json</span> <span class="p">{</span> <span class="n">render_for_api</span> <span class="ss">:name_only</span><span class="p">,</span> <span class="ss">:json</span> <span class="o">=&gt;</span> <span class="vi">@user</span> <span class="p">}</span>
217
+ <span class="k">end</span>
218
+ <span class="k">end</span>
219
+
220
+ <span class="k">end</span></pre></div>
221
+ </td>
222
+ </tr>
223
+ <tr id='section-That&amp;rsquo;s_it!'>
224
+ <td class=docs>
225
+ <div class="pilwrap">
226
+ <a class="pilcrow" href="#section-That&amp;rsquo;s_it!">&#182;</a>
227
+ </div>
228
+ <h3>That&rsquo;s it!</h3>
229
+ </td>
230
+ <td class=code>
231
+ <div class='highlight'><pre></pre></div>
232
+ </td>
233
+ </tr>
234
+ <tr id='section-14'>
235
+ <td class=docs>
236
+ <div class="pilwrap">
237
+ <a class="pilcrow" href="#section-14">&#182;</a>
238
+ </div>
239
+ <p>Try it. The JSON response of #show should now look like this:</p>
240
+
241
+ <p>Other attributes of the model like <code>created_at</code> or <code>updated_at</code> won’t be included
242
+ because they were not listed by <code>api_accessible</code> in the model.</p>
243
+ </td>
244
+ <td class=code>
245
+ <div class='highlight'><pre><span class="p">{</span>
246
+ <span class="s2">&quot;user&quot;</span><span class="p">:</span> <span class="p">{</span>
247
+ <span class="s2">&quot;first_name&quot;</span><span class="p">:</span> <span class="s2">&quot;John&quot;</span><span class="p">,</span>
248
+ <span class="s2">&quot;last_name&quot;</span><span class="p">:</span> <span class="s2">&quot;Doe&quot;</span>
249
+ <span class="p">}</span>
250
+ <span class="p">}</span></pre></div>
251
+ </td>
252
+ </tr>
253
+ <tr id='section-15'>
254
+ <td class=docs>
255
+ <div class="pilwrap">
256
+ <a class="pilcrow" href="#section-15">&#182;</a>
257
+ </div>
258
+ <hr />
259
+ </td>
260
+ <td class=code>
261
+ <div class='highlight'><pre></pre></div>
262
+ </td>
263
+ </tr>
264
+ <tr id='section-But_wait!_&amp;hellip;_there&amp;rsquo;s_more'>
265
+ <td class=docs>
266
+ <div class="pilwrap">
267
+ <a class="pilcrow" href="#section-But_wait!_&amp;hellip;_there&amp;rsquo;s_more">&#182;</a>
268
+ </div>
269
+ <h2>But wait! &hellip; there&rsquo;s more</h2>
270
+
271
+ <p>Often the pure rendering of database values is just not enough, so acts_as_api
272
+ provides you some tools to customize your API responses.</p>
273
+ </td>
274
+ <td class=code>
275
+ <div class='highlight'><pre></pre></div>
276
+ </td>
277
+ </tr>
278
+ <tr id='section-What_can_I_include_in_my_responses?'>
279
+ <td class=docs>
280
+ <div class="pilwrap">
281
+ <a class="pilcrow" href="#section-What_can_I_include_in_my_responses?">&#182;</a>
282
+ </div>
283
+ <h3>What can I include in my responses?</h3>
284
+
285
+ <p>You can do basically anything:</p>
286
+
287
+ <ul>
288
+ <li><a href="https://github.com/fabrik42/acts_as_api/wiki/Calling-a-method-of-the-model">Include attributes and all other kinds of methods of your model</a></li>
289
+ <li><a href="https://github.com/fabrik42/acts_as_api/wiki/Including-a-child-association">Include child associations (if they also act_as_api this will be considered)</a></li>
290
+ <li><a href="https://github.com/fabrik42/acts_as_api/wiki/Calling-a-lambda-in-the-api-template">Include lambdas and Procs</a></li>
291
+ <li><a href="https://github.com/fabrik42/acts_as_api/wiki/Calling-a-method-of-the-model">Call methods of a parent association</a></li>
292
+ <li><a href="https://github.com/fabrik42/acts_as_api/wiki/Calling-a-scope-of-a-sub-resource">Call scopes of your model or child associations</a></li>
293
+ <li><a href="https://github.com/fabrik42/acts_as_api/wiki/Renaming-an-attribute">Rename attributes, methods, associations</a></li>
294
+ <li><a href="https://github.com/fabrik42/acts_as_api/wiki/Creating-a-completely-different-response-structure">Create your own hierarchies</a></li>
295
+ </ul>
296
+
297
+
298
+ <p>You can find more advanced examples in the <a href="https://github.com/fabrik42/acts_as_api/wiki/">Github Wiki</a></p>
299
+ </td>
300
+ <td class=code>
301
+ <div class='highlight'><pre></pre></div>
302
+ </td>
303
+ </tr>
304
+ <tr id='section-18'>
305
+ <td class=docs>
306
+ <div class="pilwrap">
307
+ <a class="pilcrow" href="#section-18">&#182;</a>
308
+ </div>
309
+ <hr />
310
+ </td>
311
+ <td class=code>
312
+ <div class='highlight'><pre></pre></div>
313
+ </td>
314
+ </tr>
315
+ <tr id='section-Links'>
316
+ <td class=docs>
317
+ <div class="pilwrap">
318
+ <a class="pilcrow" href="#section-Links">&#182;</a>
319
+ </div>
320
+ <h2>Links</h2>
321
+
322
+ <ul>
323
+ <li>Check out the <a href="https://github.com/fabrik42/acts_as_api/">source code on Github</a></li>
324
+ <li>For more usage examples visit the <a href="https://github.com/fabrik42/acts_as_api/wiki/">wiki</a></li>
325
+ <li>Found a bug or do you have a feature request? <a href="https://github.com/fabrik42/acts_as_api/issues">issue tracker</a></li>
326
+ <li><a href="http://rdoc.info/github/fabrik42/acts_as_api">Docs</a></li>
327
+ </ul>
328
+
329
+
330
+ </td>
331
+ <td class=code>
332
+ <div class='highlight'><pre></pre></div>
333
+ </td>
334
+ </tr>
335
+ </table>
336
+ </div>
337
+ <a href="https://github.com/fabrik42/acts_as_api"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://assets1.github.com/img/7afbc8b248c68eb468279e8c17986ad46549fb71?repo=&url=http%3A%2F%2Fs3.amazonaws.com%2Fgithub%2Fribbons%2Fforkme_right_darkblue_121621.png&path=" alt="Fork me on GitHub"></a>
338
+ </body>
@@ -0,0 +1,144 @@
1
+ # The built-in XML/JSON support of Rails is great but:
2
+ # You surely don’t want to expose your models always with all attributes.
3
+ #
4
+ # acts_as_api enriches the models and controllers of your app in a rails-like way so you can easily determine how your API responses should look like.
5
+
6
+ ### Features
7
+ # * DRY templates for your api responses
8
+ # * Ships with support for **ActiveRecord** and **Mongoid**
9
+ # * Support for Rails 3 Responders
10
+ # * Plays very well together with client libs like [Backbone.js][b1] or [RestKit][r1] (iOS).
11
+ # * Easy but very flexible syntax for defining the templates
12
+ # * XML, JSON and JSON-P support out of the box, easy to extend
13
+ # * Support for meta data like pagination info, etc...
14
+ # * Minimal dependecies (you can also use it without Rails)
15
+ # * Supports multiple api rendering templates for a models. This is especially useful for API versioning or for example for private vs. public access points to a user’s profile.
16
+ # [b1]: http://documentcloud.github.com/backbone
17
+ # [r1]: http://restkit.org
18
+ # ***
19
+
20
+
21
+ ### Rails 3.x Quickstart
22
+
23
+ # Add to gemfile
24
+ gem 'acts_as_api'
25
+
26
+ # Update your bundle
27
+ bundle install
28
+
29
+ #### Setting up your Model
30
+
31
+ # Given you have a model `User`.
32
+ # If you only want to expose the `first_name` and `last_name` attribute of a user via your api, you would do something like this:
33
+
34
+ # Within your model:
35
+ #
36
+ # First you activate acts_as_api for your model by calling `acts_as_api`.
37
+ #
38
+ # Then you define an api template to render the model with `api_accessible`.
39
+ class User < ActiveRecord::Base
40
+
41
+ acts_as_api
42
+
43
+ api_accessible :name_only do |template|
44
+ template.add :first_name
45
+ template.add :last_name
46
+ end
47
+
48
+ end
49
+
50
+ # An API template with the name `:name_only` was created.
51
+ #
52
+ # See below how to use it in the controller:
53
+
54
+ #### Setting up your Controller
55
+
56
+
57
+
58
+ # Now you just have to exchange the `render` method in your controller for the `render_for_api` method.
59
+ class UsersController < ApplicationController
60
+
61
+ def index
62
+ @users = User.all
63
+ # Note that it's wise to add a `root` param when rendering lists.
64
+ respond_to do |format|
65
+ format.xml { render_for_api :name_only, :xml => @users, :root => :users }
66
+ format.json { render_for_api :name_only, :json => @users, :root => :users }
67
+ end
68
+ end
69
+
70
+ def show
71
+ @user = User.find(params[:id])
72
+
73
+ respond_to do |format|
74
+ format.xml { render_for_api :name_only, :xml => @user }
75
+ format.json { render_for_api :name_only, :json => @user }
76
+ end
77
+ end
78
+
79
+ end
80
+
81
+ #### That's it!
82
+
83
+ # Try it. The JSON response of #show should now look like this:
84
+ #
85
+ # Other attributes of the model like `created_at` or `updated_at` won’t be included
86
+ # because they were not listed by `api_accessible` in the model.
87
+ {
88
+ "user": {
89
+ "first_name": "John",
90
+ "last_name": "Doe"
91
+ }
92
+ }
93
+
94
+ # ***
95
+
96
+ ### But wait! ... there's more
97
+ #
98
+ # Often the pure rendering of database values is just not enough, so acts_as_api
99
+ # provides you some tools to customize your API responses.
100
+
101
+ #### What can I include in my responses?
102
+ #
103
+ # You can do basically anything:
104
+ #
105
+ # * [Include attributes and all other kinds of methods of your model][w1]
106
+ # * [Include child associations (if they also act_as_api this will be considered)][w2]
107
+ # * [Include lambdas and Procs][w3]
108
+ # * [Call methods of a parent association][w4]
109
+ # * [Call scopes of your model or child associations][w5]
110
+ # * [Rename attributes, methods, associations][w6]
111
+ # * [Create your own hierarchies][w7]
112
+ #
113
+ # You can find more advanced examples in the [Github Wiki][wi]
114
+ #
115
+ # [wi]: https://github.com/fabrik42/acts_as_api/wiki/
116
+ # [w1]: https://github.com/fabrik42/acts_as_api/wiki/Calling-a-method-of-the-model
117
+ # [w2]: https://github.com/fabrik42/acts_as_api/wiki/Including-a-child-association
118
+ # [w3]: https://github.com/fabrik42/acts_as_api/wiki/Calling-a-lambda-in-the-api-template
119
+ # [w4]: https://github.com/fabrik42/acts_as_api/wiki/Calling-a-method-of-the-model
120
+ # [w5]: https://github.com/fabrik42/acts_as_api/wiki/Calling-a-scope-of-a-sub-resource
121
+ # [w6]: https://github.com/fabrik42/acts_as_api/wiki/Renaming-an-attribute
122
+ # [w7]: https://github.com/fabrik42/acts_as_api/wiki/Creating-a-completely-different-response-structure
123
+
124
+ # ***
125
+
126
+ ### Links
127
+ # * Check out the [source code on Github][so]
128
+ # * For more usage examples visit the [wiki][wi]
129
+ # * Found a bug or do you have a feature request? [issue tracker][to]
130
+ # * [Docs][do]
131
+ #
132
+ # [so]: https://github.com/fabrik42/acts_as_api/
133
+ # [wi]: https://github.com/fabrik42/acts_as_api/wiki/
134
+ # [to]: https://github.com/fabrik42/acts_as_api/issues
135
+ # [do]: http://rdoc.info/github/fabrik42/acts_as_api
136
+
137
+
138
+
139
+
140
+
141
+
142
+
143
+
144
+