Module: Rooftop::Content

Defined in:
lib/rooftop/content_fields/content_fields.rb,
lib/rooftop/content_fields/field.rb,
lib/rooftop/content_fields/collection.rb

Overview

The Rooftop API returns content for basic and advanced custom fields together. This module cleans up the response, and creates a collection of ContentField objects, with which we can do things like parse links.

Defined Under Namespace

Classes: Collection, Field

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rooftop/content_fields/content_fields.rb', line 6

def self.included(base)
  base.include Rooftop::HookCalls
  base.send(:add_to_hook, :after_find, ->(r) {
    # basic content is the stuff which comes from WP by default.
    if r.respond_to?(:content)
      basic_fields = r.content[:basic].collect {|k,v| {name: k, value: v, fieldset: "Basic"}}
      # advanced fields from from ACF, and are exposed in the api in this form:
      # [
      #   {
      #     "title"=>"The fieldset title",
      #     "fields"=>[
      #       {"name"=>"field name", "label"=>"display name", "class"=>"type of field", "value"=>"The value of the field"},
      #       {"name"=>"field name", "label"=>"display name", "class"=>"type of field",
      #        "value"=>"The value of the field"},
      #       etc.
      #     ]
      #   }
      # ]
      # Given that's a bit convoluted, we get both the content types into the same output format, like this:
      # {"field name", "label"=>"display name", "class"=>"type of field", "value"=>"value of the field", "fieldset"=>"fieldset if there is one, or Basic for the builtin ones"}
      advanced_fields = r.content[:advanced].collect do |fieldset|
        fieldset[:fields].each do |field|
          field.merge!(fieldset: fieldset[:title])
          field[:type] = field[:class]
          field.delete(:class)
        end
        fieldset[:fields]
      end.flatten
      r.fields = Rooftop::Content::Collection.new((basic_fields + advanced_fields))
    end
  })
end