Skip to content

Commit

Permalink
Fix rubocop#803: add ApplicationPolicy cop
Browse files Browse the repository at this point in the history
When using the active_policy gem you should create an ApplicationPolicy class and inherit your resource policies from it.
  • Loading branch information
hoshy committed Oct 7, 2022
1 parent 4289731 commit da3f350
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/new_active_policy_cop.md
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][])
35 changes: 35 additions & 0 deletions lib/rubocop/cop/rails/application_policy.rb
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
35 changes: 35 additions & 0 deletions spec/rubocop/cop/rails/application_policy.rb
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

0 comments on commit da3f350

Please sign in to comment.