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

31 lines
701 B
Ruby
Raw Normal View History

# frozen_string_literal: true
module DeclarativePolicy
module PreferredScope
PREFERRED_SCOPE_KEY = :"DeclarativePolicy.preferred_scope"
2017-04-06 17:06:24 -04:00
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