Sha256: c95781ec9ffb36a253c6dea73a71fce3ddc650178101592885d9ba36238674a2

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

require 'hocus_pocus/middleware_util'

module HocusPocus
  module Editor
    class Middleware
      include MiddlewareUtil

      def initialize(app)
        @app = app
      end

      def call(env)
        @app.call(env).tap do |status, headers, body|
          if body.is_a?(ActionDispatch::Response) && body.request.format.html?
            if Thread.current[HocusPocus::Editor::VIEW_FILENAME]
              body.body = insert_text body.body, :after, /<div id="#{HocusPocus::CONTAINER}" .*?>/i, %Q[#{edit_link}<br>#{partials}]
              Thread.current[HocusPocus::Editor::VIEW_FILENAME] = nil
            end
          end
        end
      end

      private
      def edit_link
        %Q[<a href="/hocus_pocus/editor?template=#{Thread.current[HocusPocus::Editor::VIEW_FILENAME]}" data-remote="true" onclick="$(this).closest('div').find('div.partials').toggle()">edit</a>]
      end

      def partials
        %Q[<div class="partials" style="display:none">#{(Thread.current[HocusPocus::Editor::PARTIAL_FILENAMES] || []).map(&:virtual_path).map {|v| '<a href="/hocus_pocus/editor?template=' + v + '" data-remote="true">' + v + '</a>'}.join('<br>')}</div>]
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hocus_pocus-0.2.1 engines/editor/lib/hocus_pocus/editor/middleware.rb
hocus_pocus-0.2.0 engines/editor/lib/hocus_pocus/editor/middleware.rb