Sha256: 7556100a6c0dd8150ed465ee63a146cc6406819af1c15f16676d1267c66c68fd

Contents?: true

Size: 1.24 KB

Versions: 4

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

module RailsDevtools
  class DatabaseTableSearchForm
    include ActiveModel::Model

    DatabaseTable = Data.define(:table_name, :columns, :indexes)

    def initialize(search: "")
      @search = search.downcase
    end

    def results
      table_names = ActiveRecord::Base.connection.tables - ["schema_migrations", "ar_internal_metadata"]

      tables = table_names.map do |table|
        DatabaseTable.new(
          table_name: table,
          columns: ActiveRecord::Base.connection.columns(table),
          indexes: indexes(table)
        )
      end

      return tables if @search.empty?

      tables.select do |table|
        table.table_name.include?(@search) ||
          table.columns.any? { |column| column.name.include?(@search) }
      end
    end

    private

    ShortIndex = Data.define(:name, :columns, :unique)

    def indexes(table)
      indexes = ActiveRecord::Base.connection.indexes(table)
      indexes.map do |index|
        long_name = index.name
        prefix = "index_#{table}_on_"
        short_name = long_name.gsub(prefix, "").humanize

        ShortIndex.new(
          name: short_name,
          columns: index.columns,
          unique: index.unique
        )
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rails_devtools-0.1.3 app/forms/rails_devtools/database_table_search_form.rb
rails_devtools-0.1.2 app/forms/rails_devtools/database_table_search_form.rb
rails_devtools-0.1.1 app/forms/rails_devtools/database_table_search_form.rb
rails_devtools-0.1.0 app/forms/rails_devtools/database_table_search_form.rb