Class: Sibu::Page

Inherits:
ApplicationRecord show all
Includes:
SectionsConcern
Defined in:
app/models/sibu/page.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SectionsConcern

#child_element, #clone_element, #create_section, #delete_element, #delete_section, #element, #elements, #elt, #find_or_init, #section, #update_element

Class Method Details

.lookup(domain_name, page_query) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'app/models/sibu/page.rb', line 12

def self.lookup(domain_name, page_query)
  page_path = (page_query || '').strip.split('?')[0]
  if page_path.blank?
    joins(:site).where("sibu_sites.domain = ? AND COALESCE(sibu_pages.path, '') = ''", domain_name).first
  else
    paths = page_path.split('/').inject([]) {|p, s| p << (p.length == 0 ? s : "#{p[-1]}/#{s}")}
    joins(:site).where("sibu_sites.domain = ? AND COALESCE(sibu_pages.path, '') IN (?)", domain_name, paths)
        .order(path: :desc)
        .first
  end
end

Instance Method Details

#deep_copyObject



41
42
43
44
45
# File 'app/models/sibu/page.rb', line 41

def deep_copy
  new_page = deep_dup
  new_page.name = name + ' - copie'
  new_page
end

#reorder_sections(*ordered_ids) ⇒ Object



54
55
56
57
58
59
# File 'app/models/sibu/page.rb', line 54

def reorder_sections(*ordered_ids)
  if ordered_ids.length == sections.length
    self.sections = ordered_ids.map {|section_id| sections.find {|sec| sec["id"] == section_id}}
    save
  end
end

#save_and_initObject



24
25
26
27
28
29
30
# File 'app/models/sibu/page.rb', line 24

def save_and_init
  if valid?
    template_defaults = site.site_template.pages.first
    self.sections = template_defaults[:sections]  if template_defaults
  end
  save
end

#site_templateObject



37
38
39
# File 'app/models/sibu/page.rb', line 37

def site_template
  site.site_template
end

#update_pathObject

Todo : fix me (is_home flag ?)



33
34
35
# File 'app/models/sibu/page.rb', line 33

def update_path
  self.path = name.parameterize if self.path.blank? && name != 'Accueil'
end

#update_templates(sections_templates) ⇒ Object



47
48
49
50
51
52
# File 'app/models/sibu/page.rb', line 47

def update_templates(sections_templates)
  sections.each do |s|
    s["template"] = sections_templates[s["id"]]
  end
  save
end