From b2c904418b329c213070f3cdacab5c55481a770a Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Sun, 2 Dec 2018 16:04:30 +0500 Subject: [PATCH] Add class ApplicationPolicy::Context --- app/controllers/application_controller.rb | 6 +++++- app/policies/application_policy.rb | 22 +++++++++++++------- app/policies/passport_confirmation_policy.rb | 2 +- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index f0c81fd..f3a54e5 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -21,7 +21,11 @@ private @current_account ||= current_user&.account end - alias pundit_user current_account + def pundit_user + @pundit_user ||= ApplicationPolicy::Context.new( + account: current_account, + ) + end def set_raven_context Raven.user_context id: current_user.id if user_signed_in? diff --git a/app/policies/application_policy.rb b/app/policies/application_policy.rb index 41f46ec..6b5eb8e 100644 --- a/app/policies/application_policy.rb +++ b/app/policies/application_policy.rb @@ -1,10 +1,10 @@ # frozen_string_literal: true class ApplicationPolicy - attr_reader :account, :record + attr_reader :context, :record - def initialize(account, record) - @account = account + def initialize(context, record) + @context = context @record = record end @@ -41,14 +41,14 @@ class ApplicationPolicy # :nocov: def policy(record) - Pundit.policy account, record + Pundit.policy context, record end class Scope - attr_reader :account, :scope + attr_reader :context, :scope - def initialize(account, scope) - @account = account + def initialize(context, scope) + @context = context @scope = scope end @@ -60,4 +60,12 @@ class ApplicationPolicy # :nocov: end + + class Context + attr_reader :account + + def initialize(account:) + @account = account + end + end end diff --git a/app/policies/passport_confirmation_policy.rb b/app/policies/passport_confirmation_policy.rb index 56549c3..cd4331b 100644 --- a/app/policies/passport_confirmation_policy.rb +++ b/app/policies/passport_confirmation_policy.rb @@ -3,7 +3,7 @@ class PassportConfirmationPolicy < ApplicationPolicy def create? return false if record.passport.nil? - return false if record.account != account + return false if record.account != context.account policy(record.passport).show? end