From 1ab462cf94327c5f310710faf7656b6eed40a09e Mon Sep 17 00:00:00 2001 From: Thorsten Kohpeiss Date: Fri, 7 Oct 2022 12:24:39 +0200 Subject: [PATCH] Fix #803: add ApplicationPolicy cop When using the active_policy gem you should create an ApplicationPolicy class and inherit your resource policies from it. --- changelog/new_active_policy_cop.md | 1 + lib/rubocop/cop/rails/application_policy.rb | 36 ++++++++++++++++++++ spec/rubocop/cop/rails/application_policy.rb | 36 ++++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 changelog/new_active_policy_cop.md create mode 100644 lib/rubocop/cop/rails/application_policy.rb create mode 100644 spec/rubocop/cop/rails/application_policy.rb diff --git a/changelog/new_active_policy_cop.md b/changelog/new_active_policy_cop.md new file mode 100644 index 0000000000..664e280b81 --- /dev/null +++ b/changelog/new_active_policy_cop.md @@ -0,0 +1 @@ +* [#803](https://github.com/rubocop/rubocop-rails/issues/803): Add new `Rails/ActivePolicy` cop. ([@hoshy][]) diff --git a/lib/rubocop/cop/rails/application_policy.rb b/lib/rubocop/cop/rails/application_policy.rb new file mode 100644 index 0000000000..a7fb2fbfbf --- /dev/null +++ b/lib/rubocop/cop/rails/application_policy.rb @@ -0,0 +1,36 @@ +# 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 + + MSG = 'Policies should subclass `ApplicationPolicy`.' + SUPERCLASS = 'ApplicationPolicy' + BASE_PATTERN = '(const (const nil? :ActionPolicy) :Base)' + + include RuboCop::Cop::EnforceSuperclass + end + end + end +end diff --git a/spec/rubocop/cop/rails/application_policy.rb b/spec/rubocop/cop/rails/application_policy.rb new file mode 100644 index 0000000000..a7fb2fbfbf --- /dev/null +++ b/spec/rubocop/cop/rails/application_policy.rb @@ -0,0 +1,36 @@ +# 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 + + MSG = 'Policies should subclass `ApplicationPolicy`.' + SUPERCLASS = 'ApplicationPolicy' + BASE_PATTERN = '(const (const nil? :ActionPolicy) :Base)' + + include RuboCop::Cop::EnforceSuperclass + end + end + end +end