Sha256: d8e89081207d61cc2ca8bc6201ad0a0a46878144e2a20353706a0cdfc17479c5

Contents?: true

Size: 1.03 KB

Versions: 8

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

require 'terminal-table'
require 'ruby-pg-extras'

module RailsPGExtras
  QUERIES = RubyPGExtras::QUERIES
  DEFAULT_ARGS = RubyPGExtras::DEFAULT_ARGS

  QUERIES.each do |query_name|
    define_singleton_method query_name do |options = {}|
      run_query(
        query_name: query_name,
        in_format: options.fetch(:in_format, :display_table),
        args: options.fetch(:args, {})
      )
    end
  end

  def self.run_query(query_name:, in_format:, args: {})
    sql = if (custom_args = DEFAULT_ARGS[query_name].merge(args)) != {}
      RubyPGExtras.sql_for(query_name: query_name) % custom_args
    else
      RubyPGExtras.sql_for(query_name: query_name)
    end

    result = connection.execute(sql)

    RubyPGExtras.display_result(
      result,
      title: RubyPGExtras.description_for(query_name: query_name),
      in_format: in_format
    )
  end

  def self.connection
    ActiveRecord::Base.connection
  end

  private_class_method :connection
end

require 'rails-pg-extras/railtie' if defined?(Rails)

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rails-pg-extras-1.6.0 lib/rails-pg-extras.rb
rails-pg-extras-1.5.3 lib/rails-pg-extras.rb
rails-pg-extras-1.5.2 lib/rails-pg-extras.rb
rails-pg-extras-1.5.1 lib/rails-pg-extras.rb
rails-pg-extras-1.5.0 lib/rails-pg-extras.rb
rails-pg-extras-1.4.0 lib/rails-pg-extras.rb
rails-pg-extras-1.3.1 lib/rails-pg-extras.rb
rails-pg-extras-1.3.0 lib/rails-pg-extras.rb