From 78d7b3404b63a2175601dc15c8c8e3f7db3d8b64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janosch=20Mu=CC=88ller?= Date: Sun, 3 Mar 2024 20:15:44 +0100 Subject: [PATCH] Support setting parent_controller fixes #618 --- README.md | 7 +++++++ app/controllers/inherited_resources/base.rb | 2 +- lib/inherited_resources.rb | 4 ++++ test/parent_controller_test.rb | 19 +++++++++++++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 test/parent_controller_test.rb 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..19b88808 --- /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 + ensure + force_parent_controller(original_parent.to_s) # restore original parent + end +end