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

Define Pundit::Error using compact style. #590

Merged
merged 1 commit into from
Aug 14, 2019
Merged

Conversation

Linuus
Copy link
Collaborator

@Linuus Linuus commented Apr 17, 2019

When defining the class with:

module Pundit
  class Error
  end
end

the Error class is mixed in to the controller when using
include Pundit. This can cause issues since Error is a
very common class name. Using the compact style:

class Pundit::Error

we avoid this, since it only creates the constant Pundit::Error.

Solves #542
Closes #555 #570

Copy link
Contributor

@olleolleolle olleolleolle left a comment

Choose a reason for hiding this comment

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

LGTM!

@jensljungblad
Copy link

Yes!

@jensljungblad
Copy link

I mean the same can be said for the other errors, even though name clashes wouldn't be as common. Why does it make sense to mixin an InvalidConstructorError into your controller?

@Linuus
Copy link
Collaborator Author

Linuus commented May 24, 2019

I mean the same can be said for the other errors, even though name clashes wouldn't be as common. Why does it make sense to mixin an InvalidConstructorError into your controller?

@jensljungblad Yeah, I agree. The reason I only did the Error case is because it's the most likely one to cause issues and it's marked as @api private for some reason :) That means we can fix the Error one without, hopefully, too many issues. If we move the other ones we need to bump the major version as it's a breaking change I'm afraid :(

@jensljungblad
Copy link

jensljungblad commented May 24, 2019

Right, you mean if some people catch NotAuthorizedError instead of Pundit::NotAuthorizedError..

Well, I'm for doing it in the next major release then :)

@Linuus
Copy link
Collaborator Author

Linuus commented May 24, 2019

Right, you mean if some people catch NotAuthorizedError instead of Pundit::NotAuthorizedError..

Well, I'm for doing it in the next major release then :)

Exactly :) My thought was doing this now and then fix everything in the next major release. I have some bigger structural changes I want to do anyway :)

@kjleitz
Copy link

kjleitz commented Aug 2, 2019

Nice! This is much better.

@dgmstuart
Copy link
Collaborator

@Linuus just to check that I understand correctly: it sounds like this one can be merged and released now, and it would be nice to do change the other errors as well, but that will require a major version update?

@Linuus
Copy link
Collaborator Author

Linuus commented Aug 5, 2019

IMO yes.

When defining the class with:

```
module Pundit
  class Error
  end
end
```

the `Error` class is mixed in to the controller when using
`include Pundit`. This can cause issues since `Error` is a
very common class name. Using the compact style:

```
class Pundit::Error
```

we avoid this, since it only creates the constant `Pundit::Error`.
@dgmstuart dgmstuart force-pushed the avoid-error-name-clash branch from 09893b0 to b7b34d9 Compare August 13, 2019 13:39
@dgmstuart
Copy link
Collaborator

Rebasing...

@dgmstuart dgmstuart merged commit 44fadbb into master Aug 14, 2019
@dgmstuart dgmstuart deleted the avoid-error-name-clash branch August 14, 2019 09:37
# @api public
module Pundit
SUFFIX = "Policy".freeze

# @api private
module Generators; end

# @api private
class Error < StandardError; end

# Error that will be raised when authorization has failed
class NotAuthorizedError < Error
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't this then be < ::Pundit::Error ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

@bf4 Thanks for the comments - can you expand on this?

This does seem to inherit from the correct Error class if that's the concern:

pry(main)> require 'pundit'
=> true
pry(main)> Pundit::NotAuthorizedError.ancestors
=> [Pundit::NotAuthorizedError,
 Pundit::Error,
 StandardError,
 Exception,
 Object,
 JSON::Ext::Generator::GeneratorMethods::Object,
 PP::ObjectMixin,
 Kernel,
 BasicObject]

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.

6 participants