Project

General

Profile

Patch #7664 » add-gravatars-like-themes.patch

Yuki Sonoda, 2011-02-21 02:13

View differences:

app/controllers/settings_controller.rb
46 46
    @guessed_host_and_path << ('/'+ Redmine::Utils.relative_url_root.gsub(%r{^\/}, '')) unless Redmine::Utils.relative_url_root.blank?
47 47
    
48 48
    Redmine::Themes.rescan
49
    Redmine::Gravatars.rescan
49 50
  end
50 51

  
51 52
  def plugin
app/helpers/application_helper.rb
882 882
  # +user+ can be a User or a string that will be scanned for an email address (eg. 'joe <joe@foo.bar>')
883 883
  def avatar(user, options = { })
884 884
    if Setting.gravatar_enabled?
885
      options.merge!({:ssl => (defined?(request) && request.ssl?), :default => Setting.gravatar_default})
885
      default = Redmine::Gravatar.gravatar(Setting.gravatar_default)
886
      options.merge!({:ssl => (defined?(request) && request.ssl?), :default => default})
886 887
      email = nil
887 888
      if user.respond_to?(:mail)
888 889
        email = user.mail
app/helpers/settings_helper.rb
81 81
                         l_or_humanize(notifiable.name, :prefix => 'label_'),
82 82
                       :class => notifiable.parent.present? ? "parent" : '')
83 83
  end
84

  
85
  def options_for_gravatar_default
86
    Redmine::Gravatars.gravatars
87
  end
84 88
end
app/views/settings/_display.rhtml
15 15

  
16 16
<p><%= setting_check_box :gravatar_enabled %></p>
17 17

  
18
<p><%= setting_select :gravatar_default, [["Wavatars", 'wavatar'], ["Identicons", 'identicon'], ["Monster ids", 'monsterid'], ["Retro", "retro"]], :blank => :label_none %></p>
18
<p><%= setting_select :gravatar_default, options_for_gravatar_default, :blank => :label_none %></p>
19 19
</div>
20 20

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

  
18
module Redmine
19
  module Gravatars
20
    WELL_KNOWN_GRAVATARS = [["Wavatars", 'wavatar'], ["Identicons", 'identicon'], ["Monster ids", 'monsterid'], ["Retro", "retro"]]
21

  
22
    # Return an array of installed gravatars
23
    def self.gravatars
24
      @@installed_gravatars ||= scan_gravatars
25
    end
26

  
27
    # Rescan gravatars directory
28
    def self.rescan
29
      @@installed_gravatars = scan_gravatars
30
    end
31

  
32
    # Return gravatar for given id, or nil if it's not found
33
    def self.gravatar(id, options = {})
34
      return nil if id.blank?
35

  
36
      found = gravatars.find {|g| g.id == id}
37
      if found.nil? && options[:rescan] != false
38
        rescan
39
        found = gravatar(id, :rescan => false)
40
      end
41
      found
42
    end
43

  
44
    # Class used to represent a gravatar
45
    class Gravatar
46
      include Comparable
47

  
48
      def initialize(value)
49
        @value = value
50
      end
51

  
52
      def label
53
        value
54
      end
55

  
56
      def id
57
        value
58
      end
59

  
60
      def <=>(g)
61
        if Gravatar === g
62
          self.id <=> g.id
63
        else
64
          nil
65
        end
66
      end
67

  
68
      def to_api_option
69
        "#{Setting.protocol}://#{Setting.host_name}/gravatars/#{value}"
70
      end
71

  
72
      private
73
      attr_reader :value
74
    end
75

  
76
    class WellKnownGravatar < Gravatar
77
      def initialize(label, value)
78
        @label = label
79
        super(value)
80
      end
81

  
82
      attr_reader :label
83

  
84
      def to_api_option
85
        value
86
      end
87
    end
88
    
89
    private
90
    
91
    def self.scan_gravatars
92
      found = Dir.glob("#{Rails.public_path}/gravatars/*").select{|f|
93
        File.readable?(f)
94
      }.map{|f|
95
        Gravatar.new(File.basename(f))
96
      }
97

  
98
      WELL_KNOWN_GRAVATARS.map{|label, value|
99
        WellKnownGravatar.new(label, value)
100
      } + found
101
    end
102
  end
103
end
public/gravatars/.gitignore
1
/*
2
!/.gitignore
test/unit/lib/redmine/gravatars_test.rb
1
# Redmine - project management software
2
# Copyright (C) 2006-2010  Jean-Philippe Lang
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8
# 
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
# 
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17

  
18
require File.expand_path('../../../../test_helper', __FILE__)
19

  
20
class Redmine::GravatarsTest < ActiveSupport::TestCase
21
  def setup
22
    Redmine::Gravatars.rescan
23
  end
24
  
25
  def test_gravatars
26
    gravatars = Redmine::Gravatars.gravatars
27
    assert_kind_of Array, gravatars
28
    gravatars.each do |gravatar|
29
      assert_kind_of Redmine::Gravatars::Gravatar, gravatar
30
    end
31
  end
32
  
33
  def test_rescan
34
    Redmine::Gravatars.gravatars.pop
35
    
36
    assert_difference 'Redmine::Gravatars.gravatars.size' do
37
      Redmine::Gravatars.rescan
38
    end
39
  end
40
  
41
  def test_gravatar_loaded
42
    gravatar = Redmine::Gravatars.gravatars.last
43
    
44
    assert_equal gravatar, Redmine::Gravatars.gravatar(gravatar.id)
45
  end
46
  
47
  def test_gravatar_loaded_without_rescan
48
    gravatar = Redmine::Gravatars.gravatars.last
49
    
50
    assert_equal gravatar, Redmine::Gravatars.gravatar(gravatar.id, :rescan => false)
51
  end
52
  
53
  def test_gravatar_not_loaded
54
    gravatar = Redmine::Gravatars.gravatars.pop
55
    
56
    assert_equal gravatar, Redmine::Gravatars.gravatar(gravatar.id)
57
  end
58
  
59
  def test_gravatar_not_loaded_without_rescan
60
    gravatar = Redmine::Gravatars.gravatars.pop
61
    
62
    assert_nil Redmine::Gravatars.gravatar(gravatar.id, :rescan => false)
63
  end
64
end
(3-3/3)