Class: Fields::Block

Inherits:
Field
  • Object
show all
Defined in:
app/models/iron/fields/block.rb

Instance Method Summary collapse

Instance Method Details

#block_definitionObject



36
37
38
39
40
41
42
# File 'app/models/iron/fields/block.rb', line 36

def block_definition
  if definition.is_a?(FieldDefinitions::Block)
    definition.supported_block_definition
  else
    definition
  end
end

#block_listObject



32
33
34
# File 'app/models/iron/fields/block.rb', line 32

def block_list
  parent.is_a?(Fields::BlockList) ? parent : nil
end

#content_value=(value) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/models/iron/fields/block.rb', line 8

def content_value=(value)
  @content_errors = nil

  if value.nil?
    fields.each(&:mark_for_destruction)
    return
  end

  unless value.is_a?(Hash)
    add_content_error("must be an object")
    return
  end

  fields.each(&:mark_for_destruction)

  block_definition.field_definitions.each do |nested_def|
    raw = Field.content_fetch(value, nested_def.handle)
    next if raw == CONTENT_MISSING

    field = fields.build(type: nested_def.field_type, entry: entry, definition: nested_def)
    field.content_value = raw
  end
end

#export_attachmentsObject



80
81
82
# File 'app/models/iron/fields/block.rb', line 80

def export_attachments
  fields.flat_map(&:export_attachments)
end

#export_valueObject



72
73
74
75
76
77
78
# File 'app/models/iron/fields/block.rb', line 72

def export_value
  {
    type: "block",
    block_handle: block_definition.handle,
    fields: export_nested_fields
  }
end

#find_or_build_field(definition) ⇒ Object



44
45
46
# File 'app/models/iron/fields/block.rb', line 44

def find_or_build_field(definition)
  fields.find { |f| f.definition == definition } || fields.build(type: definition.field_type, entry:, definition: definition, locale:)
end

#thumbnailObject



88
89
90
91
# File 'app/models/iron/fields/block.rb', line 88

def thumbnail
  file_field = fields.find { |f| f.is_a?(Fields::File) && f.image? }
  file_field&.file
end

#titleObject



84
85
86
# File 'app/models/iron/fields/block.rb', line 84

def title
  fields.filter_map(&:searchable_text).first&.truncate(300)
end

#valueObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/models/iron/fields/block.rb', line 48

def value
  locale = Current.locale || Locale.default

  result = OpenStruct.new(id: id, _type: block_definition.handle)

  block_definition.field_definitions.each do |field_definition|
    field = fields.find do |f|
      f.definition_id == field_definition.id &&
      f.definition_type == field_definition.class.base_class.to_s &&
      f.locale_id == locale.id
    end

    result[field_definition.handle] = if field.present?
      field.value
    elsif field_definition.is_a?(FieldDefinitions::BlockList)
      []
    else
      nil
    end
  end

  result
end