Class: Terrazzo::Field::BelongsTo

Inherits:
Associative show all
Defined in:
lib/terrazzo/field/belongs_to.rb

Constant Summary

Constants inherited from Base

Terrazzo::Field::Base::ABSTRACT_FIELD_CLASSES

Instance Attribute Summary

Attributes inherited from Base

#attribute, #data, #options, #page, #resource

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Associative

associative?, eager_load?

Methods inherited from Base

associative?, default_options, eager_load?, #field_type, field_type, #form_input_attributes, #initialize, #required?, searchable?, sortable?, transform_param, with_options

Constructor Details

This class inherits a constructor from Terrazzo::Field::Base

Class Method Details

.permitted_attribute(attr, options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/terrazzo/field/belongs_to.rb', line 24

def permitted_attribute(attr, options = {})
  if options[:foreign_key]
    options[:foreign_key].to_sym
  elsif options[:model_class]
    reflection = options[:model_class].reflect_on_association(attr)
    reflection ? reflection.foreign_key.to_sym : :"#{attr}_id"
  else
    :"#{attr}_id"
  end
end

Instance Method Details

#serializable_options(page = nil) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/terrazzo/field/belongs_to.rb', line 14

def serializable_options(page = nil)
  return {} unless page == :form && resource
  collection = ordered_resource_options
  if options[:include_blank]
    collection = [["", nil]] + collection
  end
  { resourceOptions: collection }
end

#serialize_value(mode) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/terrazzo/field/belongs_to.rb', line 4

def serialize_value(mode)
  case mode
  when :form
    foreign_key_value&.to_s
  else
    return nil if data.nil?
    { id: data.id.to_s, display: display_name(data) }
  end
end