Skip to content

Commit

Permalink
Fix config.parent_controller to work after the class loading
Browse files Browse the repository at this point in the history
Fixes #2790, Refs. e4ae669
  • Loading branch information
mshibuya committed Oct 17, 2021
1 parent 973bd8e commit 5bd9805
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Metrics/MethodLength:
Max: 29 # TODO: Lower to 15

Metrics/ModuleLength:
Max: 200 # TODO: Lower to 100
Max: 202 # TODO: Lower to 100

Metrics/ParameterLists:
Max: 8 # TODO: Lower to 4
Expand Down
10 changes: 9 additions & 1 deletion lib/rails_admin/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class << self
attr_accessor :sidescroll

# set parent controller
attr_accessor :parent_controller
attr_reader :parent_controller

# set settings for `protect_from_forgery` method
# By default, it raises exception upon invalid CSRF tokens
Expand Down Expand Up @@ -270,6 +270,14 @@ def default_hidden_fields=(fields)
end
end

def parent_controller=(name)
@parent_controller = name
if defined?(RailsAdmin::ApplicationController)
RailsAdmin.send(:remove_const, :ApplicationController)
load RailsAdmin::Engine.root.join('app/controllers/rails_admin/application_controller.rb')
end
end

# Returns action configuration object
def actions(&block)
RailsAdmin::Config::Actions.instance_eval(&block) if block
Expand Down
19 changes: 19 additions & 0 deletions spec/rails_admin/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ class RecursivelyEmbedsMany
end

describe '.parent_controller' do
before do
class TestController < ActionController::Base; end
end

it 'uses default class' do
expect(RailsAdmin.config.parent_controller).to eq '::ActionController::Base'
end
Expand All @@ -274,6 +278,21 @@ class RecursivelyEmbedsMany
end
end

describe '.parent_controller=' do
context 'if RailsAdmin::ApplicationController is already loaded' do
after do
RailsAdmin::Config.reset
RailsAdmin.send(:remove_const, :ApplicationController)
load RailsAdmin::Engine.root.join('app/controllers/rails_admin/application_controller.rb')
end

it 'can be changed' do
RailsAdmin.config.parent_controller = 'ApplicationController'
expect(RailsAdmin::ApplicationController.superclass).to eq ApplicationController
end
end
end

describe '.forgery_protection_settings' do
it 'uses with: :exception by default' do
expect(RailsAdmin.config.forgery_protection_settings).to eq(with: :exception)
Expand Down

0 comments on commit 5bd9805

Please sign in to comment.