Module: Maglev::SectionsConcern

Included in:
Page, SectionsContentStore, Site
Defined in:
app/models/concerns/maglev/sections_concern.rb

Overview

rubocop:disable Style/ClassAndModuleChildren

Instance Method Summary collapse

Instance Method Details

#delete_section(section_id) ⇒ Object



42
43
44
45
# File 'app/models/concerns/maglev/sections_concern.rb', line 42

def delete_section(section_id)
  sections_translations_will_change!
  sections.delete_if { |section| section['id'] == section_id }
end

#find_section_block_by_id(section_id, id) ⇒ Object



51
52
53
# File 'app/models/concerns/maglev/sections_concern.rb', line 51

def find_section_block_by_id(section_id, id)
  find_section_by_id(section_id)&.dig('blocks')&.find { |block| block['id'] == id }
end

#find_section_by_id(id) ⇒ Object



28
29
30
# File 'app/models/concerns/maglev/sections_concern.rb', line 28

def find_section_by_id(id)
  sections.find { |section| section['id'] == id }
end

#find_sections_by_type(type) ⇒ Object



32
33
34
# File 'app/models/concerns/maglev/sections_concern.rb', line 32

def find_sections_by_type(type)
  sections&.find_all { |section| section['type'] == type } || []
end

#position_of_section(section_id) ⇒ Object



24
25
26
# File 'app/models/concerns/maglev/sections_concern.rb', line 24

def position_of_section(section_id)
  sections.index { |section| section['id'] == section_id }
end

#prepare_sections(theme) ⇒ Object



5
6
7
8
9
10
11
12
# File 'app/models/concerns/maglev/sections_concern.rb', line 5

def prepare_sections(theme)
  # NOTE: pages defined in the theme definition
  # don't include the ids for sections/blocks
  self.sections ||= [] # NOTE: the self is mandatory here
  sections.each do |section|
    prepare_section(theme, section)
  end
end

#prepare_sections_translations(theme) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'app/models/concerns/maglev/sections_concern.rb', line 14

def prepare_sections_translations(theme)
  return if sections_translations.blank?

  sections_translations.each_key do |locale|
    Maglev::I18n.with_locale(locale) do
      prepare_sections(theme)
    end
  end
end

#reorder_sections(sorted_section_ids, lock_version) ⇒ Object



36
37
38
39
40
# File 'app/models/concerns/maglev/sections_concern.rb', line 36

def reorder_sections(sorted_section_ids, lock_version)
  self.lock_version = lock_version
  sections_translations_will_change!
  sections.sort! { |a, b| sorted_section_ids.index(a['id']) <=> sorted_section_ids.index(b['id']) }
end

#section_idsObject



47
48
49
# File 'app/models/concerns/maglev/sections_concern.rb', line 47

def section_ids
  sections.map { |section| section['id'] }
end