Project

General

Profile

Patch #31391 » 0001-Small-refactorization-of-avatar-methods.patch

Marius BĂLTEANU, 2019-05-18 13:28

View differences:

app/controllers/application_controller.rb
27 27
  include Redmine::Pagination
28 28
  include Redmine::Hook::Helper
29 29
  include RoutesHelper
30
  include AvatarsHelper
31

  
30 32
  helper :routes
33
  helper :avatars
31 34

  
32 35
  class_attribute :accept_api_auth_actions
33 36
  class_attribute :accept_rss_auth_actions
app/helpers/application_helper.rb
23 23
module ApplicationHelper
24 24
  include Redmine::WikiFormatting::Macros::Definitions
25 25
  include Redmine::I18n
26
  include GravatarHelper::PublicMethods
27 26
  include Redmine::Pagination::Helper
28 27
  include Redmine::SudoMode::Helper
29 28
  include Redmine::Themes::Helper
......
1484 1483
    !!ActionMailer::Base.perform_deliveries
1485 1484
  end
1486 1485

  
1487
  # Returns the avatar image tag for the given +user+ if avatars are enabled
1488
  # +user+ can be a User or a string that will be scanned for an email address (eg. 'joe <joe@foo.bar>')
1489
  def avatar(user, options = { })
1490
    if Setting.gravatar_enabled?
1491
      options.merge!(:default => Setting.gravatar_default)
1492
      email = nil
1493
      if user.respond_to?(:mail)
1494
        email = user.mail
1495
      elsif user.to_s =~ %r{<(.+?)>}
1496
        email = $1
1497
      end
1498
      if email.present?
1499
        gravatar(email.to_s.downcase, options) rescue nil
1500
      elsif user.is_a?(AnonymousUser)
1501
        image_tag 'anonymous.png',
1502
                  GravatarHelper::DEFAULT_OPTIONS
1503
                    .except(:default, :rating, :ssl).merge(options)
1504
      else
1505
        nil
1506
      end
1507
    else
1508
      ''
1509
    end
1510
  end
1511

  
1512
  # Returns a link to edit user's avatar if avatars are enabled
1513
  def avatar_edit_link(user, options={})
1514
    if Setting.gravatar_enabled?
1515
      url = Redmine::Configuration['avatar_server_url']
1516
      link_to avatar(user, {:title => l(:button_edit)}.merge(options)), url, :target => '_blank'
1517
    end
1518
  end
1519

  
1520 1486
  def sanitize_anchor_name(anchor)
1521 1487
    anchor.gsub(%r{[^\s\-\p{Word}]}, '').gsub(%r{\s+(\-+\s*)?}, '-')
1522 1488
  end
app/helpers/avatars_helper.rb
1
# frozen_string_literal: true
2

  
3
# Redmine - project management software
4
# Copyright (C) 2006-2019  Jean-Philippe Lang
5
#
6
# This program is free software; you can redistribute it and/or
7
# modify it under the terms of the GNU General Public License
8
# as published by the Free Software Foundation; either version 2
9
# of the License, or (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19

  
20
module AvatarsHelper
21
  include GravatarHelper::PublicMethods
22

  
23
  def assignee_avatar(user, options={})
24
    return '' unless user
25

  
26
    options.merge!(:title => l(:field_assigned_to) + ": " + user.name)
27
    avatar(user, options).html_safe
28
  end
29

  
30
  def author_avatar(user, options={})
31
    return '' unless user
32

  
33
    options.merge!(:title => l(:field_author) + ": " + user.name)
34
    avatar(user, options).html_safe
35
  end
36

  
37
  # Returns the avatar image tag for the given +user+ if avatars are enabled
38
  # +user+ can be a User or a string that will be scanned for an email address (eg. 'joe <joe@foo.bar>')
39
  def avatar(user, options = { })
40
    if Setting.gravatar_enabled?
41
      options.merge!(:default => Setting.gravatar_default)
42
      options[:class] = GravatarHelper::DEFAULT_OPTIONS[:class] + " " + options[:class] if options[:class]
43
      email = nil
44
      if user.respond_to?(:mail)
45
        email = user.mail
46
        options[:title] = user.name unless options[:title]
47
      elsif user.to_s =~ %r{<(.+?)>}
48
        email = $1
49
      end
50
      if email.present?
51
        gravatar(email.to_s.downcase, options) rescue nil
52
      elsif user.is_a?(AnonymousUser)
53
        anonymous_avatar(options)
54
      else
55
        nil
56
      end
57
    else
58
      ''
59
    end
60
  end
61

  
62
  # Returns a link to edit user's avatar if avatars are enabled
63
  def avatar_edit_link(user, options={})
64
    if Setting.gravatar_enabled?
65
      url = Redmine::Configuration['avatar_server_url']
66
      link_to avatar(user, {:title => l(:button_edit)}.merge(options)), url, :target => '_blank'
67
    end
68
  end
69

  
70
  private
71

  
72
  def anonymous_avatar(options={})
73
    image_tag 'anonymous.png', GravatarHelper::DEFAULT_OPTIONS.except(:default, :rating, :ssl).merge(options)
74
  end
75
end
app/views/activities/_activities.html.erb
4 4
<dl>
5 5
<% sort_activity_events(events_by_day[day]).each do |e, in_group| -%>
6 6
  <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 %>">
7
  <%= avatar(e.event_author, :size => "24") if e.respond_to?(:event_author) %>
7
  <%= avatar(e.event_author) if e.respond_to?(:event_author) %>
8 8
  <span class="time"><%= format_time(e.event_datetime, false) %></span>
9 9
  <%= content_tag('span', e.project, :class => 'project') if @project.nil? || @project != e.project %>
10 10
  <%= link_to format_activity_title(e.event_title), e.event_url %>
app/views/issues/_history.html.erb
7 7
      <a href="#note-<%= journal.indice %>" class="journal-link">#<%= journal.indice %></a>
8 8
    </div>
9 9
    <h4>
10
      <%= avatar(journal.user, :size => "24") %>
10
      <%= avatar(journal.user) %>
11 11
      <%= authoring journal.created_on, journal.user, :label => :label_updated_time_by %>
12 12
      <%= render_private_notes_indicator(journal) %>
13 13
    </h4>
app/views/issues/show.html.erb
26 26
  <% end %>
27 27

  
28 28
  <div class="gravatar-with-child">
29
    <%= avatar(@issue.author, :size => "50", :title => l(:field_author)) %>
30
    <%= avatar(@issue.assigned_to, :size => "22", :class => "gravatar gravatar-child", :title => l(:field_assigned_to)) if @issue.assigned_to %>
29
    <%= author_avatar(@issue.author, :size => "50") %>
30
    <%= assignee_avatar(@issue.assigned_to, :size => "22", :class => "gravatar-child") if @issue.assigned_to %>
31 31
  </div>
32 32

  
33 33
<div class="subject">
app/views/messages/edit.html.erb
1 1
<%= board_breadcrumb(@message) %>
2 2

  
3
<h2><%= avatar(@topic.author, :size => "24") %><%= @topic.subject %></h2>
3
<h2><%= avatar(@topic.author) %><%= @topic.subject %></h2>
4 4

  
5 5
<%= form_for @message, {
6 6
               :as => :message,
app/views/messages/show.html.erb
22 22
         ) if @message.destroyable_by?(User.current) %>
23 23
</div>
24 24

  
25
<h2><%= avatar(@topic.author, :size => "24") %><%= @topic.subject %></h2>
25
<h2><%= avatar(@topic.author) %><%= @topic.subject %></h2>
26 26

  
27 27
<div class="message">
28 28
<p><span class="author"><%= authoring @topic.created_on, @topic.author %></span></p>
......
66 66
          ) if message.destroyable_by?(User.current) %>
67 67
    </div>
68 68
  <h4>
69
    <%= avatar(message.author, :size => "24") %>
69
    <%= avatar(message.author) %>
70 70
    <%= link_to message.subject, { :controller => 'messages', :action => 'show', :board_id => @board, :id => @topic, :r => message, :anchor => "message-#{message.id}" } %>
71 71
    -
72 72
    <%= authoring message.created_on, message.author %>
app/views/news/index.html.erb
24 24
<% @newss.each do |news| %>
25 25
    <article class="news-article">
26 26
      <header>
27
        <h3><%= avatar(news.author, :size => "24") %><%= link_to_project(news.project) + ': ' unless news.project == @project %>
27
        <h3><%= avatar(news.author) %><%= link_to_project(news.project) + ': ' unless news.project == @project %>
28 28
        <%= link_to h(news.title), news_path(news) %>
29 29
        <%= "(#{l(:label_x_comments, :count => news.comments_count)})" if news.comments_count > 0 %></h3>
30 30
        <p class="author"><%= authoring news.created_on, news.author %></p>
app/views/news/show.html.erb
8 8
<%= delete_link news_path(@news) if User.current.allowed_to?(:manage_news, @project) %>
9 9
</div>
10 10

  
11
<h2><%= avatar(@news.author, :size => "24") %> <%=h @news.title %></h2>
11
<h2><%= avatar(@news.author) %> <%=h @news.title %></h2>
12 12

  
13 13
<% if authorize_for('news', 'edit') %>
14 14
<div id="edit-news" style="display:none;">
......
42 42
                              :title => l(:button_delete),
43 43
                              :class => 'icon-only icon-del' %>
44 44
    </div>
45
    <h4><%= avatar(comment.author, :size => "24") %><%= authoring comment.created_on, comment.author %></h4>
45
    <h4><%= avatar(comment.author) %><%= authoring comment.created_on, comment.author %></h4>
46 46
    <div class="wiki">
47 47
    <%= textilizable(comment.comments) %>
48 48
    </div>
app/views/repositories/_changeset.html.erb
2 2

  
3 3
<div class="details">
4 4
  <h4>
5
    <%= avatar(@changeset.user, :size => "24") %>
5
    <%= avatar(@changeset.user) %>
6 6
    <%= authoring(@changeset.committed_on, @changeset.author) %>
7 7
  </h4>
8 8
  <% if @changeset.scmid.present? || @changeset.parents.present? || @changeset.children.present? %>
lib/plugins/gravatar/lib/gravatar.rb
18 18
    :default => nil,
19 19

  
20 20
    # The default size in pixels for the gravatar image (they're square).
21
    :size => 50,
21
    :size => 24,
22 22

  
23 23
    # The maximum allowed MPAA rating for gravatars. This allows you to
24 24
    # exclude gravatars that may be out of character for your site.
lib/redmine/helpers/gantt.rb
686 686
            css_classes << ' over-end-date' if progress_date > self.date_to
687 687
          end
688 688
          s = (+"").html_safe
689
          if issue.assigned_to.present?
690
            assigned_string = l(:field_assigned_to) + ": " + issue.assigned_to.name
691
            s << view.avatar(issue.assigned_to,
692
                             :class => 'gravatar icon-gravatar',
693
                             :size => 13,
694
                             :title => assigned_string).to_s.html_safe
695
          end
689
          s << view.assignee_avatar(issue.assigned_to, :size => 13, :class => 'icon-gravatar')
696 690
          s << view.link_to_issue(issue).html_safe
697 691
          s << view.content_tag(:input, nil, :type => 'checkbox', :name => 'ids[]', :value => issue.id, :style => 'display:none;', :class => 'toggle-selection')
698 692
          view.content_tag(:span, s, :class => css_classes).html_safe
test/helpers/application_helper_test.rb
1495 1495
    assert_select_in result, 'ul.pages-hierarchy li ul.pages-hierarchy a[href=?]', "##{child_page.title}"
1496 1496
  end
1497 1497

  
1498
  def test_avatar_with_user
1499
    with_settings :gravatar_enabled => '1' do
1500
      assert_include Digest::MD5.hexdigest('jsmith@somenet.foo'), avatar(User.find_by_mail('jsmith@somenet.foo'))
1501
    end
1502
  end
1503

  
1504
  def test_avatar_with_email_string
1505
    with_settings :gravatar_enabled => '1' do
1506
      assert_include Digest::MD5.hexdigest('jsmith@somenet.foo'), avatar('jsmith <jsmith@somenet.foo>')
1507
    end
1508
  end
1509

  
1510
  def test_avatar_with_anonymous_user
1511
    with_settings :gravatar_enabled => '1' do
1512
      assert_match %r{src="/images/anonymous.png(\?\d+)?"}, avatar(User.anonymous)
1513
    end
1514
  end
1515

  
1516
  def test_avatar_with_group
1517
    with_settings :gravatar_enabled => '1' do
1518
      assert_nil avatar(Group.first)
1519
    end
1520
  end
1521

  
1522
  def test_avatar_with_invalid_arg_should_return_nil
1523
    with_settings :gravatar_enabled => '1' do
1524
      assert_nil avatar('jsmith')
1525
      assert_nil avatar(nil)
1526
    end
1527
  end
1528

  
1529
  def test_avatar_default_size_should_be_50
1530
    with_settings :gravatar_enabled => '1' do
1531
      assert_include 'size=50', avatar('jsmith <jsmith@somenet.foo>')
1532
    end
1533
  end
1534

  
1535
  def test_avatar_with_size_option
1536
    with_settings :gravatar_enabled => '1' do
1537
      assert_include 'size=24', avatar('jsmith <jsmith@somenet.foo>', :size => 24)
1538
      assert_include 'width="24" height="24"', avatar(User.anonymous, :size => 24)
1539
    end
1540
  end
1541

  
1542
  def test_avatar_with_html_option
1543
    with_settings :gravatar_enabled => '1' do
1544
      # Non-avatar options should be considered html options
1545
      assert_include 'title="John Smith"', avatar('jsmith <jsmith@somenet.foo>', :title => 'John Smith')
1546
    end
1547
  end
1548

  
1549
  def test_avatar_css_class
1550
    with_settings :gravatar_enabled => '1' do
1551
      # The default class of the img tag should be gravatar
1552
      assert_include 'class="gravatar"', avatar('jsmith <jsmith@somenet.foo>')
1553
      assert_not_include 'class="gravatar"', avatar('jsmith <jsmith@somenet.foo>', :class => 'picture')
1554
      assert_include 'class="picture"', avatar('jsmith <jsmith@somenet.foo>', :class => 'picture')
1555
    end
1556
  end
1557

  
1558
  def test_avatar_disabled
1559
    with_settings :gravatar_enabled => '0' do
1560
      assert_equal '', avatar(User.find_by_mail('jsmith@somenet.foo'))
1561
    end
1562
  end
1563

  
1564
  def test_avatar_server_url
1565
    to_test = {
1566
      'https://www.gravatar.com' => %r|https://www.gravatar.com/avatar/\h{32}|,
1567
      'https://seccdn.libravatar.org' => %r|https://seccdn.libravatar.org/avatar/\h{32}|,
1568
      'http://localhost:8080' => %r|http://localhost:8080/avatar/\h{32}|,
1569
    }
1570
    with_settings :gravatar_enabled => '1' do
1571
      to_test.each do |url, expected|
1572
        Redmine::Configuration.with 'avatar_server_url' => url do
1573
          assert_match expected, avatar('<jsmith@somenet.foo>')
1574
        end
1575
      end
1576
    end
1577
  end
1578

  
1579 1498
  def test_link_to_user
1580 1499
    user = User.find(2)
1581 1500
    result = link_to("John Smith", "/users/2", :class => "user active")
test/helpers/avatars_helper_test.rb
1
# frozen_string_literal: true
2

  
3
# Redmine - project management software
4
# Copyright (C) 2006-2017  Jean-Philippe Lang
5
#
6
# This program is free software; you can redistribute it and/or
7
# modify it under the terms of the GNU General Public License
8
# as published by the Free Software Foundation; either version 2
9
# of the License, or (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19

  
20
require File.expand_path('../../test_helper', __FILE__)
21

  
22
class AvatarsHelperTest < Redmine::HelperTest
23
  include ERB::Util
24
  include Rails.application.routes.url_helpers
25
  include AvatarsHelper
26

  
27
  fixtures :users, :email_addresses
28

  
29
  def setup
30
    Setting.gravatar_enabled = '1'
31
  end
32

  
33
  def test_avatar_with_user
34
    assert_include Digest::MD5.hexdigest('jsmith@somenet.foo'), avatar(User.find_by_mail('jsmith@somenet.foo'))
35
  end
36

  
37
  def test_avatar_with_email_string
38
    assert_include Digest::MD5.hexdigest('jsmith@somenet.foo'), avatar('jsmith <jsmith@somenet.foo>')
39
  end
40

  
41
  def test_avatar_with_anonymous_user
42
    assert_match %r{src="/images/anonymous.png(\?\d+)?"}, avatar(User.anonymous)
43
  end
44

  
45
  def test_avatar_with_group
46
    assert_nil avatar(Group.first)
47
  end
48

  
49
  def test_avatar_with_invalid_arg_should_return_nil
50
    assert_nil avatar('jsmith')
51
    assert_nil avatar(nil)
52
  end
53

  
54
  def test_avatar_default_size_should_be_24
55
      assert_include 'size=24', avatar('jsmith <jsmith@somenet.foo>')
56
  end
57

  
58
  def test_avatar_with_size_option
59
    assert_include 'size=24', avatar('jsmith <jsmith@somenet.foo>', :size => 24)
60
    assert_include 'width="24" height="24"', avatar(User.anonymous, :size => 24)
61
  end
62

  
63
  def test_avatar_with_html_option
64
    # Non-avatar options should be considered html options
65
    assert_include 'title="John Smith"', avatar('jsmith <jsmith@somenet.foo>', :title => 'John Smith')
66
  end
67

  
68
  def test_avatar_css_class
69
    # The default class of the img tag should be gravatar
70
    assert_include 'class="gravatar"', avatar('jsmith <jsmith@somenet.foo>')
71
    assert_include 'class="gravatar picture"', avatar('jsmith <jsmith@somenet.foo>', :class => 'picture')
72
  end
73

  
74
  def test_avatar_disabled
75
    with_settings :gravatar_enabled => '0' do
76
      assert_equal '', avatar(User.find_by_mail('jsmith@somenet.foo'))
77
    end
78
  end
79

  
80
  def test_avatar_server_url
81
    to_test = {
82
      'https://www.gravatar.com' => %r|https://www.gravatar.com/avatar/\h{32}|,
83
      'https://seccdn.libravatar.org' => %r|https://seccdn.libravatar.org/avatar/\h{32}|,
84
      'http://localhost:8080' => %r|http://localhost:8080/avatar/\h{32}|,
85
    }
86

  
87
    to_test.each do |url, expected|
88
      Redmine::Configuration.with 'avatar_server_url' => url do
89
        assert_match expected, avatar('<jsmith@somenet.foo>')
90
      end
91
    end
92
  end
93
end
test/unit/lib/redmine/helpers/gantt_test.rb
26 26
  include ProjectsHelper
27 27
  include IssuesHelper
28 28
  include QueriesHelper
29
  include AvatarsHelper
30

  
29 31
  include ERB::Util
30 32
  include Rails.application.routes.url_helpers
31 33

  
    (1-1/1)