r404 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fc6e6557052a8a89d2cef3beb454a9496d6581fabe2432f9bb86319e810d58dc
4
+ data.tar.gz: 933b900e2c34b746e3e99f6aedd71519b1262230cff073dffc52c0536faad94c
5
+ SHA512:
6
+ metadata.gz: c6def4e9cad4d39b550357807cbee510319896c1107320d17124d295dd8be7992541e4f1b7879309a475df64a31df64a64732e93191f93cc9a6f8952a64e4067
7
+ data.tar.gz: ff5098e3b4a191218b8c707b111ae62a1dabe6ddac7d980be07a4754092a71665ce0fcab54d1cc335f48f07f78262e28a73de77fe6b6593024471c358591572f
data/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # Changelog
2
+
3
+ ### master
4
+
5
+ * nothing yet
6
+
7
+ ### 1.0.0 - 2018/01/20
8
+
9
+ * initial release
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Jonas Hübotter
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,148 @@
1
+ # r404
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/r404.svg)](https://badge.fury.io/rb/r404) <img src="https://travis-ci.org/jonhue/r404.svg?branch=master" />
4
+
5
+ An error handler & renderer for Rails.
6
+
7
+ ---
8
+
9
+ ## Table of Contents
10
+
11
+ * [Installation](#installation)
12
+ * [Usage](#usage)
13
+ * [Raising an error](#raising-an-error)
14
+ * [Rendering an error](#rendering-an-error)
15
+ * [List of all error classes](#list-of-all-error-classes)
16
+ * [To Do](#to-do)
17
+ * [Contributing](#contributing)
18
+ * [Contributors](#contributors)
19
+ * [Semantic versioning](#semantic-versioning)
20
+ * [License](#license)
21
+
22
+ ---
23
+
24
+ ## Installation
25
+
26
+ r404 works with Rails 5 onwards. You can add it to your `Gemfile` with:
27
+
28
+ ```ruby
29
+ gem 'r404'
30
+ ```
31
+
32
+ And then execute:
33
+
34
+ $ bundle
35
+
36
+ Or install it yourself as:
37
+
38
+ $ gem install r404
39
+
40
+ If you always want to be up to date fetch the latest from GitHub in your `Gemfile`:
41
+
42
+ ```ruby
43
+ gem 'r404', github: 'jonhue/r404'
44
+ ```
45
+
46
+ Now run the generator:
47
+
48
+ $ rails g r404
49
+
50
+ Finally add `include R404Controller` to your `ApplicationController` located in `app/controllers/application_controller.rb`.
51
+
52
+ ---
53
+
54
+ ## Usage
55
+
56
+ ### Raising an error
57
+
58
+ You can raise errors in controllers and views:
59
+
60
+ ```ruby
61
+ r404 :not_found
62
+
63
+ # more specific
64
+ r404 404
65
+ ```
66
+
67
+ **Note:** The status parameter defaults to `404`.
68
+
69
+ You are able to pass exception details which will be available in your template:
70
+
71
+ ```ruby
72
+ r404 :not_found, 'Here is what went wrong ...'
73
+ ```
74
+
75
+ Alternatively you can raise errors from anywhere by raising the [error class](#list-of-all-error-classes) directly:
76
+
77
+ ```ruby
78
+ raise R404::NotFound
79
+ ```
80
+
81
+ ### Rendering an error
82
+
83
+ r404 automatically looks up templates in the `app/views/r404` directory. The `status` and `exception` (if passed) variables are available in your template.
84
+
85
+ Often you don't want to render a template, but instead your goal is to redirect back passing an alert. You are able to override error specific methods in your `ApplicationController`:
86
+
87
+ ```ruby
88
+ private
89
+
90
+ def render_r404_access_denied format, status, exception
91
+ format.html { redirect_back fallback_location: root_url, alert: 'You are unauthorized to perform this action' }
92
+ end
93
+ ```
94
+
95
+ ### List of all error classes
96
+
97
+ * `R404::AccessDenied` (`:access_denied`, `403`)
98
+ * `R404::NotFound` (`:not_found`, `404`)
99
+
100
+ ---
101
+
102
+ ## To Do
103
+
104
+ [Here](https://github.com/jonhue/r404/projects/1) is the full list of current projects.
105
+
106
+ To propose your ideas, initiate the discussion by adding a [new issue](https://github.com/jonhue/r404/issues/new).
107
+
108
+ ---
109
+
110
+ ## Contributing
111
+
112
+ We hope that you will consider contributing to r404. Please read this short overview for some information about how to get started:
113
+
114
+ [Learn more about contributing to this repository](CONTRIBUTING.md), [Code of Conduct](CODE_OF_CONDUCT.md)
115
+
116
+ ### Contributors
117
+
118
+ Give the people some :heart: who are working on this project. See them all at:
119
+
120
+ https://github.com/jonhue/r404/graphs/contributors
121
+
122
+ ### Semantic Versioning
123
+
124
+ r404 follows Semantic Versioning 2.0 as defined at http://semver.org.
125
+
126
+ ## License
127
+
128
+ MIT License
129
+
130
+ Copyright (c) 2018 Jonas Hübotter
131
+
132
+ Permission is hereby granted, free of charge, to any person obtaining a copy
133
+ of this software and associated documentation files (the "Software"), to deal
134
+ in the Software without restriction, including without limitation the rights
135
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
136
+ copies of the Software, and to permit persons to whom the Software is
137
+ furnished to do so, subject to the following conditions:
138
+
139
+ The above copyright notice and this permission notice shall be included in all
140
+ copies or substantial portions of the Software.
141
+
142
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
143
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
144
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
145
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
146
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
147
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
148
+ SOFTWARE.
@@ -0,0 +1,26 @@
1
+ module R404Controller
2
+
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ rescue_from (R404::AccessDenied) { |exception| render_r404 :access_denied, 403, exception }
7
+ rescue_from (R404::NotFound) { |exception| render_r404 :not_found, 404, exception }
8
+
9
+ def render_r404 code, status, exception
10
+ logger.error exception
11
+ respond_to do |format|
12
+ begin
13
+ send "render_r404_#{code}", format, status, exception
14
+ rescue NoMethodError
15
+ render_r404_for code, format, status, exception
16
+ end
17
+ format.all { render nothing: true, status: status }
18
+ end
19
+ end
20
+
21
+ def render_r404_for code, format, status, exception = nil
22
+ format.html { render "r404/#{code}", status: status, locals: { status: status, exception: exception } }
23
+ end
24
+ end
25
+
26
+ end
@@ -0,0 +1,13 @@
1
+ module R404Helper
2
+
3
+ def r404 code = 404, exception = nil
4
+ # status = code.is_a?(Integer) ? code : nil
5
+ case code
6
+ when 403, :access_denied
7
+ raise R404::AccessDenied.new(exception)
8
+ when 404, :not_found
9
+ raise R404::NotFound.new(exception)
10
+ end
11
+ end
12
+
13
+ end
@@ -0,0 +1,16 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ class R404Generator < Rails::Generators::Base
5
+
6
+ include Rails::Generators::Migration
7
+
8
+ source_root File.join File.dirname(__FILE__), 'templates'
9
+ desc 'Install r404'
10
+
11
+ def create_views
12
+ template 'access_denied.html.erb', 'app/views/r404/access_denied.html.erb'
13
+ template 'not_found.html.erb', 'app/views/r404/not_found.html.erb'
14
+ end
15
+
16
+ end
@@ -0,0 +1,4 @@
1
+ <h1>Access denied</h1>
2
+ <p>
3
+ <%%= link_to 'Go home', root_path %>
4
+ </p>
@@ -0,0 +1,4 @@
1
+ <h1>Not found</h1>
2
+ <p>
3
+ <%%= link_to 'Go home', root_path %>
4
+ </p>
@@ -0,0 +1,9 @@
1
+ module R404
2
+ class AccessDenied < StandardError
3
+
4
+ def initialize exception
5
+ super
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,7 @@
1
+ require 'rails/railtie'
2
+
3
+ module R404
4
+ class Engine < ::Rails::Engine
5
+ # isolate_namespace R404
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ module R404
2
+ class NotFound < StandardError
3
+
4
+ def initialize exception
5
+ super
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ require 'rails/railtie'
2
+
3
+ module R404
4
+ class Railtie < ::Rails::Railtie
5
+
6
+ initializer 'r404.action_controller' do
7
+ ActiveSupport.on_load :action_controller do
8
+ include R404Helper
9
+ end
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ module R404
2
+
3
+ VERSION = '1.0.0'
4
+
5
+ end
data/lib/r404.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'r404/version'
2
+
3
+ module R404
4
+
5
+ require 'r404/access_denied'
6
+ require 'r404/not_found'
7
+
8
+ require 'r404/railtie'
9
+ require 'r404/engine'
10
+
11
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: r404
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jonas Hübotter
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-01-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.52'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.52'
55
+ description: Error handler & renderer for Rails
56
+ email: me@jonhue.me
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - CHANGELOG.md
62
+ - LICENSE
63
+ - README.md
64
+ - app/controllers/concerns/r404_controller.rb
65
+ - app/helpers/r404_helper.rb
66
+ - lib/generators/r404_generator.rb
67
+ - lib/generators/templates/access_denied.html.erb
68
+ - lib/generators/templates/not_found.html.erb
69
+ - lib/r404.rb
70
+ - lib/r404/access_denied.rb
71
+ - lib/r404/engine.rb
72
+ - lib/r404/not_found.rb
73
+ - lib/r404/railtie.rb
74
+ - lib/r404/version.rb
75
+ homepage: https://github.com/jonhue/r404
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message: |
80
+ **Thank you for installing r404!**
81
+
82
+
83
+ There are two more steps to take:
84
+
85
+ 1) Run `rails g r404`
86
+ 2) Add `include R404Controller` to `app/controllers/application_controller.rb`
87
+
88
+
89
+ Learn more at https://github.com/jonhue/r404
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '2.3'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.7.4
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Error handler & renderer for Rails
109
+ test_files: []