From 002ae9f32728279a1aae33a7bf0a8cf9971916e2 Mon Sep 17 00:00:00 2001 From: jasder Date: Mon, 19 Apr 2021 15:06:56 +0800 Subject: [PATCH] ADD auto generate user avatar --- .gitignore | 4 +++- Gemfile | 2 ++ Gemfile.lock | 2 ++ app/helpers/application_helper.rb | 7 +----- app/models/concerns/.keep | 0 app/models/user.rb | 1 + app/models/user/avatar.rb | 32 ++++++++++++++++++++++++++++ config/initializers/letter_avatar.rb | 7 ++++++ 8 files changed, 48 insertions(+), 7 deletions(-) delete mode 100644 app/models/concerns/.keep create mode 100644 app/models/user/avatar.rb create mode 100644 config/initializers/letter_avatar.rb diff --git a/.gitignore b/.gitignore index 77104d20a..6b4fd25c1 100644 --- a/.gitignore +++ b/.gitignore @@ -36,6 +36,7 @@ public/react/yarn.lock /.idea/* # Ignore react node_modules +public/system/* public/react/* /public/react/.cache /public/react/node_modules/ @@ -82,4 +83,5 @@ docker/ educoder.sql redis_data/ Dockerfile -dump.rdb \ No newline at end of file +dump.rdb +.tags* diff --git a/Gemfile b/Gemfile index 281dd705a..713eb860f 100644 --- a/Gemfile +++ b/Gemfile @@ -126,3 +126,5 @@ gem 'request_store' gem 'harmonious_dictionary', '~> 0.0.1' gem 'parallel', '~> 1.19', '>= 1.19.1' + +gem 'letter_avatar' diff --git a/Gemfile.lock b/Gemfile.lock index cbd598f59..015c0a515 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -162,6 +162,7 @@ GEM activerecord kaminari-core (= 1.2.0) kaminari-core (1.2.0) + letter_avatar (0.3.8) listen (3.1.5) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) @@ -456,6 +457,7 @@ DEPENDENCIES jbuilder (~> 2.5) jquery-rails kaminari (~> 1.1, >= 1.1.1) + letter_avatar listen (>= 3.0.5, < 3.2) mysql2 (>= 0.4.4, < 0.6.0) oauth2 diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 30953bf57..18523d700 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -149,12 +149,7 @@ module ApplicationHelper File.join("images/avatars", ["#{source.class}", "#{source.id}"]) + "?t=#{ctime}" end elsif source.class.to_s == 'User' - str = source.user_extension.try(:gender).to_i == 0 ? "b" : "g" - File.join(relative_path, "#{source.class}", str) - elsif source.class.to_s == 'Subject' - File.join("images","educoder", "index", "subject", "subject#{rand(17)}.jpg") - elsif source.class.to_s == 'Shixun' - File.join("images","educoder", "index", "shixun", "shixun#{rand(23)}.jpg") + source.letter_avatar_url end end diff --git a/app/models/concerns/.keep b/app/models/concerns/.keep deleted file mode 100644 index e69de29bb..000000000 diff --git a/app/models/user.rb b/app/models/user.rb index bf4ec1048..20b3a8543 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -66,6 +66,7 @@ class User < Owner include Likeable include BaseModel include Droneable + include User::Avatar # include Searchable::Dependents::User # devops step diff --git a/app/models/user/avatar.rb b/app/models/user/avatar.rb new file mode 100644 index 000000000..4b62c28fb --- /dev/null +++ b/app/models/user/avatar.rb @@ -0,0 +1,32 @@ +require 'letter_avatar/has_avatar' + +class User + module Avatar + extend ActiveSupport::Concern + include LetterAvatar::HasAvatar + + def name + lastname.blank? ? login : Pinyin.t(lastname) + end + + def letter_avatar_url(size = :lg) + avatar_url(avatar_size(size)) + end + + # 返回头像尺寸 + # xs: 22px + # sm: 32px + # md: 48px + # lg: 120px + def avatar_size(size) + case size + when :xs then 22 + when :sm then 32 + when :md then 48 + when :lg then 120 + else size + end + end + + end +end diff --git a/config/initializers/letter_avatar.rb b/config/initializers/letter_avatar.rb new file mode 100644 index 000000000..88f3851e9 --- /dev/null +++ b/config/initializers/letter_avatar.rb @@ -0,0 +1,7 @@ +LetterAvatar.setup do |config| + config.fill_color = 'rgba(255, 255, 255, 1)' # default is 'rgba(255, 255, 255, 0.65)' + config.cache_base_path = 'public/system/lets' # default is 'public/system' + config.colors_palette = :iwanthue # default is :google + config.annotate_position = '-0+10' # default is -0+5 + config.letters_count = 2 # default is 1 +end