Skip to content
This repository has been archived by the owner on Jan 28, 2022. It is now read-only.

Commit

Permalink
FEATURE: use random user avatar image from 'avatars' directory.
Browse files Browse the repository at this point in the history
  • Loading branch information
vinothkannans committed Apr 9, 2021
1 parent 36fb6c8 commit 6a121c3
Show file tree
Hide file tree
Showing 15 changed files with 47 additions and 1 deletion.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 46 additions & 0 deletions lib/discourse_dev/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

module DiscourseDev
class User < Record
attr_reader :images

def initialize(count = DEFAULT_COUNT)
super(::User, count)
@images = Dir[File.join(__dir__, '..', '..', 'avatars', '*.*')]
end

def data
Expand All @@ -31,6 +33,7 @@ def data
def create!
super do |user|
user.activate
set_random_avatar(user)
Faker::Number.between(from: 0, to: 2).times do
group = Group.random

Expand All @@ -42,5 +45,48 @@ def create!
def self.random
super(::User)
end

def set_random_avatar(user)
return if images.blank?
return unless Faker::Boolean.boolean

avatar_index = Faker::Number.between(from: 0, to: images.count - 1)
avatar_path = images[avatar_index]
create_avatar(user, avatar_path)
@images.delete_at(avatar_index)
end

def create_avatar(user, avatar_path)
tempfile = copy_to_tempfile(avatar_path)
filename = "avatar#{File.extname(avatar_path)}"
upload = UploadCreator.new(tempfile, filename, type: "avatar").create_for(user.id)

if upload.present? && upload.persisted?
user.create_user_avatar
user.user_avatar.update(custom_upload_id: upload.id)
user.update(uploaded_avatar_id: upload.id)
else
STDERR.puts "Failed to upload avatar for user #{user.username}: #{avatar_path}"
STDERR.puts upload.errors.inspect if upload
end
rescue
STDERR.puts "Failed to create avatar for user #{user.username}: #{avatar_path}"
ensure
tempfile.close! if tempfile
end

private

def copy_to_tempfile(source_path)
extension = File.extname(source_path)
tmp = Tempfile.new(['discourse-upload', extension])

File.open(source_path) do |source_stream|
IO.copy_stream(source_stream, tmp)
end

tmp.rewind
tmp
end
end
end
2 changes: 1 addition & 1 deletion lib/discourse_dev/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module DiscourseDev
VERSION = "0.0.8"
VERSION = "0.0.9"
end

0 comments on commit 6a121c3

Please sign in to comment.