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

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

Instance Method Summary collapse

Constructor Details

#initialize(model_class, fields) ⇒ TitleFieldFinder

Returns a new instance of TitleFieldFinder.



2
3
4
5
# File 'lib/adaptors/wallaby/active_record/model_decorator/title_field_finder.rb', line 2

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

Instance Method Details

#findObject



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/adaptors/wallaby/active_record/model_decorator/title_field_finder.rb', line 7

def find
  possible_title_fields = @fields.select do |field_name, |
    %w( string ).include? [:type]
  end
  target_field = possible_title_fields.values.find do ||
    %w( name title string text ).any?{ |v| [:name].index v }
  end
  target_field ||= possible_title_fields.values.first
  target_field ||= { name: @model_class.primary_key }
  target_field[:name]
end