Module: Recourse::Helpers::References

Included in:
Recourse::Helpers
Defined in:
lib/recourse/helpers/references.rb

Overview

Fields for a foreign key: a value to type, or a list to pick from.

Instance Method Summary collapse

Instance Method Details

#belongs_to_association(column) ⇒ Object

The belongs_to a column is the foreign key of, or nil when it is not one.



6
7
8
9
# File 'lib/recourse/helpers/references.rb', line 6

def belongs_to_association(column)
  resource_model.reflect_on_all_associations(:belongs_to)
                .find { |one| one.foreign_key.to_s == column }
end

#reference_cell(resource, association) ⇒ Object

What the record a foreign key points at is called, rather than the id that points at it. association reads the target without naming a method for it.



21
22
23
24
25
26
# File 'lib/recourse/helpers/references.rb', line 21

def reference_cell(resource, association)
  record = resource.association(association.name).reader
  return unless record

  record.attributes[association.klass.recourse_label.to_s]
end

#reference_field(form, column, association) ⇒ Object

A field for a foreign key. Where the label has a length it is short enough to type, and typing it beats listing every record there is to pick from.



13
14
15
16
17
# File 'lib/recourse/helpers/references.rb', line 13

def reference_field(form, column, association)
  return typed_reference form, column, association if typed_reference? association

  combobox form, column, association
end

#reference_title(column, association) ⇒ Object

Heading for a column, naming the attribute too where one has to be typed: ZIP code rather than ZIP, since a code is what the field asks for.



30
31
32
33
34
35
36
37
# File 'lib/recourse/helpers/references.rb', line 30

def reference_title(column, association)
  return resource_column_title column unless typed_reference? association

  klass = association.klass
  attribute = klass.human_attribute_name klass.recourse_label

  "#{klass.model_name.human} #{attribute.downcase}"
end