-
Notifications
You must be signed in to change notification settings - Fork 115
/
view_context.rb
51 lines (44 loc) · 1.07 KB
/
view_context.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# frozen_string_literal: true
# A module that carries the controllers' view_context to decorators.
module ActiveDecorator
module ViewContext
class << self
def current
view_context_stack.last
end
def push(view_context)
view_context_stack.push view_context
end
def pop
view_context_stack.pop
end
def view_context_stack
Thread.current[:active_decorator_view_contexts] ||= []
end
def run_with(view_context)
push view_context
yield
ensure
pop
end
end
module Filter
extend ActiveSupport::Concern
included do
if Rails::VERSION::MAJOR >= 4
around_action do |controller, blk|
ActiveDecorator::ViewContext.run_with(controller.view_context) do
blk.call
end
end
else
around_filter do |controller, blk|
ActiveDecorator::ViewContext.run_with(controller.view_context) do
blk.call
end
end
end
end
end
end
end