Sha256: 92f7e71359aaaa8c0aef2cd39084222344903c2f671c19ba2886cad962209f71

Contents?: true

Size: 1.63 KB

Versions: 87

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

module Alchemy
  class Page
    # = The url_path for this page
    #
    # Use this to build relative links to this page
    #
    # It takes several circumstances into account:
    #
    # 1. It returns just a slash for language root pages of the default langauge
    # 2. It returns a url path with a leading slash for regular pages
    # 3. It returns a url path with a leading slash and language code prefix for pages not having the default language
    # 4. It returns a url path with a leading slash and the language code for language root pages of a non-default language
    #
    # == Examples
    #
    # Using Rails' link_to helper
    #
    #     link_to page.url
    #
    class UrlPath
      def initialize(page)
        @page = page
        @language = @page.language
        @site = @language.site
      end

      def call
        if @page.language_root?
          language_root_path
        elsif @site.languages.count(&:public?) > 1
          page_path_with_language_prefix
        else
          page_path_with_leading_slash
        end
      end

      private

      def language_root_path
        @language.default? ? root_path : language_path
      end

      def page_path_with_language_prefix
        @language.default? ? page_path : language_path + page_path
      end

      def page_path_with_leading_slash
        @page.language_root? ? root_path : page_path
      end

      def language_path
        "#{root_path}#{@page.language_code}"
      end

      def page_path
        "#{root_path}#{@page.urlname}"
      end

      def root_path
        Engine.routes.url_helpers.root_path
      end
    end
  end
end

Version data entries

87 entries across 87 versions & 1 rubygems

Version Path
alchemy_cms-7.1.8 app/models/alchemy/page/url_path.rb
alchemy_cms-7.1.7 app/models/alchemy/page/url_path.rb
alchemy_cms-7.0.12 app/models/alchemy/page/url_path.rb
alchemy_cms-7.0.11 app/models/alchemy/page/url_path.rb
alchemy_cms-7.1.6 app/models/alchemy/page/url_path.rb
alchemy_cms-7.1.5 app/models/alchemy/page/url_path.rb
alchemy_cms-7.0.10 app/models/alchemy/page/url_path.rb
alchemy_cms-7.1.4 app/models/alchemy/page/url_path.rb
alchemy_cms-7.0.9 app/models/alchemy/page/url_path.rb
alchemy_cms-7.1.3 app/models/alchemy/page/url_path.rb
alchemy_cms-7.1.2 app/models/alchemy/page/url_path.rb
alchemy_cms-7.1.1 app/models/alchemy/page/url_path.rb
alchemy_cms-7.1.0 app/models/alchemy/page/url_path.rb
alchemy_cms-7.1.0.pre.rc1 app/models/alchemy/page/url_path.rb
alchemy_cms-7.1.0.pre.b2 app/models/alchemy/page/url_path.rb
alchemy_cms-7.1.0.pre.b1 app/models/alchemy/page/url_path.rb
alchemy_cms-7.0.8 app/models/alchemy/page/url_path.rb
alchemy_cms-7.0.7 app/models/alchemy/page/url_path.rb
alchemy_cms-7.0.6 app/models/alchemy/page/url_path.rb
alchemy_cms-7.0.5 app/models/alchemy/page/url_path.rb