Skip to content

Commit 81e1ef7

Browse files
authored
Merge pull request #498 from datafruits/default-avatars
set default user avatars
2 parents 976adb5 + 892a165 commit 81e1ef7

File tree

7 files changed

+22
-0
lines changed

7 files changed

+22
-0
lines changed

app/models/user.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ class User < ActiveRecord::Base
7575

7676
after_update :maybe_send_update_notification
7777

78+
before_save :set_default_avatar, unless: -> { ::Rails.env.test? }
79+
7880
def login=(login)
7981
@login = login
8082
end
@@ -144,4 +146,17 @@ def maybe_send_update_notification
144146
Notification.create! notification_type: "avatar_update", source: self, send_to_chat: true, send_to_user: false, user: self, url: url
145147
end
146148
end
149+
150+
def set_default_avatar
151+
unless self.image.present?
152+
# TODO use in test when we switch to active storage
153+
if ::Rails.env === "test"
154+
random_avatar = File.new ::Rails.root.join("config/default_avatars/default_avatar_1.png")
155+
self.image = random_avatar
156+
else
157+
random_avatar = File.new ::Rails.root.join("config/default_avatars/default_avatar_#{rand(1..5)}.png")
158+
self.image = random_avatar
159+
end
160+
end
161+
end
147162
end
6.32 KB
Loading
6.32 KB
Loading
6.33 KB
Loading
6.55 KB
Loading
6.58 KB
Loading

spec/models/user_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,11 @@
4343
expect(user.deleted_at).to_not eq nil
4444
end
4545
end
46+
47+
describe "default avatar" do
48+
xit "sets a random default avatar if none is set" do
49+
user = User.create! email: "[email protected]", time_zone: "Tokyo", role: "admin", password: "testtest"
50+
expect(user.image.path).to include("default_avatar")
51+
end
52+
end
4653
end

0 commit comments

Comments
 (0)