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

Introduce base_controller_class config option #67

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ You can configure Solid Errors via the Rails configuration object, under the `so
* `email_from` - The email address to send a notification from. See [Email notifications](#email-notifications) for more information.
* `email_to` - The email address(es) to send a notification to. See [Email notifications](#email-notifications) for more information.
* `email_subject_prefix` - Prefix added to the subject line for email notifications. See [Email notifications](#email-notifications) for more information.
* `base_controller_class` - Specify a different controller as the base class for the Solid Errors controller. See [Authentication](#authentication) for more information.

#### Database Configuration

Expand Down Expand Up @@ -207,6 +208,13 @@ authenticate :user, -> (user) { user.admin? } do
end
```

You can also specify a different controller to use as the Solid Errors controller base class:

```ruby
# Override the base controller class with your own controller
config.solid_errors.base_controller_class = "YourAdminController"
```

#### Email notifications

Solid Errors _can_ send email notifications whenever an error occurs, if your application has ActionMailer already properly setup to send emails. However, in order to activate this feature you must define the email address(es) to send the notifications to. Optionally, you can also define the email address to send the notifications from (useful if your email provider only allows emails to be sent from a predefined list of addresses) or simply turn off this feature altogether. You can also define a subject prefix for the email notifications to quickly identify the source of the error.
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/solid_errors/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module SolidErrors
class ApplicationController < ActionController::Base
class ApplicationController < SolidErrors.base_controller_class.constantize
layout "solid_errors/application"
protect_from_forgery with: :exception

http_basic_authenticate_with name: SolidErrors.username, password: SolidErrors.password if SolidErrors.password
Expand Down
3 changes: 1 addition & 2 deletions lib/solid_errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

module SolidErrors
mattr_accessor :connects_to
mattr_accessor :base_controller_class, default: "::ActionController::Base"

Choose a reason for hiding this comment

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

for consistency we can remove the default here and have it in a getter method as below:

Suggested change
mattr_accessor :base_controller_class, default: "::ActionController::Base"
mattr_accessor :base_controller_class

mattr_writer :username
mattr_writer :password
mattr_writer :send_emails
Expand All @@ -21,8 +22,6 @@ def username
@username ||= ENV["SOLIDERRORS_USERNAME"] || @@username
end

Choose a reason for hiding this comment

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

def base_controller_class
   @base_controller_class ||= @@base_controller_class || ::ActionController::Base
end

# use method instead of attr_accessor to ensure
# this works if variable set after SolidErrors is loaded
def password
@password ||= ENV["SOLIDERRORS_PASSWORD"] || @@password
end
Expand Down
4 changes: 4 additions & 0 deletions test/test_solid_errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ class TestSolidErrors < Minitest::Test
def test_that_it_has_a_version_number
refute_nil ::SolidErrors::VERSION
end

def test_default_base_controller
assert_equal ActionController::Base, SolidErrors::ApplicationController.superclass
end
end

Choose a reason for hiding this comment

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

Seems like we're only testing the default Superclass. we could also have test to assert that the custom configured controller is used as expected.