Class: Plum::FieldExpander

Inherits:
Object
  • Object
show all
Defined in:
app/services/plum/field_expander.rb

Overview

Expands raw stored field values (as kept in entry.data or a block's fields hash) into the richer structures themes expect in Liquid: image ids become asset hashes, relationship ids become published entry hashes. The same rules are reused for entry fields and block fields so blocks behave identically.

Constant Summary collapse

MAX_RELATIONSHIP_DEPTH =
2

Instance Method Summary collapse

Constructor Details

#initialize(site:, url_builder: nil) ⇒ FieldExpander

Returns a new instance of FieldExpander.



9
10
11
12
# File 'app/services/plum/field_expander.rb', line 9

def initialize(site:, url_builder: nil)
  @site = site
  @url_builder = url_builder
end

Instance Method Details

#expand(values:, fields:, relationship_depth: MAX_RELATIONSHIP_DEPTH) ⇒ Object

values: hash of stored field values (handle => raw value) fields: array of blueprint/block field definitions ({ "handle", "type" })



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/services/plum/field_expander.rb', line 16

def expand(values:, fields:, relationship_depth: MAX_RELATIONSHIP_DEPTH)
  data = (values || {}).deep_dup
  data = {} unless data.is_a?(Hash)

  Array(fields).each do |field|
    handle = field["handle"].to_s
    next if handle.blank?

    case field["type"]
    when "image"
      data[handle] = image_asset_context(data[handle])
    when "relationship"
      data[handle] = relationship_entry_context(data[handle], relationship_depth: relationship_depth)
    end
  end

  data
end