Class: Wallaby::ActiveRecord::ModelDecorator::TitleFieldFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/adapters/wallaby/active_record/model_decorator/title_field_finder.rb

Overview

Try to find the field that can be used as title

Constant Summary collapse

TITLE_FIELD_TYPES =
%w[string].freeze

Instance Method Summary collapse

Constructor Details

#initialize(model_class, fields) ⇒ TitleFieldFinder

Returns a new instance of TitleFieldFinder.

Parameters:

  • model_class (Class)
  • fields (Hash)

    fields metadata



12
13
14
15
# File 'lib/adapters/wallaby/active_record/model_decorator/title_field_finder.rb', line 12

def initialize(model_class, fields)
  @model_class  = model_class
  @fields       = fields
end

Instance Method Details

#findString

Returns field name that can be used as title.

Returns:

  • (String)

    field name that can be used as title



18
19
20
21
22
23
24
25
# File 'lib/adapters/wallaby/active_record/model_decorator/title_field_finder.rb', line 18

def find
  FieldUtils.first_field_by(
    {
      name: /title|name|label|string/,
      type: 'string'
    }, @fields
  ) || @model_class.primary_key
end