Sha256: e215a64339a0da90fbaf70315bea9befa4c46aae1d01e419f9f6eb8f333c5c2e

Contents?: true

Size: 1.26 KB

Versions: 25

Compression:

Stored size: 1.26 KB

Contents

module RailsPgExtras::Web
  class QueriesController < RailsPgExtras::Web::ApplicationController
    before_action :load_queries
    helper_method :unavailable_extensions

    def index
      if params[:query_name].present?
        @query_name = params[:query_name].to_sym.presence_in(@all_queries.keys)
        return unless @query_name

        begin
          @result = RailsPgExtras.run_query(query_name: @query_name.to_sym, in_format: :raw)
        rescue ActiveRecord::StatementInvalid => e
          @error = e.message
        end

        render :show
      end
    end

    private

    def load_queries
      @all_queries = (RailsPgExtras::QUERIES - RailsPgExtras::Web::ACTIONS).inject({}) do |memo, query_name|
        unless query_name.in? %i[mandelbrot]
          memo[query_name] = { disabled: query_disabled?(query_name) }
        end

        memo
      end
    end

    def query_disabled?(query_name)
      unavailable_extensions.values.flatten.include?(query_name)
    end

    def unavailable_extensions
      return @unavailable_extensions if defined?(@unavailable_extensions)

      enabled_extensions = ActiveRecord::Base.connection.extensions
      @unavailable_extensions = REQUIRED_EXTENSIONS.delete_if { |ext| ext.to_s.in?(enabled_extensions)  }
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
rails-pg-extras-5.3.1 app/controllers/rails_pg_extras/web/queries_controller.rb
rails-pg-extras-5.3.0 app/controllers/rails_pg_extras/web/queries_controller.rb
rails-pg-extras-5.2.5 app/controllers/rails_pg_extras/web/queries_controller.rb
rails-pg-extras-5.2.4 app/controllers/rails_pg_extras/web/queries_controller.rb
rails-pg-extras-5.2.3 app/controllers/rails_pg_extras/web/queries_controller.rb
rails-pg-extras-5.2.2 app/controllers/rails_pg_extras/web/queries_controller.rb
rails-pg-extras-5.2.1 app/controllers/rails_pg_extras/web/queries_controller.rb
rails-pg-extras-5.2.0 app/controllers/rails_pg_extras/web/queries_controller.rb
rails-pg-extras-5.1.0 app/controllers/rails_pg_extras/web/queries_controller.rb
rails-pg-extras-5.0.0 app/controllers/rails_pg_extras/web/queries_controller.rb
rails-pg-extras-4.13.0 app/controllers/rails_pg_extras/web/queries_controller.rb
rails-pg-extras-4.12.2 app/controllers/rails_pg_extras/web/queries_controller.rb
rails-pg-extras-4.12.1 app/controllers/rails_pg_extras/web/queries_controller.rb
rails-pg-extras-4.12.0 app/controllers/rails_pg_extras/web/queries_controller.rb
rails-pg-extras-4.10.0 app/controllers/rails_pg_extras/web/queries_controller.rb
rails-pg-extras-4.9.0 app/controllers/rails_pg_extras/web/queries_controller.rb
rails-pg-extras-4.8.1 app/controllers/rails_pg_extras/web/queries_controller.rb
rails-pg-extras-4.7.2 app/controllers/rails_pg_extras/web/queries_controller.rb
rails-pg-extras-4.7.1 app/controllers/rails_pg_extras/web/queries_controller.rb
rails-pg-extras-4.7.0 app/controllers/rails_pg_extras/web/queries_controller.rb