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

set_version does not reset lookup context version #54

Merged
merged 2 commits into from
Jun 17, 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
Expand Up @@ -6,6 +6,7 @@ Bug Fixes:

* Deprecated versions would not render properly (#47)
* Blank `api_version` does not raise `MissingVersionError` (#50)
* Fix `set_version` not overriding version number (#54)

Enhancements:

Expand Down
4 changes: 4 additions & 0 deletions lib/versioncake/controller_additions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ def configure_rails_view_versioning(version_context)
end

def set_version(version)
service = VersionCake::VersionContextService.new(VersionCake.config)
request.env['versioncake.context'] = service.create_context_from_context(version_context, version)
@request_version = version

check_version!
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/versioncake/version_context.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module VersionCake
class VersionContext
attr_reader :version, :resource, :result
attr_reader :resource, :result
attr_accessor :version

def initialize(version, resource, result)
@version, @resource, @result = version, resource, result
Expand Down
6 changes: 6 additions & 0 deletions lib/versioncake/version_context_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ def create_context(uri, version)
VersionCake::VersionContext.new(version, resource, result)
end

def create_context_from_context(context, version)
result = check_version(context.resource, version)

VersionCake::VersionContext.new(version, context.resource, result)
end

private

def check_version(resource, version)
Expand Down
6 changes: 3 additions & 3 deletions spec/integration/controller/renders_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@
end

context '#set_version' do
let(:request_options) { { 'override_version' => 2 } }
let(:request_options) { { 'override_version' => 1 } }
let(:request_version) { 3 }

it { expect(controller.request_version).to eq 2 }
it { expect(response_body).to eq 'template v2' }
it { expect(controller.request_version).to eq 1 }
it { expect(response_body).to eq 'template v1' }
end
end

Expand Down
30 changes: 30 additions & 0 deletions spec/unit/version_context_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,34 @@
it { expect(context.result).to eq :obsolete }
end
end

describe '#create_context_from_context' do
let(:uri) { 'users/21' }
let(:existing_version) { 1 }
let(:existing_context) { service.create_context(uri, existing_version) }
let(:version) { 5 }
subject(:context) { service.create_context_from_context(existing_context, version) }

it { expect(context.version).to eq 5 }
it { expect(context.resource).to eq resource_user }
it { expect(context.result).to eq :supported }

context 'for a deprecated version' do
let(:uri) { 'posts/21' }
let(:version) { 5 }

it { expect(context.version).to eq 5 }
it { expect(context.resource).to eq resource_all }
it { expect(context.result).to eq :deprecated }
end

context 'for an obsolete version' do
let(:uri) { 'posts/21' }
let(:version) { 2 }

it { expect(context.version).to eq 2 }
it { expect(context.resource).to eq resource_all }
it { expect(context.result).to eq :obsolete }
end
end
end