Skip to content

Commit ba3c3dd

Browse files
committed
Add backwards compatibility with Rails 4
Most of the consuming apps are on Rails 4, so it is important that this gem continue to work with them. There are two main changed in this commit. First to fix the monkey patch to ActionController's `request_http_token_authentication` because it changes airty between Rails 4 and Rails 5. It adds a second optional param, but rails won't let you override a method unless the signature matches -- so this required two different `def`s to set this based on the Rails version number. I looked for a way to get the airty of a module method but after 20 min gave up and just checked the rails version number instead. The second is to use the different syntax with ActionDispatch's integration tests syntax. The `get` method now expects `params` and `headers` parameters to be named parameters. Again, I just gave up and checked the Rails version and made the decision. This was needed because I was juggling the rails versions in the gemspec to test compatibility.
1 parent 729f605 commit ba3c3dd

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

lib/kracken/controllers/token_authenticatable.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,16 @@ def self.included(base)
1313
end
1414
end
1515

16-
# NOTE: Monkey-patch until this is merged into the gem
17-
def request_http_token_authentication(realm = 'Application', message = nil)
18-
headers["WWW-Authenticate"] = %(Token realm="#{realm.gsub(/"/, "")}")
19-
raise TokenUnauthorized, "Invalid Credentials"
16+
if Rails::VERSION::MAJOR >= 5
17+
def request_http_token_authentication(realm = 'Application', message = nil)
18+
headers["WWW-Authenticate"] = %(Token realm="#{realm.gsub(/"/, "")}")
19+
raise TokenUnauthorized, "Invalid Credentials"
20+
end
21+
else
22+
def request_http_token_authentication(realm = 'Application')
23+
headers["WWW-Authenticate"] = %(Token realm="#{realm.gsub(/"/, "")}")
24+
raise TokenUnauthorized, "Invalid Credentials"
25+
end
2026
end
2127

2228
private

spec/dummy/config/environments/test.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,15 @@
1414
config.eager_load = false
1515

1616
# Configure static asset server for tests with Cache-Control for performance.
17-
config.public_file_server.enabled = true
18-
config.public_file_server.headers = { 'Cache-Control' => 'public, max-age=3600' }
17+
if config.respond_to? :public_file_server
18+
# Hot new Rails 5 Settings
19+
config.public_file_server.enabled = true
20+
config.public_file_server.headers = { 'Cache-Control' => 'public, max-age=3600' }
21+
else
22+
# Deprecated Rails 4 Settings
23+
config.serve_static_files = true
24+
config.static_cache_control = "public, max-age=3600"
25+
end
1926

2027
# Show full error reports and disable caching.
2128
config.consider_all_requests_local = true

spec/requests/api_spec.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ def headers_with_token(token)
1818
stub_request(:get, "https://account.radiusnetworks.com/auth/radius/user.json?oauth_token=123")
1919
.to_return(status: 200, body: json)
2020

21-
get api_index_path, params: {}, headers: headers_with_token("123")
21+
# Temporary work around while we support versions of Rails before 4
22+
if Rails::VERSION::MAJOR >= 5
23+
get api_index_path, params: {}, headers: headers_with_token("123")
24+
else
25+
get api_index_path, {}, headers_with_token("123")
26+
end
2227

2328
expect(response.status).to be 200
2429
end

0 commit comments

Comments
 (0)