Created
June 8, 2010 18:51
-
-
Save coryschires/430470 to your computer and use it in GitHub Desktop.
seo helpers module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module SeoHelper | |
def meta_keywords(keywords) | |
tag(:meta, { :name => "keywords", :content => keywords }) | |
end | |
def meta_description(description) | |
tag(:meta, { :name => "description", :content => description }) | |
end | |
def meta_author(author_name) | |
tag(:meta, { :name => "author", :content => author_name }) | |
end | |
def meta_noindex | |
tag(:meta, { :name => "robots", :content => "noindex" }) | |
end | |
def comma_separated_keywords(array, keyword_attr = :name) | |
keyword_attr.to_sym if keyword_attr.is_a?(String) | |
array.map(&keyword_attr).join(', ') if array.is_a?(Array) | |
end | |
end | |
# something like this in the layout: | |
# -content_for :seo_tags do | |
# =meta_description "This the meta description for a specific page." | |
# =meta_keywords "keyword, keyword, keyword, keyword, keyword, keyword" | |
# and in the layout: | |
# =yield :seo_tags | |
# tests | |
describe SeoHelper do | |
include SeoHelper | |
it "should return a meta description tag" do | |
description = "This is a sample meta description." | |
meta_description(description).should == '<meta content="This is a sample meta description." name="description" />' | |
end | |
it "should return a meta keyword tag" do | |
keywords = "keyword one, keyword two, keyword three" | |
meta_keywords(keywords).should == '<meta content="keyword one, keyword two, keyword three" name="keywords" />' | |
end | |
it "should return a meta author tag" do | |
author = "John Smith" | |
meta_author(author).should == '<meta content="John Smith" name="author" />' | |
end | |
it "should return a meta noindex tag" do | |
meta_noindex.should == '<meta content="noindex" name="robots" />' | |
end | |
it "should recieve a collection of objects return a comma separated list of keywords" do | |
tags = [ | |
Factory.create(:tag, :name => "Lions"), | |
Factory.create(:tag, :name => "Tigers"), | |
Factory.create(:tag, :name => "Bears") | |
] | |
news_item = Factory.create(:news_item, :tags => tags) | |
comma_separated_keywords(news_item.tags).should === "Lions, Tigers, Bears" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment