Class: ActiveElement::Components::Util::DefaultDisplayValue

Inherits:
Object
  • Object
show all
Defined in:
lib/active_element/components/util/default_display_value.rb

Overview

Infers a default display value from any given object using multiple strategies.

Constant Summary collapse

DEFAULT_FIELDS =
%i[display_name email name username].freeze

Instance Method Summary collapse

Constructor Details

#initialize(object:) ⇒ DefaultDisplayValue

Returns a new instance of DefaultDisplayValue.



10
11
12
# File 'lib/active_element/components/util/default_display_value.rb', line 10

def initialize(object:)
  @object = object
end

Instance Method Details

#valueObject



14
15
16
17
18
19
20
21
22
23
# File 'lib/active_element/components/util/default_display_value.rb', line 14

def value
  if object.respond_to?(:default_display_attribute)
    return object.public_send(object.default_display_attribute)
  end

  DEFAULT_FIELDS.each do |field|
    return object.public_send(field) if active_record_value?(field)
    return object[field] if hash_key(field) if hash_value?(field)
  end
end