diff --git a/README.md b/README.md index 67967d5d..95ba6a79 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 dbfbc6c7..c324e233 100644 --- a/app/controllers/inherited_resources/base.rb +++ b/app/controllers/inherited_resources/base.rb @@ -8,7 +8,7 @@ module InheritedResources # call default class method, call <actions 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 ae8c1455..78f03a9f 100644 --- a/lib/inherited_resources.rb +++ b/lib/inherited_resources.rb @@ -25,6 +25,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 diff --git a/test/parent_controller_test.rb b/test/parent_controller_test.rb new file mode 100644 index 00000000..5e471d79 --- /dev/null +++ b/test/parent_controller_test.rb @@ -0,0 +1,19 @@ +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 + force_parent_controller(original_parent.to_s) # restore original parent + end +end