Merge branch 'more-opengraph' into 'master'

Add Open Graph data for group, project and commit.

See merge request !2211
This commit is contained in:
Robert Speicher 2016-01-06 19:11:05 +00:00
commit d47b3e63d5
7 changed files with 42 additions and 78 deletions

View file

@ -27,35 +27,20 @@ module PageLayoutHelper
# #
# Returns an HTML-safe String. # Returns an HTML-safe String.
def page_description(description = nil) def page_description(description = nil)
@page_description ||= page_description_default
if description.present? if description.present?
@page_description = description.squish @page_description = description.squish
else elsif @page_description.present?
sanitize(@page_description, tags: []).truncate_words(30) sanitize(@page_description, tags: []).truncate_words(30)
end end
end end
# Default value for page_description when one hasn't been defined manually by
# a view
def page_description_default
if @project
@project.description || brand_title
else
brand_title
end
end
def page_image def page_image
default = image_url('gitlab_logo.png') default = image_url('gitlab_logo.png')
if @project subject = @project || @user || @group
@project.avatar_url || default
elsif @user image = subject.avatar_url if subject.present?
avatar_icon(@user) image || default
else
default
end
end end
# Define or get attributes to be used as Twitter card metadata # Define or get attributes to be used as Twitter card metadata

View file

@ -1,10 +1,13 @@
- page_description brand_title unless page_description
- site_name = "GitLab"
%head{prefix: "og: http://ogp.me/ns#"} %head{prefix: "og: http://ogp.me/ns#"}
%meta{charset: "utf-8"} %meta{charset: "utf-8"}
%meta{'http-equiv' => 'X-UA-Compatible', content: 'IE=edge'} %meta{'http-equiv' => 'X-UA-Compatible', content: 'IE=edge'}
-# Open Graph - http://ogp.me/ -# Open Graph - http://ogp.me/
%meta{property: 'og:type', content: "object"} %meta{property: 'og:type', content: "object"}
%meta{property: 'og:site_name', content: "GitLab"} %meta{property: 'og:site_name', content: site_name}
%meta{property: 'og:title', content: page_title} %meta{property: 'og:title', content: page_title}
%meta{property: 'og:description', content: page_description} %meta{property: 'og:description', content: page_description}
%meta{property: 'og:image', content: page_image} %meta{property: 'og:image', content: page_image}
@ -17,7 +20,7 @@
%meta{property: 'twitter:image', content: page_image} %meta{property: 'twitter:image', content: page_image}
= page_card_meta_tags = page_card_meta_tags
%title= page_title('GitLab') %title= page_title(site_name)
%meta{name: "description", content: page_description} %meta{name: "description", content: page_description}
= favicon_link_tag 'favicon.ico' = favicon_link_tag 'favicon.ico'

View file

@ -1,4 +1,5 @@
- page_title @group.name - page_title @group.name
- page_description @group.description unless page_description
- header_title group_title(@group) unless header_title - header_title group_title(@group) unless header_title
- sidebar "group" unless sidebar - sidebar "group" unless sidebar

View file

@ -1,4 +1,5 @@
- page_title @project.name_with_namespace - page_title @project.name_with_namespace
- page_description @project.description unless page_description
- header_title project_title(@project) unless header_title - header_title project_title(@project) unless header_title
- sidebar "project" unless sidebar - sidebar "project" unless sidebar

View file

@ -1,4 +1,6 @@
- page_title "#{@commit.title} (#{@commit.short_id})", "Commits" - page_title "#{@commit.title} (#{@commit.short_id})", "Commits"
- page_description @commit.description
= render "projects/commits/header_title" = render "projects/commits/header_title"
= render "commit_box" = render "commit_box"
- if @ci_commit - if @ci_commit

View file

@ -2,10 +2,8 @@ require 'rails_helper'
describe PageLayoutHelper do describe PageLayoutHelper do
describe 'page_description' do describe 'page_description' do
it 'defaults to value returned by page_description_default helper' do it 'defaults to nil' do
allow(helper).to receive(:page_description_default).and_return('Foo') expect(helper.page_description).to eq nil
expect(helper.page_description).to eq 'Foo'
end end
it 'returns the last-pushed description' do it 'returns the last-pushed description' do
@ -42,58 +40,32 @@ describe PageLayoutHelper do
end end
end end
describe 'page_description_default' do
it 'uses Project description when available' do
project = double(description: 'Project Description')
helper.instance_variable_set(:@project, project)
expect(helper.page_description_default).to eq 'Project Description'
end
it 'uses brand_title when Project description is nil' do
project = double(description: nil)
helper.instance_variable_set(:@project, project)
expect(helper).to receive(:brand_title).and_return('Brand Title')
expect(helper.page_description_default).to eq 'Brand Title'
end
it 'falls back to brand_title' do
allow(helper).to receive(:brand_title).and_return('Brand Title')
expect(helper.page_description_default).to eq 'Brand Title'
end
end
describe 'page_image' do describe 'page_image' do
it 'defaults to the GitLab logo' do it 'defaults to the GitLab logo' do
expect(helper.page_image).to end_with 'assets/gitlab_logo.png' expect(helper.page_image).to end_with 'assets/gitlab_logo.png'
end end
context 'with @project' do %w(project user group).each do |type|
it 'uses Project avatar if available' do context "with @#{type} assigned" do
project = double(avatar_url: 'http://example.com/uploads/avatar.png') it "uses #{type.titlecase} avatar if available" do
helper.instance_variable_set(:@project, project) object = double(avatar_url: 'http://example.com/uploads/avatar.png')
assign(type, object)
expect(helper.page_image).to eq project.avatar_url expect(helper.page_image).to eq object.avatar_url
end end
it 'falls back to the default' do it 'falls back to the default when avatar_url is nil' do
project = double(avatar_url: nil) object = double(avatar_url: nil)
helper.instance_variable_set(:@project, project) assign(type, object)
expect(helper.page_image).to end_with 'assets/gitlab_logo.png' expect(helper.page_image).to end_with 'assets/gitlab_logo.png'
end end
end end
context 'with @user' do context "with no assignments" do
it 'delegates to avatar_icon helper' do it 'falls back to the default' do
user = double('User') expect(helper.page_image).to end_with 'assets/gitlab_logo.png'
helper.instance_variable_set(:@user, user) end
expect(helper).to receive(:avatar_icon).with(user)
helper.page_image
end end
end end
end end