Skip to content

Commit

Permalink
Fix backward compatible Pundit include (thoughtbot#2383)
Browse files Browse the repository at this point in the history
This commit fixes thoughtbot#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: thoughtbot#2141
See: thoughtbot#1068
  • Loading branch information
fbuys authored May 27, 2023
1 parent 95b201d commit 403d688
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 5 deletions.
4 changes: 4 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ end
appraise "rails70" do
gem "rails", "~> 6.1"
end

appraise "pundit21" do
gem "pundit", "~> 2.1.0"
end
7 changes: 6 additions & 1 deletion app/controllers/concerns/administrate/punditize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 49 additions & 0 deletions gemfiles/pundit21.gemfile
Original file line number Diff line number Diff line change
@@ -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: "../"
21 changes: 17 additions & 4 deletions spec/controllers/concerns/administrate/punditize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,26 @@ module Administrate
end
end

unless Pundit.const_defined?(:Authorization)
# Spec for pundit < 2.2
it "exists when Pundit::Authorization is not defined" do
reload_punditize
expect(Pundit.const_defined?(:Authorization)).to be_falsey
expect { build_dummy }.not_to raise_error
end
end

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(
"..",
Expand All @@ -21,10 +38,6 @@ def unload_constants
"administrate",
"punditize.rb",
)

yield

Object.const_set("Pundit", original)
end

def dummy_class
Expand Down

0 comments on commit 403d688

Please sign in to comment.