Sha256: cb9141a43af22b1ea1f382a74c0dc0221071b618b946d9dc176c0b6485b5dbf1

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

module RailsMiniProfiler
  class ApplicationController < RailsMiniProfiler.configuration.ui.base_controller
    rescue_from ActiveRecord::RecordNotFound, with: ->(error) { handle(error, 404) }

    before_action :check_rmp_user

    protected

    def present(model, presenter_class = nil, **kwargs)
      klass = presenter_class || "RailsMiniProfiler::#{model.class.to_s.demodulize}Presenter".constantize
      presenter = klass.new(model, view_context, **kwargs)
      yield(presenter) if block_given?
      presenter
    end

    private

    def handle(error, status = 500)
      respond_to do |format|
        format.html { redirect_back(fallback_location: profiled_requests_path, alert: error.to_s) }
        format.json { render status: status, json: { message: error.to_s } }
      end
    end

    def check_rmp_user
      user = User.get(request.env).present?
      redirect_back(fallback_location: fallback_location) unless user
    end

    def fallback_location
      defined?(main_app.root_path) ? main_app.root_path : '/'
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_mini_profiler-0.7.0 app/controllers/rails_mini_profiler/application_controller.rb