mirror of
https://github.com/meineerde/redmine.git
synced 2025-10-17 17:01:01 +00:00
Refactor avatar helper method (#29824).
git-svn-id: https://svn.redmine.org/redmine/trunk@23907 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
parent
65f68c4c59
commit
1a5fb1325b
@ -38,30 +38,9 @@ module AvatarsHelper
|
|||||||
# +user+ can be a User or a string that will be scanned for an email address (eg. 'joe <joe@foo.bar>')
|
# +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 = {})
|
def avatar(user, options = {})
|
||||||
if Setting.gravatar_enabled?
|
if Setting.gravatar_enabled?
|
||||||
options[:default] = Setting.gravatar_default
|
gravatar_avatar_tag(user, options)
|
||||||
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]
|
|
||||||
options[:initials] = user.initials if options[:default] == "initials" && user.initials.present?
|
|
||||||
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)
|
|
||||||
elsif user.is_a?(Group)
|
|
||||||
group_avatar(options)
|
|
||||||
else
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
elsif user.respond_to?(:initials)
|
elsif user.respond_to?(:initials)
|
||||||
size = options.delete(:size) || GravatarHelper::DEFAULT_OPTIONS[:size]
|
initials_avatar_tag(user, options)
|
||||||
css_class = "avatar-color-#{user.id % 8} avatar s#{size}"
|
|
||||||
css_class += " #{options[:class]}" if options[:class]
|
|
||||||
content_tag('span', user.initials, role: 'img', class: css_class)
|
|
||||||
else
|
else
|
||||||
''
|
''
|
||||||
end
|
end
|
||||||
@ -75,8 +54,6 @@ module AvatarsHelper
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def anonymous_avatar(options={})
|
def anonymous_avatar(options={})
|
||||||
image_tag 'anonymous.png', GravatarHelper::DEFAULT_OPTIONS.except(:default, :rating, :ssl).merge(options)
|
image_tag 'anonymous.png', GravatarHelper::DEFAULT_OPTIONS.except(:default, :rating, :ssl).merge(options)
|
||||||
end
|
end
|
||||||
@ -84,4 +61,42 @@ module AvatarsHelper
|
|||||||
def group_avatar(options={})
|
def group_avatar(options={})
|
||||||
image_tag 'group.png', GravatarHelper::DEFAULT_OPTIONS.except(:default, :rating, :ssl).merge(options)
|
image_tag 'group.png', GravatarHelper::DEFAULT_OPTIONS.except(:default, :rating, :ssl).merge(options)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def gravatar_avatar_tag(user, options)
|
||||||
|
options[:default] = Setting.gravatar_default
|
||||||
|
options[:class] = [GravatarHelper::DEFAULT_OPTIONS[:class], options[:class]].compact.join(' ')
|
||||||
|
|
||||||
|
email = extract_email_from_user(user)
|
||||||
|
|
||||||
|
if user.respond_to?(:mail)
|
||||||
|
options[:title] ||= user.name
|
||||||
|
options[:initials] = user.initials if options[:default] == "initials" && user.initials.present?
|
||||||
|
end
|
||||||
|
|
||||||
|
if email.present?
|
||||||
|
gravatar(email.to_s.downcase, options) rescue nil
|
||||||
|
elsif user.is_a?(AnonymousUser)
|
||||||
|
anonymous_avatar(options)
|
||||||
|
elsif user.is_a?(Group)
|
||||||
|
group_avatar(options)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def initials_avatar_tag(user, options)
|
||||||
|
size = (options.delete(:size) || GravatarHelper::DEFAULT_OPTIONS[:size]).to_i
|
||||||
|
|
||||||
|
css_class = ["avatar-color-#{user.id % 8}", 'avatar', "s#{size}", options[:class]].compact.join(' ')
|
||||||
|
|
||||||
|
content_tag('span', user.initials, role: 'img', class: css_class, title: options[:title])
|
||||||
|
end
|
||||||
|
|
||||||
|
def extract_email_from_user(user)
|
||||||
|
if user.respond_to?(:mail)
|
||||||
|
user.mail
|
||||||
|
elsif user.to_s =~ %r{<(.+?)>}
|
||||||
|
$1
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user