Repository: activeadmin/inherited_resources (Ruby)

Issue:
Add an option to provide another superclass other than ApplicationController
`InheritedResource::Base` [inherits from](https://github.com/activeadmin/inherited_resources/blob/a863c1b58019f63e0ecf34960d4fe4737fc94bfb/app/controllers/inherited_resources/base.rb#L11) `::ApplicationController` and that's the source of a lot of issues for us during development. (We use it with ActiveAdmin). ApplicationController can be touched by many people and be reloaded but InheritedResource::Base is only loaded once. This leads to confusing behavior or breaking behavior with ActiveAdmin.

Also, we have an ApplicationController subclassing ActionController::API (for API applications), Inherited Resource/ActiveAdmin wouldn't work and we would _have to_ change our ApplicationController code for it to work.

See this related issue in ActiveAdmin repo: https://github.com/activeadmin/activeadmin/issues/2334

Request: Allowing InheritedResource::Base to inherit from a custom controller, or from ActionController::Base. 
For example: It can inherit from an `InheritedResource::BaseController`, and that file is generated. It can subclass ApplicationController but user can change it to subclass a different controller if they want.


Discussion:

Patch:
diff --git a/README.md b/README.md
index 8ca8d25e..b6b56d54 100644
--- a/README.md
+++ b/README.md
@@ -168,6 +168,13 @@ class AccountsController < ApplicationController
 end
 ```
 
+By default, `InheritedResources::Base` will inherit from `::ApplicationController`. You can change this in a rails initializer:
+
+```ruby
+# config/initializers/inherited_resources.rb
+InheritedResources.parent_controller = 'MyController'
+```
+
 ## Overwriting defaults
 
 Whenever you inherit from InheritedResources, several defaults are assumed.
diff --git a/app/controllers/inherited_resources/base.rb b/app/controllers/inherited_resources/base.rb
index db8f7516..5b0ed9b8 100644
--- a/app/controllers/inherited_resources/base.rb
+++ b/app/controllers/inherited_resources/base.rb
@@ -9,7 +9,7 @@ module InheritedResources
   # call <tt>default</tt> class method, call <<tt>actions</tt> class method
   # or overwrite some helpers in the base_helpers.rb file.
   #
-  class Base < ::ApplicationController
+  class Base < InheritedResources.parent_controller.constantize
     # Overwrite inherit_resources to add specific InheritedResources behavior.
     def self.inherit_resources(base)
       base.class_eval do
diff --git a/lib/inherited_resources.rb b/lib/inherited_resources.rb
index bc0d68d4..73b5f398 100644
--- a/lib/inherited_resources.rb
+++ b/lib/inherited_resources.rb
@@ -26,6 +26,10 @@ module InheritedResources
   def self.flash_keys=(array)
     Responders::FlashResponder.flash_keys = array
   end
+
+  # Inherit from a different controller. This only has an effect if changed
+  # before InheritedResources::Base is loaded, e.g. in a rails initializer.
+  mattr_accessor(:parent_controller) { '::ApplicationController' }
 end
 
 ActiveSupport.on_load(:action_controller_base) do


Test patch:
diff --git a/test/parent_controller_test.rb b/test/parent_controller_test.rb
new file mode 100644
index 00000000..0ab64a99
--- /dev/null
+++ b/test/parent_controller_test.rb
@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+require 'test_helper'
+
+def force_parent_controller(value)
+  InheritedResources.send(:remove_const, :Base)
+  InheritedResources.parent_controller = value
+  load File.join(__dir__, '..', 'app', 'controllers', 'inherited_resources', 'base.rb')
+end
+
+class ParentControllerTest < ActionController::TestCase
+  def test_setting_parent_controller
+    original_parent = InheritedResources::Base.superclass
+
+    assert_equal ApplicationController, original_parent
+
+    force_parent_controller('ActionController::Base')
+
+    assert_equal ActionController::Base, InheritedResources::Base.superclass
+  ensure
+    force_parent_controller(original_parent.to_s) # restore original parent
+  end
+end