Sha256: cce0c3dc105fb50be7182723314b3e50e0c44fe61f8381c885b32369b1f550a3

Contents?: true

Size: 851 Bytes

Versions: 7

Compression:

Stored size: 851 Bytes

Contents

# 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
        around_action do |controller, blk|
          ActiveDecorator::ViewContext.run_with(controller.view_context) do
            blk.call
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
active_decorator-1.4.1 lib/active_decorator/view_context.rb
active_decorator-1.4.0 lib/active_decorator/view_context.rb
active_decorator-1.3.4 lib/active_decorator/view_context.rb
active_decorator-1.3.3 lib/active_decorator/view_context.rb
active_decorator-1.3.2 lib/active_decorator/view_context.rb
active_decorator-1.3.1 lib/active_decorator/view_context.rb
active_decorator-1.3.0 lib/active_decorator/view_context.rb