Sha256: 255ec57942d164580a9e0b8bb86df76eb99fec46d50e204f3ac0ac220ea5f865

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

require "forwardable"
require "dato/local/field_type/seo"
require "active_support/core_ext/object/blank"

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

1 entries across 1 versions & 1 rubygems

Version Path
dato-0.8.1 lib/dato/utils/meta_tags/base.rb