Skip to content
Snippets Groups Projects
with_path.rb 1.93 KiB
Newer Older
Arnaud Levy's avatar
Arnaud Levy committed
module Communication::Website::Page::WithPath
  extend ActiveSupport::Concern

  included do
Arnaud Levy's avatar
Arnaud Levy committed
    before_validation :set_slug
Arnaud Levy's avatar
Arnaud Levy committed
    validate :validate_slug
Arnaud Levy's avatar
Arnaud Levy committed
  end

pabois's avatar
pabois committed
  def path
    path = ''
pabois's avatar
pabois committed
    # TODO i18n remplacer le choix de la langue
    if website.languages.many?
      path += "/#{website.default_language.iso_code}"
pabois's avatar
pabois committed
    end
pabois's avatar
pabois committed
    path += "/#{slug_with_ancestors}/"
    path.gsub(/\/+/, '/')
Arnaud Levy's avatar
Arnaud Levy committed
  end

pabois's avatar
pabois committed
  def slug_with_ancestors
    (ancestors.map(&:slug) << slug).reject(&:blank?).join('/')
Arnaud Levy's avatar
Arnaud Levy committed
  end

  def git_path(website)
Arnaud Levy's avatar
Arnaud Levy committed
    # Same website only, page published only
    # FIXME is it ever called for other websites?
    return unless website.id == communication_website_id && published
Arnaud Levy's avatar
Arnaud Levy committed
    current_git_path
Arnaud Levy's avatar
Arnaud Levy committed
  end

  def url
    return unless published
    return if website.url.blank?
Arnaud Levy's avatar
Arnaud Levy committed
    "#{website.url}#{path}".gsub('//', '/')
Arnaud Levy's avatar
Arnaud Levy committed
  end

  protected

Arnaud Levy's avatar
Arnaud Levy committed
  def current_git_path
    @current_git_path ||= "#{git_path_prefix}pages/#{slug_with_ancestors}/_index.html"
  end

Arnaud Levy's avatar
Arnaud Levy committed
  def git_path_prefix
Arnaud Levy's avatar
Arnaud Levy committed
    @git_path_prefix ||= git_path_content_prefix(website)
Arnaud Levy's avatar
Arnaud Levy committed
  end

Arnaud Levy's avatar
Arnaud Levy committed
  def set_slug
Sébastien Gaya's avatar
Sébastien Gaya committed
    self.slug = to_s.parameterize if self.slug.blank?
Arnaud Levy's avatar
Arnaud Levy committed
    current_slug = self.slug
    n = 0
    while slug_unavailable?(self.slug)
      n += 1
      self.slug = [current_slug, n].join('-')
    end
  end

  def slug_unavailable?(slug)
    self.class.unscoped
              .where(communication_website_id: self.communication_website_id, slug: slug)
              .where.not(id: self.id)
              .exists?
  end

Arnaud Levy's avatar
Arnaud Levy committed
  def validate_slug
    slug_must_be_present
    slug_must_be_unique
    slug_must_have_proper_format
  end

  def slug_must_be_present
    errors.add(:slug, ActiveRecord::Errors.default_error_messages[:absent]) if slug.blank?
  end

Arnaud Levy's avatar
Arnaud Levy committed
  def slug_must_be_unique
    errors.add(:slug, ActiveRecord::Errors.default_error_messages[:taken]) if slug_unavailable?(slug)
  end

Arnaud Levy's avatar
Arnaud Levy committed
  def slug_must_have_proper_format
Arnaud Levy's avatar
Arnaud Levy committed
    errors.add(:slug, I18n.t('slug_error')) unless /\A[a-z0-9\-]+\z/.match?(slug)
Arnaud Levy's avatar
Arnaud Levy committed
  end

Arnaud Levy's avatar
Arnaud Levy committed
end