Sha256: d97f52cfb99c96d2b4c31147d4e5cce34a7cfc70d33743198afbb372bb5eb1d9

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true
# A module that carries the controllers' view_context to decorators.
module ActiveDecorator
  module ViewContext
    class << self
      def current
        Thread.current[:active_decorator_view_contexts].last
      end

      def push(view_context)
        Thread.current[:active_decorator_view_contexts] ||= []
        Thread.current[:active_decorator_view_contexts] << view_context
      end

      def pop
        Thread.current[:active_decorator_view_contexts].pop if Thread.current[:active_decorator_view_contexts]
      end
    end

    module Filter
      extend ActiveSupport::Concern

      included do
        if Rails::VERSION::MAJOR >= 4
          around_action do |controller, blk|
            begin
              ActiveDecorator::ViewContext.push controller.view_context
              blk.call
            ensure
              ActiveDecorator::ViewContext.pop
            end
          end
        else
          around_filter do |controller, blk|
            begin
              ActiveDecorator::ViewContext.push controller.view_context
              blk.call
            ensure
              ActiveDecorator::ViewContext.pop
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_decorator-0.9.0 lib/active_decorator/view_context.rb