Class: Locomotive::PageParsingService

Inherits:
Struct
  • Object
show all
Includes:
ActiveSupport::Benchmarkable
Defined in:
app/services/locomotive/page_parsing_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#localeObject

Returns the value of attribute locale

Returns:

  • (Object)

    the current value of locale



5
6
7
# File 'app/services/locomotive/page_parsing_service.rb', line 5

def locale
  @locale
end

#siteObject

Returns the value of attribute site

Returns:

  • (Object)

    the current value of site



5
6
7
# File 'app/services/locomotive/page_parsing_service.rb', line 5

def site
  @site
end

Instance Method Details

#blocks_from_grouped_editable_elements(groups) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'app/services/locomotive/page_parsing_service.rb', line 52

def blocks_from_grouped_editable_elements(groups)
  groups.map do |block, elements|
    next if elements.empty?

    element = elements.first.last

    { name: block, label: element.block_label, priority: element.block_priority || 0 }
  end.compact.sort { |a, b| b[:priority] <=> a[:priority] }
end

#find_all_elements(page) ⇒ Object



9
10
11
# File 'app/services/locomotive/page_parsing_service.rb', line 9

def find_all_elements(page)
  find_or_create_editable_elements(page)&.slice(:elements, :sections)
end

#find_or_create_editable_elements(page) ⇒ Object



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
38
39
40
41
# File 'app/services/locomotive/page_parsing_service.rb', line 13

def find_or_create_editable_elements(page)
  benchmark "Parse page #{page._id} find_or_create_editable_elements" do
    parsed = {
      extends:                  {},
      blocks:                   {},
      super_blocks:             {},
      elements:                 [],
      sections:                 { top: [], bottom: [], dropzone: false }
    }

    subscribe(parsed) do
      parse(page)

      # remove the sections if hidden (sections might be hidden by an overidding block).
      # also sort them by their placement.
      # finally, only return their types (+ sectionId)
      extract_section_attributes!(page, parsed)

      # !Important! Non visible editable elements are not removed
      persist_editable_elements!(page, parsed)
    end

    parsed
  end
rescue Exception => e
  logger.error "[PageParsing] " + e.message + "\n\t" + e.backtrace.join("\n\t")
  puts "[PageParsing] " + e.message + "\n\t" + e.backtrace.join("\n\t")
  nil
end

#group_and_sort_editable_elements(elements) ⇒ Object

Each element of the elements parameter is a couple: Page, EditableElement



44
45
46
47
48
49
50
# File 'app/services/locomotive/page_parsing_service.rb', line 44

def group_and_sort_editable_elements(elements)
  elements.group_by { |(_, el)| el.block }.tap do |groups|
    groups.each do |_, list|
      list.sort! { |(_, a), (_, b)| (b.priority || 0) <=> (a.priority || 0) }
    end
  end
end