Sha256: a0d401aecbcaab2e0252369bfb65b09469f8a498ecf1712c9ac2349e276254e7

Contents?: true

Size: 1.78 KB

Versions: 48

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

module Motor
  module LoadAndAuthorizeDynamicResource
    extend ActiveSupport::Concern

    INSTANCE_VARIABLE_NAME = 'resource'

    included do
      before_action :load_and_authorize_resource
      before_action :load_and_authorize_association
    end

    def resource_class
      @resource_class ||=
        Motor::BuildSchema::Utils.classify_slug(resource_name_prefix + params[:resource])
    end

    def resource_name_prefix
      ''
    end

    def load_and_authorize_resource
      options = {
        class: resource_class,
        parent: false,
        instance_name: INSTANCE_VARIABLE_NAME
      }

      if params[:resource_id].present?
        options = options.merge(
          parent: true,
          id_param: :resource_id
        )
      end

      CanCan::ControllerResource.new(
        self,
        options
      ).load_and_authorize_resource
    rescue ActiveRecord::RecordNotFound
      head :not_found
    rescue StandardError => e
      render json: { errors: [e.message] }, status: :unprocessable_entity
    end

    def load_and_authorize_association
      return if params[:association].blank?

      association = resource_class.reflections[params[:association]]

      if association
        CanCan::ControllerResource.new(
          self,
          class: association.klass,
          parent: false,
          through: :resource,
          through_association: params[:association].to_sym,
          instance_name: INSTANCE_VARIABLE_NAME
        ).load_and_authorize_resource
      else
        render json: { message: 'Unknown association' }, status: :not_found
      end
    rescue ActiveRecord::RecordNotFound
      head :not_found
    rescue StandardError => e
      render json: { errors: [e.message] }, status: :unprocessable_entity
    end
  end
end

Version data entries

48 entries across 48 versions & 1 rubygems

Version Path
motor-admin-0.1.87 app/controllers/concerns/motor/load_and_authorize_dynamic_resource.rb
motor-admin-0.1.86 app/controllers/concerns/motor/load_and_authorize_dynamic_resource.rb
motor-admin-0.1.85 app/controllers/concerns/motor/load_and_authorize_dynamic_resource.rb
motor-admin-0.1.84 app/controllers/concerns/motor/load_and_authorize_dynamic_resource.rb
motor-admin-0.1.83 app/controllers/concerns/motor/load_and_authorize_dynamic_resource.rb
motor-admin-0.1.82 app/controllers/concerns/motor/load_and_authorize_dynamic_resource.rb
motor-admin-0.1.81 app/controllers/concerns/motor/load_and_authorize_dynamic_resource.rb
motor-admin-0.1.80 app/controllers/concerns/motor/load_and_authorize_dynamic_resource.rb
motor-admin-0.1.79 app/controllers/concerns/motor/load_and_authorize_dynamic_resource.rb
motor-admin-0.1.78 app/controllers/concerns/motor/load_and_authorize_dynamic_resource.rb
motor-admin-0.1.77 app/controllers/concerns/motor/load_and_authorize_dynamic_resource.rb
motor-admin-0.1.76 app/controllers/concerns/motor/load_and_authorize_dynamic_resource.rb
motor-admin-0.1.75 app/controllers/concerns/motor/load_and_authorize_dynamic_resource.rb
motor-admin-0.1.74 app/controllers/concerns/motor/load_and_authorize_dynamic_resource.rb
motor-admin-0.1.73 app/controllers/concerns/motor/load_and_authorize_dynamic_resource.rb
motor-admin-0.1.72 app/controllers/concerns/motor/load_and_authorize_dynamic_resource.rb
motor-admin-0.1.71 app/controllers/concerns/motor/load_and_authorize_dynamic_resource.rb
motor-admin-0.1.70 app/controllers/concerns/motor/load_and_authorize_dynamic_resource.rb
motor-admin-0.1.68 app/controllers/concerns/motor/load_and_authorize_dynamic_resource.rb
motor-admin-0.1.67 app/controllers/concerns/motor/load_and_authorize_dynamic_resource.rb