class ActiveSeo::SeoMeta

Attributes

config[RW]
context[RW]
locale[RW]
record[RW]

Public Class Methods

new(record, locale=I18n.locale) click to toggle source
# File lib/active_seo/seo_meta.rb, line 5
def initialize(record, locale=I18n.locale)
  @record  = record
  @locale  = locale
  @config  = record.class.seo_config
  @context = contextualizer.new(record)
end

Public Instance Methods

description() click to toggle source
# File lib/active_seo/seo_meta.rb, line 24
def description
  attribute = localized_attribute(:seo_description)
  defaults  = localized_attributes(:content, :description, :excerpt, :body)

  attribute_fallbacks_for(attribute, defaults, config.description_fallback)
end
keywords() click to toggle source
# File lib/active_seo/seo_meta.rb, line 31
def keywords
  text = record.try localized_attribute(:seo_keywords)
  text = "#{title} #{description}" if generate_keywords?(text)

  helpers.generate_keywords(text)
end
nofollow() click to toggle source
# File lib/active_seo/seo_meta.rb, line 42
def nofollow
  record.seo_nofollow || false
end
noindex() click to toggle source
# File lib/active_seo/seo_meta.rb, line 38
def noindex
  record.seo_noindex || false
end
og() click to toggle source
# File lib/active_seo/seo_meta.rb, line 46
def og
  @config.opengraph.merge(context.og_meta)
end
result() click to toggle source
# File lib/active_seo/seo_meta.rb, line 12
def result
  data = [:title, :description, :keywords, :noindex, :nofollow, :og, :twitter]
  Hash[data.map { |i| [i, send(i)] }]
end
title() click to toggle source
# File lib/active_seo/seo_meta.rb, line 17
def title
  attribute = localized_attribute(:seo_title)
  defaults  = localized_attributes(:title, :name)

  attribute_fallbacks_for(attribute, defaults, config.title_fallback)
end
twitter() click to toggle source
# File lib/active_seo/seo_meta.rb, line 50
def twitter
  @config.twitter.merge(context.twitter_meta)
end

Private Instance Methods

attribute_fallbacks(candidates) click to toggle source
# File lib/active_seo/seo_meta.rb, line 103
def attribute_fallbacks(candidates)
  method = candidates.find { |item| record.try(item).present? }
  helpers.strip_tags(record.try(method).to_s) unless method.nil?
end
attribute_fallbacks_for(attribute, defaults, fallback) click to toggle source
# File lib/active_seo/seo_meta.rb, line 83
def attribute_fallbacks_for(attribute, defaults, fallback)
  candidates = [attribute]

  case fallback
  when TrueClass
    candidates.concat(defaults)
  when Symbol
    candidates << fallback
  when Array
    candidates.concat(fallback)
  end

  name = attribute.to_s.remove(/^seo_/, /_#{locale}$/)
  size = config[:"#{name}_limit"]
  text = attribute_fallbacks(candidates)
  text = text.truncate(size, separator: ' ') if text && size

  text
end
contextualizer() click to toggle source
# File lib/active_seo/seo_meta.rb, line 56
def contextualizer
  "#{record.class.seo_context}".safe_constantize ||
  "#{record.class.name}Contextualizer".safe_constantize ||
  'ActiveSeo::Contextualizer'.constantize
end
generate_keywords?(text=nil) click to toggle source
# File lib/active_seo/seo_meta.rb, line 70
def generate_keywords?(text=nil)
  config.generate_keywords and text.blank?
end
helpers() click to toggle source
# File lib/active_seo/seo_meta.rb, line 62
def helpers
  ActiveSeo::Helpers
end
localized?() click to toggle source
# File lib/active_seo/seo_meta.rb, line 66
def localized?
  record.class.seo_locale_accessors?
end
localized_attribute(attr_name) click to toggle source
# File lib/active_seo/seo_meta.rb, line 74
def localized_attribute(attr_name)
  localized_name = "#{attr_name}_#{locale}"
  localized? && record.respond_to?(localized_name) ? localized_name : attr_name
end
localized_attributes(*attr_names) click to toggle source
# File lib/active_seo/seo_meta.rb, line 79
def localized_attributes(*attr_names)
  localized? ? attr_names.map { |a| localized_attribute(a) } : attr_names
end