Class: PagePath

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/page_path.rb

Defined Under Namespace

Classes: NoLocaleError, NoPathError, PageNotSavedError

Class Method Summary collapse

Class Method Details

.associate(page, locale: nil, path: nil) ⇒ Object

Raises:


30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/page_path.rb', line 30

def associate(page, locale: nil, path: nil)
  locale ||= page.locale
  path ||= page.full_path
  raise PageNotSavedError unless page.id?
  raise NoLocaleError unless locale
  raise NoPathError unless path
  existing = get(locale, path)

  return create(locale: locale, path: path, page: page) unless existing

  existing.update(page: page) unless existing.page_id == page.id
  existing
end

.build(page) ⇒ Object


11
12
13
14
15
16
17
18
# File 'app/models/page_path.rb', line 11

def build(page)
  page.locales.each do |locale|
    localized = page.localize(locale)
    localized.ensure_path_segment
    associate(localized) if !localized.deleted? && localized.full_path?
  end
  page.children.each { |p| build(p) }
end

.build_allObject


20
21
22
23
24
# File 'app/models/page_path.rb', line 20

def build_all
  Page.roots.each do |p|
    build(p)
  end
end

.get(locale, path) ⇒ Object


26
27
28
# File 'app/models/page_path.rb', line 26

def get(locale, path)
  find_by(locale: locale, path: path)
end