Skip to content

Commit

Permalink
Merge pull request #83 from stitchfix/chore/change-rails-parent-to-mo…
Browse files Browse the repository at this point in the history
…dule_parent

Resolve DEPRECATION WARNINGs for Rails.application.class.parent
  • Loading branch information
Joel Lubrano authored Sep 10, 2019
2 parents 46a6b5a + 3c8a798 commit 07f460c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
13 changes: 12 additions & 1 deletion lib/stitches/api_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ApiKey < Stitches::AllowlistMiddleware

def initialize(app,options = {})
super(app,options)
@realm = Rails.application.class.parent.to_s
@realm = rails_app_module
end

protected
Expand Down Expand Up @@ -57,6 +57,17 @@ def do_call(env)

private

# TODO: (jdlubrano)
# Once Rails 5 support is no longer necessary, we can simply call
# Rails.application.class.module_parent. The module_parent method
# does not exist in Rails <= 5, though, so we need to gracefully fallback
# Rails.application.class.parent for Rails versions predating Rails 6.0.0.
def rails_app_module
application_class = Rails.application.class
parent = application_class.try(:module_parent) || application_class.parent
parent.to_s
end

class UnauthorizedResponse < Rack::Response
def initialize(reason,realm,custom_http_auth_scheme)
super("Unauthorized - #{reason}", 401, { "WWW-Authenticate" => "#{custom_http_auth_scheme} realm=#{realm}" })
Expand Down
12 changes: 10 additions & 2 deletions lib/stitches/generator_files/spec/features/api_spec.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,22 @@ feature "general API stuff" do
failure_message do
correct_code,_ = evaluate_response(response)
if correct_code
"Expected WWW-Authenticate header to be 'CustomKeyAuth realm=#{Rails.application.class.parent.to_s}', but was #{response['WWW-Authenticate']}"
"Expected WWW-Authenticate header to be 'CustomKeyAuth realm=#{realm}', but was #{response['WWW-Authenticate']}"
else
"Expected response to be 401, but was #{response.response_code}"
end
end

def realm
<% if ::Rails::VERSION::MAJOR >= 6 -%>
Rails.application.class.module_parent.to_s
<% else %>
Rails.application.class.parent.to_s
<% end %>
end

def evaluate_response(response)
[response.response_code == 401, response.headers["WWW-Authenticate"] == "CustomKeyAuth realm=#{Rails.application.class.parent.to_s}" ]
[response.response_code == 401, response.headers["WWW-Authenticate"] == "CustomKeyAuth realm=#{realm}"]
end
end
end

0 comments on commit 07f460c

Please sign in to comment.