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

Merged
Merged
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 @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/inherited_resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
tagliala marked this conversation as resolved.
Show resolved Hide resolved
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'

Check warning on line 1 in test/parent_controller_test.rb

View workflow job for this annotation

GitHub Actions / Run rubocop

[rubocop] reported by reviewdog 🐶 Missing frozen string literal comment. Raw Output: test/parent_controller_test.rb:1:1: C: Style/FrozenStringLiteralComment: Missing frozen string literal comment.
javierjulio marked this conversation as resolved.
Show resolved Hide resolved

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
Loading