Sha256: 33ecba16a4703aa40feb288dcf0f25db5a023641ab61292fdf821a607e072a88

Contents?: true

Size: 1.56 KB

Versions: 12

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

module PagesCore
  class SitemapsController < ::ApplicationController
    include PagesCore::PagePathHelper
    static_cache :show

    def show
      @entries = formatted_entries
    end

    private

    def format_time(timestamp)
      if timestamp.is_a?(Date)
        timestamp.strftime("%Y-%m-%d")
      else
        timestamp.strftime("%Y-%m-%dT%H:%M:%S#{timestamp.formatted_offset}")
      end
    end

    def format_record(record)
      { loc: record_url(record),
        lastmod: format_time(record.updated_at) }
    end

    def formatted_entries
      records.map { |r| format_record(r) }
    end

    def locales
      if PagesCore.config.locales
        PagesCore.config.locales.keys
      else
        [I18n.default_locale]
      end
    end

    def localized?(record)
      record.is_a?(LocalizableModel::InstanceMethods)
    end

    def pages
      ([Page.root.try(:localize, I18n.default_locale)] +
       locales.flat_map do |locale|
         Page.published.localized(locale).includes(:parent)
       end).compact
    end

    def page_record_url(record)
      if record == root_page && record.locale == I18n.default_locale
        root_url
      else
        page_url(record.locale, record)
      end
    end

    def record_url(record)
      if record.is_a?(Page)
        page_record_url(record)
      elsif localized?(record)
        polymorphic_url(record, locale: record.locale)
      else
        polymorphic_url(record)
      end
    end

    def records
      pages
    end

    def root_page
      @root_page ||= Page.root
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
pages_core-3.15.4 app/controllers/pages_core/sitemaps_controller.rb
pages_core-3.15.3 app/controllers/pages_core/sitemaps_controller.rb
pages_core-3.15.2 app/controllers/pages_core/sitemaps_controller.rb
pages_core-3.15.1 app/controllers/pages_core/sitemaps_controller.rb
pages_core-3.14.0 app/controllers/pages_core/sitemaps_controller.rb
pages_core-3.13.0 app/controllers/pages_core/sitemaps_controller.rb
pages_core-3.12.7 app/controllers/pages_core/sitemaps_controller.rb
pages_core-3.12.6 app/controllers/pages_core/sitemaps_controller.rb
pages_core-3.12.5 app/controllers/pages_core/sitemaps_controller.rb
pages_core-3.12.4 app/controllers/pages_core/sitemaps_controller.rb
pages_core-3.12.3 app/controllers/pages_core/sitemaps_controller.rb
pages_core-3.12.2 app/controllers/pages_core/sitemaps_controller.rb