Sha256: 2c24cbc555b7067b8ff6cafa1c9e2fb322f383f55fc4c687a75385e5d26d727b

Contents?: true

Size: 1.59 KB

Versions: 149

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

module Motor
  class RunQueriesController < ApiBaseController
    wrap_parameters :data

    load_and_authorize_resource :query, only: :show, parent: false

    before_action :build_query, only: :create
    authorize_resource :query, only: :create

    def show
      render_result
    end

    def create
      render_result
    end

    private

    def render_result
      query_result = Queries::RunQuery.call(@query, variables_hash: variables_params,
                                                    limit: params[:limit].presence,
                                                    filters: filter_params)

      if query_result.error
        render json: { errors: [{ detail: query_result.error }] }, status: :unprocessable_entity
      else
        render json: query_result_hash(query_result)
      end
    end

    def current_user_variables
      return {} unless current_user

      current_user
        .attributes
        .slice('id', 'email')
        .transform_keys { |key| "current_user_#{key}" }
        .compact
    end

    def query_result_hash(query_result)
      {
        data: query_result.data,
        meta: {
          columns: query_result.columns
        }
      }
    end

    def build_query
      @query = Motor::Queries::Persistance.build_from_params(query_params)
    end

    def query_params
      params.require(:data).permit(:sql_body, preferences: {})
    end

    def variables_params
      params.fetch(:variables, {}).merge(current_user_variables)
    end

    def filter_params
      (params[:filter] || params[:filters])&.to_unsafe_h
    end
  end
end

Version data entries

149 entries across 149 versions & 2 rubygems

Version Path
motor-admin-0.3.17 app/controllers/motor/run_queries_controller.rb
motor-admin-0.3.16 app/controllers/motor/run_queries_controller.rb
motor-admin-0.3.15 app/controllers/motor/run_queries_controller.rb
mafia-admin-0.3.18 app/controllers/motor/run_queries_controller.rb
mafia-admin-0.3.17 app/controllers/motor/run_queries_controller.rb
mafia-admin-0.3.16 app/controllers/motor/run_queries_controller.rb
mafia-admin-0.3.15 app/controllers/motor/run_queries_controller.rb
mafia-admin-0.3.14 app/controllers/motor/run_queries_controller.rb
motor-admin-0.3.14 app/controllers/motor/run_queries_controller.rb
motor-admin-0.3.13 app/controllers/motor/run_queries_controller.rb
motor-admin-0.3.12 app/controllers/motor/run_queries_controller.rb
motor-admin-0.3.11 app/controllers/motor/run_queries_controller.rb
motor-admin-0.3.10 app/controllers/motor/run_queries_controller.rb
motor-admin-0.3.9 app/controllers/motor/run_queries_controller.rb
motor-admin-0.3.8 app/controllers/motor/run_queries_controller.rb
motor-admin-0.3.7 app/controllers/motor/run_queries_controller.rb
motor-admin-0.3.6 app/controllers/motor/run_queries_controller.rb
motor-admin-0.3.5 app/controllers/motor/run_queries_controller.rb
motor-admin-0.3.4 app/controllers/motor/run_queries_controller.rb
motor-admin-0.3.3 app/controllers/motor/run_queries_controller.rb