Module: Motor::BuildSchema::FindDisplayColumn

Defined in:
lib/motor/build_schema/find_display_column.rb

Constant Summary collapse

DISPLAY_NAMES =
Set.new(
  %w[
    name
    full_name
    fullname
    last_name
    lastname
    first_name
    firstname
    fname
    lname
    sname
    company
    domain
    website
    title
    phone
    phone_number
    email
    phone
    filename
    file_name
    url
    make
    brand
    manufacturer
    model
    address
    code
  ]
).freeze
DISPLAY_NAME_REGEXP =
Regexp.new(Regexp.union(DISPLAY_NAMES.to_a).source, Regexp::IGNORECASE)

Class Method Summary collapse

Class Method Details

.call(model) ⇒ Object


42
43
44
45
46
# File 'lib/motor/build_schema/find_display_column.rb', line 42

def call(model)
  column_names = fetch_column_names(model)

  select_column_name(column_names)
end

.fetch_column_names(model) ⇒ Object


55
56
57
58
59
60
61
62
# File 'lib/motor/build_schema/find_display_column.rb', line 55

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

    column.name
  end.compact
end

.select_column_name(column_names) ⇒ Object


48
49
50
51
52
53
# File 'lib/motor/build_schema/find_display_column.rb', line 48

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