Sha256: 427e45d4faca06b8a6149f4496e105edbbce2c46d6fa83653ed556963d8da07e

Contents?: true

Size: 823 Bytes

Versions: 8

Compression:

Stored size: 823 Bytes

Contents

# frozen_string_literal: true

# A module that carries the controllers' view_context to decorators.
module Gifted
  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[:gifted_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|
          Gifted::ViewContext.run_with(controller.view_context) do
            blk.call
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
gifted-1.3.0 lib/gifted/view_context.rb
gifted-1.2.0 lib/gifted/view_context.rb
gifted-1.1.1 lib/gifted/view_context.rb
gifted-1.1.0 lib/gifted/view_context.rb
gifted-1.0.0 lib/gifted/view_context.rb
gifted-0.2.1 lib/gifted/view_context.rb
gifted-0.2.0 lib/gifted/view_context.rb
gifted-0.1.0 lib/gifted/view_context.rb