Class: ActiveElement::Components::Util::AssociationMapping

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

Overview

Utility class for mapping an association to a linked URL display value, (e.g. for displaying associations in a table, form, etc.).

Instance Method Summary collapse

Constructor Details

#initialize(controller:, field:, record:, associated_record:, options: {}) ⇒ AssociationMapping

Returns a new instance of AssociationMapping.



9
10
11
12
13
14
15
# File 'lib/active_element/components/util/association_mapping.rb', line 9

def initialize(controller:, field:, record:, associated_record:, options: {})
  @controller = controller
  @field = field
  @record = record
  @associated_record = associated_record
  @options = options || {}
end

Instance Method Details

#associated_modelObject



81
82
83
# File 'lib/active_element/components/util/association_mapping.rb', line 81

def associated_model
  record.association(field).klass
end

#display_fieldObject



49
50
51
52
53
54
55
56
# File 'lib/active_element/components/util/association_mapping.rb', line 49

def display_field
  @display_field ||= options.fetch(:attribute) do
    next associated_model.default_display_attribute if defined_display_attribute?
    next default_display_attribute if default_display_attribute.present?

    associated_model.primary_key
  end
end

#display_value(value = associated_record) ⇒ Object



42
43
44
45
46
47
# File 'lib/active_element/components/util/association_mapping.rb', line 42

def display_value(value = associated_record)
  return value&.public_send(display_field) if display_field.present?
  return nil if associated_model&.primary_key.blank?

  value.public_send(associated_model.primary_key)
end


17
18
19
20
21
22
# File 'lib/active_element/components/util/association_mapping.rb', line 17

def link_tag
  verify_display_attribute
  return nil if associated_record.blank?
  return associated_record.map { |value| link_to(value) } if multiple_association?
  return link_to(associated_record) if single_association?
end

#multiple_association?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/active_element/components/util/association_mapping.rb', line 77

def multiple_association?
  [:has_and_belongs_to_many, :has_many].include?(relation.macro)
end

#options_for_select(scope: nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/active_element/components/util/association_mapping.rb', line 62

def options_for_select(scope: nil)
  return [] if associated_model.blank?

  base = scope.nil? ? associated_model : associated_model.public_send(scope)
  base.all.pluck(display_field, associated_model.primary_key).sort.map do |title, value|
    next [title, value] if display_field == associated_model.primary_key

    ["#{title} (#{value})", value]
  end
end

#relation_idObject

rubocop:disable Metrics/CyclomaticComplexity



24
25
26
27
28
29
30
31
# File 'lib/active_element/components/util/association_mapping.rb', line 24

def relation_id # rubocop:disable Metrics/CyclomaticComplexity
  case relation.macro
  when :has_one, :belongs_to
    associated_record&.public_send(relation_key)
  when :has_many
    associated_record&.map(&relation_key.to_sym)
  end
end

#relation_keyObject



33
34
35
36
37
38
39
40
# File 'lib/active_element/components/util/association_mapping.rb', line 33

def relation_key
  case relation.macro
  when :has_one, :has_many
    relation.klass.primary_key
  when :belongs_to
    relation.association_primary_key
  end
end

#single_association?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/active_element/components/util/association_mapping.rb', line 73

def single_association?
  %i[has_one belongs_to].include?(relation.macro)
end

#total_countObject



58
59
60
# File 'lib/active_element/components/util/association_mapping.rb', line 58

def total_count
  associated_model&.count
end