resource_full 0.7.6 → 0.7.7
Sign up to get free protection for your applications and to get access to all the features.
@@ -12,7 +12,12 @@ module ResourceFull
|
|
12
12
|
end
|
13
13
|
def show_json
|
14
14
|
self.model_object = send("find_#{model_name}")
|
15
|
-
|
15
|
+
|
16
|
+
json_representation = with_root_included_in_json do
|
17
|
+
model_object.to_json(show_json_options)
|
18
|
+
end
|
19
|
+
|
20
|
+
render :json => json_representation
|
16
21
|
rescue ActiveRecord::RecordNotFound => e
|
17
22
|
render :json => e.to_json, :status => :not_found
|
18
23
|
rescue => e
|
@@ -24,7 +29,12 @@ module ResourceFull
|
|
24
29
|
end
|
25
30
|
def index_json
|
26
31
|
self.model_objects = send("find_all_#{model_name.pluralize}")
|
27
|
-
|
32
|
+
|
33
|
+
json_representation = with_root_included_in_json do
|
34
|
+
model_objects.to_json(index_json_options)
|
35
|
+
end
|
36
|
+
|
37
|
+
render :json => json_representation
|
28
38
|
end
|
29
39
|
|
30
40
|
def count_json
|
@@ -36,7 +46,11 @@ module ResourceFull
|
|
36
46
|
{}
|
37
47
|
end
|
38
48
|
def new_json
|
39
|
-
|
49
|
+
json_representation = with_root_included_in_json do
|
50
|
+
send("new_#{model_name}").to_json(new_json_options)
|
51
|
+
end
|
52
|
+
|
53
|
+
render :json => json_representation
|
40
54
|
end
|
41
55
|
|
42
56
|
def create_json_options
|
@@ -94,14 +108,25 @@ module ResourceFull
|
|
94
108
|
end
|
95
109
|
rescue ActiveRecord::RecordNotFound => e
|
96
110
|
render :json => e.to_json, :status => :not_found
|
111
|
+
rescue ActiveRecord::RecordInvalid => e
|
112
|
+
render :json => e.to_json, :status => :unprocessable_entity
|
97
113
|
rescue => e
|
98
114
|
handle_generic_error_in_json(e)
|
99
115
|
end
|
100
116
|
|
101
117
|
private
|
102
|
-
|
103
|
-
|
104
|
-
|
118
|
+
|
119
|
+
def handle_generic_error_in_json(exception)
|
120
|
+
render :json => exception, :status => :internal_server_error
|
121
|
+
end
|
122
|
+
|
123
|
+
def with_root_included_in_json
|
124
|
+
old_value = ActiveRecord::Base.include_root_in_json
|
125
|
+
ActiveRecord::Base.include_root_in_json = true
|
126
|
+
yield
|
127
|
+
ensure
|
128
|
+
ActiveRecord::Base.include_root_in_json = old_value
|
129
|
+
end
|
105
130
|
end
|
106
131
|
end
|
107
132
|
end
|
@@ -93,13 +93,15 @@ module ResourceFull
|
|
93
93
|
end
|
94
94
|
rescue ActiveRecord::RecordNotFound => e
|
95
95
|
render :xml => e.to_xml, :status => :not_found
|
96
|
+
rescue ActiveRecord::RecordInvalid => e
|
97
|
+
render :xml => e.to_xml, :status => :unprocessable_entity
|
96
98
|
rescue => e
|
97
99
|
handle_generic_error_in_xml(e)
|
98
100
|
end
|
99
101
|
|
100
102
|
private
|
101
103
|
def handle_generic_error_in_xml(exception)
|
102
|
-
render :xml => exception, :status => :
|
104
|
+
render :xml => exception, :status => :internal_server_error
|
103
105
|
end
|
104
106
|
end
|
105
107
|
end
|
@@ -9,6 +9,7 @@ describe "ResourceFull::Render::JSON", :type => :controller do
|
|
9
9
|
rescue_action_in_public!
|
10
10
|
ResourceFullMockUser.delete_all
|
11
11
|
ResourceFullMockUsersController.resource_identifier = :id
|
12
|
+
ActiveRecord::Base.include_root_in_json = true
|
12
13
|
end
|
13
14
|
|
14
15
|
describe "index" do
|
@@ -21,6 +22,15 @@ describe "ResourceFull::Render::JSON", :type => :controller do
|
|
21
22
|
hash.size.should == 2
|
22
23
|
response.code.should == '200'
|
23
24
|
end
|
25
|
+
|
26
|
+
it "renders the model object with the json root included even if ActiveRecord isn't configured that way" do
|
27
|
+
2.times { ResourceFullMockUser.create! }
|
28
|
+
ActiveRecord::Base.include_root_in_json = false
|
29
|
+
|
30
|
+
get :index, :format => 'json'
|
31
|
+
hash = Hash.from_json(response.body)
|
32
|
+
hash.first.should have_key("resource_full_mock_user")
|
33
|
+
end
|
24
34
|
|
25
35
|
it "rescues all unhandled exceptions with an JSON response" do
|
26
36
|
ResourceFullMockUser.expects(:find).raises SomeNonsenseException, "sparrow farts"
|
@@ -87,6 +97,17 @@ describe "ResourceFull::Render::JSON", :type => :controller do
|
|
87
97
|
hash = Hash.from_json(response.body)
|
88
98
|
hash["error"]["text"].should == "Couldn't find ResourceFullMockUser with id=1"
|
89
99
|
end
|
100
|
+
|
101
|
+
it "renders the model object with the json root included even if ActiveRecord isn't configured that way" do
|
102
|
+
ActiveRecord::Base.include_root_in_json = false
|
103
|
+
user = ResourceFullMockUser.create!
|
104
|
+
|
105
|
+
get :show, :id => user.id, :format => 'json'
|
106
|
+
response.code.should == '200'
|
107
|
+
hash = Hash.from_json(response.body)
|
108
|
+
hash.should have_key("resource_full_mock_user")
|
109
|
+
ActiveRecord::Base.include_root_in_json.should be_false
|
110
|
+
end
|
90
111
|
|
91
112
|
it "renders appropriate errors if a generic exception occurs" do
|
92
113
|
mock_user = ResourceFullMockUser.create!
|
@@ -112,6 +133,12 @@ describe "ResourceFull::Render::JSON", :type => :controller do
|
|
112
133
|
|
113
134
|
response.body.should == ResourceFullMockUser.new.to_json
|
114
135
|
end
|
136
|
+
|
137
|
+
it "renders the model object with the json root included even if ActiveRecord isn't configured that way" do
|
138
|
+
ActiveRecord::Base.include_root_in_json = false
|
139
|
+
get :new, :format => 'json'
|
140
|
+
Hash.from_json(response.body).should have_key("resource_full_mock_user")
|
141
|
+
end
|
115
142
|
end
|
116
143
|
|
117
144
|
describe "create" do
|
@@ -222,7 +249,7 @@ describe "ResourceFull::Render::JSON", :type => :controller do
|
|
222
249
|
hash["error"]["text"].should == "Couldn't find ResourceFullMockUser with id=1"
|
223
250
|
end
|
224
251
|
|
225
|
-
it "renders appropriate errors if
|
252
|
+
it "renders appropriate errors if RecordInvalid is raised" do
|
226
253
|
mock_user = ResourceFullMockUser.create!
|
227
254
|
ResourceFullMockUser.send :define_method, :destroy do
|
228
255
|
errors.add_to_base("Cannot delete")
|
@@ -240,6 +267,17 @@ describe "ResourceFull::Render::JSON", :type => :controller do
|
|
240
267
|
end
|
241
268
|
end
|
242
269
|
|
270
|
+
it "renders appropriate errors if a generic error is raised" do
|
271
|
+
mock_user = ResourceFullMockUser.create!
|
272
|
+
ResourceFullMockUser.any_instance.expects(:destroy).raises SomeNonsenseException, "sparrow farts"
|
273
|
+
|
274
|
+
delete :destroy, :id => mock_user.id.to_s, :format => 'json'
|
275
|
+
|
276
|
+
response.code.should == '500'
|
277
|
+
hash = Hash.from_json(response.body)
|
278
|
+
hash["error"]["text"].should == "sparrow farts"
|
279
|
+
end
|
280
|
+
|
243
281
|
it "renders error if the model could not be destroyed"
|
244
282
|
end
|
245
283
|
|
@@ -355,7 +355,7 @@ describe "ResourceFull::Render::XML" , :type => :controller do
|
|
355
355
|
response.should have_tag("errors") { with_tag("error", "Couldn't find ResourceFullMockUser with id=1")}
|
356
356
|
end
|
357
357
|
|
358
|
-
it "renders appropriate errors if
|
358
|
+
it "renders appropriate errors if RecordInvalid exception is raised" do
|
359
359
|
mock_user = ResourceFullMockUser.create!
|
360
360
|
ResourceFullMockUser.send :define_method, :destroy do
|
361
361
|
errors.add_to_base("Cannot delete")
|
@@ -371,6 +371,15 @@ describe "ResourceFull::Render::XML" , :type => :controller do
|
|
371
371
|
ResourceFullMockUser.send :remove_method, :destroy
|
372
372
|
end
|
373
373
|
end
|
374
|
+
|
375
|
+
it "renders appropriate errors if a generic exception is raised" do
|
376
|
+
mock_user = ResourceFullMockUser.create!
|
377
|
+
ResourceFullMockUser.any_instance.expects(:destroy).raises SomeNonsenseException, "sparrow farts"
|
378
|
+
delete :destroy, :id => mock_user.id.to_s, :format => 'xml'
|
379
|
+
|
380
|
+
response.code.should == '500'
|
381
|
+
response.should have_tag("errors") { with_tag("error", "sparrow farts") }
|
382
|
+
end
|
374
383
|
|
375
384
|
it "renders error if the model could not be destroyed"
|
376
385
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 7
|
8
|
-
-
|
9
|
-
version: 0.7.
|
8
|
+
- 7
|
9
|
+
version: 0.7.7
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Brian Guthrie
|
@@ -14,11 +14,11 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-04-
|
17
|
+
date: 2010-04-19 00:00:00 +05:30
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
|
-
name:
|
21
|
+
name: actionpack
|
22
22
|
prerelease: false
|
23
23
|
requirement: &id001 !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
type: :runtime
|
33
33
|
version_requirements: *id001
|
34
34
|
- !ruby/object:Gem::Dependency
|
35
|
-
name:
|
35
|
+
name: activerecord
|
36
36
|
prerelease: false
|
37
37
|
requirement: &id002 !ruby/object:Gem::Requirement
|
38
38
|
requirements:
|