Skip to content

Commit

Permalink
support rails 2 and 3, also fail or autoload classes we depend on to …
Browse files Browse the repository at this point in the history
…prevent silent failures
  • Loading branch information
grosser committed Jul 12, 2012
1 parent d60a7c6 commit e42d448
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
15 changes: 8 additions & 7 deletions lib/http_accept_language.rb
Expand Up @@ -54,12 +54,13 @@ def compatible_language_from(available_languages)
end
end.compact.first
end

end
if defined?(ActionDispatch::Request)
ActionDispatch::Request.send :include, HttpAcceptLanguage
elsif defined?(ActionDispatch::AbstractRequest)
ActionDispatch::AbstractRequest.send :include, HttpAcceptLanguage
elsif defined?(ActionDispatch::CgiRequest)
ActionDispatch::CgiRequest.send :include, HttpAcceptLanguage

if defined?(ActionPack)
classes = if ActionPack::VERSION::MAJOR == 2
[ActionController::Request, ActionController::CgiRequest]
else
[ActionDispatch::Request]
end
classes.each{|c| c.send :include, HttpAcceptLanguage }
end
46 changes: 45 additions & 1 deletion test/http_accept_language_test.rb
Expand Up @@ -2,6 +2,24 @@
require 'http_accept_language'
require 'test/unit'

module ActionPack
module VERSION
MAJOR = 3
end
end

module ActionDispatch
class Request
end
end

module ActionController
class CgiRequest
end
class Request
end
end

class MockedCgiRequest
include HttpAcceptLanguage
def env
Expand Down Expand Up @@ -37,13 +55,39 @@ def test_should_find_first_compatible_from_user_preferred
request.env['HTTP_ACCEPT_LANGUAGE'] = 'en-us,de-de'
assert_equal 'en', request.compatible_language_from(%w{de en})
end

def test_should_accept_symbols_as_available_languages
request.env['HTTP_ACCEPT_LANGUAGE'] = 'en-us'
assert_equal :"en-HK", request.compatible_language_from([:"en-HK"])
end

def test_should_be_included_into_actionpack_v2
silence_warnings do
ActionPack::VERSION.const_set(:MAJOR, 2)
end
load "http_accept_language.rb"

assert_include ActionController::Request.ancestors, HttpAcceptLanguage
assert_include ActionController::CgiRequest.ancestors, HttpAcceptLanguage
end

def test_should_be_included_into_actionpack_v3
silence_warnings do
ActionPack::VERSION.const_set(:MAJOR, 3)
end
load "http_accept_language.rb"
assert_include ActionDispatch::Request.ancestors, HttpAcceptLanguage
end

private

def silence_warnings
old_verbose, $VERBOSE = $VERBOSE, nil
yield
ensure
$VERBOSE = old_verbose
end

def request
@request ||= MockedCgiRequest.new
end
Expand Down

0 comments on commit e42d448

Please sign in to comment.