straides 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +41 -4
- data/lib/straides/configuration.rb +30 -0
- data/lib/straides/railtie.rb +15 -0
- data/lib/straides/version.rb +1 -1
- data/lib/straides.rb +4 -2
- metadata +21 -8
data/README.md
CHANGED
@@ -3,17 +3,29 @@
|
|
3
3
|
Better HTTP STatus coDES for RAIls!
|
4
4
|
|
5
5
|
Straides provides a convenient and more consistent way for handling error conditions
|
6
|
-
in controllers.
|
6
|
+
in controllers. It makes it possible, for example, to abort execution of a request in a before_filter.
|
7
7
|
|
8
|
-
|
8
|
+
# Installation
|
9
|
+
|
10
|
+
1. Add the gem to your Gemfile.
|
11
|
+
|
12
|
+
gem 'straides'
|
13
|
+
|
14
|
+
2. Update your gem bundle.
|
15
|
+
|
16
|
+
$ bundle install
|
9
17
|
|
10
18
|
|
11
19
|
# Usage
|
12
20
|
|
21
|
+
Straides provides a helper method called `error`. Call it at any time with the HTTP error code
|
22
|
+
and optionally additional parameters for `render` to abort the current request and render an
|
23
|
+
error message.
|
24
|
+
|
13
25
|
# Stop controller execution and return the request with the given HTTP error.
|
14
26
|
error 404 unless [condition]
|
15
27
|
|
16
|
-
#
|
28
|
+
# Abort with an error if an operation didn't return a result.
|
17
29
|
user = User.find_by_username(params[:id]) or error 401
|
18
30
|
|
19
31
|
|
@@ -24,7 +36,7 @@ Straides returns reasonable default responses dependent on the request format.
|
|
24
36
|
* If the request returns HTML, it renders `public/[error code].html`, similar to what Rails does.
|
25
37
|
* If the request returns JSON, it leaves the response body empty by default.
|
26
38
|
|
27
|
-
These behaviors can be
|
39
|
+
These behaviors can be customized by providing the call to `error` with the same parameters
|
28
40
|
as you would give to [render](http://apidock.com/rails/ActionController/Base/render) in Rails.
|
29
41
|
|
30
42
|
# Provide a custom error message in the response body.
|
@@ -35,3 +47,28 @@ as you would give to [render](http://apidock.com/rails/ActionController/Base/ren
|
|
35
47
|
|
36
48
|
# Render a custom JSON response.
|
37
49
|
error 401, :json => { :code => 401, :message => 'Please log in first.' }
|
50
|
+
|
51
|
+
|
52
|
+
# Customizing Straides
|
53
|
+
|
54
|
+
Straides can be configured by creating an initializer file at _config/initializers/straides.rb_.
|
55
|
+
|
56
|
+
Straides.configure do |config|
|
57
|
+
|
58
|
+
# Disable auto-loading of Straides into every controller.
|
59
|
+
config.auto_load = false
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
## Auto-loading
|
64
|
+
|
65
|
+
Straides loads the `error` helper method by default into every controller.
|
66
|
+
If you don't want that, for example because you only want to use Straides in certain controllers,
|
67
|
+
you can disable this auto-loading behavior with `config.auto_load = false`.
|
68
|
+
Please note that you then have to include Straides into every controller manually, like this:
|
69
|
+
|
70
|
+
class FooController < ApplicationController
|
71
|
+
include Straides
|
72
|
+
|
73
|
+
...
|
74
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Straides
|
2
|
+
|
3
|
+
class Configuration
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
|
7
|
+
# Whether to auto-include the Straides config into every controller.
|
8
|
+
@auto_load = true
|
9
|
+
end
|
10
|
+
|
11
|
+
attr_accessor :auto_load
|
12
|
+
|
13
|
+
# Returns the singleton instance of this Configuration class,
|
14
|
+
# to be used for configuring the Rails app that uses this gem.
|
15
|
+
def self.instance
|
16
|
+
@__instance__ ||= new
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# Helper method for configuring Straides in an initializer file.
|
21
|
+
#
|
22
|
+
# Example:
|
23
|
+
# Straides::Configuration.configure do |config|
|
24
|
+
# config.auto_load = false
|
25
|
+
# end
|
26
|
+
def self.configure
|
27
|
+
raise RuntimeError("ERROR: You must provide a block to Straides.configure.") unless block_given?
|
28
|
+
yield Configuration.instance
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "rails/railtie"
|
2
|
+
|
3
|
+
module Straides
|
4
|
+
|
5
|
+
class Railtie < Rails::Railtie
|
6
|
+
|
7
|
+
# Initialize Straides after the Rails initializers have run.
|
8
|
+
config.after_initialize do
|
9
|
+
if Straides::Configuration.instance.auto_load
|
10
|
+
ActionController::Base.send :include, Straides
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
data/lib/straides/version.rb
CHANGED
data/lib/straides.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
require 'straides/configuration'
|
3
|
+
require 'straides/railtie'
|
1
4
|
require 'straides/return_http_code_error'
|
2
5
|
require 'straides/version'
|
3
|
-
require 'active_support/concern'
|
4
6
|
|
5
7
|
module Straides
|
6
8
|
extend ActiveSupport::Concern
|
@@ -25,7 +27,7 @@ module Straides
|
|
25
27
|
def show_error(error)
|
26
28
|
unless error.has_template?
|
27
29
|
if request.send(:format).html?
|
28
|
-
error.render_options[:file] = "public/#{error.render_options[:status] || '
|
30
|
+
error.render_options[:file] = "public/#{error.render_options[:status] || '500'}"
|
29
31
|
error.render_options[:formats] = [:html]
|
30
32
|
else
|
31
33
|
error.render_options[:nothing] = true
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: straides
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-02-
|
12
|
+
date: 2012-02-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement: &
|
16
|
+
requirement: &70323458738280 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,21 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70323458738280
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rails
|
27
|
+
requirement: &70323458737760 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70323458737760
|
25
36
|
- !ruby/object:Gem::Dependency
|
26
37
|
name: rake
|
27
|
-
requirement: &
|
38
|
+
requirement: &70323458736960 !ruby/object:Gem::Requirement
|
28
39
|
none: false
|
29
40
|
requirements:
|
30
41
|
- - ! '>='
|
@@ -32,10 +43,10 @@ dependencies:
|
|
32
43
|
version: '0'
|
33
44
|
type: :development
|
34
45
|
prerelease: false
|
35
|
-
version_requirements: *
|
46
|
+
version_requirements: *70323458736960
|
36
47
|
- !ruby/object:Gem::Dependency
|
37
48
|
name: rspec
|
38
|
-
requirement: &
|
49
|
+
requirement: &70323458736120 !ruby/object:Gem::Requirement
|
39
50
|
none: false
|
40
51
|
requirements:
|
41
52
|
- - ! '>='
|
@@ -43,7 +54,7 @@ dependencies:
|
|
43
54
|
version: '0'
|
44
55
|
type: :development
|
45
56
|
prerelease: false
|
46
|
-
version_requirements: *
|
57
|
+
version_requirements: *70323458736120
|
47
58
|
description: A more convenient way to return different HTTP status codes from Rails.
|
48
59
|
email:
|
49
60
|
- kevin.goslar@gmail.com
|
@@ -51,6 +62,8 @@ executables: []
|
|
51
62
|
extensions: []
|
52
63
|
extra_rdoc_files: []
|
53
64
|
files:
|
65
|
+
- lib/straides/configuration.rb
|
66
|
+
- lib/straides/railtie.rb
|
54
67
|
- lib/straides/return_http_code_error.rb
|
55
68
|
- lib/straides/version.rb
|
56
69
|
- lib/straides.rb
|