Sha256: 6d80414b8a3d97f05d07447d2c18e8426d74b5891afdd38ef1b0479c4e2ee7a3

Contents?: true

Size: 1.75 KB

Versions: 4

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

module RailsMiniProfiler
  class Badge
    include InlineSvg::ActionView::Helpers
    include Engine.routes.url_helpers

    def initialize(request_context, configuration: RailsMiniProfiler.configuration)
      @configuration = configuration
      @profiled_request = request_context.profiled_request
      @original_response = request_context.response
    end

    def render
      content_type = @original_response.headers['Content-Type']
      return @original_response unless content_type =~ %r{text/html}

      modified_response = Rack::Response.new([], @original_response.status, @original_response.headers)
      modified_response.write(modified_body)
      modified_response.finish

      response = @original_response.response
      response.close if response.respond_to?(:close)

      ResponseWrapper.new(@original_response.status,
                          @original_response.headers,
                          modified_response)
    end

    private

    def modified_body
      body = @original_response.response.body
      index = body.rindex(%r{</body>}i) || body.rindex(%r{</html>}i)
      if index
        body.dup.insert(index, badge_content)
      else
        body
      end
    end

    def badge_content
      html = IO.read(File.expand_path('../../app/views/rails_mini_profiler/badge.html.erb', __dir__))
      @position = css_position
      template = ERB.new(html)
      template.result(binding)
    end

    def css_position
      case RailsMiniProfiler.configuration.badge_position
      when 'top-right'
        'top: 5px; right: 5px;'
      when 'bottom-left'
        'bottom: 5px; left: 5px;'
      when 'bottom-right'
        'bottom: 5px; right: 5px;'
      else
        'top: 5px; left: 5px;'
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rails_mini_profiler-0.1.3 lib/rails_mini_profiler/badge.rb
rails_mini_profiler-0.1.2 lib/rails_mini_profiler/badge.rb
rails_mini_profiler-0.1.1 lib/rails_mini_profiler/badge.rb
rails_mini_profiler-0.1.0 lib/rails_mini_profiler/badge.rb