Sha256: b136749bd6441c3f3ff2d41143f062abcc79f4103a20b0b24a65504dada6204b

Contents?: true

Size: 1.21 KB

Versions: 26

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

module Motor
  module BuildSchema
    module FindDisplayColumn
      DISPLAY_NAMES = %w[
        name
        full_name
        fullname
        last_name
        lastname
        first_name
        firstname
        fname
        lname
        sname
        phone
        phone_number
        email
        domain
        phone
        company
        filename
        file_name
        title
        url
        make
        brand
        manufacturer
        model
        address
      ].freeze

      DISPLAY_NAME_REGEXP = Regexp.new(Regexp.union(DISPLAY_NAMES).source, Regexp::IGNORECASE)

      module_function

      def call(model)
        column_names = fetch_column_names(model)

        select_column_name(column_names)
      end

      def select_column_name(column_names)
        name   = column_names.find { |column_name| column_name.in?(DISPLAY_NAMES) }
        name ||= column_names.find { |column_name| column_name.match?(DISPLAY_NAME_REGEXP) }

        name
      end

      def fetch_column_names(model)
        model.columns.map do |column|
          next unless column.type.in?(BuildSchema::SEARCHABLE_COLUMN_TYPES)

          column.name
        end.compact
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
motor-admin-0.1.40 lib/motor/build_schema/find_display_column.rb
motor-admin-0.1.39 lib/motor/build_schema/find_display_column.rb
motor-admin-0.1.37 lib/motor/build_schema/find_display_column.rb
motor-admin-0.1.36 lib/motor/build_schema/find_display_column.rb
motor-admin-0.1.35 lib/motor/build_schema/find_display_column.rb
motor-admin-0.1.34 lib/motor/build_schema/find_display_column.rb
motor-admin-0.1.33 lib/motor/build_schema/find_display_column.rb
motor-admin-0.1.32 lib/motor/build_schema/find_display_column.rb
motor-admin-0.1.31 lib/motor/build_schema/find_display_column.rb
motor-admin-0.1.29 lib/motor/build_schema/find_display_column.rb
motor-admin-0.1.28 lib/motor/build_schema/find_display_column.rb
motor-admin-0.1.27 lib/motor/build_schema/find_display_column.rb
motor-admin-0.1.25 lib/motor/build_schema/find_display_column.rb
motor-admin-0.1.23 lib/motor/build_schema/find_display_column.rb
motor-admin-0.1.22 lib/motor/build_schema/find_display_column.rb
motor-admin-0.1.21 lib/motor/build_schema/find_display_column.rb
motor-admin-0.1.20 lib/motor/build_schema/find_display_column.rb
motor-admin-0.1.18 lib/motor/build_schema/find_display_column.rb
motor-admin-0.1.17 lib/motor/build_schema/find_display_column.rb
motor-admin-0.1.15 lib/motor/build_schema/find_display_column.rb