Sha256: 6f4535cd982ed5bc4e9f5b66e5f42955191efac221132186f1fe3a15a492c0ba

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

require 'multi_json'

module Jb
  module PartialRenderer
    module JbTemplateDetector
      # A monkey-patch to inject StrongArray module to Jb partial renderer
      private def find_partial
        template = super
        extend RenderCollectionExtension if template && (template.handler == Jb::Handler)
        template
      end
    end

    # A horrible monkey-patch to prevent rendered collection from being converted to String
    module StrongArray
      def join(*)
        self
      end

      def html_safe
        self
      end
    end

    # A monkey-patch strengthening rendered collection
    module RenderCollectionExtension
      private def collection_with_template
        super.extend StrongArray
      end

      private def collection_without_template
        super.extend StrongArray
      end

      private def render_collection
        return [] if @collection.blank?
        super
      end
    end
  end

  # A monkey-patch that converts non-partial result to a JSON String
  module TemplateRenderer
    module JSONizer
      def render_template(template, *)
        template.respond_to?(:handler) && (template.handler == Jb::Handler) ? MultiJson.dump(super) : super
      end
    end
  end
end

::ActionView::PartialRenderer.prepend ::Jb::PartialRenderer::JbTemplateDetector
::ActionView::TemplateRenderer.prepend ::Jb::TemplateRenderer::JSONizer

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jb-0.6.1 lib/jb/action_view_monkeys.rb
jb-0.6.0 lib/jb/action_view_monkeys.rb