gitlab-org--gitlab-foss/lib/declarative_policy/preferred_scope.rb

31 lines
691 B
Ruby
Raw Normal View History

# frozen_string_literal: true
module DeclarativePolicy
2017-04-06 17:06:24 -04:00
PREFERRED_SCOPE_KEY = :"DeclarativePolicy.preferred_scope"
class << self
2018-07-04 10:02:01 -04:00
def with_preferred_scope(scope)
2017-04-06 17:06:24 -04:00
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-07-04 10:02:01 -04:00
def user_scope(&block)
with_preferred_scope(:user, &block)
2017-04-06 17:06:24 -04:00
end
2018-07-04 10:02:01 -04:00
def subject_scope(&block)
with_preferred_scope(:subject, &block)
2017-04-06 17:06:24 -04:00
end
def preferred_scope=(scope)
Thread.current[PREFERRED_SCOPE_KEY] = scope
end
end
end