Skip to content

Commit 83617bd

Browse files
serggldblock
authored andcommitted
Rails 5 support (#70)
1 parent 9d313b2 commit 83617bd

File tree

6 files changed

+49
-14
lines changed

6 files changed

+49
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### 0.2.3 (Next)
22

3+
* [#70](https://github.com/ruby-grape/grape-swagger-rails/pull/70): Rails 5 support - [@serggl](https://github.com/serggl).
34
* [#68](https://github.com/ruby-grape/grape-swagger-rails/pull/68): Added danger, PR linter - [@dblock](https://github.com/dblock).
45
* Your contribution here.
56

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ GrapeSwaggerRails.options.url = '/swagger_doc.json'
4848
GrapeSwaggerRails.options.app_url = 'http://swagger.wordnik.com'
4949
```
5050

51-
You can dynamically set `app_url` for each request use a `before_filter_proc`:
51+
You can dynamically set `app_url` for each request use a `before_action`:
5252

5353
```ruby
54-
GrapeSwaggerRails.options.before_filter_proc = proc {
54+
GrapeSwaggerRails.options.before_action do
5555
GrapeSwaggerRails.options.app_url = request.protocol + request.host_with_port
56-
}
56+
end
5757
```
5858

5959
You can set the app name, default is "Swagger".
@@ -118,7 +118,7 @@ If you will know the Authentication key prior to page load or you wish to set it
118118
GrapeSwaggerRails.options.api_key_default_value = 'your_default_value'
119119
```
120120

121-
To set it based on the `current_user` or other request-based parameters, try using it inside of your `before_filter` (See Swagger UI Authorization)
121+
To set it based on the `current_user` or other request-based parameters, try using it inside of your `before_action` (See Swagger UI Authorization)
122122

123123
### API Token Authentication
124124

@@ -136,7 +136,7 @@ You may want to authenticate users before displaying the Swagger UI, particularl
136136
Use the `before` option to inspect the request before Swagger UI:
137137

138138
```ruby
139-
GrapeSwaggerRails.options.before_filter do |request|
139+
GrapeSwaggerRails.options.before_action do |request|
140140
# 1. Inspect the `request` or access the Swagger UI controller via `self`.
141141
# 2. Check `current_user` or `can? :access, :api`, etc.
142142
# 3. Redirect or error in case of failure.
@@ -148,7 +148,7 @@ end
148148
Add the following code to the initializer (swagger.rb):
149149

150150
```ruby
151-
GrapeSwaggerRails.options.before_filter do |request|
151+
GrapeSwaggerRails.options.before_action do |request|
152152
GrapeSwaggerRails.options.api_key_default_value = current_user.token.token
153153
end
154154
```

UPGRADING.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Upgrading Grape Swagger Rails
2+
=============================
3+
4+
### Upgrading to >= 0.2.3
5+
6+
#### Changes to Options
7+
8+
The `GrapeSwaggerRails.options.before_filter` option have been deprecated since 0.2.3.
9+
10+
Please use `GrapeSwaggerRails.options.before_action` instead.
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
module GrapeSwaggerRails
22
class ApplicationController < ActionController::Base
3-
before_filter do
4-
if GrapeSwaggerRails.options.before_filter
5-
instance_exec(request, &GrapeSwaggerRails.options.before_filter)
6-
end
3+
if Rails::VERSION::MAJOR >= 4
4+
before_action { run_before_action }
5+
else
6+
before_filter { run_before_action }
77
end
88

99
def index
1010
end
11+
12+
private
13+
14+
def run_before_action
15+
return unless GrapeSwaggerRails.options.before_action
16+
instance_exec(request, &GrapeSwaggerRails.options.before_action)
17+
end
1118
end
1219
end

lib/grape-swagger-rails.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@
33
module GrapeSwaggerRails
44
class Options < OpenStruct
55
def before_filter(&block)
6+
ActiveSupport::Deprecation.warn('This option is deprecated and going to be removed in 0.3.0. ' \
7+
'Please use `before_action` instead')
8+
before_action(&block)
9+
end
10+
11+
def before_action(&block)
612
if block_given?
7-
self.before_filter_proc = block
13+
self.before_action_proc = block
814
else
9-
before_filter_proc
15+
before_action_proc
1016
end
1117
end
1218
end
@@ -29,7 +35,7 @@ def before_filter(&block)
2935
doc_expansion: 'none',
3036
supported_submit_methods: %w(get post put delete patch),
3137

32-
before_filter_proc: nil, # Proc used as a controller before filter
38+
before_action_proc: nil, # Proc used as a controller before action
3339

3440
hide_url_input: false,
3541
hide_api_key_input: false

spec/features/swagger_spec.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,18 @@
101101
end
102102
context '#before_filter' do
103103
before do
104-
GrapeSwaggerRails.options.before_filter do |_request|
104+
allow(ActiveSupport::Deprecation).to receive(:warn)
105+
end
106+
it 'throws deprecation warning' do
107+
GrapeSwaggerRails.options.before_filter { true }
108+
109+
expect(ActiveSupport::Deprecation).to have_received(:warn).with('This option is deprecated ' \
110+
'and going to be removed in 0.3.0. Please use `before_action` instead')
111+
end
112+
end
113+
context '#before_action' do
114+
before do
115+
GrapeSwaggerRails.options.before_action do |_request|
105116
flash[:error] = 'Unauthorized Access'
106117
redirect_to '/'
107118
false

0 commit comments

Comments
 (0)