Skip to content

Commit

Permalink
Merge pull request #11 from Econify/master
Browse files Browse the repository at this point in the history
Add Default Version override
  • Loading branch information
bwillis committed May 16, 2013
2 parents b1c6542 + a8a5d93 commit 0e4262e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/versioncake/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ module Configuration
mattr_accessor :extraction_strategies
self.extraction_strategies = [VersionCake::QueryParameterStrategy.new]

mattr_accessor :default_version
self.default_version = nil

def self.extraction_strategy=(val)
@@extraction_strategies.clear
Array.wrap(val).each do |configured_strategy|
Expand Down
4 changes: 4 additions & 0 deletions lib/versioncake/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class ActionViewVersions < Rails::Railtie
if app.config.respond_to?(:view_version_string)
VersionCake::ExtractionStrategy.version_string = app.config.view_version_string
end

if app.config.respond_to?(:default_version)
VersionCake::Configuration.default_version = app.config.default_version
end
end
end
end
2 changes: 1 addition & 1 deletion lib/versioncake/versioned_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def apply_strategies(request)
def extract_version(request)
@extracted_version = apply_strategies(request)
if @extracted_version.nil?
@version = VersionCake::Configuration.latest_version
@version = VersionCake::Configuration.default_version || VersionCake::Configuration.latest_version
elsif VersionCake::Configuration.supports_version? @extracted_version
@version = @extracted_version
elsif @extracted_version > VersionCake::Configuration.latest_version
Expand Down
18 changes: 17 additions & 1 deletion test/functional/renders_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,18 @@ class RendersControllerTest < ActionController::TestCase
assert !@controller.is_latest_version
end

test "exposes the derived version when the version is not set" do
test "exposes the derived version when the version is not set and no default" do
get :index
assert_equal 3, @controller.derived_version
end

test "exposes the default version when the version is not set default is set" do
VersionCake::Configuration.default_version = 1
get :index
assert_equal 1, @controller.derived_version
VersionCake::Configuration.default_version = nil
end

test "requested version is blank when the version is not set" do
get :index
assert_blank @controller.requested_version
Expand Down Expand Up @@ -141,6 +148,15 @@ class AcceptHeaderStrategyTest < ActionController::TestCase
get :index
assert_equal @response.body, "index.v2.html.erb"
end

test "render the default version version of the partial" do
VersionCake::Configuration.default_version = 1
@controller.request.stubs(:headers).returns({"HTTP_ACCEPT" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8;api_version=abc"})
get :index
assert_equal @response.body, "index.v1.html.erb"
VersionCake::Configuration.default_version = nil
end

end

class CustomStrategyTest < ActionController::TestCase
Expand Down

0 comments on commit 0e4262e

Please sign in to comment.