Class: Terrazzo::Field::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/terrazzo/field/base.rb

Constant Summary collapse

ABSTRACT_FIELD_CLASSES =
%w[Base Associative Deferred].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute, data = nil, page = nil, resource: nil, options: {}) ⇒ Base

Returns a new instance of Base.



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

def initialize(attribute, data = nil, page = nil, resource: nil, options: {})
  @attribute = attribute
  @data = data
  @page = page
  @resource = resource
  @options = self.class.default_options.merge(options)
  @data = resolve_data if resource && data.nil?
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



4
5
6
# File 'lib/terrazzo/field/base.rb', line 4

def attribute
  @attribute
end

#dataObject (readonly)

Returns the value of attribute data.



4
5
6
# File 'lib/terrazzo/field/base.rb', line 4

def data
  @data
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/terrazzo/field/base.rb', line 4

def options
  @options
end

#pageObject (readonly)

Returns the value of attribute page.



4
5
6
# File 'lib/terrazzo/field/base.rb', line 4

def page
  @page
end

#resourceObject (readonly)

Returns the value of attribute resource.



4
5
6
# File 'lib/terrazzo/field/base.rb', line 4

def resource
  @resource
end

Class Method Details

.associative?Boolean

Returns:



86
87
88
# File 'lib/terrazzo/field/base.rb', line 86

def associative?
  false
end

.default_optionsObject



102
103
104
# File 'lib/terrazzo/field/base.rb', line 102

def default_options
  {}
end

.eager_load?Boolean

Returns:



82
83
84
# File 'lib/terrazzo/field/base.rb', line 82

def eager_load?
  false
end

.field_typeObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/terrazzo/field/base.rb', line 58

def field_type
  # Walk ancestors to find the nearest concrete Terrazzo::Field class.
  # This ensures custom subclasses (e.g. HasManyScopedField < HasMany)
  # resolve to the parent's field_type for FieldRenderer mapping.
  ancestors.each do |klass|
    next unless klass.is_a?(Class) && klass < Terrazzo::Field::Base
    next if klass == Terrazzo::Field::Base
    if klass.name&.start_with?("Terrazzo::Field::")
      demod = klass.name.demodulize
      next if ABSTRACT_FIELD_CLASSES.include?(demod)
      return demod.underscore
    end
  end
  name.demodulize.underscore
end

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



94
95
96
# File 'lib/terrazzo/field/base.rb', line 94

def permitted_attribute(attr, _options = {})
  attr.to_sym
end

.searchable?Boolean

Returns:



74
75
76
# File 'lib/terrazzo/field/base.rb', line 74

def searchable?
  false
end

.sortable?Boolean

Returns:



78
79
80
# File 'lib/terrazzo/field/base.rb', line 78

def sortable?
  true
end

.transform_param(value) ⇒ Object



98
99
100
# File 'lib/terrazzo/field/base.rb', line 98

def transform_param(value)
  value
end

.with_options(**opts) ⇒ Object



90
91
92
# File 'lib/terrazzo/field/base.rb', line 90

def with_options(**opts)
  Deferred.new(self, **opts)
end

Instance Method Details

#field_typeObject



15
16
17
# File 'lib/terrazzo/field/base.rb', line 15

def field_type
  self.class.field_type
end

#form_input_attributes(param_key) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/terrazzo/field/base.rb', line 35

def form_input_attributes(param_key)
  opts = options.dup
  opts[:model_class] ||= resource.class if resource
  perm = self.class.permitted_attribute(attribute, opts)
  case perm
  when Hash
    key = perm.keys.first.to_s
    { name: "#{param_key}[#{key}][]", id: "#{param_key}_#{key}" }
  when Array
    # polymorphic returns [type_attr, id_attr]
    {
      name: "#{param_key}[#{perm.last}]",
      id: "#{param_key}_#{perm.last}",
      typeName: "#{param_key}[#{perm.first}]"
    }
  else
    { name: "#{param_key}[#{perm}]", id: "#{param_key}_#{perm}" }
  end
end

#required?Boolean

Returns:



27
28
29
30
31
32
33
# File 'lib/terrazzo/field/base.rb', line 27

def required?
  return false unless resource

  resource.class.validators_on(attribute).any? do |v|
    v.is_a?(ActiveModel::Validations::PresenceValidator)
  end
end

#serializable_options(page = nil) ⇒ Object



23
24
25
# File 'lib/terrazzo/field/base.rb', line 23

def serializable_options(page = nil)
  {}
end

#serialize_value(mode) ⇒ Object



19
20
21
# File 'lib/terrazzo/field/base.rb', line 19

def serialize_value(mode)
  data
end