Sha256: eaedd5b887048154304bc7897254e2130181b8c45dc5f69471452a8aac977e98

Contents?: true

Size: 1.07 KB

Versions: 3

Compression:

Stored size: 1.07 KB

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
        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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
active_decorator-1.2.0 lib/active_decorator/view_context.rb
active_decorator-1.1.1 lib/active_decorator/view_context.rb
active_decorator-1.1.0 lib/active_decorator/view_context.rb