rescue-dog 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rescue/controller.rb +1 -1
- data/lib/rescue/version.rb +1 -1
- data/public/404.html +11 -0
- data/public/404.json +1 -0
- data/public/500.html +11 -0
- data/public/500.json +1 -0
- data/spec/controller_spec.rb +40 -2
- data/spec/rails_spec_app.rb +10 -7
- data/spec/spec_helper.rb +6 -0
- data/spec/test_case.rb +8 -0
- metadata +8 -2
data/lib/rescue/controller.rb
CHANGED
@@ -28,7 +28,7 @@ module Rescue
|
|
28
28
|
def define_respond_method name, code
|
29
29
|
return if method_defined?(name)
|
30
30
|
define_method name do |e = nil|
|
31
|
-
render status: code, file: "#{Rails.root}/public/#{code}.
|
31
|
+
render status: code, file: "#{Rails.root}/public/#{code}.#{request.format.to_sym}", layout: false and return
|
32
32
|
end
|
33
33
|
end
|
34
34
|
end
|
data/lib/rescue/version.rb
CHANGED
data/public/404.html
ADDED
data/public/404.json
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"errors":[{"code":404,"status":"Not Found"}]}
|
data/public/500.html
ADDED
data/public/500.json
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"errors":[{"code":500,"status":"Server Error"}]}
|
data/spec/controller_spec.rb
CHANGED
@@ -4,9 +4,47 @@ require 'spec_helper'
|
|
4
4
|
describe Rescue::Controller do
|
5
5
|
|
6
6
|
before do
|
7
|
-
@r =
|
7
|
+
@r = ErrorsController.new
|
8
8
|
end
|
9
9
|
|
10
|
-
|
10
|
+
TestCase::Controller::ERRORS.each do |name, code|
|
11
|
+
describe "#{name} exception class" do
|
12
|
+
subject { Object.const_defined? name }
|
13
|
+
it "should define '#{name}' class" do
|
14
|
+
should be_true
|
15
|
+
end
|
16
|
+
|
17
|
+
subject { Object.const_get(name).new }
|
18
|
+
it "should be a kind of StandardError" do
|
19
|
+
should be_a_kind_of StandardError
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "respond_#{code} method" do
|
24
|
+
subject { ApplicationController.method_defined? :"respond_#{code}" }
|
25
|
+
it "should be defined in ApplicationController" do
|
26
|
+
should be_true
|
27
|
+
end
|
28
|
+
|
29
|
+
subject { @r.methods.include?(:"respond_#{code}") }
|
30
|
+
it "should be called in a subclass of ApplicationController" do
|
31
|
+
should be_true
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "raise #{name}" do
|
36
|
+
TestCase::Controller::FORMATS.each do |format|
|
37
|
+
context "request format => #{format}" do
|
38
|
+
before do
|
39
|
+
visit "/#{name.to_s.underscore}.#{format.to_sym}"
|
40
|
+
end
|
41
|
+
|
42
|
+
subject { page }
|
43
|
+
it { should have_content name.to_s.gsub(/([A-Z]+)([A-Z][a-z])/,'\1 \2').gsub(/([a-z\d])([A-Z])/,'\1 \2') }
|
44
|
+
it { response_headers["Content-Type"].should include(format.to_s) }
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end ## end each
|
11
49
|
|
12
50
|
end
|
data/spec/rails_spec_app.rb
CHANGED
@@ -4,23 +4,26 @@ require 'action_view/railtie'
|
|
4
4
|
# config
|
5
5
|
app = Class.new Rails::Application
|
6
6
|
app.config.active_support.deprecation = :log
|
7
|
+
app.config.secret_token = 'ccedfce890492dd9fe2908a69a8732104ae133f1e2488bf6a1e96685b05a96d7e11aeaa3da5ade27604a50c3b2c7cc8323dd03ad11bb2e52e95256fb67ef9c8a'
|
7
8
|
app.initialize!
|
8
9
|
|
9
10
|
# routing
|
10
11
|
app.routes.draw do
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
# models
|
15
|
-
class User
|
12
|
+
[:not_found, :server_error].each do |e|
|
13
|
+
get "/#{e}"=>"errors##{e}" ,as: e
|
14
|
+
end
|
16
15
|
end
|
17
16
|
|
18
17
|
# controllers
|
19
18
|
class ApplicationController < ActionController::Base;
|
20
19
|
include Rescue::Controller
|
21
|
-
end
|
22
|
-
class UsersController < ApplicationController
|
23
20
|
define_errors ServerError: 500, NotFound: 404
|
24
21
|
end
|
22
|
+
class ErrorsController < ApplicationController
|
23
|
+
|
24
|
+
def not_found ; raise NotFound ; end
|
25
|
+
def server_error ; raise ServerError ; end
|
26
|
+
|
27
|
+
end
|
25
28
|
|
26
29
|
Object.const_set(:ApplicationHelper,Module.new)
|
data/spec/spec_helper.rb
CHANGED
@@ -3,3 +3,9 @@ require 'rubygems'
|
|
3
3
|
require 'rescue-dog'
|
4
4
|
|
5
5
|
require File.join(File.dirname(__FILE__), 'rails_spec_app')
|
6
|
+
require File.join(File.dirname(__FILE__), 'test_case')
|
7
|
+
require 'capybara/rails'
|
8
|
+
RSpec.configure do |config|
|
9
|
+
config.mock_with :rspec
|
10
|
+
config.include Capybara::DSL
|
11
|
+
end
|
data/spec/test_case.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rescue-dog
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-03-
|
12
|
+
date: 2013-03-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -44,10 +44,15 @@ files:
|
|
44
44
|
- lib/rescue-dog.rb
|
45
45
|
- lib/rescue/controller.rb
|
46
46
|
- lib/rescue/version.rb
|
47
|
+
- public/404.html
|
48
|
+
- public/404.json
|
49
|
+
- public/500.html
|
50
|
+
- public/500.json
|
47
51
|
- rescue-dog.gemspec
|
48
52
|
- spec/controller_spec.rb
|
49
53
|
- spec/rails_spec_app.rb
|
50
54
|
- spec/spec_helper.rb
|
55
|
+
- spec/test_case.rb
|
51
56
|
homepage: https://github.com/yulii/rescue-dog
|
52
57
|
licenses: []
|
53
58
|
post_install_message:
|
@@ -76,3 +81,4 @@ test_files:
|
|
76
81
|
- spec/controller_spec.rb
|
77
82
|
- spec/rails_spec_app.rb
|
78
83
|
- spec/spec_helper.rb
|
84
|
+
- spec/test_case.rb
|