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.
Files changed (54) hide show
  1. data/Gemfile +1 -1
  2. data/History.txt +5 -0
  3. data/README.rdoc +9 -2
  4. data/Rakefile +15 -1
  5. data/acts_as_api.gemspec +1 -0
  6. data/examples/introduction/index.html +7 -6
  7. data/examples/introduction/index.rb +9 -8
  8. data/lib/acts_as_api.rb +12 -5
  9. data/lib/acts_as_api/adapters.rb +5 -0
  10. data/lib/acts_as_api/adapters/mongoid.rb +11 -0
  11. data/lib/acts_as_api/array.rb +4 -8
  12. data/lib/acts_as_api/responder.rb +40 -0
  13. data/lib/acts_as_api/version.rb +1 -1
  14. data/spec/controllers/respond_with_users_controller_spec.rb +30 -2
  15. data/spec/controllers/users_controller_spec.rb +17 -235
  16. data/spec/models/active_record_spec.rb +28 -0
  17. data/spec/models/mongoid_spec.rb +28 -0
  18. data/spec/rails_app/app/controllers/respond_with_users_controller.rb +19 -8
  19. data/spec/rails_app/app/controllers/users_controller.rb +13 -4
  20. data/spec/rails_app/app/models/mongo_profile.rb +10 -0
  21. data/spec/rails_app/app/models/mongo_task.rb +12 -0
  22. data/spec/rails_app/app/models/mongo_untouched.rb +7 -0
  23. data/spec/rails_app/app/models/mongo_user.rb +149 -0
  24. data/spec/rails_app/app/models/user.rb +6 -6
  25. data/spec/rails_app/config/initializers/acts_as_api_mongoid.rb +6 -0
  26. data/spec/rails_app/config/mongoid.yml +23 -0
  27. data/spec/support/controller_examples.rb +246 -0
  28. data/spec/support/it_supports.rb +3 -0
  29. data/spec/support/model_examples/associations.rb +272 -0
  30. data/spec/support/model_examples/closures.rb +49 -0
  31. data/spec/support/model_examples/conditional_if.rb +165 -0
  32. data/spec/support/model_examples/conditional_unless.rb +165 -0
  33. data/spec/support/model_examples/enabled.rb +10 -0
  34. data/spec/support/model_examples/extending.rb +112 -0
  35. data/spec/support/model_examples/methods.rb +23 -0
  36. data/spec/support/model_examples/renaming.rb +50 -0
  37. data/spec/support/model_examples/simple.rb +23 -0
  38. data/spec/support/model_examples/sub_nodes.rb +105 -0
  39. data/spec/support/model_examples/undefined.rb +7 -0
  40. data/spec/support/model_examples/untouched.rb +13 -0
  41. data/spec/support/simple_fixtures.rb +39 -8
  42. metadata +67 -28
  43. data/spec/models/base/associations_spec.rb +0 -284
  44. data/spec/models/base/closures_spec.rb +0 -62
  45. data/spec/models/base/conditional_if_spec.rb +0 -178
  46. data/spec/models/base/conditional_unless_spec.rb +0 -178
  47. data/spec/models/base/enabled_spec.rb +0 -15
  48. data/spec/models/base/extending_spec.rb +0 -125
  49. data/spec/models/base/methods_spec.rb +0 -33
  50. data/spec/models/base/renaming_spec.rb +0 -63
  51. data/spec/models/base/simple_spec.rb +0 -33
  52. data/spec/models/base/sub_nodes_spec.rb +0 -118
  53. data/spec/models/base/undefined_spec.rb +0 -20
  54. data/spec/models/base/untouched_spec.rb +0 -18
data/Gemfile CHANGED
@@ -3,9 +3,9 @@ source "http://rubygems.org"
3
3
  # Specify your gem's dependencies in acts_as_api.gemspec
4
4
  gemspec
5
5
 
6
-
7
6
  group :test do
8
7
  gem 'sqlite3-ruby'
8
+ gem 'mongoid', '>= 2.0.0'
9
9
  gem 'rspec-rails', '>= 2.4.1'
10
10
  gem 'webrat'
11
11
  gem 'rocco', :git => 'git://github.com/fabrik42/rocco.git'
data/History.txt CHANGED
@@ -1,3 +1,8 @@
1
+ === 0.3.6 2011-05-13
2
+
3
+ * Finally added Mongoid Support out of the box
4
+ * Support for Rails 3 Responders
5
+
1
6
  === 0.3.4 2011-04-26
2
7
 
3
8
  * Fixed a bug concerning the inheritance of sub templates/additional options
data/README.rdoc CHANGED
@@ -4,7 +4,7 @@ acts_as_api makes creating XML/JSON responses in Rails 3 easy and fun.
4
4
 
5
5
  It provides a simple interface to determine the representation of your model data, that should be rendered in your API responses.
6
6
 
7
- In addition to Rails it theoretically can be used with any ruby app and any database as it only has few dependencies.
7
+ In addition to Rails it theoretically can be used with any ruby app and any database (*ActiveRecord* and *Mongoid* are supported out of the box) as it only has few dependencies.
8
8
 
9
9
  == Introduction
10
10
 
@@ -44,8 +44,9 @@ https://github.com/fabrik42/acts_as_api/wiki
44
44
  * DRY templates for your api responses
45
45
  * Easy but very flexible syntax for defining the templates
46
46
  * XML, JSON and JSON-P support out of the box, easy to extend
47
+ * Support for Rails 3 Responders
47
48
  * Minimal dependecies (you can also use it without Rails)
48
- * Does not rely on ActiveRecord (can be used with other ORMs like Mongoid)
49
+ * Ships with support for *ActiveRecord* and *Mongoid*
49
50
  * Supports multiple api rendering templates per model. This is especially useful for API versioning or for example for private vs. public access points to a user’s profile.
50
51
 
51
52
  === Requirements:
@@ -64,6 +65,12 @@ https://github.com/fabrik42/acts_as_api/wiki
64
65
 
65
66
  * Wiki: https://github.com/fabrik42/acts_as_api/wiki
66
67
 
68
+ === Tested with:
69
+
70
+ * MRI 1.9.2-p180
71
+ * MRI 1.8.7-p334
72
+ * But it just should work fine with other versions too... :)
73
+
67
74
  === Downwards Compatibility
68
75
 
69
76
  Note that upgrading to 0.3.0 will break code that worked with previous versions due to a complete overhaul of the lib.
data/Rakefile CHANGED
@@ -7,14 +7,28 @@ Bundler::GemHelper.install_tasks
7
7
 
8
8
  RSpec::Core::RakeTask.new
9
9
 
10
+ namespace :spec do
11
+
12
+ desc "Run ActiveRecord RSpec code examples only"
13
+ RSpec::Core::RakeTask.new do |t|
14
+ t.name = :active_record
15
+ t.rspec_opts = ["--color", "--format", "documentation", "--tag", "orm:active_record"]
16
+ end
17
+
18
+ end
19
+
20
+
21
+
22
+
10
23
  gemspec = Gem::Specification.load("acts_as_api.gemspec")
11
24
 
12
25
  Rake::RDocTask.new do |rdoc|
13
26
  rdoc.rdoc_dir = 'doc'
14
27
  rdoc.title = "#{gemspec.name} #{gemspec.version}"
15
28
  rdoc.options += gemspec.rdoc_options
29
+ rdoc.options << '--line-numbers' << '--inline-source'
16
30
  rdoc.rdoc_files.include(gemspec.extra_rdoc_files)
17
- rdoc.rdoc_files.include('lib/**/*.rb')
31
+ rdoc.rdoc_files.include('README.rdoc')
18
32
  end
19
33
 
20
34
  #bundle exec rocco examples/introduction/index.rb -t examples/introduction/layout.mustache
data/acts_as_api.gemspec CHANGED
@@ -17,6 +17,7 @@ Gem::Specification.new do |s|
17
17
  s.add_dependency('rack','>= 1.1.0')
18
18
 
19
19
  s.add_development_dependency('rails', ['>= 3.0.0'])
20
+ s.add_development_dependency('mongoid', ['>= 2.0.0'])
20
21
 
21
22
  s.has_rdoc = true
22
23
  s.rdoc_options = ['--main', 'README.rdoc', '--charset=UTF-8']
@@ -58,8 +58,9 @@ You surely don’t want to expose your models always with all attributes.</p>
58
58
  <li>DRY templates for your api responses</li>
59
59
  <li>Easy but very flexible syntax for defining the templates</li>
60
60
  <li>XML, JSON and JSON-P support out of the box, easy to extend</li>
61
+ <li>Support for Rails 3 Responders</li>
61
62
  <li>Minimal dependecies (you can also use it without Rails)</li>
62
- <li>Does not rely on ActiveRecord (can be used with other ORMs like Mongoid)</li>
63
+ <li>Ships with support for <strong>ActiveRecord</strong> and <strong>Mongoid</strong></li>
63
64
  <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>
64
65
  </ul>
65
66
  </td>
@@ -191,7 +192,7 @@ If you only want to expose the <code>first_name</code> and <code>last_name</code
191
192
  </td>
192
193
  <td class=code>
193
194
  <div class='highlight'><pre><span class="k">class</span> <span class="nc">UsersController</span> <span class="o">&lt;</span> <span class="no">ApplicationController</span>
194
-
195
+
195
196
  <span class="k">def</span> <span class="nf">index</span>
196
197
  <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>
197
198
  </td>
@@ -209,16 +210,16 @@ If you only want to expose the <code>first_name</code> and <code>last_name</code
209
210
  <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>
210
211
  <span class="k">end</span>
211
212
  <span class="k">end</span>
212
-
213
+
213
214
  <span class="k">def</span> <span class="nf">show</span>
214
215
  <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>
215
-
216
+
216
217
  <span class="n">respond_to</span> <span class="k">do</span> <span class="o">|</span><span class="nb">format</span><span class="o">|</span>
217
218
  <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>
218
219
  <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>
219
220
  <span class="k">end</span>
220
221
  <span class="k">end</span>
221
-
222
+
222
223
  <span class="k">end</span></pre></div>
223
224
  </td>
224
225
  </tr>
@@ -247,7 +248,7 @@ because they were not listed by <code>api_accessible</code> in the model.</p>
247
248
  <div class='highlight'><pre><span class="p">{</span>
248
249
  <span class="s2">&quot;user&quot;</span><span class="p">:</span> <span class="p">{</span>
249
250
  <span class="s2">&quot;first_name&quot;</span><span class="p">:</span> <span class="s2">&quot;John&quot;</span><span class="p">,</span>
250
- <span class="s2">&quot;last_name&quot;</span><span class="p">:</span> <span class="s2">&quot;Doe&quot;</span>
251
+ <span class="s2">&quot;last_name&quot;</span><span class="p">:</span> <span class="s2">&quot;Doe&quot;</span>
251
252
  <span class="p">}</span>
252
253
  <span class="p">}</span></pre></div>
253
254
  </td>
@@ -7,8 +7,9 @@
7
7
  # * DRY templates for your api responses
8
8
  # * Easy but very flexible syntax for defining the templates
9
9
  # * XML, JSON and JSON-P support out of the box, easy to extend
10
+ # * Support for Rails 3 Responders
10
11
  # * Minimal dependecies (you can also use it without Rails)
11
- # * Does not rely on ActiveRecord (can be used with other ORMs like Mongoid)
12
+ # * Ships with support for **ActiveRecord** and **Mongoid**
12
13
  # * 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.
13
14
 
14
15
  # ***
@@ -52,7 +53,7 @@ end
52
53
 
53
54
  # Now you just have to exchange the `render` method in your controller for the `render_for_api` method.
54
55
  class UsersController < ApplicationController
55
-
56
+
56
57
  def index
57
58
  @users = User.all
58
59
  # Note that it's wise to add a `root` param when rendering lists.
@@ -61,28 +62,28 @@ class UsersController < ApplicationController
61
62
  format.json { render_for_api :name_only, :json => @users, :root => :users }
62
63
  end
63
64
  end
64
-
65
+
65
66
  def show
66
67
  @user = User.find(params[:id])
67
-
68
+
68
69
  respond_to do |format|
69
70
  format.xml { render_for_api :name_only, :xml => @user }
70
71
  format.json { render_for_api :name_only, :json => @user }
71
72
  end
72
73
  end
73
-
74
+
74
75
  end
75
76
 
76
77
  #### That's it!
77
78
 
78
79
  # Try it. The JSON response of #show should now look like this:
79
80
  #
80
- # Other attributes of the model like `created_at` or `updated_at` won’t be included
81
+ # Other attributes of the model like `created_at` or `updated_at` won’t be included
81
82
  # because they were not listed by `api_accessible` in the model.
82
83
  {
83
84
  "user": {
84
85
  "first_name": "John",
85
- "last_name": "Doe"
86
+ "last_name": "Doe"
86
87
  }
87
88
  }
88
89
 
@@ -96,7 +97,7 @@ end
96
97
  #### What can I include in my responses?
97
98
  #
98
99
  # You can do basically anything:
99
- #
100
+ #
100
101
  # * [Include attributes and all other kinds of methods of your model][w1]
101
102
  # * [Include child associations (if they also act_as_api this will be considered)][w2]
102
103
  # * [Include lambdas and Procs][w3]
data/lib/acts_as_api.rb CHANGED
@@ -3,7 +3,7 @@ require 'active_support/core_ext/class'
3
3
 
4
4
  $:.unshift(File.dirname(__FILE__)) unless
5
5
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
6
-
6
+
7
7
  require "acts_as_api/array"
8
8
  require "acts_as_api/rails_renderer"
9
9
  require "acts_as_api/exceptions"
@@ -20,16 +20,23 @@ module ActsAsApi
20
20
  autoload :Config, "acts_as_api/config"
21
21
  autoload :ApiTemplate, "acts_as_api/api_template"
22
22
  autoload :Base, "acts_as_api/base"
23
- autoload :Rendering, "acts_as_api/rendering"
23
+ autoload :Rendering, "acts_as_api/rendering"
24
+ autoload :Responder, "acts_as_api/responder"
25
+ autoload :Adapters, "acts_as_api/adapters"
24
26
  end
25
27
 
26
- # Attach ourselves to active record
28
+ # Attach ourselves to ActiveRecord
27
29
  if defined?(ActiveRecord::Base)
28
30
  ActiveRecord::Base.extend ActsAsApi::Base
29
31
  end
30
32
 
31
- # Attach ourselves to the action controller of rails
33
+ # Attach ourselves to Mongoid
34
+ if defined?(Mongoid::Document)
35
+ Mongoid::Document.send :include, ActsAsApi::Adapters::Mongoid
36
+ end
37
+
38
+ # Attach ourselves to the action controller of Rails
32
39
  if defined?(ActionController::Base)
33
40
  ActionController::Base.send :include, ActsAsApi::Rendering
34
41
  ActsAsApi::RailsRenderer.setup
35
- end
42
+ end
@@ -0,0 +1,5 @@
1
+ module ActsAsApi
2
+ module Adapters
3
+ autoload :Mongoid, "acts_as_api/adapters/mongoid"
4
+ end
5
+ end
@@ -0,0 +1,11 @@
1
+ module ActsAsApi
2
+ module Adapters
3
+ module Mongoid
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ extend ActsAsApi::Base
8
+ end
9
+ end
10
+ end
11
+ end
@@ -8,18 +8,14 @@ class Array
8
8
  # If they don't, the item itself will be collected.
9
9
  def as_api_response(api_template)
10
10
 
11
- sub_items = []
12
-
13
- each do |item|
11
+ collect do |item|
14
12
  if item.respond_to?(:as_api_response)
15
- sub_items << item.as_api_response(api_template)
13
+ item.as_api_response(api_template)
16
14
  else
17
- sub_items << item
15
+ item
18
16
  end
19
17
  end
20
18
 
21
- sub_items
22
-
23
19
  end
24
20
 
25
- end
21
+ end
@@ -0,0 +1,40 @@
1
+ module ActsAsApi
2
+ # A custom Rails responder class to automatically use render_for_api in your
3
+ # controller actions.
4
+ #
5
+ # Example:
6
+ #
7
+ # class UsersController < ApplicationController
8
+ # # Set this controller to use our custom responder
9
+ # # (This could be done in a base controller class, if desired)
10
+ # self.responder = ActsAsApi::Responder
11
+ #
12
+ # respond_to :json, :xml
13
+ #
14
+ # def index
15
+ # @users = User.all
16
+ # respond_with @users, :api_template => :name_only
17
+ # end
18
+ # end
19
+ #
20
+ # The `:api_template` parameter is required so the responder knows which api template it should render.
21
+
22
+ class Responder < ActionController::Responder
23
+
24
+ # Should be specified as an option to the `respond_with` call
25
+ attr_reader :api_template
26
+
27
+ # Grabs the required :api_template parameter, then hands control back to
28
+ # the base ActionController::Responder initializer.
29
+ def initialize(controller, resources, options={})
30
+ @api_template = options.delete(:api_template)
31
+ super(controller, resources, options)
32
+ end
33
+
34
+ # Overrides the base implementation of respond, replacing it with
35
+ # the render_for_api method.
36
+ def respond
37
+ controller.render_for_api api_template, options.merge!(format => resource)
38
+ end
39
+ end
40
+ end
@@ -1,3 +1,3 @@
1
1
  module ActsAsApi
2
- VERSION = "0.3.5"
2
+ VERSION = "0.3.6"
3
3
  end
@@ -1,5 +1,33 @@
1
- require File.dirname(__FILE__) + '/../spec_helper.rb'
1
+ require 'spec_helper'
2
2
 
3
3
  describe RespondWithUsersController do
4
- pending "we need to fix the root node for json first"
4
+
5
+ context "using active record", :orm => :active_record do
6
+
7
+ before(:each) do
8
+ setup_active_record_models
9
+ end
10
+
11
+ after(:each) do
12
+ clean_up_active_record_models
13
+ end
14
+
15
+ # see spec/support/controller_examples.rb
16
+ it_behaves_like "a controller with ActsAsApi responses"
17
+ end
18
+
19
+ context "using mongoid", :orm => :mongoid do
20
+
21
+ before(:each) do
22
+ setup_mongoid_models
23
+ end
24
+
25
+ after(:each) do
26
+ clean_up_mongoid_models
27
+ end
28
+
29
+ # see spec/support/controller_examples.rb
30
+ it_behaves_like "a controller with ActsAsApi responses"
31
+ end
32
+
5
33
  end
@@ -1,250 +1,32 @@
1
- require File.dirname(__FILE__) + '/../spec_helper.rb'
1
+ require 'spec_helper'
2
2
 
3
- describe UsersController, :orm => :active_record do
4
-
5
- include ApiTestHelpers
6
-
7
- before(:each) do
8
- @luke = User.create({ :first_name => 'Luke', :last_name => 'Skywalker', :age => 25, :active => true })
9
- @han = User.create({ :first_name => 'Han', :last_name => 'Solo', :age => 35, :active => true })
10
- @leia = User.create({ :first_name => 'Princess', :last_name => 'Leia', :age => 25, :active => false })
11
- end
12
-
13
- after(:each) do
14
- User.delete_all
15
- end
16
-
17
- describe 'xml responses' do
18
-
19
- describe 'get all users' do
20
-
21
- before(:each) do
22
- get :index, :format => 'xml', :api_template => :name_only
23
- end
24
-
25
- it "should have a root node named users" do
26
- response_body.should have_selector("users")
27
- end
28
-
29
- it "should contain all users" do
30
- response_body.should have_selector("users > user") do |users|
31
- users.size.should eql(3)
32
- end
33
- end
34
-
35
- it "should contain the specified attributes" do
36
- response_body.should have_selector("users > user > first-name")
37
- response_body.should have_selector("users > user > last-name")
38
- end
39
-
40
- end
41
-
42
- describe 'get a single user' do
43
-
44
- before(:each) do
45
- get :show, :format => 'xml', :api_template => :name_only, :id => @luke.id
46
- end
47
-
48
- it "should have a root node named user" do
49
- response_body.should have_selector("user")
50
- end
51
-
52
- it "should contain the specified attributes" do
53
- response_body.should have_selector("user > first-name")
54
- response_body.should have_selector("user > last-name")
55
- end
56
-
57
- end
58
-
59
- end
60
-
61
-
62
- describe 'json responses' do
63
-
64
- describe 'get all users' do
65
-
66
- before(:each) do
67
- get :index, :format => 'json', :api_template => :name_only
68
- end
69
-
70
- it "should have a root node named users" do
71
- response_body_json.should have_key("users")
72
- end
73
-
74
- it "should contain all users" do
75
- response_body_json["users"].should be_a(Array)
76
- end
77
-
78
- it "should contain the specified attributes" do
79
- response_body_json["users"].first.should have_key("first_name")
80
- response_body_json["users"].first.should have_key("last_name")
81
- end
82
-
83
- it "should contain the specified values" do
84
- response_body_json["users"].first["first_name"].should eql("Luke")
85
- response_body_json["users"].first["last_name"].should eql("Skywalker")
86
- end
3
+ describe UsersController do
87
4
 
5
+ context "using active record", :orm => :active_record do
6
+
7
+ before(:each) do
8
+ setup_active_record_models
88
9
  end
89
-
90
- describe 'get a single user' do
91
-
92
- before(:each) do
93
- get :show, :format => 'json', :api_template => :name_only, :id => @luke.id
94
- end
95
-
96
- it "should have a root node named user" do
97
- response_body_json.should have_key("user")
98
- end
99
-
100
- it "should contain the specified attributes" do
101
- response_body_json["user"].should have_key("first_name")
102
- response_body_json["user"].should have_key("last_name")
103
- end
104
-
105
- it "should contain the specified values" do
106
- response_body_json["user"]["first_name"].should eql("Luke")
107
- response_body_json["user"]["last_name"].should eql("Skywalker")
108
- end
109
-
10
+
11
+ after(:each) do
12
+ clean_up_active_record_models
110
13
  end
111
-
112
- describe 'get a single user with a nil profile' do
113
-
114
- before(:each) do
115
- Profile.acts_as_api
116
- Profile.api_accessible :include_profile do |t|
117
- t.add :avatar
118
- t.add :homepage
119
- end
120
-
121
- get :show, :format => 'json', :api_template => :include_profile, :id => @han.id
122
- end
123
-
124
- it "should have a root node named user" do
125
- response_body_json.should have_key("user")
126
- end
127
-
128
- it "should contain the specified attributes" do
129
- response_body_json["user"].should have(1).key
130
- response_body_json["user"].should have_key("profile")
131
- end
132
-
133
- it "should contain the specified values" do
134
- response_body_json["user"]["profile"].should be_nil
135
- end
136
-
137
- end
138
-
14
+
15
+ # see spec/support/controller_examples.rb
16
+ it_behaves_like "a controller with ActsAsApi responses"
139
17
  end
140
18
 
141
- describe 'Rails 3 default style json responses' do
19
+ context "using mongoid", :orm => :mongoid do
142
20
 
143
21
  before(:each) do
144
- @org_include_root_in_json_collections = ActsAsApi::Config.include_root_in_json_collections
145
- ActsAsApi::Config.include_root_in_json_collections = true
22
+ setup_mongoid_models
146
23
  end
147
24
 
148
25
  after(:each) do
149
- ActsAsApi::Config.include_root_in_json_collections = @org_include_root_in_json_collections
150
- end
151
-
152
- describe 'get all users' do
153
-
154
- before(:each) do
155
- get :index, :format => 'json', :api_template => :name_only
156
- end
157
-
158
- it "should have a root node named users" do
159
- response_body_json.should have_key("users")
160
- end
161
-
162
- it "should contain all users" do
163
- response_body_json["users"].should be_a(Array)
164
- end
165
-
166
- it "should contain the specified attributes" do
167
- response_body_json["users"].first["user"].should have_key("first_name")
168
- response_body_json["users"].first["user"].should have_key("last_name")
169
- end
170
-
171
- it "contains the user root nodes" do
172
- response_body_json["users"].collect(&:keys).flatten.uniq.should eql(["user"])
173
- end
174
-
175
- it "should contain the specified values" do
176
- response_body_json["users"].first["user"]["first_name"].should eql("Luke")
177
- response_body_json["users"].first["user"]["last_name"].should eql("Skywalker")
178
- end
179
-
180
- end
181
-
182
- describe 'get a single user' do
183
-
184
- before(:each) do
185
- get :show, :format => 'json', :api_template => :name_only, :id => @luke.id
186
- end
187
-
188
- it "should have a root node named user" do
189
- response_body_json.should have_key("user")
190
- end
191
-
192
- it "should contain the specified attributes" do
193
- response_body_json["user"].should have_key("first_name")
194
- response_body_json["user"].should have_key("last_name")
195
- end
196
-
197
- it "should contain the specified values" do
198
- response_body_json["user"]["first_name"].should eql("Luke")
199
- response_body_json["user"]["last_name"].should eql("Skywalker")
200
- end
26
+ clean_up_mongoid_models
201
27
  end
202
28
 
29
+ # see spec/support/controller_examples.rb
30
+ it_behaves_like "a controller with ActsAsApi responses"
203
31
  end
204
-
205
- describe 'jsonp responses with callback' do
206
-
207
- it "should be disabled by default" do
208
- @callback = "mycallback"
209
- get :index, :format => 'json', :api_template => :name_only, :callback => @callback
210
- response_body_jsonp(@callback).should be_nil
211
- end
212
-
213
- describe "enabled jsonp callbacks" do
214
-
215
- before(:each) do
216
- @callback = "mycallback"
217
-
218
- User.acts_as_api do |config|
219
- config.allow_jsonp_callback = true
220
- end
221
- end
222
-
223
- describe 'get all users' do
224
-
225
- before(:each) do
226
- get :index, :format => 'json', :api_template => :name_only, :callback => @callback
227
- end
228
-
229
- it "should wrap the response in the callback" do
230
- response_body_jsonp(@callback).should_not be_nil
231
- end
232
-
233
- end
234
-
235
- describe 'get a single user' do
236
-
237
- before(:each) do
238
- get :show, :format => 'json', :api_template => :name_only, :id => @luke.id, :callback => @callback
239
- end
240
-
241
- it "should wrap the response in the callback" do
242
- response_body_jsonp(@callback).should_not be_nil
243
- end
244
-
245
- end
246
-
247
- end
248
- end
249
-
250
32
  end