forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix rubocop#803: add ApplicationPolicy cop
When using the active_policy gem you should create an ApplicationPolicy class and inherit your resource policies from it.
- Loading branch information
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#803](https://github.com/rubocop/rubocop-rails/issues/803): Add new `Rails/ActivePolicy` cop. ([@hoshy][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rubocop-rails' | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# Checks that policies subclass `ApplicationPolicy`. | ||
# | ||
# @safety | ||
# This cop's autocorrection is unsafe because it may let the logic from `ApplicationPolicy` | ||
# sneak into a policy that is not purposed to inherit logic common among other policies. | ||
# | ||
# @example | ||
# | ||
# # good | ||
# class MyPolicy < ApplicationPolicy | ||
# # ... | ||
# end | ||
# | ||
# # bad | ||
# class MyPolicy < ActionPolicy::Base | ||
# # ... | ||
# end | ||
class ApplicationPolicy < Base | ||
extend AutoCorrector | ||
|
||
include RuboCop::Cop::EnforceSuperclass | ||
MSG = 'Policies should subclass `ApplicationPolicy`.' | ||
SUPERCLASS = 'ApplicationPolicy' | ||
BASE_PATTERN = '(const (const nil? :ActionPolicy) :Base)' | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rubocop-rails' | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# Checks that policies subclass `ApplicationPolicy`. | ||
# | ||
# @safety | ||
# This cop's autocorrection is unsafe because it may let the logic from `ApplicationPolicy` | ||
# sneak into a policy that is not purposed to inherit logic common among other policies. | ||
# | ||
# @example | ||
# | ||
# # good | ||
# class MyPolicy < ApplicationPolicy | ||
# # ... | ||
# end | ||
# | ||
# # bad | ||
# class MyPolicy < ActionPolicy::Base | ||
# # ... | ||
# end | ||
class ApplicationPolicy < Base | ||
extend AutoCorrector | ||
|
||
include RuboCop::Cop::EnforceSuperclass | ||
MSG = 'Policies should subclass `ApplicationPolicy`.' | ||
SUPERCLASS = 'ApplicationPolicy' | ||
BASE_PATTERN = '(const (const nil? :ActionPolicy) :Base)' | ||
end | ||
end | ||
end | ||
end |