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



38
39
40
41
42
43
44
45
46
# File 'app/services/locomotive/page_parsing_service.rb', line 38

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_or_create_editable_elements(page) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/services/locomotive/page_parsing_service.rb', line 9

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

    subscribe(parsed) do
      parse(page)

      persist_editable_elements(page, parsed).tap do |elements|
        # FIXME (Did): do not remove "useless" editable elements since
        # they might have become orphaned because of a typo for instance.
        # Instead, we should warn the Wagon developer (TODO).
        # remove_useless_editable_elements(page, elements)
      end
    end
  end
rescue Exception => e
  logger.error "[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



30
31
32
33
34
35
36
# File 'app/services/locomotive/page_parsing_service.rb', line 30

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