From 846cd1909fef5ad46ab05a7e6d13b801f49134af Mon Sep 17 00:00:00 2001 From: Francois Buys Date: Wed, 24 May 2023 01:46:19 +0200 Subject: [PATCH] Fix backward compatible Pundit include This commit fixes: https://github.com/thoughtbot/administrate/issues/2378 By adding a conditional that includes Pundit::Authorization when it is defined else it includes Pundit. This change ensures that we remain compatible with Pundit < 2.2.0 To test with Pundit "~> 2.1.0", we run: ``` bundle exec appraisal pundit21 rspec spec/controllers/concerns/administrate/punditize_spec.rb ``` See: https://github.com/thoughtbot/administrate/pull/2141 See: https://github.com/thoughtbot/administrate/pull/1068 --- Appraisals | 4 ++ .../concerns/administrate/punditize.rb | 7 ++- gemfiles/pundit21.gemfile | 49 +++++++++++++++++++ .../concerns/administrate/punditize_spec.rb | 25 +++++++--- 4 files changed, 78 insertions(+), 7 deletions(-) create mode 100644 gemfiles/pundit21.gemfile diff --git a/Appraisals b/Appraisals index 5a95ffc65a..dc1cba6bc7 100644 --- a/Appraisals +++ b/Appraisals @@ -9,3 +9,7 @@ end appraise "rails70" do gem "rails", "~> 6.1" end + +appraise "pundit21" do + gem "pundit", "~> 2.1.0" +end diff --git a/app/controllers/concerns/administrate/punditize.rb b/app/controllers/concerns/administrate/punditize.rb index f655cc16c0..6b4c67aba1 100644 --- a/app/controllers/concerns/administrate/punditize.rb +++ b/app/controllers/concerns/administrate/punditize.rb @@ -2,7 +2,12 @@ module Administrate module Punditize if Object.const_defined?("Pundit") extend ActiveSupport::Concern - include Pundit::Authorization + + if Pundit.const_defined?(:Authorization) + include Pundit::Authorization + else + include Pundit + end included do private diff --git a/gemfiles/pundit21.gemfile b/gemfiles/pundit21.gemfile new file mode 100644 index 0000000000..67a3866199 --- /dev/null +++ b/gemfiles/pundit21.gemfile @@ -0,0 +1,49 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +ruby "3.2.2" + +gem "administrate-field-image" +gem "faker" +gem "front_matter_parser" +gem "globalid" +gem "kaminari-i18n" +gem "pg" +gem "pundit", "~> 2.1.0" +gem "redcarpet" +gem "sentry-raven" +gem "unicorn" + +group :development, :test do + gem "appraisal" + gem "awesome_print" + gem "byebug" + gem "dotenv-rails" + gem "factory_bot_rails" + gem "i18n-tasks", "1.0.12" + gem "pry" + gem "yard" +end + +group :test do + gem "ammeter" + gem "capybara" + gem "database_cleaner" + gem "formulaic" + gem "launchy" + gem "selenium-webdriver" + gem "shoulda-matchers" + gem "timecop" + gem "webdrivers" + gem "webmock" + gem "webrick" + gem "xpath", "3.2.0" +end + +group :staging, :production do + gem "rack-timeout" + gem "uglifier" +end + +gemspec path: "../" diff --git a/spec/controllers/concerns/administrate/punditize_spec.rb b/spec/controllers/concerns/administrate/punditize_spec.rb index a865ee105e..1318ce4bad 100644 --- a/spec/controllers/concerns/administrate/punditize_spec.rb +++ b/spec/controllers/concerns/administrate/punditize_spec.rb @@ -2,8 +2,17 @@ module Administrate RSpec.describe Punditize do - it "exists when 'Pundit' is not defined" do - unload_constants do + if Pundit.const_defined?(:Authorization) + # Spec for pundit >= 2.2.0 + it "exists when 'Pundit' is not defined" do + unload_constants do + expect { build_dummy }.not_to raise_error + end + end + else + # Spec for pundit < 2.2 + it "exists when Pundit::Authorization is not defined" do + reload_punditize expect { build_dummy }.not_to raise_error end end @@ -11,6 +20,14 @@ module Administrate def unload_constants original = Pundit Object.send(:remove_const, "Pundit") + reload_punditize + + yield + + Object.const_set("Pundit", original) + end + + def reload_punditize Administrate.send(:remove_const, "Punditize") load Rails.root.join( "..", @@ -21,10 +38,6 @@ def unload_constants "administrate", "punditize.rb", ) - - yield - - Object.const_set("Pundit", original) end def dummy_class