class HocusPocus::Editor::Middleware
Public Class Methods
new(app)
click to toggle source
# File engines/editor/lib/hocus_pocus/editor/middleware.rb, line 8 def initialize(app) @app = app end
Public Instance Methods
call(env)
click to toggle source
# File engines/editor/lib/hocus_pocus/editor/middleware.rb, line 12 def call(env) status, headers, body = @app.call env if headers && headers['Content-Type']&.include?('text/html') && (env['REQUEST_PATH'] !~ %r[^/*hocus_pocus/]) case body when ActionDispatch::Response, ActionDispatch::Response::RackBody body = body.body when Array body = body[0] end if Thread.current[HocusPocus::Editor::VIEW_FILENAME] body.sub!(/<div id="#{HocusPocus::CONTAINER}" .*?>/i) { %Q[#{$~}#{edit_link}#{partials}] } Thread.current[HocusPocus::Editor::VIEW_FILENAME] = nil end body.sub!(/<\/head>/i) { %Q[<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script></head>] } unless body =~ /jquery(\.min)?\.js/ [status, headers, [body]] else [status, headers, body] end end
Private Instance Methods
edit_link()
click to toggle source
# File engines/editor/lib/hocus_pocus/editor/middleware.rb, line 37 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()" class="edit">edit</a>] end
partials()
click to toggle source
# File engines/editor/lib/hocus_pocus/editor/middleware.rb, line 41 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" class="partials">' + v + '</a>'}.join('')}</div>] end