Skip to content

add support for scoped callbcaks within a version block #901

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

Merged
merged 1 commit into from
Jan 18, 2015
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
Expand Up @@ -6,6 +6,7 @@ Next Release
* [#876](https://github.com/intridea/grape/pull/876): Call to `declared(params)` now returns a `Hashie::Mash` - [@rodzyn](https://github.com/rodzyn).
* [#879](https://github.com/intridea/grape/pull/879): The `route_info` value is no longer included in `params` Hash - [@rodzyn](https://github.com/rodzyn).
* [#881](https://github.com/intridea/grape/issues/881): Fixed `Grape::Validations::ValuesValidator` support for `Range` type - [@ajvondrak](https://github.com/ajvondrak).
* [#901](https://github.com/intridea/grape/pull/901): Fix: callbacks defined in a version block are only called for the routes defined in that block - [@kushkella](https://github.com/kushkella).
* Your contribution here.

0.10.1 (12/28/2014)
Expand Down
41 changes: 37 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2162,6 +2162,39 @@ GET /123 # 'Fixnum'
GET /foo # 400 error - 'blah is invalid'
```

When a callback is defined within a version block, it's only called for the routes defined in that block.

```ruby
class Test < Grape::API
resource :foo do
version 'v1', :using => :path do
before do
@output ||= 'v1-'
end
get '/' do
@output += 'hello'
end
end

version 'v2', :using => :path do
before do
@output ||= 'v2-'
end
get '/' do
@output += 'hello'
end
end
end
end
```

The behaviour is then:

```bash
GET /foo/v1 # 'v1-hello'
GET /foo/v2 # 'v2-hello'
```

## Anchoring

Grape by default anchors all request paths, which means that the request URL
Expand Down Expand Up @@ -2270,13 +2303,13 @@ class Twitter::APITest < MiniTest::Unit::TestCase
def app
Twitter::API
end

def test_get_api_statuses_public_timeline_returns_an_empty_array_of_statuses
get "/api/statuses/public_timeline"
assert last_response.ok?
assert_equal JSON.parse(last_response.body), []
end

def test_get_api_statuses_id_returns_a_status_by_id
status = Status.create!
get "/api/statuses/#{status.id}"
Expand Down Expand Up @@ -2326,13 +2359,13 @@ class Twitter::APITest < ActiveSupport::TestCase
def app
Rails.application
end

test "GET /api/statuses/public_timeline returns an empty array of statuses" do
get "/api/statuses/public_timeline"
assert last_response.ok?
assert_equal JSON.parse(last_response.body), []
end

test "GET /api/statuses/:id returns a status by id" do
status = Status.create!
get "/api/statuses/#{status.id}"
Expand Down
7 changes: 7 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ Key route_info is excluded from params.

See [#879](https://github.com/intridea/grape/pull/879) for more information.


#### Fix callbacks within a version block

Callbacks defined in a version block are only called for the routes defined in that block. This was a regression introduced in Grape 0.10.0, and is fixed in this version.

See [#901](https://github.com/intridea/grape/pull/901) for more information.

### Upgrading to >= 0.10.1

#### Changes to `declared(params, include_missing: false)`
Expand Down
13 changes: 10 additions & 3 deletions lib/grape/dsl/routing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,17 @@ def version(*args, &block)

@versions = versions | args

namespace_inheritable(:version, args)
namespace_inheritable(:version_options, options)
if block_given?
within_namespace do
namespace_inheritable(:version, args)
namespace_inheritable(:version_options, options)

instance_eval(&block) if block_given?
instance_eval(&block)
end
else
namespace_inheritable(:version, args)
namespace_inheritable(:version_options, options)
end

# reset_validations!
end
Expand Down
32 changes: 32 additions & 0 deletions spec/shared/versioning_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,38 @@
end
end

context 'with before block defined within a version block' do
it 'calls before block that is defined within the version block' do
subject.format :txt
subject.prefix 'api'
subject.version 'v2', macro_options do
before do
@output ||= 'v2-'
end
get 'version' do
@output += 'version'
end
end

subject.version 'v1', macro_options do
before do
@output ||= 'v1-'
end
get 'version' do
@output += 'version'
end
end

versioned_get '/version', 'v1', macro_options.merge(prefix: subject.prefix)
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('v1-version')

versioned_get '/version', 'v2', macro_options.merge(prefix: subject.prefix)
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('v2-version')
end
end

it 'does not overwrite version parameter with API version' do
subject.format :txt
subject.version 'v1', macro_options
Expand Down