Skip to content
Browse files

support rails 2 and 3, also fail or autoload classes we depend on to …

…prevent silent failures
  • Loading branch information...
1 parent d60a7c6 commit e42d448dc7c20cceae6a61efce6223f76dc51576 @grosser grosser committed
Showing with 53 additions and 8 deletions.
  1. +8 −7 lib/http_accept_language.rb
  2. +45 −1 test/http_accept_language_test.rb
View
15 lib/http_accept_language.rb
@@ -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
View
46 test/http_accept_language_test.rb
@@ -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
@@ -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

0 comments on commit e42d448

Please sign in to comment.
Something went wrong with that request. Please try again.