commit b7184cd3bf29d7443761b955df23081a77a7383b Author: Yuki Sonoda (Yugui) Date: Mon Feb 21 10:06:37 2011 +0900 add options for the default gravatar like for themes diff --git a/app/controllers/settings_controller.rb b/app/controllers/settings_controller.rb index 804a7fd..1527622 100644 --- a/app/controllers/settings_controller.rb +++ b/app/controllers/settings_controller.rb @@ -46,6 +46,7 @@ class SettingsController < ApplicationController @guessed_host_and_path << ('/'+ Redmine::Utils.relative_url_root.gsub(%r{^\/}, '')) unless Redmine::Utils.relative_url_root.blank? Redmine::Themes.rescan + Redmine::Gravatars.rescan end def plugin diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index c6d8848..3cf78dc 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -882,7 +882,8 @@ module ApplicationHelper # +user+ can be a User or a string that will be scanned for an email address (eg. 'joe ') def avatar(user, options = { }) if Setting.gravatar_enabled? - options.merge!({:ssl => (defined?(request) && request.ssl?), :default => Setting.gravatar_default}) + default = Redmine::Gravatar.gravatar(Setting.gravatar_default) + options.merge!({:ssl => (defined?(request) && request.ssl?), :default => default}) email = nil if user.respond_to?(:mail) email = user.mail diff --git a/app/helpers/settings_helper.rb b/app/helpers/settings_helper.rb index 6dc33a8..8641319 100644 --- a/app/helpers/settings_helper.rb +++ b/app/helpers/settings_helper.rb @@ -81,4 +81,8 @@ module SettingsHelper l_or_humanize(notifiable.name, :prefix => 'label_'), :class => notifiable.parent.present? ? "parent" : '') end + + def options_for_gravatar_default + Redmine::Gravatars.gravatars + end end diff --git a/app/views/settings/_display.rhtml b/app/views/settings/_display.rhtml index c6fe833..dfcad6f 100644 --- a/app/views/settings/_display.rhtml +++ b/app/views/settings/_display.rhtml @@ -15,7 +15,7 @@

<%= setting_check_box :gravatar_enabled %>

-

<%= setting_select :gravatar_default, [["Wavatars", 'wavatar'], ["Identicons", 'identicon'], ["Monster ids", 'monsterid'], ["Retro", "retro"]], :blank => :label_none %>

+

<%= setting_select :gravatar_default, options_for_gravatar_default, :blank => :label_none %>

<%= submit_tag l(:button_save) %> diff --git a/lib/redmine/gravatars.rb b/lib/redmine/gravatars.rb new file mode 100644 index 0000000..a9dcb11 --- /dev/null +++ b/lib/redmine/gravatars.rb @@ -0,0 +1,103 @@ +# redMine - project management software +# Copyright (C) 2006-2011 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 Redmine + module Gravatars + WELL_KNOWN_GRAVATARS = [["Wavatars", 'wavatar'], ["Identicons", 'identicon'], ["Monster ids", 'monsterid'], ["Retro", "retro"]] + + # Return an array of installed gravatars + def self.gravatars + @@installed_gravatars ||= scan_gravatars + end + + # Rescan gravatars directory + def self.rescan + @@installed_gravatars = scan_gravatars + end + + # Return gravatar for given id, or nil if it's not found + def self.gravatar(id, options = {}) + return nil if id.blank? + + found = gravatars.find {|g| g.id == id} + if found.nil? && options[:rescan] != false + rescan + found = gravatar(id, :rescan => false) + end + found + end + + # Class used to represent a gravatar + class Gravatar + include Comparable + + def initialize(value) + @value = value + end + + def label + value + end + + def id + value + end + + def <=>(g) + if Gravatar === g + self.id <=> g.id + else + nil + end + end + + def to_api_option + "#{Setting.protocol}://#{Setting.host_name}/gravatars/#{value}" + end + + private + attr_reader :value + end + + class WellKnownGravatar < Gravatar + def initialize(label, value) + @label = label + super(value) + end + + attr_reader :label + + def to_api_option + value + end + end + + private + + def self.scan_gravatars + found = Dir.glob("#{Rails.public_path}/gravatars/*").select{|f| + File.readable?(f) + }.map{|f| + Gravatar.new(File.basename(f)) + } + + WELL_KNOWN_GRAVATARS.map{|label, value| + WellKnownGravatar.new(label, value) + } + found + end + end +end diff --git a/public/gravatars/.gitignore b/public/gravatars/.gitignore new file mode 100644 index 0000000..a68d087 --- /dev/null +++ b/public/gravatars/.gitignore @@ -0,0 +1,2 @@ +/* +!/.gitignore diff --git a/test/unit/lib/redmine/gravatars_test.rb b/test/unit/lib/redmine/gravatars_test.rb new file mode 100644 index 0000000..0511cc1 --- /dev/null +++ b/test/unit/lib/redmine/gravatars_test.rb @@ -0,0 +1,64 @@ +# Redmine - project management software +# Copyright (C) 2006-2010 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 Redmine::GravatarsTest < ActiveSupport::TestCase + def setup + Redmine::Gravatars.rescan + end + + def test_gravatars + gravatars = Redmine::Gravatars.gravatars + assert_kind_of Array, gravatars + gravatars.each do |gravatar| + assert_kind_of Redmine::Gravatars::Gravatar, gravatar + end + end + + def test_rescan + Redmine::Gravatars.gravatars.pop + + assert_difference 'Redmine::Gravatars.gravatars.size' do + Redmine::Gravatars.rescan + end + end + + def test_gravatar_loaded + gravatar = Redmine::Gravatars.gravatars.last + + assert_equal gravatar, Redmine::Gravatars.gravatar(gravatar.id) + end + + def test_gravatar_loaded_without_rescan + gravatar = Redmine::Gravatars.gravatars.last + + assert_equal gravatar, Redmine::Gravatars.gravatar(gravatar.id, :rescan => false) + end + + def test_gravatar_not_loaded + gravatar = Redmine::Gravatars.gravatars.pop + + assert_equal gravatar, Redmine::Gravatars.gravatar(gravatar.id) + end + + def test_gravatar_not_loaded_without_rescan + gravatar = Redmine::Gravatars.gravatars.pop + + assert_nil Redmine::Gravatars.gravatar(gravatar.id, :rescan => false) + end +end