policies: DRY the scope
This commit is contained in:
parent
ac922c456e
commit
c17ceb4440
1 changed files with 6 additions and 4 deletions
|
@ -5,16 +5,18 @@ class ChampPolicy < ApplicationPolicy
|
||||||
return scope.none
|
return scope.none
|
||||||
end
|
end
|
||||||
|
|
||||||
# Users can access public champs on their own dossiers.
|
# The join must be the same for all elements of the WHERE clause.
|
||||||
resolved_scope = scope
|
joined_scope = scope
|
||||||
.left_outer_joins(dossier: { groupe_instructeur: [:instructeurs] })
|
.left_outer_joins(dossier: { groupe_instructeur: [:instructeurs] })
|
||||||
|
|
||||||
|
# Users can access public champs on their own dossiers.
|
||||||
|
resolved_scope = joined_scope
|
||||||
.where('dossiers.user_id': user.id, private: false)
|
.where('dossiers.user_id': user.id, private: false)
|
||||||
|
|
||||||
if instructeur.present?
|
if instructeur.present?
|
||||||
# Additionnaly, instructeurs can access private champs
|
# Additionnaly, instructeurs can access private champs
|
||||||
# on dossiers they are allowed to instruct.
|
# on dossiers they are allowed to instruct.
|
||||||
instructeur_clause = scope
|
instructeur_clause = joined_scope
|
||||||
.left_outer_joins(dossier: { groupe_instructeur: [:instructeurs] })
|
|
||||||
.where('instructeurs.id': instructeur.id, private: true)
|
.where('instructeurs.id': instructeur.id, private: true)
|
||||||
resolved_scope = resolved_scope.or(instructeur_clause)
|
resolved_scope = resolved_scope.or(instructeur_clause)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue