Class: Formatic::Choices::Records

Inherits:
Object
  • Object
show all
Includes:
Calls
Defined in:
lib/formatic/choices/records.rb

Overview

Returns a list of countries suitable for a <select> box.

Instance Method Summary collapse

Instance Method Details

#associationObject



38
39
40
41
42
43
44
# File 'lib/formatic/choices/records.rb', line 38

def association
  model_klass = f&.object&.class
  return false unless model_klass.respond_to?(:reflect_on_all_associations)

  model_klass.reflect_on_all_associations(:belongs_to)
             .detect { it.foreign_key == attribute_name.to_s }
end

#callObject



14
15
16
17
18
19
20
21
# File 'lib/formatic/choices/records.rb', line 14

def call
  candidates = records_to_options || []
  return candidates unless currently_associated_record && include_current?
  return candidates if records&.include?(currently_associated_record)

  candidates.prepend currently_associated_record.presenters.for_select
  candidates
end

#currently_associated_recordObject



31
32
33
34
35
36
# File 'lib/formatic/choices/records.rb', line 31

def currently_associated_record
  return unless association
  return unless f.object

  f.object.public_send(association.name)
end

#include_current?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/formatic/choices/records.rb', line 23

def include_current?
  include_current != false
end

#records_to_optionsObject



27
28
29
# File 'lib/formatic/choices/records.rb', line 27

def records_to_options
  records&.map(&:presenters)&.map(&:for_select)
end