Sha256: 3ac307b9b4017633ba4ec4bb26e05c7e4d8e8774f51450ff482a19e11a96a23d

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

# encoding: utf-8

class PagePath < ActiveRecord::Base
  class PageNotSavedError < StandardError; end
  class NoPathError < StandardError; end
  class NoLocaleError < StandardError; end

  belongs_to :page
  validates :locale, presence: true
  validates :path, presence: true, uniqueness: { scope: :locale }

  class << self
    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

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

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

    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pages_core-3.5.1 app/models/page_path.rb