Add support for twitter:label meta tags

This commit is contained in:
Robert Speicher
2015-12-24 17:11:08 -05:00
parent 99dc1fce5e
commit ab3d855c0e
7 changed files with 86 additions and 4 deletions
+24
View File
@@ -58,6 +58,30 @@ module PageLayoutHelper
end
end
# Define or get attributes to be used as Twitter card metadata
#
# map - Hash of label => data pairs. Keys become labels, values become data
#
# Raises ArgumentError if given more than two attributes
def page_card_attributes(map = {})
raise ArgumentError, 'cannot provide more than two attributes' if map.length > 2
@page_card_attributes ||= {}
@page_card_attributes = map.reject { |_,v| v.blank? } if map.present?
@page_card_attributes
end
def page_card_meta_tags
tags = ''
page_card_attributes.each_with_index do |pair, i|
tags << tag(:meta, property: "twitter:label#{i + 1}", content: pair[0])
tags << tag(:meta, property: "twitter:data#{i + 1}", content: pair[1])
end
tags.html_safe
end
def header_title(title = nil, title_url = nil)
if title
@header_title = title
+8
View File
@@ -161,6 +161,14 @@ module Issuable
self.class.to_s.underscore
end
# Returns a Hash of attributes to be used for Twitter card metadata
def card_attributes
{
'Author' => author.try(:name),
'Assignee' => assignee.try(:name)
}
end
def notes_with_associations
notes.includes(:author, :project)
end
+1
View File
@@ -12,6 +12,7 @@
%meta{property: 'og:description', content: page_description}
%meta{property: 'og:image', content: page_image}
%meta{property: 'og:url', content: request.base_url + request.fullpath}
= page_card_meta_tags
%title= page_title
+3 -2
View File
@@ -1,5 +1,6 @@
- page_title "#{@issue.title} (##{@issue.iid})", "Issues"
- page_description @issue.description
- page_title "#{@issue.title} (##{@issue.iid})", "Issues"
- page_description @issue.description
- page_card_attributes @issue.card_attributes
= render "header_title"
@@ -1,5 +1,6 @@
- page_title "#{@merge_request.title} (##{@merge_request.iid})", "Merge Requests"
- page_description @merge_request.description
- page_title "#{@merge_request.title} (##{@merge_request.iid})", "Merge Requests"
- page_description @merge_request.description
- page_card_attributes @merge_request.card_attributes
= render "header_title"
+29
View File
@@ -97,4 +97,33 @@ describe PageLayoutHelper do
end
end
end
describe 'page_card_attributes' do
it 'raises ArgumentError when given more than two attributes' do
map = { foo: 'foo', bar: 'bar', baz: 'baz' }
expect { helper.page_card_attributes(map) }.
to raise_error(ArgumentError, /more than two attributes/)
end
it 'rejects blank values' do
map = { foo: 'foo', bar: '' }
helper.page_card_attributes(map)
expect(helper.page_card_attributes).to eq({ foo: 'foo' })
end
end
describe 'page_card_meta_tags' do
it 'returns the twitter:label and twitter:data tags' do
allow(helper).to receive(:page_card_attributes).and_return(foo: 'bar')
tags = helper.page_card_meta_tags
aggregate_failures do
expect(tags).to include %q(<meta property="twitter:label1" content="foo" />)
expect(tags).to include %q(<meta property="twitter:data1" content="bar" />)
end
end
end
end
+18
View File
@@ -81,4 +81,22 @@ describe Issue, "Issuable" do
expect(hook_data[:object_attributes]).to eq(issue.hook_attrs)
end
end
describe '#card_attributes' do
it 'includes the author name' do
allow(issue).to receive(:author).and_return(double(name: 'Robert'))
allow(issue).to receive(:assignee).and_return(nil)
expect(issue.card_attributes).
to eq({'Author' => 'Robert', 'Assignee' => nil})
end
it 'includes the assignee name' do
allow(issue).to receive(:author).and_return(double(name: 'Robert'))
allow(issue).to receive(:assignee).and_return(double(name: 'Douwe'))
expect(issue.card_attributes).
to eq({'Author' => 'Robert', 'Assignee' => 'Douwe'})
end
end
end