Sha256: 6a1ada246c662aacf78792a358bba70a127d83d937a2b6988ed1392b5703db34

Contents?: true

Size: 1.26 KB

Versions: 17

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

module Maglev
  class PagePath < ApplicationRecord
    ## associations ##
    belongs_to :page, class_name: 'Maglev::Page', foreign_key: 'maglev_page_id', inverse_of: :paths

    ## scopes ##
    scope :canonical, -> { where(canonical: true) }
    scope :not_canonical, -> { where(canonical: false) }
    scope :by_value, ->(value, locale = nil) { where(value: value, locale: locale || Maglev::I18n.current_locale) }

    ## validations ##
    validates :value, presence: true, exclusion: { in: Maglev.config.reserved_paths }
    validates :value, uniqueness: { scope: %i[locale canonical] }, if: :canonical?
    validates :canonical, uniqueness: { scope: %i[locale maglev_page_id] }, if: :canonical?

    ## callbacks ##
    after_initialize -> { self.locale ||= Maglev::I18n.current_locale }
    before_validation :clean_value!

    ## methods ##

    ## class methods ##

    def self.build_hash(page_id = nil)
      query = page_id ? where(maglev_page_id: page_id) : all
      query.canonical.pluck(:locale, :value).to_h
    end

    private

    def clean_value!
      return if value.blank?

      self.value = clean_value
    end

    def clean_value
      value
        .strip
        .gsub(%r{(^/|/$)}, '')
        .gsub(%r{//+/}, '/')
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
maglevcms-1.4.0 app/models/maglev/page_path.rb
maglevcms-1.3.0 app/models/maglev/page_path.rb
maglevcms-1.2.2 app/models/maglev/page_path.rb
maglevcms-1.2.1 app/models/maglev/page_path.rb
maglevcms-1.2.0 app/models/maglev/page_path.rb
maglevcms-1.1.7 app/models/maglev/page_path.rb
maglevcms-1.1.6 app/models/maglev/page_path.rb
maglevcms-1.1.5 app/models/maglev/page_path.rb
maglevcms-1.1.4 app/models/maglev/page_path.rb
maglevcms-1.1.3 app/models/maglev/page_path.rb
maglevcms-1.1.2 app/models/maglev/page_path.rb
maglevcms-1.1.1 app/models/maglev/page_path.rb
maglevcms-1.1.0 app/models/maglev/page_path.rb
maglevcms-1.0.0 app/models/maglev/page_path.rb
maglevcms-1.0.0.rc3 app/models/maglev/page_path.rb
maglevcms-1.0.0.rc2 app/models/maglev/page_path.rb
maglevcms-1.0.0.rc1 app/models/maglev/page_path.rb