Class: DiscourseDev::User

Inherits:
Record
  • Object
show all
Defined in:
lib/discourse_dev/user.rb

Constant Summary

Constants inherited from Record

Record::DEFAULT_COUNT

Instance Attribute Summary collapse

Attributes inherited from Record

#model, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Record

#current_count, #index, #populate!, populate!

Constructor Details

#initialize(count = DEFAULT_COUNT) ⇒ User

Returns a new instance of User.



11
12
13
14
15
16
17
# File 'lib/discourse_dev/user.rb', line 11

def initialize(count = DEFAULT_COUNT)
  super(::User, count)

  # Using the stock avatar images from https://tinyfac.es
  # Tiny Faces is a free crowd-sourced avatar gallery
  @images = Dir[File.join(__dir__, '..', '..', 'avatars', '*.*')]
end

Instance Attribute Details

#imagesObject (readonly)

Returns the value of attribute images.



9
10
11
# File 'lib/discourse_dev/user.rb', line 9

def images
  @images
end

Class Method Details

.randomObject



48
49
50
# File 'lib/discourse_dev/user.rb', line 48

def self.random
  super(::User)
end

Instance Method Details

#create!Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/discourse_dev/user.rb', line 36

def create!
  super do |user|
    user.activate
    set_random_avatar(user)
    Faker::Number.between(from: 0, to: 2).times do
      group = Group.random

      group.add(user)
    end
  end
end

#create_avatar(user, avatar_path) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/discourse_dev/user.rb', line 62

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

#dataObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/discourse_dev/user.rb', line 19

def data
  name = Faker::Name.unique.name
  email = Faker::Internet.unique.email(name: name)
  username = Faker::Internet.unique.username(specifier: name)[0, SiteSetting.max_username_length]
  username_lower = username.downcase

  {
    name: name,
    email: email,
    username: username,
    username_lower: username_lower,
    moderator: Faker::Boolean.boolean(true_ratio: 0.1),
    trust_level: Faker::Number.between(from: 1, to: 4),
    created_at: Faker::Time.between(from: DiscourseDev.config.start_date, to: DateTime.now),
  }
end

#set_random_avatar(user) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/discourse_dev/user.rb', line 52

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