Sha256: b2bb636c63d8373522a6f2511df314ef660ed265ee1d322334b9000faf4f8afb

Contents?: true

Size: 904 Bytes

Versions: 27

Compression:

Stored size: 904 Bytes

Contents

# frozen_string_literal: true

module Motor
  module BuildSchema
    module FindSearchableColumns
      SELECT_COLUMNS_AMOUNT = 2
      COLUMN_TYPES = BuildSchema::SEARCHABLE_COLUMN_TYPES

      module_function

      def call(model)
        columns = find_searchable_columns(model)

        selected_columns =
          columns.grep(BuildSchema::FindDisplayColumn::DISPLAY_NAME_REGEXP).presence

        selected_columns ||= columns.first(SELECT_COLUMNS_AMOUNT)

        ([model.primary_key] + selected_columns).compact.sort
      end

      def find_searchable_columns(model)
        model.columns.map do |column|
          next unless column.type.in?(COLUMN_TYPES)
          next if column.respond_to?(:array?) && column.array?
          next if model.validators_on(column.name).any?(ActiveModel::Validations::InclusionValidator)

          column.name
        end.compact
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
motor-admin-0.2.69 lib/motor/build_schema/find_searchable_columns.rb
motor-admin-0.2.68 lib/motor/build_schema/find_searchable_columns.rb
motor-admin-0.2.67 lib/motor/build_schema/find_searchable_columns.rb
motor-admin-0.2.66 lib/motor/build_schema/find_searchable_columns.rb
motor-admin-0.2.65 lib/motor/build_schema/find_searchable_columns.rb
motor-admin-0.2.64 lib/motor/build_schema/find_searchable_columns.rb
motor-admin-0.2.63 lib/motor/build_schema/find_searchable_columns.rb
motor-admin-0.2.62 lib/motor/build_schema/find_searchable_columns.rb
motor-admin-0.2.61 lib/motor/build_schema/find_searchable_columns.rb
motor-admin-0.2.60 lib/motor/build_schema/find_searchable_columns.rb
motor-admin-0.2.59 lib/motor/build_schema/find_searchable_columns.rb
motor-admin-0.2.58 lib/motor/build_schema/find_searchable_columns.rb
motor-admin-0.2.57 lib/motor/build_schema/find_searchable_columns.rb
motor-admin-0.2.56 lib/motor/build_schema/find_searchable_columns.rb
motor-admin-0.2.55 lib/motor/build_schema/find_searchable_columns.rb
motor-admin-0.2.54 lib/motor/build_schema/find_searchable_columns.rb
motor-admin-0.2.53 lib/motor/build_schema/find_searchable_columns.rb
motor-admin-0.2.51 lib/motor/build_schema/find_searchable_columns.rb
motor-admin-0.2.50 lib/motor/build_schema/find_searchable_columns.rb
motor-admin-0.2.49 lib/motor/build_schema/find_searchable_columns.rb