Feature #44717 » 44717-initials-avatar-color.patch
| app/assets/stylesheets/application.css | ||
|---|---|---|
|
}
|
||
|
.avatar.locked, .avatar.registered {opacity: .6;}
|
||
|
.avatar-preview {display: block; margin-block-start: 8px;}
|
||
|
.avatar-colors {display: inline-flex; flex-wrap: wrap; gap: 4px 8px; vertical-align: middle;}
|
||
|
.avatar-colors label.inline {display: inline-flex; align-items: center; gap: 2px;}
|
||
|
span[role="img"].avatar {
|
||
|
font-family: 'Roboto', 'Helvetica Neue', Helvetica, Arial, sans-serif;
|
||
| app/controllers/avatars_controller.rb | ||
|---|---|---|
|
before_action :find_avatar, :only => :show
|
||
|
before_action :find_user, :except => :show
|
||
|
before_action :check_avatar_available, :except => :edit
|
||
|
before_action :check_avatar_available, :except => [:edit, :update_color]
|
||
|
accept_api_auth :show
|
||
| ... | ... | |
|
redirect_to @avatar_path
|
||
|
end
|
||
|
def update_color
|
||
|
@user.avatar_color = params[:avatar_color]
|
||
|
if @user.save
|
||
|
flash[:notice] = l(:notice_successful_update)
|
||
|
redirect_to @avatar_path
|
||
|
else
|
||
|
render :edit, :status => :unprocessable_content
|
||
|
end
|
||
|
end
|
||
|
private
|
||
|
def find_avatar
|
||
| ... | ... | |
|
@user = User.find_by(:id => params[:user_id])
|
||
|
if @user
|
||
|
@avatar_path = edit_user_avatar_path(@user)
|
||
|
@avatar_color_path = user_avatar_color_path(@user)
|
||
|
else
|
||
|
render_404
|
||
|
end
|
||
|
elsif require_login
|
||
|
@user = User.current
|
||
|
@avatar_path = my_avatar_path
|
||
|
@avatar_color_path = my_avatar_color_path
|
||
|
end
|
||
|
end
|
||
| app/helpers/avatars_helper.rb | ||
|---|---|---|
|
def initials_avatar_css_class(user, options)
|
||
|
size = (options[:size] || GravatarHelper::DEFAULT_OPTIONS[:size]).to_i
|
||
|
["avatar-color-#{user.id % 16}", "s#{size}", options[:class]].compact.join(' ')
|
||
|
["avatar-color-#{user.initials_avatar_color}", "s#{size}", options[:class]].compact.join(' ')
|
||
|
end
|
||
|
def extract_email_from_user(user)
|
||
| app/models/user.rb | ||
|---|---|---|
|
attr_accessor :oauth_scope
|
||
|
AVATAR_CONTENT_TYPES = %w(image/avif image/gif image/jpeg image/png image/webp).freeze
|
||
|
# Number of background colors of the initials avatar (avatar-color-0 to avatar-color-15 in CSS)
|
||
|
AVATAR_COLORS = 16
|
||
|
LOGIN_LENGTH_LIMIT = 60
|
||
|
MAIL_LENGTH_LIMIT = 254
|
||
| ... | ... | |
|
validates_length_of :firstname, :maximum => 30
|
||
|
validates_length_of :lastname, :maximum => 255
|
||
|
validates_inclusion_of :mail_notification, :in => MAIL_NOTIFICATION_OPTIONS.collect(&:first), :allow_blank => true
|
||
|
validates_inclusion_of :avatar_color, :in => 0...AVATAR_COLORS, :allow_nil => true
|
||
|
Setting::PASSWORD_CHAR_CLASSES.each do |k, v|
|
||
|
validates_format_of :password, :with => v, :message => :"must_contain_#{k}", :allow_blank => true, :if => Proc.new {Setting.password_required_char_classes.include?(k)}
|
||
|
end
|
||
| ... | ... | |
|
OpenSSL::HMAC.hexdigest('SHA256', secret, avatar_id.to_s).first(16)
|
||
|
end
|
||
|
# Returns the index of the background color of the initials avatar,
|
||
|
# derived from the user id unless the user has chosen one
|
||
|
def initials_avatar_color
|
||
|
avatar_color || (id % AVATAR_COLORS)
|
||
|
end
|
||
|
def delete_avatar
|
||
|
replace_avatar(nil)
|
||
|
end
|
||
| app/views/avatars/edit.html.erb | ||
|---|---|---|
|
<% end %>
|
||
|
</span>
|
||
|
</p>
|
||
|
<hr>
|
||
|
<%= form_tag(@avatar_color_path, :method => :put) do %>
|
||
|
<p>
|
||
|
<label><%= l(:field_avatar_color) %></label>
|
||
|
<span class="avatar-colors">
|
||
|
<% User::AVATAR_COLORS.times do |color| %>
|
||
|
<label class="inline">
|
||
|
<%= radio_button_tag 'avatar_color', color, @user.initials_avatar_color == color %>
|
||
|
<%= content_tag 'span', @user.initials, :role => 'img', :class => "avatar avatar-color-#{color} s24" %>
|
||
|
</label>
|
||
|
<% end %>
|
||
|
</span>
|
||
|
<%= submit_tag l(:button_save) %>
|
||
|
</p>
|
||
|
<% end %>
|
||
|
<% if User.avatar_available? %>
|
||
|
<hr>
|
||
|
<%= form_tag(@avatar_path, :method => :put, :multipart => true) do %>
|
||
| config/locales/en.yml | ||
|---|---|---|
|
field_done_ratio: "% Done"
|
||
|
field_auth_source: Authentication mode
|
||
|
field_avatar: Avatar
|
||
|
field_avatar_color: Avatar color
|
||
|
field_hide_mail: Hide my email address
|
||
|
field_comments: Comment
|
||
|
field_url: URL
|
||
| config/routes.rb | ||
|---|---|---|
|
get 'my/avatar', :to => 'avatars#edit', :as => 'my_avatar', :format => false
|
||
|
put 'my/avatar', :to => 'avatars#update', :format => false
|
||
|
delete 'my/avatar', :to => 'avatars#destroy', :format => false
|
||
|
put 'my/avatar/color', :to => 'avatars#update_color', :as => 'my_avatar_color', :format => false
|
||
|
match 'my/add_block', :controller => 'my', :action => 'add_block', :via => :post
|
||
|
match 'my/remove_block', :controller => 'my', :action => 'remove_block', :via => :post
|
||
|
match 'my/order_blocks', :controller => 'my', :action => 'order_blocks', :via => :post
|
||
| ... | ... | |
|
get 'users/:user_id/avatar', :to => 'avatars#edit', :user_id => /\d+/, :as => 'edit_user_avatar', :format => false
|
||
|
put 'users/:user_id/avatar', :to => 'avatars#update', :user_id => /\d+/, :format => false
|
||
|
delete 'users/:user_id/avatar', :to => 'avatars#destroy', :user_id => /\d+/, :format => false
|
||
|
put 'users/:user_id/avatar/color', :to => 'avatars#update_color', :user_id => /\d+/, :as => 'user_avatar_color', :format => false
|
||
|
post 'watchers/watch', :to => 'watchers#watch', :as => 'watch'
|
||
|
delete 'watchers/watch', :to => 'watchers#unwatch'
|
||
| db/migrate/20261119000000_add_avatar_color_to_users.rb | ||
|---|---|---|
|
class AddAvatarColorToUsers < ActiveRecord::Migration[8.1]
|
||
|
def change
|
||
|
add_column :users, :avatar_color, :integer
|
||
|
end
|
||
|
end
|
||
| test/functional/avatars_controller_test.rb | ||
|---|---|---|
|
assert_response :success
|
||
|
assert_select 'h2 a[href="/users/2/edit"]'
|
||
|
assert_select 'form[action="/users/2/avatar"]'
|
||
|
assert_select 'form[action="/users/2/avatar/color"]'
|
||
|
end
|
||
|
def test_edit_should_show_avatar_colors
|
||
|
@request.session[:user_id] = 2
|
||
|
with_settings :avatar_enabled => '0' do
|
||
|
get :edit
|
||
|
end
|
||
|
assert_response :success
|
||
|
assert_select 'form[action="/my/avatar/color"]' do
|
||
|
assert_select 'input[type=radio][name=avatar_color]', User::AVATAR_COLORS
|
||
|
assert_select 'input[type=radio][name=avatar_color][checked]', 1
|
||
|
assert_select 'input#avatar_color_2[checked]'
|
||
|
assert_select 'label.inline span[role="img"].avatar.avatar-color-5', :text => 'JS'
|
||
|
assert_select 'input[type=submit][value=Save]'
|
||
|
end
|
||
|
end
|
||
|
def test_edit_with_user_id_should_require_admin
|
||
| ... | ... | |
|
assert_equal User.find(2), avatar.author
|
||
|
end
|
||
|
def test_update_color
|
||
|
@request.session[:user_id] = 2
|
||
|
with_settings :avatar_enabled => '0' do
|
||
|
put :update_color, :params => {:avatar_color => '5'}
|
||
|
end
|
||
|
assert_redirected_to '/my/avatar'
|
||
|
assert_equal 'Successful update.', flash[:notice]
|
||
|
assert_equal 5, User.find(2).avatar_color
|
||
|
end
|
||
|
def test_update_color_without_color_should_reset_color
|
||
|
User.find(2).update_column(:avatar_color, 5)
|
||
|
@request.session[:user_id] = 2
|
||
|
put :update_color
|
||
|
assert_redirected_to '/my/avatar'
|
||
|
assert_nil User.find(2).avatar_color
|
||
|
end
|
||
|
def test_update_color_with_invalid_color
|
||
|
@request.session[:user_id] = 2
|
||
|
put :update_color, :params => {:avatar_color => User::AVATAR_COLORS.to_s}
|
||
|
assert_response :unprocessable_content
|
||
|
assert_select_error /Avatar color is not included in the list/
|
||
|
assert_nil User.find(2).avatar_color
|
||
|
end
|
||
|
def test_update_color_with_user_id
|
||
|
@request.session[:user_id] = 1
|
||
|
put :update_color, :params => {:user_id => 2, :avatar_color => '5'}
|
||
|
assert_redirected_to '/users/2/avatar'
|
||
|
assert_equal 5, User.find(2).avatar_color
|
||
|
end
|
||
|
def test_update_color_with_user_id_should_require_admin
|
||
|
@request.session[:user_id] = 2
|
||
|
put :update_color, :params => {:user_id => 3, :avatar_color => '5'}
|
||
|
assert_response :forbidden
|
||
|
assert_nil User.find(3).avatar_color
|
||
|
end
|
||
|
def test_update_color_should_require_login
|
||
|
put :update_color, :params => {:avatar_color => '5'}
|
||
|
assert_response :redirect
|
||
|
assert_match %r{/login}, response.location
|
||
|
end
|
||
|
def test_destroy
|
||
|
Redmine::Thumbnail.stubs(:convert_available?).returns(true)
|
||
|
set_tmp_attachments_directory
|
||
| test/helpers/avatars_helper_test.rb | ||
|---|---|---|
|
end
|
||
|
end
|
||
|
def test_initials_avatar_should_use_chosen_color
|
||
|
user = User.find_by_mail('miscuser8@foo.bar')
|
||
|
user.avatar_color = 3
|
||
|
with_settings :gravatar_enabled => '0' do
|
||
|
assert_equal "<span role=\"img\" class=\"avatar-color-3 s24 avatar active\">UM</span>", avatar(user)
|
||
|
end
|
||
|
with_settings :gravatar_default => '' do
|
||
|
assert_select_in avatar(user), 'img[data-initials-avatar-fallback-class-value="avatar-color-3 s24 avatar active"]'
|
||
|
end
|
||
|
end
|
||
|
def test_avatar_should_have_user_status_class
|
||
|
assert_select_in avatar(User.find(1)), '.avatar.active'
|
||
|
assert_select_in avatar(User.find(5)), '.avatar.locked'
|
||
| test/integration/routing/my_test.rb | ||
|---|---|---|
|
should_route 'GET /my/avatar' => 'avatars#edit'
|
||
|
should_route 'PUT /my/avatar' => 'avatars#update'
|
||
|
should_route 'DELETE /my/avatar' => 'avatars#destroy'
|
||
|
should_route 'PUT /my/avatar/color' => 'avatars#update_color'
|
||
|
should_route 'POST /my/add_block' => 'my#add_block'
|
||
|
should_route 'POST /my/remove_block' => 'my#remove_block'
|
||
| test/integration/routing/users_test.rb | ||
|---|---|---|
|
should_route 'GET /users/44/avatar' => 'avatars#edit', :user_id => '44'
|
||
|
should_route 'PUT /users/44/avatar' => 'avatars#update', :user_id => '44'
|
||
|
should_route 'DELETE /users/44/avatar' => 'avatars#destroy', :user_id => '44'
|
||
|
should_route 'PUT /users/44/avatar/color' => 'avatars#update_color', :user_id => '44'
|
||
|
should_route 'GET /users/44/avatar/0123456789abcdef/48' => 'avatars#show', :user_id => '44', :avatar_key => '0123456789abcdef', :size => '48'
|
||
|
end
|
||
|
end
|
||
| test/unit/user_test.rb | ||
|---|---|---|
|
assert_not User.avatar_available?
|
||
|
end
|
||
|
def test_initials_avatar_color_should_be_derived_from_id_by_default
|
||
|
assert_nil @jsmith.avatar_color
|
||
|
assert_equal 2, @jsmith.initials_avatar_color
|
||
|
end
|
||
|
def test_initials_avatar_color_should_return_chosen_color
|
||
|
@jsmith.avatar_color = 5
|
||
|
assert_equal 5, @jsmith.initials_avatar_color
|
||
|
end
|
||
|
def test_avatar_color_should_be_one_of_the_initials_avatar_colors
|
||
|
[nil, 0, User::AVATAR_COLORS - 1].each do |color|
|
||
|
@jsmith.avatar_color = color
|
||
|
assert @jsmith.valid?, "#{color.inspect} should be valid"
|
||
|
end
|
||
|
[-1, User::AVATAR_COLORS].each do |color|
|
||
|
@jsmith.avatar_color = color
|
||
|
assert_not @jsmith.valid?, "#{color.inspect} should be invalid"
|
||
|
assert_include 'Avatar color is not included in the list', @jsmith.errors.full_messages
|
||
|
end
|
||
|
end
|
||
|
def test_update_avatar_should_reject_images_without_imagemagick
|
||
|
Redmine::Thumbnail.stubs(:convert_available?).returns(false)
|
||
|
assert_no_difference 'Attachment.count' do
|
||
- « Previous
- 1
- …
- 7
- 8
- 9
- Next »