2018-12-13 13:39:08 +05:30
|
|
|
# rubocop:disable Naming/FileName
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DeclarativePolicy
|
2017-09-10 17:25:29 +05:30
|
|
|
PREFERRED_SCOPE_KEY = :"DeclarativePolicy.preferred_scope"
|
|
|
|
|
|
|
|
class << self
|
2018-11-18 11:00:15 +05:30
|
|
|
def with_preferred_scope(scope)
|
2017-09-10 17:25:29 +05:30
|
|
|
Thread.current[PREFERRED_SCOPE_KEY], old_scope = scope, Thread.current[PREFERRED_SCOPE_KEY]
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
Thread.current[PREFERRED_SCOPE_KEY] = old_scope
|
|
|
|
end
|
|
|
|
|
|
|
|
def preferred_scope
|
|
|
|
Thread.current[PREFERRED_SCOPE_KEY]
|
|
|
|
end
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
def user_scope(&block)
|
|
|
|
with_preferred_scope(:user, &block)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
def subject_scope(&block)
|
|
|
|
with_preferred_scope(:subject, &block)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def preferred_scope=(scope)
|
|
|
|
Thread.current[PREFERRED_SCOPE_KEY] = scope
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|