Skip to content

Commit

Permalink
set_version does not reset lookup context version
Browse files Browse the repository at this point in the history
Fixes #53
  • Loading branch information
bwillis committed Jun 16, 2016
1 parent a530463 commit 7e0aaf0
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 4 deletions.
4 changes: 4 additions & 0 deletions lib/versioncake/controller_additions.rb
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
@@ -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
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
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
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

0 comments on commit 7e0aaf0

Please sign in to comment.