diff options
author | Robert Speicher <rspeicher@gmail.com> | 2015-12-24 17:03:54 -0500 |
---|---|---|
committer | Robert Speicher <rspeicher@gmail.com> | 2015-12-24 17:11:08 -0500 |
commit | ab3d855c0e1869fd1986c3bcdf7519f6b1cf1fa8 (patch) | |
tree | 7d0a1117b4cae0660004c00684dcbab1cde4069a | |
parent | 99dc1fce5ed84fb78bd993423db9c470021ea3a2 (diff) | |
download | gitlab-ce-rs-opengraph.tar.gz |
Add support for `twitter:label` meta tagsrs-opengraph
-rw-r--r-- | app/helpers/page_layout_helper.rb | 24 | ||||
-rw-r--r-- | app/models/concerns/issuable.rb | 8 | ||||
-rw-r--r-- | app/views/layouts/_head.html.haml | 1 | ||||
-rw-r--r-- | app/views/projects/issues/show.html.haml | 5 | ||||
-rw-r--r-- | app/views/projects/merge_requests/_show.html.haml | 5 | ||||
-rw-r--r-- | spec/helpers/page_layout_helper_spec.rb | 29 | ||||
-rw-r--r-- | spec/models/concerns/issuable_spec.rb | 18 |
7 files changed, 86 insertions, 4 deletions
diff --git a/app/helpers/page_layout_helper.rb b/app/helpers/page_layout_helper.rb index 4f1276f93ec..791cb9e50bd 100644 --- a/app/helpers/page_layout_helper.rb +++ b/app/helpers/page_layout_helper.rb @@ -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 diff --git a/app/models/concerns/issuable.rb b/app/models/concerns/issuable.rb index f56fd3e02d4..919833f6df5 100644 --- a/app/models/concerns/issuable.rb +++ b/app/models/concerns/issuable.rb @@ -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 diff --git a/app/views/layouts/_head.html.haml b/app/views/layouts/_head.html.haml index d1cb07eaa66..434605e59ae 100644 --- a/app/views/layouts/_head.html.haml +++ b/app/views/layouts/_head.html.haml @@ -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 diff --git a/app/views/projects/issues/show.html.haml b/app/views/projects/issues/show.html.haml index f2a261ab426..f548383008d 100644 --- a/app/views/projects/issues/show.html.haml +++ b/app/views/projects/issues/show.html.haml @@ -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" diff --git a/app/views/projects/merge_requests/_show.html.haml b/app/views/projects/merge_requests/_show.html.haml index 75f44557964..ba7c2c01e93 100644 --- a/app/views/projects/merge_requests/_show.html.haml +++ b/app/views/projects/merge_requests/_show.html.haml @@ -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" diff --git a/spec/helpers/page_layout_helper_spec.rb b/spec/helpers/page_layout_helper_spec.rb index 530e9bab343..fd7107779f6 100644 --- a/spec/helpers/page_layout_helper_spec.rb +++ b/spec/helpers/page_layout_helper_spec.rb @@ -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 diff --git a/spec/models/concerns/issuable_spec.rb b/spec/models/concerns/issuable_spec.rb index 0f13c4410cd..901eb936688 100644 --- a/spec/models/concerns/issuable_spec.rb +++ b/spec/models/concerns/issuable_spec.rb @@ -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 |