Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails 5 support #70

Merged
merged 7 commits into from
Sep 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### 0.2.3 (Next)

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

Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ GrapeSwaggerRails.options.url = '/swagger_doc.json'
GrapeSwaggerRails.options.app_url = 'http://swagger.wordnik.com'
```

You can dynamically set `app_url` for each request use a `before_filter_proc`:
You can dynamically set `app_url` for each request use a `before_action`:

```ruby
GrapeSwaggerRails.options.before_filter_proc = proc {
GrapeSwaggerRails.options.before_action do
GrapeSwaggerRails.options.app_url = request.protocol + request.host_with_port
}
end
```

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

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)
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)

### API Token Authentication

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

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

```ruby
GrapeSwaggerRails.options.before_filter do |request|
GrapeSwaggerRails.options.before_action do |request|
GrapeSwaggerRails.options.api_key_default_value = current_user.token.token
end
```
Expand Down
10 changes: 10 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Upgrading Grape Swagger Rails
=============================

### Upgrading to >= 0.2.3

#### Changes to Options

The `GrapeSwaggerRails.options.before_filter` option have been deprecated since 0.2.3.

Please use `GrapeSwaggerRails.options.before_action` instead.
15 changes: 11 additions & 4 deletions app/controllers/grape_swagger_rails/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
module GrapeSwaggerRails
class ApplicationController < ActionController::Base
before_filter do
if GrapeSwaggerRails.options.before_filter
instance_exec(request, &GrapeSwaggerRails.options.before_filter)
end
if Rails::VERSION::MAJOR >= 4
before_action { run_before_action }
else
before_filter { run_before_action }
end

def index
end

private

def run_before_action
return unless GrapeSwaggerRails.options.before_action
instance_exec(request, &GrapeSwaggerRails.options.before_action)
end
end
end
12 changes: 9 additions & 3 deletions lib/grape-swagger-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
module GrapeSwaggerRails
class Options < OpenStruct
def before_filter(&block)
ActiveSupport::Deprecation.warn('This option is deprecated and going to be removed in 0.3.0. ' \
'Please use `before_action` instead')
before_action(&block)
end

def before_action(&block)
if block_given?
self.before_filter_proc = block
self.before_action_proc = block
else
before_filter_proc
before_action_proc
end
end
end
Expand All @@ -29,7 +35,7 @@ def before_filter(&block)
doc_expansion: 'none',
supported_submit_methods: %w(get post put delete patch),

before_filter_proc: nil, # Proc used as a controller before filter
before_action_proc: nil, # Proc used as a controller before action

hide_url_input: false,
hide_api_key_input: false
Expand Down
13 changes: 12 additions & 1 deletion spec/features/swagger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,18 @@
end
context '#before_filter' do
before do
GrapeSwaggerRails.options.before_filter do |_request|
allow(ActiveSupport::Deprecation).to receive(:warn)
end
it 'throws deprecation warning' do
GrapeSwaggerRails.options.before_filter { true }

expect(ActiveSupport::Deprecation).to have_received(:warn).with('This option is deprecated ' \
'and going to be removed in 0.3.0. Please use `before_action` instead')
end
end
context '#before_action' do
before do
GrapeSwaggerRails.options.before_action do |_request|
flash[:error] = 'Unauthorized Access'
redirect_to '/'
false
Expand Down