From 683d977a4131b19c473099c909b8709d22a88e05 Mon Sep 17 00:00:00 2001 From: Joel Lubrano Date: Thu, 5 Sep 2019 18:12:57 -0400 Subject: [PATCH 1/4] Resolve DEPRECATION WARNINGs for Rails.application.class.parent Removes/resolves the following deprecation warning by replacing calls to Module#parent with Module#module_parent. ``` DEPRECATION WARNING: `Module#parent` has been renamed to `module_parent`. `parent` is deprecated and will be removed in Rails 6.1. (called from initialize at /Users/joellubrano/Projects/stitch_fix/stitches/lib/stitches/api_key.rb:25) ``` This change is safe as `Module#parent` calls `module_parent` in its implementation. Rails docs: https://edgeapi.rubyonrails.org/classes/Module.html#method-i-parent Rails source: https://github.com/rails/rails/blob/0434e5ba23ab0d5b07e1d3802360cef626d3630e/activesupport/lib/active_support/core_ext/module/introspection.rb#L51 --- lib/stitches/api_key.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/stitches/api_key.rb b/lib/stitches/api_key.rb index d9f4716..9d5b453 100644 --- a/lib/stitches/api_key.rb +++ b/lib/stitches/api_key.rb @@ -22,7 +22,7 @@ class ApiKey < Stitches::AllowlistMiddleware def initialize(app,options = {}) super(app,options) - @realm = Rails.application.class.parent.to_s + @realm = Rails.application.class.module_parent.to_s end protected From 20d91522b048dd0daa2bcb70ac7604d0a95208b0 Mon Sep 17 00:00:00 2001 From: Joel Lubrano Date: Thu, 5 Sep 2019 18:48:57 -0400 Subject: [PATCH 2/4] Modify the use of Module#module_parent to be Rails 5 compatible --- lib/stitches/api_key.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/stitches/api_key.rb b/lib/stitches/api_key.rb index 9d5b453..04b2af8 100644 --- a/lib/stitches/api_key.rb +++ b/lib/stitches/api_key.rb @@ -22,7 +22,7 @@ class ApiKey < Stitches::AllowlistMiddleware def initialize(app,options = {}) super(app,options) - @realm = Rails.application.class.module_parent.to_s + @realm = rails_app_module end protected @@ -57,6 +57,12 @@ def do_call(env) private + 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}" }) From 69f79cdd8aaf619e3afdca5841059cbcb6484edb Mon Sep 17 00:00:00 2001 From: Joel Lubrano Date: Fri, 6 Sep 2019 15:19:18 -0400 Subject: [PATCH 3/4] Add a version guard to generated feature spec The goal is to avoid using the deprecated Module#parent method in Rails 6 apps. --- .../generator_files/spec/features/api_spec.rb.erb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/stitches/generator_files/spec/features/api_spec.rb.erb b/lib/stitches/generator_files/spec/features/api_spec.rb.erb index 21a1154..a5b2046 100644 --- a/lib/stitches/generator_files/spec/features/api_spec.rb.erb +++ b/lib/stitches/generator_files/spec/features/api_spec.rb.erb @@ -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 From 3c8a798ed5e702e405ab3147a39d8862c18a1e9b Mon Sep 17 00:00:00 2001 From: Joel Lubrano Date: Tue, 10 Sep 2019 16:57:04 -0400 Subject: [PATCH 4/4] Add comment explaining the cautious use of Rails.application.class.module_parent --- lib/stitches/api_key.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/stitches/api_key.rb b/lib/stitches/api_key.rb index 04b2af8..9081c8a 100644 --- a/lib/stitches/api_key.rb +++ b/lib/stitches/api_key.rb @@ -57,6 +57,11 @@ 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