Class: ActiveElement::Components::Util::AssociationMapping
- Inherits:
-
Object
- Object
- ActiveElement::Components::Util::AssociationMapping
- 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
- #associated_model ⇒ Object
- #display_field ⇒ Object
- #display_value(value = associated_record) ⇒ Object
-
#initialize(controller:, field:, record:, associated_record:, options: {}) ⇒ AssociationMapping
constructor
A new instance of AssociationMapping.
- #link_tag ⇒ Object
- #multiple_association? ⇒ Boolean
- #options_for_select(scope: nil) ⇒ Object
-
#relation_id ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity.
- #relation_key ⇒ Object
- #single_association? ⇒ Boolean
- #total_count ⇒ Object
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 = || {} end |
Instance Method Details
#associated_model ⇒ Object
81 82 83 |
# File 'lib/active_element/components/util/association_mapping.rb', line 81 def associated_model record.association(field).klass end |
#display_field ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/active_element/components/util/association_mapping.rb', line 49 def display_field @display_field ||= .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 |
#link_tag ⇒ Object
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
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 (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_id ⇒ Object
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_key ⇒ Object
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
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_count ⇒ Object
58 59 60 |
# File 'lib/active_element/components/util/association_mapping.rb', line 58 def total_count associated_model&.count end |