Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support setting parent_controller #903

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/inherited_resources/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,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
Expand Down
4 changes: 4 additions & 0 deletions lib/inherited_resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tagliala i'd say its equivalent – the block is evaluated and the class variable is set immediately:

https://github.com/rails/rails/blob/56fbc632a378acc40d99f5b4be321f80a5d8b8c4/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb#L69-L70

i think the devise code is just like that because when José Valim wrote these lines 13 years ago, mattr_accessor did not yet support passing a default value:

https://github.com/rails/rails/blob/bf526c2dbeb73bf11553004e43889a804b72866d/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb#L60

end

ActiveSupport.on_load(:action_controller_base) do
Expand Down
21 changes: 21 additions & 0 deletions test/parent_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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
jaynetics marked this conversation as resolved.
Show resolved Hide resolved
end
end