Sha256: 8af47013f198edcda8b95470efbf70f05985ab441a6c3c9f4808806d5a90c7ec

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

require "forwardable"
require "dato/local/field_type/seo"

module Dato
  module Utils
    module MetaTags
      class Base
        attr_reader :site, :item

        def initialize(item, site)
          @item = item
          @site = site
        end

        def seo_field_with_fallback(attribute, alternative)
          fallback_seo = site.global_seo && site.global_seo.fallback_seo

          seo_field = item &&
                      item.fields.detect { |f| f.field_type == "seo" }

          item_seo_value = seo_field &&
                           item[seo_field.api_key] &&
                           item[seo_field.api_key].send(attribute)

          fallback_seo_value = fallback_seo &&
                               fallback_seo.send(attribute)

          item_seo_value.presence || alternative.presence || fallback_seo_value
        end

        def tag(tag_name, attributes)
          { tag_name: tag_name, attributes: attributes }
        end

        def meta_tag(name, content)
          tag("meta", name: name, content: content)
        end

        def og_tag(property, content)
          tag("meta", property: property, content: content)
        end

        def card_tag(name, content)
          meta_tag(name, content)
        end

        def content_tag(tag_name, content)
          { tag_name: tag_name, content: content }
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dato-0.8.3 lib/dato/utils/meta_tags/base.rb
dato-0.8.2 lib/dato/utils/meta_tags/base.rb