Sha256: 7594449c09b23157469358ad489ce2285df86baf93a3bf358129bc963461f913

Contents?: true

Size: 1.52 KB

Versions: 12

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

module Motor
  class DataController < ApiBaseController
    wrap_parameters :data, except: %i[include fields]

    include Motor::WrapIoParams
    include Motor::LoadAndAuthorizeDynamicResource

    def index
      @resources = Motor::ApiQuery.call(@resources, params)

      render json: {
        data: Motor::ApiQuery::BuildJson.call(@resources, params),
        meta: Motor::ApiQuery::BuildMeta.call(@resources, params)
      }
    end

    def show
      render json: { data: Motor::ApiQuery::BuildJson.call(@resource, params) }
    end

    def create
      @resource.save!

      render json: { data: Motor::ApiQuery::BuildJson.call(@resource, params) }
    rescue ActiveRecord::RecordInvalid
      render json: { errors: @resource.errors }, status: :unprocessable_entity
    end

    def update
      @resource.update!(resource_params)

      render json: { data: Motor::ApiQuery::BuildJson.call(@resource, params) }
    rescue ActiveRecord::RecordInvalid
      render json: { errors: @resource.errors }, status: :unprocessable_entity
    end

    def destroy
      if @resource.respond_to?(:deleted_at)
        @resource.update(deleted_at: Time.current)
      else
        @resource.destroy!
      end

      head :ok
    end

    def execute
      @resource.public_send(params[:method].to_sym)

      head :ok
    end

    private

    def resource_params
      if params[:data].present?
        params.require(:data).except(resource_class.primary_key).permit!
      else
        {}
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
motor-admin-0.1.57 app/controllers/motor/data_controller.rb
motor-admin-0.1.56 app/controllers/motor/data_controller.rb
motor-admin-0.1.55 app/controllers/motor/data_controller.rb
motor-admin-0.1.54 app/controllers/motor/data_controller.rb
motor-admin-0.1.53 app/controllers/motor/data_controller.rb
motor-admin-0.1.52 app/controllers/motor/data_controller.rb
motor-admin-0.1.51 app/controllers/motor/data_controller.rb
motor-admin-0.1.50 app/controllers/motor/data_controller.rb
motor-admin-0.1.49 app/controllers/motor/data_controller.rb
motor-admin-0.1.48 app/controllers/motor/data_controller.rb
motor-admin-0.1.47 app/controllers/motor/data_controller.rb
motor-admin-0.1.46 app/controllers/motor/data_controller.rb