-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lean into namespaced policies per controller
Demodulize model policy class names as string and rebuild the classes by following the guidance in Pundit's README: https://github.com/varvet/pundit?tab=readme-ov-file#policy-namespacing. We have service namespaces and then the "Support" namespace within each service. To avoid duplicate service namespaces - as by default `authorize [:claims, :support, Claims::Claim]` would attempt to lookup a `Claims::Support::Claims::ClaimPolicy`. As both the model and controller both have the service namespaces, we need to de-dup them. By demodulizing policy class names from models, The `Claims::Claim` model will return a `ClaimPolicy`. This policy does not exist by itself, but we override each of Pundit's controller methods by injecting namespaces.
- Loading branch information
1 parent
33e563d
commit e8e66a4
Showing
42 changed files
with
394 additions
and
202 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
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
1 change: 1 addition & 0 deletions
1
app/controllers/claims/schools/claims/mentor_trainings_controller.rb
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
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
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
3 changes: 2 additions & 1 deletion
3
app/controllers/claims/support/schools/claims/mentor_trainings_controller.rb
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
22 changes: 13 additions & 9 deletions
22
app/controllers/claims/support/schools/claims/mentors_controller.rb
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
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
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
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
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
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
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
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
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
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,9 @@ | ||
module DemodulizesPolicyClass | ||
extend ActiveSupport::Concern | ||
|
||
class_methods do | ||
def policy_class | ||
"#{name.demodulize}Policy" | ||
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
2 changes: 1 addition & 1 deletion
2
.../grant_conditions/claims/school_policy.rb → .../claims/grant_conditions/school_policy.rb
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
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 |
---|---|---|
@@ -1,31 +1,7 @@ | ||
class Claims::SchoolPolicy < Claims::ApplicationPolicy | ||
def update? | ||
true | ||
end | ||
|
||
def school_options? | ||
true | ||
end | ||
|
||
def check_school_option? | ||
true | ||
end | ||
|
||
def remove_grant_conditions_acceptance_check? | ||
user != record && user.support_user? | ||
end | ||
|
||
def remove_grant_conditions_acceptance? | ||
remove_grant_conditions_acceptance_check? | ||
end | ||
|
||
class Scope < ApplicationPolicy::Scope | ||
def resolve | ||
if user.support_user? | ||
scope.all | ||
else | ||
scope.joins(:users).where(users: { id: user }) | ||
end | ||
scope.joins(:users).where(users: { id: user }) | ||
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,52 @@ | ||
class Claims::Support::ClaimPolicy < Claims::ApplicationPolicy | ||
def create? | ||
current_claim_window? | ||
end | ||
|
||
def edit? | ||
claim_claim_window_current? && !record.submitted? | ||
end | ||
|
||
def update? | ||
edit? | ||
end | ||
|
||
def create_revision? | ||
edit? | ||
end | ||
|
||
def submit? | ||
current_claim_window? && !user.support_user? && !record.submitted? | ||
end | ||
|
||
def rejected? | ||
submit? || draft? | ||
end | ||
|
||
def destroy? | ||
record.draft? | ||
end | ||
|
||
# TODO: Remove record.draft? and not create drafts for existing drafts | ||
def draft? | ||
current_claim_window? && user.support_user? && (record.internal_draft? || record.draft?) | ||
end | ||
|
||
def check? | ||
record.draft? || record.internal_draft? | ||
end | ||
|
||
def download_csv? | ||
user.support_user? | ||
end | ||
|
||
private | ||
|
||
def current_claim_window? | ||
Claims::ClaimWindow.current.present? | ||
end | ||
|
||
def claim_claim_window_current? | ||
record.claim_window.current? | ||
end | ||
end |
2 changes: 1 addition & 1 deletion
2
app/policies/claims/claim_window_policy.rb → ...ies/claims/support/claim_window_policy.rb
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
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,13 @@ | ||
class Claims::Support::MentorMembershipPolicy < Claims::ApplicationPolicy | ||
def update? | ||
true | ||
end | ||
|
||
def destroy? | ||
!Claims::Claim.active.joins(:mentor_trainings).where(school_id: record.school_id, mentor_trainings: { mentor_id: record.mentor_id }).exists? | ||
end | ||
|
||
def remove? | ||
true | ||
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,9 @@ | ||
class Claims::Support::MentorPolicy < Claims::ApplicationPolicy | ||
def update? | ||
true | ||
end | ||
|
||
def destroy? | ||
true | ||
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,19 @@ | ||
class Claims::Support::SchoolPolicy < Claims::ApplicationPolicy | ||
def update? | ||
true | ||
end | ||
|
||
def remove_grant_conditions_acceptance_check? | ||
user != record && user.support_user? | ||
end | ||
|
||
def remove_grant_conditions_acceptance? | ||
remove_grant_conditions_acceptance_check? | ||
end | ||
|
||
class Scope < ApplicationPolicy::Scope | ||
def resolve | ||
scope.all | ||
end | ||
end | ||
end |
Oops, something went wrong.