Sha256: f50e2ee88319c6a65bd81a87c67d40c7efaeb5fd28fcd26b4fbfa6b18de4f8e3

Contents?: true

Size: 1.41 KB

Versions: 25

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

module Maglev
  # Editors might want to create a link to a non Maglev powered page of the site.
  class FetchStaticPages
    include Injectable

    dependency :config

    def call
      return [] if config.static_pages.blank?

      build_static_pages
    end

    protected

    def build_static_pages
      @build_static_pages ||= config.static_pages.map do |raw_attributes|
        build_static_page(raw_attributes.symbolize_keys)
      end
    end

    def build_static_page(attributes)
      Maglev::StaticPage.new(
        id: fetch_id(attributes),
        title_translations: fetch_attribute(attributes, :title),
        path_translations: fetch_path_attribute(attributes),
        seo_title_translations: {},
        meta_description_translations: {},
        og_title_translations: {},
        og_description_translations: {},
        og_image_url_translations: {}
      )
    end

    def fetch_id(attributes)
      Digest::MD5.hexdigest(fetch_path_attribute(attributes).to_json)
    end

    def fetch_path_attribute(attributes)
      fetch_attribute(attributes, :path)
    end

    def fetch_attribute(attributes, name)
      value = attributes[name]
      return {} if value.blank?

      value.is_a?(Hash) ? value.stringify_keys : { default_locale => value }
    end

    def default_locale
      (config.default_site_locales&.first&.prefix || Maglev::I18n.current_locale).to_s
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
maglevcms-1.8.0 app/services/maglev/fetch_static_pages.rb
maglevcms-1.7.3 app/services/maglev/fetch_static_pages.rb
maglevcms-1.7.2 app/services/maglev/fetch_static_pages.rb
maglevcms-1.7.1 app/services/maglev/fetch_static_pages.rb
maglevcms-1.7.0 app/services/maglev/fetch_static_pages.rb
maglevcms-1.6.1 app/services/maglev/fetch_static_pages.rb
maglevcms-1.6.0 app/services/maglev/fetch_static_pages.rb
maglevcms-1.5.1 app/services/maglev/fetch_static_pages.rb
maglevcms-1.4.0 app/services/maglev/fetch_static_pages.rb
maglevcms-1.3.0 app/services/maglev/fetch_static_pages.rb
maglevcms-1.2.2 app/services/maglev/fetch_static_pages.rb
maglevcms-1.2.1 app/services/maglev/fetch_static_pages.rb
maglevcms-1.2.0 app/services/maglev/fetch_static_pages.rb
maglevcms-1.1.7 app/services/maglev/fetch_static_pages.rb
maglevcms-1.1.6 app/services/maglev/fetch_static_pages.rb
maglevcms-1.1.5 app/services/maglev/fetch_static_pages.rb
maglevcms-1.1.4 app/services/maglev/fetch_static_pages.rb
maglevcms-1.1.3 app/services/maglev/fetch_static_pages.rb
maglevcms-1.1.2 app/services/maglev/fetch_static_pages.rb
maglevcms-1.1.1 app/services/maglev/fetch_static_pages.rb