Skip to content

Commit 8a20bdf

Browse files
Support setting parent_controller (#903)
* Support setting parent_controller fixes #618 * Resolve rubocop frozen_string_literal warning --------- Co-authored-by: Javier Julio <[email protected]>
1 parent 43645af commit 8a20bdf

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,13 @@ class AccountsController < ApplicationController
168168
end
169169
```
170170

171+
By default, `InheritedResources::Base` will inherit from `::ApplicationController`. You can change this in a rails initializer:
172+
173+
```ruby
174+
# config/initializers/inherited_resources.rb
175+
InheritedResources.parent_controller = 'MyController'
176+
```
177+
171178
## Overwriting defaults
172179

173180
Whenever you inherit from InheritedResources, several defaults are assumed.

app/controllers/inherited_resources/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module InheritedResources
99
# call <tt>default</tt> class method, call <<tt>actions</tt> class method
1010
# or overwrite some helpers in the base_helpers.rb file.
1111
#
12-
class Base < ::ApplicationController
12+
class Base < InheritedResources.parent_controller.constantize
1313
# Overwrite inherit_resources to add specific InheritedResources behavior.
1414
def self.inherit_resources(base)
1515
base.class_eval do

lib/inherited_resources.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ module InheritedResources
2626
def self.flash_keys=(array)
2727
Responders::FlashResponder.flash_keys = array
2828
end
29+
30+
# Inherit from a different controller. This only has an effect if changed
31+
# before InheritedResources::Base is loaded, e.g. in a rails initializer.
32+
mattr_accessor(:parent_controller) { '::ApplicationController' }
2933
end
3034

3135
ActiveSupport.on_load(:action_controller_base) do

test/parent_controller_test.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
require 'test_helper'
3+
4+
def force_parent_controller(value)
5+
InheritedResources.send(:remove_const, :Base)
6+
InheritedResources.parent_controller = value
7+
load File.join(__dir__, '..', 'app', 'controllers', 'inherited_resources', 'base.rb')
8+
end
9+
10+
class ParentControllerTest < ActionController::TestCase
11+
def test_setting_parent_controller
12+
original_parent = InheritedResources::Base.superclass
13+
14+
assert_equal ApplicationController, original_parent
15+
16+
force_parent_controller('ActionController::Base')
17+
18+
assert_equal ActionController::Base, InheritedResources::Base.superclass
19+
ensure
20+
force_parent_controller(original_parent.to_s) # restore original parent
21+
end
22+
end

0 commit comments

Comments
 (0)