Sha256: 551ffbf5e0f1938bbd2338ef06f342ce8b5ab5bd5a1d96e20c5679cf9dea2516

Contents?: true

Size: 1.77 KB

Versions: 5

Compression:

Stored size: 1.77 KB

Contents

module Avo
  class ApplicationController < ActionController::Base
    rescue_from ActiveRecord::RecordInvalid, with: :exception_logger
    protect_from_forgery with: :exception
    before_action :init_app

    def init_app
      Avo::App.boot if Avo::IN_DEVELOPMENT
      Avo::App.init request

      @license = Avo::App.license
    end

    def exception_logger(exception)
      respond_to do |format|
        format.html { raise exception }
        format.json { render json: {
          errors: exception.record.present? ? exception.record.errors : [],
          message: exception.message,
          traces: exception.backtrace,
        }, status: ActionDispatch::ExceptionWrapper.status_code_for_exception(exception.class.name) }
      end
    end

    private
      def resource
        eager_load_files(resource_model).find params[:id]
      end

      def eager_load_files(query)
        if avo_resource.attached_file_fields.present?
          avo_resource.attached_file_fields.map(&:id).map do |field|
            query = query.send :"with_attached_#{field}"
          end
        end

        query
      end

      def resource_model
        avo_resource.model
      end

      def avo_resource
        App.get_resource params[:resource_name].to_s.camelize.singularize
      end

      def authorize_user
        return if params[:controller] == 'avo/search'

        model = record = avo_resource.model

        if ['show', 'edit', 'update'].include?(params[:action]) && params[:controller] == 'avo/resources'
          record = resource
        end

        return render_unauthorized unless AuthorizationService::authorize_action current_user, record, params[:action]
      end

      def render_unauthorized
        render json: { message: I18n.t('avo.unauthorized') }, status: 403
      end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
avo-0.4.5 app/controllers/avo/application_controller.rb
avo-0.4.4 app/controllers/avo/application_controller.rb
avo-0.4.3 app/controllers/avo/application_controller.rb
avo-0.4.2 app/controllers/avo/application_controller.rb
avo-0.4.1 app/controllers/avo/application_controller.rb