Sha256: 15936cd08d87379c7619b02cc26c8188126751f2f10559da47f4d539a99985c2

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

module Dicer
  module ActionController
    extend ActiveSupport::Concern

    included do
      prepend_before_filter do
        Dicer::Context.current_controller = self

        default_context = self.class.default_contexts[params[:action].to_sym]
        Dicer::Context.current = default_context.new if default_context.present?
      end

      helper_method :context
      helper_method :in_context
    end

    module ClassMethods
      def context_for(action, context_class = nil, &context_define)
        context = context_class || Class.new(Dicer::Context, &context_define)

        default_contexts[action.to_s.to_sym] = context
      end

      def default_contexts
        @default_contexts ||= {}.tap do |hash|
          begin
            hash.default = ::ApplicationContext
          rescue NameError
          end
        end
      end
    end

    def context(context = nil)
      context.present? ?
        Dicer::Context.current = context :
        Dicer::Context.current
    end
    private :context

    def in_context(context, &block)
      current_context = Dicer::Context.current
      Dicer::Context.current = context.is_a?(Class) ? context.new : context

      block.call(Dicer::Context.current)
    ensure
      Dicer::Context.current = current_context
    end
    private :in_context
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dicer-0.3.0 lib/dicer/railtie/action_controller.rb