Skip to content

Commit

Permalink
Prevent autoloading on initialization for Zeitwerk compatibility
Browse files Browse the repository at this point in the history
Fixes #3190
  • Loading branch information
mshibuya committed Dec 30, 2019
1 parent 43778cb commit e275012
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 15 deletions.
2 changes: 2 additions & 0 deletions app/controllers/rails_admin/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class ActionNotAllowed < ::StandardError
end

class ApplicationController < Config.parent_controller.constantize
include RailsAdmin::Extensions::ControllerExtension

protect_from_forgery(Config.forgery_protection_settings)

before_action :_authenticate!
Expand Down
12 changes: 12 additions & 0 deletions lib/rails_admin/extension.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'rails_admin/extensions/controller_extension'

module RailsAdmin
EXTENSIONS = [] # rubocop:disable Style/MutableConstant
Expand Down Expand Up @@ -26,4 +27,15 @@ def self.add_extension(extension_key, extension_definition, options = {})
AUDITING_ADAPTERS[extension_key] = extension_definition::AuditingAdapter
end
end

# Setup all extensions for testing
def self.setup_all_extensions
(AUTHORIZATION_ADAPTERS.values + AUDITING_ADAPTERS.values).each do |klass|
begin
klass.setup if klass.respond_to? :setup
rescue # rubocop:disable Lint/HandleExceptions
# ignore errors
end
end
end
end
6 changes: 6 additions & 0 deletions lib/rails_admin/extensions/controller_extension.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module RailsAdmin
module Extensions
module ControllerExtension
end
end
end
12 changes: 7 additions & 5 deletions lib/rails_admin/extensions/paper_trail/auditing_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ def item
end
end

module ControllerExtension
def user_for_paper_trail
_current_user.try(:id) || _current_user
end
end

class AuditingAdapter
COLUMN_MAPPING = {
table: :item_type,
Expand All @@ -52,11 +58,7 @@ class AuditingAdapter

def self.setup
raise('PaperTrail not found') unless defined?(::PaperTrail)
RailsAdmin::ApplicationController.class_eval do
def user_for_paper_trail
_current_user.try(:id) || _current_user
end
end
RailsAdmin::Extensions::ControllerExtension.send(:include, ControllerExtension)
end

def initialize(controller, user_class = 'User', version_class = '::Version')
Expand Down
4 changes: 1 addition & 3 deletions lib/rails_admin/extensions/pundit/authorization_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ module Pundit
class AuthorizationAdapter
# This method is called first time only and used for setup
def self.setup
RailsAdmin::ApplicationController.class_eval do
include ::Pundit
end unless RailsAdmin::ApplicationController.ancestors.include? 'Pundit'
RailsAdmin::Extensions::ControllerExtension.send(:include, ::Pundit)
end

# See the +authorize_with+ config method for where the initialization happens.
Expand Down
7 changes: 0 additions & 7 deletions spec/integration/history/rails_admin_paper_trail_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@
end
end

after(:each) do
# if #user_for_paper_trail is left unused, PaperTrail complains about it
RailsAdmin::ApplicationController.class_eval do
undef user_for_paper_trail
end
end

shared_examples :paper_on_object_creation do
describe 'on object creation', type: :request do
subject { page }
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ def password_digest(password)
Capybara.javascript_driver = :poltergeist
Capybara.server = :webrick

RailsAdmin.setup_all_extensions

RSpec.configure do |config|
config.expect_with :rspec do |c|
c.syntax = :expect
Expand Down

0 comments on commit e275012

Please sign in to comment.