2018-10-06 19:10:08 -04:00
|
|
|
# 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
|