Skip to content

DEV: Do not attempt to overwrite UserOption enums during autoloading #22150

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

Merged
merged 1 commit into from
Jun 16, 2023

Conversation

davidtaylorhq
Copy link
Member

@davidtaylorhq davidtaylorhq commented Jun 16, 2023

Unfortunately, Discourse's UserOption model is not currently autoloaded. That means that modifying it via a reloadable_patch will try to apply the changes repeatedly. Normally this doesn't matter since the changes are idempotent. However, introducing ActiveRecord enums is not idempotent - they raise an error if the same enum already exists on the model. This commit adds a check to avoid hitting this 'duplicate definition' error.

Reproduced via

rails runner 'Rails.application.reloader.reload!'

@github-actions github-actions bot added the chat PRs which include a change to Chat plugin label Jun 16, 2023
Unfortunately, Discourse's `UserOption` model is not currently autoloaded. That means that modifying it via a `reloadable_patch` will try to apply the changes repeatedly. Normally this doesn't matter since the changes are idempotent. However, introducing ActiveRecord enums is not idempotent - they raise an error if the same enum already exists on the model. This commit adds a check to avoid hitting this 'duplicate definition' error.

Reproduced

```
rails runner 'Rails.application.reloader.reload!'
```
@davidtaylorhq davidtaylorhq force-pushed the chat-autoloading-fix branch from ca31451 to 7609892 Compare June 16, 2023 10:45
@davidtaylorhq davidtaylorhq changed the title DEV: Do not attempt to overwrite User enums during autoloading DEV: Do not attempt to overwrite UserOption enums during autoloading Jun 16, 2023
Comment on lines +25 to +26
if !base.method_defined?(:never?) # Avoid attempting to override when autoloading
base.enum :chat_header_indicator_preference, base.chat_header_indicator_preferences
Copy link
Member Author

Choose a reason for hiding this comment

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

@jjaffeux not strictly related to this PR, I think we should probably add a prefix to this enum. Right now it's auto-defining three methods:

  • UserOption#never?
  • UserOption#all_new?
  • UserOption#dm_and_mentions?

IMO would be much better to prefix so the methods are something like #chat_header_indicator_never?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes agreed.

@davidtaylorhq davidtaylorhq merged commit 54cae12 into main Jun 16, 2023
@davidtaylorhq davidtaylorhq deleted the chat-autoloading-fix branch June 16, 2023 11:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chat PRs which include a change to Chat plugin
Development

Successfully merging this pull request may close these issues.

3 participants