acts_as_api 0.3.4 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,6 +12,11 @@ module ActsAsApi
12
12
  # Default is <tt>[:xml]</tt>
13
13
  attr_accessor_with_default :dasherize_for, [:xml]
14
14
 
15
+ # If true, the root node in json collections will be added
16
+ # so the response will look like the default Rails 3 json
17
+ # response
18
+ attr_accessor_with_default :include_root_in_json_collections, false
19
+
15
20
  # Holds references to formats that need
16
21
  # to get added an additional root node
17
22
  # with the name of the model.
@@ -59,6 +59,10 @@ module ActsAsApi
59
59
 
60
60
  api_response = api_model.as_api_response(api_template)
61
61
 
62
+ if api_response.is_a?(Array) && api_format.to_sym == :json && ActsAsApi::Config.include_root_in_json_collections
63
+ api_response = api_response.collect{|f| { api_root_name.singularize => f } }
64
+ end
65
+
62
66
  if meta_hash or ActsAsApi::Config.add_root_node_for.include? api_format
63
67
  api_response = { api_root_name.to_sym => api_response}
64
68
  end
@@ -1,3 +1,3 @@
1
1
  module ActsAsApi
2
- VERSION = "0.3.4"
2
+ VERSION = "0.3.5"
3
3
  end
@@ -138,6 +138,69 @@ describe UsersController, :orm => :active_record do
138
138
 
139
139
  end
140
140
 
141
+ describe 'Rails 3 default style json responses' do
142
+
143
+ 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
146
+ end
147
+
148
+ 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
201
+ end
202
+
203
+ end
141
204
 
142
205
  describe 'jsonp responses with callback' do
143
206
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_api
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 4
10
- version: 0.3.4
9
+ - 5
10
+ version: 0.3.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Christian B\xC3\xA4uerlein"