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

Qualify the Scope class name #792

Merged
merged 1 commit into from
Jan 15, 2024

Conversation

andyw8
Copy link
Contributor

@andyw8 andyw8 commented Dec 24, 2023

I'd like to propose this small change to the generator, and the corresponding examples in the README. However, I'm very new to Pundit so I'm seeking input from others with more experience.

When using the generator, it results in a Scope < Scope inheritance, equivalent to Scope < ApplicationPolicy::Scope.

This may be a matter of personal taste, but I find that having a class inheriting from another with (visually) the same name is jarring, and reduces readability, since it requires reading the surrounding context.

Additionally, this causes complications with typing system. For example, if using Sorbet:

app/policies/list_policy.rb:37: Circular dependency: ListPolicy::Scope is a parent of itself https://srb.help/5011
    37 |  class Scope < Scope

(although it can be argued that this is a bug/limitation of Sorbet).

Looking forward to hearing some feedback.

Copy link
Member

@Burgestrand Burgestrand left a comment

Choose a reason for hiding this comment

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

This may be a matter of personal taste, but I find that having a class inheriting from another with (visually) the same name is jarring, and reduces readability, since it requires reading the surrounding context.

Thank you for the PR! I agree. I think your proposal is great. We should merge it.

However.

This changes behaviour compared to code generated before this change in a subtle way: https://gist.github.com/Burgestrand/4b4bc22f31c8a95c425fc0e30d7ef1f5

I don't think code should take advantage of < Scope being different from < ApplicationPolicy::Scope, but even more strongly I believe Pundit should avoid unpleasant surprises between updates.

I'm uncertain if a mere CHANGELOG entry is sufficient, and I think a comment in the generated code would be good. Generated code should always be inspected, so a comment should be sufficient to highlight this change to avoid developers being caught off-guard.

Could you please:

  • Update the CHANGELOG.
  • Add a comment to the policy generator template to highlight that the code might be different from previously generated policies.

@andyw8 andyw8 force-pushed the andyw8/qualify-scope-class-name branch 2 times, most recently from cf35274 to bb32baf Compare January 12, 2024 01:56
@andyw8
Copy link
Contributor Author

andyw8 commented Jan 12, 2024

@Burgestrand thanks for the feedback! I've pushed some updates.

@Burgestrand
Copy link
Member

I think this is good! I'll give you a chance to read the comments, and if you're up for it removing or adjusting the version specificer in the comment. Once you're happy we'll merge!

@andyw8
Copy link
Contributor Author

andyw8 commented Jan 12, 2024

Thanks, updated.

Burgestrand
Burgestrand previously approved these changes Jan 12, 2024
@Burgestrand Burgestrand merged commit 9dd0511 into varvet:main Jan 15, 2024
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants