Sha256: ab8fb602dac5e1713c6e524290842d3324f2b46a800da1558f3a95452c554a6f

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

module Metamagic
  module HelperMethods
    def meta_tags
      @meta_tags ||= []
    end

    def meta(*options)
      # add page specific meta tags
      add_meta_tags options
    end

    def add_tag_if_not_existing(new_tag)
      # add meta tag if there's not an existing one with the same name or property attribute
      return if meta_tags.find { |tag| tag[:name] && new_tag[:name] && tag[:name] == new_tag[:name] }
      return if meta_tags.find { |tag| tag[:property] && new_tag[:property] && tag[:property] == new_tag[:property] }
      meta_tags << new_tag
    end

    def add_meta_tags(options)
      options.each do |option|
        if option.is_a?(Hash)
          option.each_pair do |key, value|
            if value.is_a?(Array)
              add_tag_if_not_existing :name => key, :content => value.join(", ")
            else
              add_tag_if_not_existing :name => key, :content => value
            end
          end
        elsif option.is_a?(Array)
          option.each do |tag|
            add_tag_if_not_existing tag
          end
        else
          raise TypeError, "Unknown tag type #{tag.class.name}. Use either Hash or Array."
        end
      end
    end

    def metamagic(*options)
      # apply default meta tags if they don't exist
      add_meta_tags options

      # loop through the added tags
      out = []
      meta_tags.each do |tag|
        if tag[:name] == :title
          out << content_tag(:title, tag[:content])
        else
          # add tag
          out << tag(:meta, tag)
        end
      end

      # return tags
      out.join("\n").html_safe
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
metamagic-2.0.6 lib/metamagic/helper_methods.rb