Module: DeclarativePolicy::PreferredScope

Included in:
DeclarativePolicy
Defined in:
lib/declarative_policy/preferred_scope.rb

Constant Summary collapse

PREFERRED_SCOPE_KEY =
:"DeclarativePolicy.preferred_scope"

Instance Method Summary collapse

Instance Method Details

#preferred_scopeObject



15
16
17
# File 'lib/declarative_policy/preferred_scope.rb', line 15

def preferred_scope
  Thread.current[PREFERRED_SCOPE_KEY]
end

#preferred_scope=(scope) ⇒ Object



27
28
29
# File 'lib/declarative_policy/preferred_scope.rb', line 27

def preferred_scope=(scope)
  Thread.current[PREFERRED_SCOPE_KEY] = scope
end

#subject_scope(&block) ⇒ Object



23
24
25
# File 'lib/declarative_policy/preferred_scope.rb', line 23

def subject_scope(&block)
  with_preferred_scope(:subject, &block)
end

#user_scope(&block) ⇒ Object



19
20
21
# File 'lib/declarative_policy/preferred_scope.rb', line 19

def user_scope(&block)
  with_preferred_scope(:user, &block)
end

#with_preferred_scope(scope) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/declarative_policy/preferred_scope.rb', line 7

def with_preferred_scope(scope)
  old_scope = Thread.current[PREFERRED_SCOPE_KEY]
  Thread.current[PREFERRED_SCOPE_KEY] = scope
  yield
ensure
  Thread.current[PREFERRED_SCOPE_KEY] = old_scope
end