-
-
Notifications
You must be signed in to change notification settings - Fork 484
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CaseGroups request spec & custom policy generator (#5999)
* Add CaseGroups request spec * Create PolicyGenerator: replaces pundit:policy gen * generated policy and spec * post-generation to implement prior behavior * use CaseGroupPolicy in CaseGroupsController * Include PolicyGenerator in Scaffold/Controller generation * policy#same_org? improvement * policy_scope for set_case_group * no fancy super subject * add volunteer? method for AllCasaAdmin * case_group slight improvement * case group factory case casa org association * better action-less generator plus spec notes
- Loading branch information
1 parent
9296150
commit ac35c95
Showing
14 changed files
with
595 additions
and
20 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
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,46 @@ | ||
class CaseGroupPolicy < ApplicationPolicy | ||
class Scope < ApplicationPolicy::Scope | ||
def resolve | ||
case user | ||
when CasaAdmin, Supervisor | ||
scope.where(casa_org: @user.casa_org) | ||
when Volunteer | ||
scope.none | ||
else | ||
scope.none | ||
end | ||
end | ||
end | ||
|
||
def index? | ||
is_admin? || is_supervisor? | ||
end | ||
|
||
def new? | ||
admin_or_supervisor_same_org? | ||
end | ||
|
||
def show? | ||
admin_or_supervisor_same_org? | ||
end | ||
|
||
def create? | ||
admin_or_supervisor_same_org? | ||
end | ||
|
||
def edit? | ||
admin_or_supervisor_same_org? | ||
end | ||
|
||
def update? | ||
admin_or_supervisor_same_org? | ||
end | ||
|
||
def destroy? | ||
admin_or_supervisor_same_org? | ||
end | ||
|
||
def same_org? | ||
user&.casa_org.present? && user&.casa_org == record&.casa_org | ||
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
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,38 @@ | ||
# Allows rails generators (scaffold/controller) to use custom policy generator. | ||
# Arguments for rails generators will be passed to the policy generator. | ||
# Options will be shown in the help text for the rails generators, | ||
# including the option to skip the policy generator (--skip-policy). | ||
module PolicyGenerator | ||
module ControllerGenerator | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
hook_for :policy, in: nil, default: true, type: :boolean do |generator| | ||
# use actions from controller invocation | ||
invoke generator, [name.singularize, *actions] | ||
end | ||
end | ||
end | ||
|
||
module ScaffoldControllerGenerator | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
hook_for :policy, in: nil, default: true, type: :boolean do |generator| | ||
# prevent attribute arguments (name:string) being confused with actions | ||
scaffold_actions = %w[index new create show edit update destroy] | ||
invoke generator, [name.singularize, *scaffold_actions] | ||
end | ||
end | ||
end | ||
end | ||
|
||
module ActiveModel | ||
class Railtie < Rails::Railtie | ||
generators do |app| | ||
Rails::Generators.configure! app.config.generators | ||
Rails::Generators::ControllerGenerator.include PolicyGenerator::ControllerGenerator | ||
Rails::Generators::ScaffoldControllerGenerator.include PolicyGenerator::ScaffoldControllerGenerator | ||
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,9 @@ | ||
Description: | ||
Generates a pundit policy and corresponding spec. | ||
|
||
Example: | ||
bin/rails generate policy CasaThing | ||
|
||
This will create: | ||
- app/policies/casa_thing_policy.rb | ||
- spec/policies/casa_thing_policy_spec.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# NOTE: Rails namespace in order to be able to called from rails generators (see initializers/generators.rb) | ||
class Rails::PolicyGenerator < Rails::Generators::NamedBase | ||
source_root File.expand_path("templates", __dir__) | ||
|
||
remove_class_option :skip_namespace | ||
remove_class_option :skip_collision_check | ||
|
||
argument :actions, type: :array, banner: "action action", default: [] | ||
|
||
class_option :headless, type: :boolean, default: false, | ||
desc: "Policy for non-model routes (dashboard, collection, etc)" | ||
|
||
def create_policy | ||
template "policy.rb", File.join("app/policies", class_path, "#{file_name}_policy.rb") | ||
end | ||
|
||
def create_policy_spec | ||
template "policy_spec.rb", File.join("spec/policies", class_path, "#{file_name}_policy_spec.rb") | ||
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 @@ | ||
<%- module_namespacing do -%> | ||
class <%= class_name %>Policy < ApplicationPolicy | ||
<%- if options[:headless] -%> | ||
# Headless policy (used when no corresponding ActiveRecord/Model): | ||
# - use `authorize(:<%= singular_name %>, :action?)` in controller actions | ||
# - see https://github.com/varvet/pundit#headless-policies | ||
def initialize(user, _record) | ||
@user = user | ||
end | ||
<%- else -%> | ||
class Scope < ApplicationPolicy::Scope | ||
def resolve | ||
case user | ||
when CasaAdmin, Supervisor | ||
scope.where(casa_org: @user.casa_org) | ||
when Volunteer | ||
# REMOVE IF NOT APPLICABLE (just an example, doesn't work for all cases) | ||
# scope.assigned_to_user(@user) | ||
scope.none | ||
else | ||
scope.none | ||
end | ||
end | ||
end | ||
<%- end -%> | ||
<%- user_example = "is_admin? || is_supervisor?" -%> | ||
<%- record_example = "admin_or_supervisor_same_org?" -%> | ||
<%- if actions.empty? -%> | ||
|
||
# No actions specified, Example usage: | ||
# def index? | ||
# <%= user_example %> | ||
# end | ||
# | ||
# def show? | ||
# <%= record_example %> | ||
# end | ||
<%- end -%> | ||
<%- actions.each do |action| -%> | ||
|
||
<%- if action == "index" -%> | ||
def index? | ||
<%= user_example %> | ||
end | ||
<%- else -%> | ||
def <%= action %>? | ||
<%= options[:headless] ? user_example : record_example %> | ||
end | ||
<%- end -%> | ||
<%- end -%> | ||
end | ||
<% end -%> |
Oops, something went wrong.