Sha256: d2817713d05c4834dc30cba243e80c3773e4b2d333549cb17c932cfca60c6c06

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

module PageMeta
  class Base
    attr_reader :controller, :store

    delegate :[], :[]=, to: :store

    def initialize(controller)
      @controller = controller
      @store = {}
    end

    def meta_tags
      @meta_tags ||= {}
    end

    def links
      @links ||= {}
    end

    def tag(name, value)
      meta_tags[name] = value
    end

    def link(name, value)
      links[name] = value
    end

    def title
      @title ||= Translator.new(:titles, naming, store)
    end

    def description
      @description ||= Translator.new(:descriptions, naming, store)
    end

    def keywords
      @keywords ||= Translator.new(:keywords, naming, store)
    end

    def render
      compute_default_meta_tags
      render_meta_tags + render_links
    end
    alias_method :to_s, :render

    def naming
      @naming ||= Naming.new(controller)
    end

    def render_meta_tags
      meta_tags
        .map {|name, value| MetaTag.build(name, value).render }
        .join("")
        .html_safe
    end

    def render_links
      links
        .map {|rel, options| Link.build(rel, options).render }
        .join("")
        .html_safe
    end

    def compute_default_meta_tags
      tag(:language, I18n.locale)
      tag(:charset, Rails.configuration.encoding)
      tag(:title, title) unless title.to_s.empty?
      tag(:keywords, keywords.to_s) unless keywords.to_s.empty?
      tag(:description, description.to_s) unless description.to_s.empty?
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
page_meta-0.1.1 lib/page_meta/base.rb