mirror of
https://github.com/redmine/redmine.git
synced 2025-11-02 11:25:55 +01:00
Small refactorization of avatar methods (#31391).
* move existing methods from ApplicationHelper to a new helper file (AvatarsHelper) * change default avatar size from 50 to 24 because most of the avatars are using the size 24 * class 'gravatar' is always added and all custom classes are appended * added user name as default title for avatar images * added two new methods: @assignee_avatar@ and @author_avatar@ Patch by Marius BALTEANU. git-svn-id: http://svn.redmine.org/redmine/trunk@18175 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
@@ -27,7 +27,10 @@ class ApplicationController < ActionController::Base
|
||||
include Redmine::Pagination
|
||||
include Redmine::Hook::Helper
|
||||
include RoutesHelper
|
||||
include AvatarsHelper
|
||||
|
||||
helper :routes
|
||||
helper :avatars
|
||||
|
||||
class_attribute :accept_api_auth_actions
|
||||
class_attribute :accept_rss_auth_actions
|
||||
|
@@ -23,7 +23,6 @@ require 'cgi'
|
||||
module ApplicationHelper
|
||||
include Redmine::WikiFormatting::Macros::Definitions
|
||||
include Redmine::I18n
|
||||
include GravatarHelper::PublicMethods
|
||||
include Redmine::Pagination::Helper
|
||||
include Redmine::SudoMode::Helper
|
||||
include Redmine::Themes::Helper
|
||||
@@ -1484,39 +1483,6 @@ module ApplicationHelper
|
||||
!!ActionMailer::Base.perform_deliveries
|
||||
end
|
||||
|
||||
# Returns the avatar image tag for the given +user+ if avatars are enabled
|
||||
# +user+ can be a User or a string that will be scanned for an email address (eg. 'joe <joe@foo.bar>')
|
||||
def avatar(user, options = { })
|
||||
if Setting.gravatar_enabled?
|
||||
options.merge!(:default => Setting.gravatar_default)
|
||||
email = nil
|
||||
if user.respond_to?(:mail)
|
||||
email = user.mail
|
||||
elsif user.to_s =~ %r{<(.+?)>}
|
||||
email = $1
|
||||
end
|
||||
if email.present?
|
||||
gravatar(email.to_s.downcase, options) rescue nil
|
||||
elsif user.is_a?(AnonymousUser)
|
||||
image_tag 'anonymous.png',
|
||||
GravatarHelper::DEFAULT_OPTIONS
|
||||
.except(:default, :rating, :ssl).merge(options)
|
||||
else
|
||||
nil
|
||||
end
|
||||
else
|
||||
''
|
||||
end
|
||||
end
|
||||
|
||||
# Returns a link to edit user's avatar if avatars are enabled
|
||||
def avatar_edit_link(user, options={})
|
||||
if Setting.gravatar_enabled?
|
||||
url = Redmine::Configuration['avatar_server_url']
|
||||
link_to avatar(user, {:title => l(:button_edit)}.merge(options)), url, :target => '_blank'
|
||||
end
|
||||
end
|
||||
|
||||
def sanitize_anchor_name(anchor)
|
||||
anchor.gsub(%r{[^\s\-\p{Word}]}, '').gsub(%r{\s+(\-+\s*)?}, '-')
|
||||
end
|
||||
|
75
app/helpers/avatars_helper.rb
Normal file
75
app/helpers/avatars_helper.rb
Normal file
@@ -0,0 +1,75 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Redmine - project management software
|
||||
# Copyright (C) 2006-2019 Jean-Philippe Lang
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU General Public License
|
||||
# as published by the Free Software Foundation; either version 2
|
||||
# of the License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
module AvatarsHelper
|
||||
include GravatarHelper::PublicMethods
|
||||
|
||||
def assignee_avatar(user, options={})
|
||||
return '' unless user
|
||||
|
||||
options.merge!(:title => l(:field_assigned_to) + ": " + user.name)
|
||||
avatar(user, options).html_safe
|
||||
end
|
||||
|
||||
def author_avatar(user, options={})
|
||||
return '' unless user
|
||||
|
||||
options.merge!(:title => l(:field_author) + ": " + user.name)
|
||||
avatar(user, options).html_safe
|
||||
end
|
||||
|
||||
# Returns the avatar image tag for the given +user+ if avatars are enabled
|
||||
# +user+ can be a User or a string that will be scanned for an email address (eg. 'joe <joe@foo.bar>')
|
||||
def avatar(user, options = { })
|
||||
if Setting.gravatar_enabled?
|
||||
options.merge!(:default => Setting.gravatar_default)
|
||||
options[:class] = GravatarHelper::DEFAULT_OPTIONS[:class] + " " + options[:class] if options[:class]
|
||||
email = nil
|
||||
if user.respond_to?(:mail)
|
||||
email = user.mail
|
||||
options[:title] = user.name unless options[:title]
|
||||
elsif user.to_s =~ %r{<(.+?)>}
|
||||
email = $1
|
||||
end
|
||||
if email.present?
|
||||
gravatar(email.to_s.downcase, options) rescue nil
|
||||
elsif user.is_a?(AnonymousUser)
|
||||
anonymous_avatar(options)
|
||||
else
|
||||
nil
|
||||
end
|
||||
else
|
||||
''
|
||||
end
|
||||
end
|
||||
|
||||
# Returns a link to edit user's avatar if avatars are enabled
|
||||
def avatar_edit_link(user, options={})
|
||||
if Setting.gravatar_enabled?
|
||||
url = Redmine::Configuration['avatar_server_url']
|
||||
link_to avatar(user, {:title => l(:button_edit)}.merge(options)), url, :target => '_blank'
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def anonymous_avatar(options={})
|
||||
image_tag 'anonymous.png', GravatarHelper::DEFAULT_OPTIONS.except(:default, :rating, :ssl).merge(options)
|
||||
end
|
||||
end
|
@@ -4,7 +4,7 @@
|
||||
<dl>
|
||||
<% sort_activity_events(events_by_day[day]).each do |e, in_group| -%>
|
||||
<dt class="<%= e.event_type %> icon icon-<%= e.event_type %> <%= "grouped" if in_group %> <%= User.current.logged? && e.respond_to?(:event_author) && User.current == e.event_author ? 'me' : nil %>">
|
||||
<%= avatar(e.event_author, :size => "24") if e.respond_to?(:event_author) %>
|
||||
<%= avatar(e.event_author) if e.respond_to?(:event_author) %>
|
||||
<span class="time"><%= format_time(e.event_datetime, false) %></span>
|
||||
<%= content_tag('span', e.project, :class => 'project') if @project.nil? || @project != e.project %>
|
||||
<%= link_to format_activity_title(e.event_title), e.event_url %>
|
||||
|
@@ -7,7 +7,7 @@
|
||||
<a href="#note-<%= journal.indice %>" class="journal-link">#<%= journal.indice %></a>
|
||||
</div>
|
||||
<h4>
|
||||
<%= avatar(journal.user, :size => "24") %>
|
||||
<%= avatar(journal.user) %>
|
||||
<%= authoring journal.created_on, journal.user, :label => :label_updated_time_by %>
|
||||
<%= render_private_notes_indicator(journal) %>
|
||||
</h4>
|
||||
|
@@ -26,8 +26,8 @@
|
||||
<% end %>
|
||||
|
||||
<div class="gravatar-with-child">
|
||||
<%= avatar(@issue.author, :size => "50", :title => l(:field_author)) %>
|
||||
<%= avatar(@issue.assigned_to, :size => "22", :class => "gravatar gravatar-child", :title => l(:field_assigned_to)) if @issue.assigned_to %>
|
||||
<%= author_avatar(@issue.author, :size => "50") %>
|
||||
<%= assignee_avatar(@issue.assigned_to, :size => "22", :class => "gravatar-child") if @issue.assigned_to %>
|
||||
</div>
|
||||
|
||||
<div class="subject">
|
||||
|
@@ -1,6 +1,6 @@
|
||||
<%= board_breadcrumb(@message) %>
|
||||
|
||||
<h2><%= avatar(@topic.author, :size => "24") %><%= @topic.subject %></h2>
|
||||
<h2><%= avatar(@topic.author) %><%= @topic.subject %></h2>
|
||||
|
||||
<%= form_for @message, {
|
||||
:as => :message,
|
||||
|
@@ -22,7 +22,7 @@
|
||||
) if @message.destroyable_by?(User.current) %>
|
||||
</div>
|
||||
|
||||
<h2><%= avatar(@topic.author, :size => "24") %><%= @topic.subject %></h2>
|
||||
<h2><%= avatar(@topic.author) %><%= @topic.subject %></h2>
|
||||
|
||||
<div class="message">
|
||||
<p><span class="author"><%= authoring @topic.created_on, @topic.author %></span></p>
|
||||
@@ -66,7 +66,7 @@
|
||||
) if message.destroyable_by?(User.current) %>
|
||||
</div>
|
||||
<h4>
|
||||
<%= avatar(message.author, :size => "24") %>
|
||||
<%= avatar(message.author) %>
|
||||
<%= link_to message.subject, { :controller => 'messages', :action => 'show', :board_id => @board, :id => @topic, :r => message, :anchor => "message-#{message.id}" } %>
|
||||
-
|
||||
<%= authoring message.created_on, message.author %>
|
||||
|
@@ -24,7 +24,7 @@
|
||||
<% @newss.each do |news| %>
|
||||
<article class="news-article">
|
||||
<header>
|
||||
<h3><%= avatar(news.author, :size => "24") %><%= link_to_project(news.project) + ': ' unless news.project == @project %>
|
||||
<h3><%= avatar(news.author) %><%= link_to_project(news.project) + ': ' unless news.project == @project %>
|
||||
<%= link_to h(news.title), news_path(news) %>
|
||||
<%= "(#{l(:label_x_comments, :count => news.comments_count)})" if news.comments_count > 0 %></h3>
|
||||
<p class="author"><%= authoring news.created_on, news.author %></p>
|
||||
|
@@ -8,7 +8,7 @@
|
||||
<%= delete_link news_path(@news) if User.current.allowed_to?(:manage_news, @project) %>
|
||||
</div>
|
||||
|
||||
<h2><%= avatar(@news.author, :size => "24") %> <%=h @news.title %></h2>
|
||||
<h2><%= avatar(@news.author) %> <%=h @news.title %></h2>
|
||||
|
||||
<% if authorize_for('news', 'edit') %>
|
||||
<div id="edit-news" style="display:none;">
|
||||
@@ -42,7 +42,7 @@
|
||||
:title => l(:button_delete),
|
||||
:class => 'icon-only icon-del' %>
|
||||
</div>
|
||||
<h4><%= avatar(comment.author, :size => "24") %><%= authoring comment.created_on, comment.author %></h4>
|
||||
<h4><%= avatar(comment.author) %><%= authoring comment.created_on, comment.author %></h4>
|
||||
<div class="wiki">
|
||||
<%= textilizable(comment.comments) %>
|
||||
</div>
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
<div class="details">
|
||||
<h4>
|
||||
<%= avatar(@changeset.user, :size => "24") %>
|
||||
<%= avatar(@changeset.user) %>
|
||||
<%= authoring(@changeset.committed_on, @changeset.author) %>
|
||||
</h4>
|
||||
<% if @changeset.scmid.present? || @changeset.parents.present? || @changeset.children.present? %>
|
||||
|
@@ -18,7 +18,7 @@ module GravatarHelper
|
||||
:default => nil,
|
||||
|
||||
# The default size in pixels for the gravatar image (they're square).
|
||||
:size => 50,
|
||||
:size => 24,
|
||||
|
||||
# The maximum allowed MPAA rating for gravatars. This allows you to
|
||||
# exclude gravatars that may be out of character for your site.
|
||||
|
@@ -686,13 +686,7 @@ module Redmine
|
||||
css_classes << ' over-end-date' if progress_date > self.date_to
|
||||
end
|
||||
s = (+"").html_safe
|
||||
if issue.assigned_to.present?
|
||||
assigned_string = l(:field_assigned_to) + ": " + issue.assigned_to.name
|
||||
s << view.avatar(issue.assigned_to,
|
||||
:class => 'gravatar icon-gravatar',
|
||||
:size => 13,
|
||||
:title => assigned_string).to_s.html_safe
|
||||
end
|
||||
s << view.assignee_avatar(issue.assigned_to, :size => 13, :class => 'icon-gravatar')
|
||||
s << view.link_to_issue(issue).html_safe
|
||||
s << view.content_tag(:input, nil, :type => 'checkbox', :name => 'ids[]', :value => issue.id, :style => 'display:none;', :class => 'toggle-selection')
|
||||
view.content_tag(:span, s, :class => css_classes).html_safe
|
||||
|
@@ -451,7 +451,7 @@ div.square {
|
||||
|
||||
form {display: inline;}
|
||||
input, select {vertical-align: middle; margin-top: 1px; margin-bottom: 1px; height: 24px; padding: 0 7px;}
|
||||
input, select, textarea {border:1px solid #ccc; border-radius:3px; box-sizing: border-box;}
|
||||
input, select, textarea { color: #333; background-color: #fff; border:1px solid #ccc; border-radius:3px; box-sizing: border-box;}
|
||||
select {
|
||||
-webkit-appearance: none;
|
||||
-moz-appearance: none;
|
||||
|
@@ -1495,87 +1495,6 @@ RAW
|
||||
assert_select_in result, 'ul.pages-hierarchy li ul.pages-hierarchy a[href=?]', "##{child_page.title}"
|
||||
end
|
||||
|
||||
def test_avatar_with_user
|
||||
with_settings :gravatar_enabled => '1' do
|
||||
assert_include Digest::MD5.hexdigest('jsmith@somenet.foo'), avatar(User.find_by_mail('jsmith@somenet.foo'))
|
||||
end
|
||||
end
|
||||
|
||||
def test_avatar_with_email_string
|
||||
with_settings :gravatar_enabled => '1' do
|
||||
assert_include Digest::MD5.hexdigest('jsmith@somenet.foo'), avatar('jsmith <jsmith@somenet.foo>')
|
||||
end
|
||||
end
|
||||
|
||||
def test_avatar_with_anonymous_user
|
||||
with_settings :gravatar_enabled => '1' do
|
||||
assert_match %r{src="/images/anonymous.png(\?\d+)?"}, avatar(User.anonymous)
|
||||
end
|
||||
end
|
||||
|
||||
def test_avatar_with_group
|
||||
with_settings :gravatar_enabled => '1' do
|
||||
assert_nil avatar(Group.first)
|
||||
end
|
||||
end
|
||||
|
||||
def test_avatar_with_invalid_arg_should_return_nil
|
||||
with_settings :gravatar_enabled => '1' do
|
||||
assert_nil avatar('jsmith')
|
||||
assert_nil avatar(nil)
|
||||
end
|
||||
end
|
||||
|
||||
def test_avatar_default_size_should_be_50
|
||||
with_settings :gravatar_enabled => '1' do
|
||||
assert_include 'size=50', avatar('jsmith <jsmith@somenet.foo>')
|
||||
end
|
||||
end
|
||||
|
||||
def test_avatar_with_size_option
|
||||
with_settings :gravatar_enabled => '1' do
|
||||
assert_include 'size=24', avatar('jsmith <jsmith@somenet.foo>', :size => 24)
|
||||
assert_include 'width="24" height="24"', avatar(User.anonymous, :size => 24)
|
||||
end
|
||||
end
|
||||
|
||||
def test_avatar_with_html_option
|
||||
with_settings :gravatar_enabled => '1' do
|
||||
# Non-avatar options should be considered html options
|
||||
assert_include 'title="John Smith"', avatar('jsmith <jsmith@somenet.foo>', :title => 'John Smith')
|
||||
end
|
||||
end
|
||||
|
||||
def test_avatar_css_class
|
||||
with_settings :gravatar_enabled => '1' do
|
||||
# The default class of the img tag should be gravatar
|
||||
assert_include 'class="gravatar"', avatar('jsmith <jsmith@somenet.foo>')
|
||||
assert_not_include 'class="gravatar"', avatar('jsmith <jsmith@somenet.foo>', :class => 'picture')
|
||||
assert_include 'class="picture"', avatar('jsmith <jsmith@somenet.foo>', :class => 'picture')
|
||||
end
|
||||
end
|
||||
|
||||
def test_avatar_disabled
|
||||
with_settings :gravatar_enabled => '0' do
|
||||
assert_equal '', avatar(User.find_by_mail('jsmith@somenet.foo'))
|
||||
end
|
||||
end
|
||||
|
||||
def test_avatar_server_url
|
||||
to_test = {
|
||||
'https://www.gravatar.com' => %r|https://www.gravatar.com/avatar/\h{32}|,
|
||||
'https://seccdn.libravatar.org' => %r|https://seccdn.libravatar.org/avatar/\h{32}|,
|
||||
'http://localhost:8080' => %r|http://localhost:8080/avatar/\h{32}|,
|
||||
}
|
||||
with_settings :gravatar_enabled => '1' do
|
||||
to_test.each do |url, expected|
|
||||
Redmine::Configuration.with 'avatar_server_url' => url do
|
||||
assert_match expected, avatar('<jsmith@somenet.foo>')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_link_to_user
|
||||
user = User.find(2)
|
||||
result = link_to("John Smith", "/users/2", :class => "user active")
|
||||
|
93
test/helpers/avatars_helper_test.rb
Normal file
93
test/helpers/avatars_helper_test.rb
Normal file
@@ -0,0 +1,93 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Redmine - project management software
|
||||
# Copyright (C) 2006-2017 Jean-Philippe Lang
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU General Public License
|
||||
# as published by the Free Software Foundation; either version 2
|
||||
# of the License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
require File.expand_path('../../test_helper', __FILE__)
|
||||
|
||||
class AvatarsHelperTest < Redmine::HelperTest
|
||||
include ERB::Util
|
||||
include Rails.application.routes.url_helpers
|
||||
include AvatarsHelper
|
||||
|
||||
fixtures :users, :email_addresses
|
||||
|
||||
def setup
|
||||
Setting.gravatar_enabled = '1'
|
||||
end
|
||||
|
||||
def test_avatar_with_user
|
||||
assert_include Digest::MD5.hexdigest('jsmith@somenet.foo'), avatar(User.find_by_mail('jsmith@somenet.foo'))
|
||||
end
|
||||
|
||||
def test_avatar_with_email_string
|
||||
assert_include Digest::MD5.hexdigest('jsmith@somenet.foo'), avatar('jsmith <jsmith@somenet.foo>')
|
||||
end
|
||||
|
||||
def test_avatar_with_anonymous_user
|
||||
assert_match %r{src="/images/anonymous.png(\?\d+)?"}, avatar(User.anonymous)
|
||||
end
|
||||
|
||||
def test_avatar_with_group
|
||||
assert_nil avatar(Group.first)
|
||||
end
|
||||
|
||||
def test_avatar_with_invalid_arg_should_return_nil
|
||||
assert_nil avatar('jsmith')
|
||||
assert_nil avatar(nil)
|
||||
end
|
||||
|
||||
def test_avatar_default_size_should_be_24
|
||||
assert_include 'size=24', avatar('jsmith <jsmith@somenet.foo>')
|
||||
end
|
||||
|
||||
def test_avatar_with_size_option
|
||||
assert_include 'size=24', avatar('jsmith <jsmith@somenet.foo>', :size => 24)
|
||||
assert_include 'width="24" height="24"', avatar(User.anonymous, :size => 24)
|
||||
end
|
||||
|
||||
def test_avatar_with_html_option
|
||||
# Non-avatar options should be considered html options
|
||||
assert_include 'title="John Smith"', avatar('jsmith <jsmith@somenet.foo>', :title => 'John Smith')
|
||||
end
|
||||
|
||||
def test_avatar_css_class
|
||||
# The default class of the img tag should be gravatar
|
||||
assert_include 'class="gravatar"', avatar('jsmith <jsmith@somenet.foo>')
|
||||
assert_include 'class="gravatar picture"', avatar('jsmith <jsmith@somenet.foo>', :class => 'picture')
|
||||
end
|
||||
|
||||
def test_avatar_disabled
|
||||
with_settings :gravatar_enabled => '0' do
|
||||
assert_equal '', avatar(User.find_by_mail('jsmith@somenet.foo'))
|
||||
end
|
||||
end
|
||||
|
||||
def test_avatar_server_url
|
||||
to_test = {
|
||||
'https://www.gravatar.com' => %r|https://www.gravatar.com/avatar/\h{32}|,
|
||||
'https://seccdn.libravatar.org' => %r|https://seccdn.libravatar.org/avatar/\h{32}|,
|
||||
'http://localhost:8080' => %r|http://localhost:8080/avatar/\h{32}|,
|
||||
}
|
||||
|
||||
to_test.each do |url, expected|
|
||||
Redmine::Configuration.with 'avatar_server_url' => url do
|
||||
assert_match expected, avatar('<jsmith@somenet.foo>')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@@ -26,6 +26,8 @@ class Redmine::Helpers::GanttHelperTest < Redmine::HelperTest
|
||||
include ProjectsHelper
|
||||
include IssuesHelper
|
||||
include QueriesHelper
|
||||
include AvatarsHelper
|
||||
|
||||
include ERB::Util
|
||||
include Rails.application.routes.url_helpers
|
||||
|
||||
|
Reference in New Issue
Block a user