Skip to content

Commit ba2088c

Browse files
committed
Introduce setup hook for authorization/auditing adapters
1 parent 53eef4f commit ba2088c

7 files changed

Lines changed: 33 additions & 12 deletions

File tree

app/controllers/rails_admin/application_controller.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@ def _audit!
5656
instance_eval(&RailsAdmin::Config.audit_with)
5757
end
5858

59-
def user_for_paper_trail
60-
_current_user.try(:id) || _current_user
61-
end
62-
6359
rescue_from RailsAdmin::ObjectNotFound do
6460
flash[:error] = I18n.t('admin.flash.object_not_found', model: @model_name, id: params[:id])
6561
params[:action] = 'index'

lib/rails_admin/config.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,10 @@ def authenticate_with(&blk)
110110
def audit_with(*args, &block)
111111
extension = args.shift
112112
if extension
113+
klass = RailsAdmin::AUDITING_ADAPTERS[extension]
114+
klass.setup if klass.respond_to? :setup
113115
@audit = proc do
114-
@auditing_adapter = RailsAdmin::AUDITING_ADAPTERS[extension].new(*([self] + args).compact)
116+
@auditing_adapter = klass.new(*([self] + args).compact)
115117
end
116118
elsif block
117119
@audit = block
@@ -145,8 +147,10 @@ def audit_with(*args, &block)
145147
def authorize_with(*args, &block)
146148
extension = args.shift
147149
if extension
150+
klass = RailsAdmin::AUTHORIZATION_ADAPTERS[extension]
151+
klass.setup if klass.respond_to? :setup
148152
@authorize = proc do
149-
@authorization_adapter = RailsAdmin::AUTHORIZATION_ADAPTERS[extension].new(*([self] + args).compact)
153+
@authorization_adapter = klass.new(*([self] + args).compact)
150154
end
151155
elsif block
152156
@authorize = block

lib/rails_admin/extensions/paper_trail/auditing_adapter.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,16 @@ class AuditingAdapter
3838
message: :event,
3939
}.freeze
4040

41+
def self.setup
42+
raise('PaperTrail not found') unless defined?(::PaperTrail)
43+
RailsAdmin::ApplicationController.class_eval do
44+
def user_for_paper_trail
45+
_current_user.try(:id) || _current_user
46+
end
47+
end
48+
end
49+
4150
def initialize(controller, user_class = 'User', version_class = '::Version')
42-
raise('PaperTrail not found') unless defined?(PaperTrail)
4351
@controller = controller
4452
@controller.send(:set_paper_trail_whodunnit) if @controller
4553
begin

lib/rails_admin/extensions/pundit/authorization_adapter.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ module Pundit
55
# You can create another adapter for different authorization behavior, just be certain it
66
# responds to each of the public methods here.
77
class AuthorizationAdapter
8+
# This method is called first time only and used for setup
9+
def self.setup
10+
RailsAdmin::ApplicationController.class_eval do
11+
include ::Pundit
12+
end unless RailsAdmin::ApplicationController.ancestors.include? 'Pundit'
13+
end
14+
815
# See the +authorize_with+ config method for where the initialization happens.
916
def initialize(controller)
1017
@controller = controller

spec/integration/authorization/pundit_spec.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
require 'spec_helper'
22

33
describe 'RailsAdmin Pundit Authorization', type: :request do
4-
before(:all) do
5-
ApplicationController.send :include, ::Pundit
6-
end
7-
84
subject { page }
95

106
before do
@@ -104,10 +100,12 @@
104100
context 'when ApplicationController already has pundit_user' do
105101
let(:admin) { FactoryGirl.create :user, roles: [:admin] }
106102
before do
103+
RailsAdmin.config.parent_controller = 'ApplicationController'
107104
allow_any_instance_of(ApplicationController).to receive(:pundit_user).and_return(admin)
108105
end
109106

110107
it 'uses original pundit_user' do
108+
pending 'no way to dynamically change superclass'
111109
expect { visit dashboard_path }.not_to raise_error
112110
end
113111
end
@@ -137,6 +135,7 @@
137135
dashboard do
138136
authorization_key :dashboard?
139137
end
138+
index
140139
end
141140
end
142141
end

spec/integration/history/rails_admin_paper_trail_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
end
99
end
1010

11+
after(:each) do
12+
# if #user_for_paper_trail is left unused, PaperTrail complains about it
13+
RailsAdmin::ApplicationController.class_eval do
14+
undef user_for_paper_trail
15+
end
16+
end
17+
1118
describe 'on object creation', type: :request do
1219
subject { page }
1320
before do

spec/rails_admin/config_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ class RecursivelyEmbedsMany
263263

264264
describe '.parent_controller' do
265265
it 'uses default class' do
266-
expect(RailsAdmin.config.parent_controller).to eq '::ApplicationController'
266+
expect(RailsAdmin.config.parent_controller).to eq '::ActionController::Base'
267267
end
268268

269269
it 'uses other class' do

0 commit comments

Comments
 (0)