Class: Terrazzo::Field::Polymorphic

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

Constant Summary

Constants inherited from Base

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?, transform_param, with_options

Constructor Details

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

Class Method Details

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



32
33
34
# File 'lib/terrazzo/field/polymorphic.rb', line 32

def permitted_attribute(attr, _options = {})
  [:"#{attr}_type", :"#{attr}_id"]
end

.sortable?Boolean

Returns:



28
29
30
# File 'lib/terrazzo/field/polymorphic.rb', line 28

def sortable?
  false
end

Instance Method Details

#serializable_options(page = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/terrazzo/field/polymorphic.rb', line 15

def serializable_options(page = nil)
  return {} unless page == :form
  classes = options[:classes] || []
  order = options[:order]
  grouped = classes.each_with_object({}) do |klass, hash|
    klass = klass.constantize if klass.is_a?(::String)
    scope = order ? klass.order(order) : klass.all
    hash[klass.name] = scope.map { |r| [display_name(r), r.id.to_s] }
  end
  { groupedOptions: grouped }
end

#serialize_value(mode) ⇒ Object



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

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