gaffe 0.1.2 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.0.0
5
+ - 1.9.3
6
+
7
+ gemfile:
8
+ - gemfiles/Gemfile.rails-4.0
9
+ - gemfiles/Gemfile.rails-3.2.x
10
+
11
+ script: "echo 'DO IT' && bundle exec rake spec"
data/README.md CHANGED
@@ -1,6 +1,13 @@
1
1
  # Gaffe
2
2
 
3
- Gaffe handles Rails error pages in a clean, simple way.
3
+ [![Gem Version](https://badge.fury.io/rb/gaffe.png)](https://rubygems.org/gems/gaffe)
4
+ [![Build Status](https://travis-ci.org/mirego/gaffe.png?branch=master)](https://travis-ci.org/mirego/gaffe)
5
+
6
+ Gaffe handles Rails error pages in a clean, simple way. The default error page looks like this:
7
+
8
+ ![](http://i.imgur.com/9gPfCOm.png)
9
+
10
+ You should overwrite it though, it’s very easy to do so.
4
11
 
5
12
  ## Installation
6
13
 
@@ -32,6 +39,22 @@ end
32
39
  Gaffe.enable!
33
40
  ```
34
41
 
42
+ It’s also possible to use a custom controller based on the URL in which the error has occured. This is especially
43
+ useful if you have an application that also serves API requests via JSON. You would probably want to serve API errors
44
+ through JSON and regular errors through HTML pages.
45
+
46
+ ```ruby
47
+ # config/initializers/gaffe.rb
48
+ Gaffe.configure do |config|
49
+ config.errors_controller = {
50
+ %r[^/api/] => Api::ErrorsController,
51
+ %r[^/] => ErrorsController
52
+ }
53
+ end
54
+
55
+ Gaffe.enable!
56
+ ```
57
+
35
58
  The only required thing to do in your custom controller is to include the `Gaffe::Errors` module.
36
59
 
37
60
  Only `show` will be called so you might want to overwrite it. You might also want to get rid of filters and other stuff.
@@ -54,7 +77,7 @@ end
54
77
 
55
78
  You can (and should!) also use your own views. You just have to create a layout:
56
79
 
57
- ```
80
+ ```erb
58
81
  <!-- app/views/layouts/error.html.erb -->
59
82
  <h1>Error!</h1>
60
83
  <%= yield %>
@@ -62,7 +85,7 @@ You can (and should!) also use your own views. You just have to create a layout:
62
85
 
63
86
  And create a different view for [each possible error rescue response](https://github.com/rails/rails/blob/f9ceefd3b9c3cea2460a89799156f2c532c4491c/actionpack/lib/action_dispatch/middleware/exception_wrapper.rb). For example, for `404` errors:
64
87
 
65
- ```
88
+ ```erb
66
89
  <!-- app/views/errors/not_found.html.erb -->
67
90
  <p>This page does not exist.</p>
68
91
  ```
@@ -85,4 +108,6 @@ config.action_dispatch.rescue_responses.merge!('MyCustomException' => :not_accep
85
108
 
86
109
  ## About Mirego
87
110
 
88
- Mirego is a team of passionate people who believe that work is a place where you can innovate and have fun. We proudly built mobile applications for [iPhone](http://mirego.com/en/iphone-app-development/ "iPhone application development"), [iPad](http://mirego.com/en/ipad-app-development/ "iPad application development"), [Android](http://mirego.com/en/android-app-development/ "Android application development"), [Blackberry](http://mirego.com/en/blackberry-app-development/ "Blackberry application development"), [Windows Phone](http://mirego.com/en/windows-phone-app-development/ "Windows Phone application development") and [Windows 8](http://mirego.com/en/windows-8-app-development/ "Windows 8 application development").
111
+ Mirego is a team of passionate people who believe that work is a place where you can innovate and have fun. We proudly build mobile applications for [iPhone](http://mirego.com/en/iphone-app-development/ "iPhone application development"), [iPad](http://mirego.com/en/ipad-app-development/ "iPad application development"), [Android](http://mirego.com/en/android-app-development/ "Android application development"), [Blackberry](http://mirego.com/en/blackberry-app-development/ "Blackberry application development"), [Windows Phone](http://mirego.com/en/windows-phone-app-development/ "Windows Phone application development") and [Windows 8](http://mirego.com/en/windows-8-app-development/ "Windows 8 application development") in beautiful Quebec City.
112
+
113
+ We also love [open-source software](http://open.mirego.com/) and we try to extract as much code as possible from our projects to give back to the community.
data/Rakefile CHANGED
@@ -1 +1,11 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler'
2
+ require 'rake'
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ task default: :spec
7
+
8
+ desc 'Run all specs'
9
+ RSpec::Core::RakeTask.new(:spec) do |task|
10
+ task.pattern = 'spec/**/*_spec.rb'
11
+ end
data/gaffe.gemspec CHANGED
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency 'bundler', '~> 1.3'
22
22
  spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'rspec', '~> 2.14'
23
24
 
24
25
  spec.add_dependency 'rails', '>= 3.2.0'
25
26
  end
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec path: '../'
4
+
5
+ gem 'rails', '~> 3.2.0'
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec path: '../'
4
+
5
+ gem 'rails', '~> 4.0.0'
data/lib/gaffe.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  require 'gaffe/version'
2
+
3
+ require 'ostruct'
2
4
  require 'gaffe/errors'
3
5
 
4
6
  module Gaffe
@@ -12,11 +14,6 @@ module Gaffe
12
14
  @configuration ||= OpenStruct.new
13
15
  end
14
16
 
15
- # Return either the user-defined controller or our default controller
16
- def self.errors_controller
17
- @errors_controller ||= (configuration.errors_controller || builtin_errors_controller)
18
- end
19
-
20
17
  # Return our default controller
21
18
  def self.builtin_errors_controller
22
19
  require 'gaffe/errors_controller'
@@ -26,8 +23,20 @@ module Gaffe
26
23
  # Configure Rails to use our code when encountering exceptions
27
24
  def self.enable!
28
25
  Rails.application.config.exceptions_app = lambda do |env|
29
- Gaffe.errors_controller.action(:show).call(env)
26
+ Gaffe.errors_controller_for_request(env).action(:show).call(env)
27
+ end
28
+ end
29
+
30
+ # Return the right errors controller to use for the request that
31
+ # triggered the error
32
+ def self.errors_controller_for_request(env)
33
+ controller = configuration.errors_controller
34
+
35
+ if controller.is_a?(Hash)
36
+ controller = controller.detect { |pattern, _| env["REQUEST_URI"] =~ pattern }.try(:last)
30
37
  end
38
+
39
+ controller || builtin_errors_controller
31
40
  end
32
41
 
33
42
  # Return the root path of the gem
data/lib/gaffe/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Gaffe
2
- VERSION = '0.1.2'
2
+ VERSION = '0.2'
3
3
  end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe Gaffe::Errors do
4
+ describe :Actions do
5
+ describe :show do
6
+ let(:request) { ActionDispatch::TestRequest.new }
7
+ let(:env) { request.env.merge 'action_dispatch.exception' => exception }
8
+ let(:exception) { ActionController::RoutingError.new(:foo) }
9
+
10
+ let(:response) { Gaffe.errors_controller_for_request(env).action(:show).call(env) }
11
+ subject { response.last }
12
+
13
+ its(:status) { should eql 404 }
14
+ its(:body) { should match /Not Found/ }
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ describe Gaffe do
4
+ describe :ClassMethods do
5
+ before do
6
+ # Make sure we clear memoized variables before each test
7
+ [:@configuration].each do |variable|
8
+ Gaffe.send :remove_instance_variable, variable if Gaffe.instance_variable_defined?(variable)
9
+ end
10
+ end
11
+
12
+ describe :configure do
13
+ subject { Gaffe.configuration }
14
+ before do
15
+ Gaffe.configure do |config|
16
+ config.foo = :bar
17
+ config.bar = :foo
18
+ end
19
+ end
20
+
21
+ its(:foo) { should eql :bar }
22
+ its(:bar) { should eql :foo }
23
+ end
24
+
25
+ describe :errors_controller_for_request do
26
+ let(:controller) { Gaffe.errors_controller_for_request(env) }
27
+ let(:request) { ActionDispatch::TestRequest.new }
28
+ let(:env) { request.env }
29
+
30
+ context 'with custom-defined controller' do
31
+ before do
32
+ Gaffe.configure do |config|
33
+ config.errors_controller = :foo
34
+ end
35
+ end
36
+
37
+ it { expect(controller).to eql :foo }
38
+ end
39
+
40
+ context 'with multiple custom-defined controllers' do
41
+ before do
42
+ Gaffe.configure do |config|
43
+ config.errors_controller = {
44
+ %r[^/web/] => :web_controller,
45
+ %r[^/api/] => :api_controller
46
+ }
47
+ end
48
+ end
49
+
50
+ context 'with error coming from matching URL' do
51
+ let(:env) { request.env.merge 'REQUEST_URI' => '/api/users' }
52
+ it { expect(controller).to eql :api_controller }
53
+ end
54
+
55
+ context 'with errors coming from non-matching URL' do
56
+ let(:env) { request.env.merge 'REQUEST_URI' => '/what' }
57
+ it { expect(controller).to eql Gaffe::ErrorsController }
58
+ end
59
+ end
60
+
61
+ context 'without custom-defined controller' do
62
+ it { expect(controller).to eql Gaffe::ErrorsController }
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,18 @@
1
+ $:.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'rspec'
4
+ require "action_controller/railtie"
5
+ require 'gaffe'
6
+
7
+ RSpec.configure do |config|
8
+ config.before(:each) do
9
+ Rails.stub(:root).and_return(RAILS_ROOT)
10
+ end
11
+ end
12
+
13
+ # We need a fake "ApplicationController" because Gaffe's default controller inherits from it
14
+ class ApplicationController < ActionController::Base
15
+ end
16
+
17
+ # We need Rails.root
18
+ RAILS_ROOT = Pathname.new(File.expand_path('../../', __FILE__))
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gaffe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: '0.2'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-07-15 00:00:00.000000000 Z
13
+ date: 2013-08-01 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bundler
@@ -44,6 +44,22 @@ dependencies:
44
44
  - - ! '>='
45
45
  - !ruby/object:Gem::Version
46
46
  version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.14'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ version: '2.14'
47
63
  - !ruby/object:Gem::Dependency
48
64
  name: rails
49
65
  requirement: !ruby/object:Gem::Requirement
@@ -69,6 +85,8 @@ extensions: []
69
85
  extra_rdoc_files: []
70
86
  files:
71
87
  - .gitignore
88
+ - .rspec
89
+ - .travis.yml
72
90
  - Gemfile
73
91
  - LICENSE.md
74
92
  - README.md
@@ -80,10 +98,15 @@ files:
80
98
  - app/views/errors/unauthorized.html.erb
81
99
  - app/views/layouts/error.html.erb
82
100
  - gaffe.gemspec
101
+ - gemfiles/Gemfile.rails-3.2.x
102
+ - gemfiles/Gemfile.rails-4.0
83
103
  - lib/gaffe.rb
84
104
  - lib/gaffe/errors.rb
85
105
  - lib/gaffe/errors_controller.rb
86
106
  - lib/gaffe/version.rb
107
+ - spec/gaffe/errors_spec.rb
108
+ - spec/gaffe/gaffe_spec.rb
109
+ - spec/spec_helper.rb
87
110
  homepage: https://github.com/mirego/gaffe
88
111
  licenses:
89
112
  - BSD 3-Clause
@@ -99,7 +122,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
99
122
  version: '0'
100
123
  segments:
101
124
  - 0
102
- hash: -3771495489662381768
125
+ hash: -2535410059027227615
103
126
  required_rubygems_version: !ruby/object:Gem::Requirement
104
127
  none: false
105
128
  requirements:
@@ -108,11 +131,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
131
  version: '0'
109
132
  segments:
110
133
  - 0
111
- hash: -3771495489662381768
134
+ hash: -2535410059027227615
112
135
  requirements: []
113
136
  rubyforge_project:
114
137
  rubygems_version: 1.8.23
115
138
  signing_key:
116
139
  specification_version: 3
117
140
  summary: Gaffe handles Rails error pages in a clean, simple way.
118
- test_files: []
141
+ test_files:
142
+ - spec/gaffe/errors_spec.rb
143
+ - spec/gaffe/gaffe_spec.rb
144
+ - spec/spec_helper.rb