Class: DiscourseDev::User

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

Constant Summary

Constants inherited from Record

Record::AUTO_POPULATED, 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, #populate!, populate!

Constructor Details

#initializeUser

Returns a new instance of User.



10
11
12
13
14
# File 'lib/discourse_dev/user.rb', line 10

def initialize
  super(::User, DiscourseDev.config.user[:count])

  @images = DiscourseDevAssets.avatars
end

Instance Attribute Details

#imagesObject (readonly)

Returns the value of attribute images.



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

def images
  @images
end

Class Method Details

.randomObject



52
53
54
# File 'lib/discourse_dev/user.rb', line 52

def self.random
  super(::User, use_existing_records: false)
end

Instance Method Details

#create!Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/discourse_dev/user.rb', line 38

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/discourse_dev/user.rb', line 66

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 StandardError
  STDERR.puts "Failed to create avatar for user #{user.username}: #{avatar_path}"
ensure
  tempfile.close! if tempfile
end

#dataObject



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

def data
  name = Faker::Name.unique.name
  email = Faker::Internet.unique.email(name: name, domain: "faker.invalid")
  username = Faker::Internet.unique.username(specifier: ::User.username_length)
  username = UserNameSuggester.suggest(username)
  username_lower = ::User.normalize_username(username)
  created_at = Faker::Time.between(from: DiscourseDev.config.start_date, to: DateTime.now)
  seen_at = Faker::Time.between(from: created_at, to: DateTime.now)

  {
    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: created_at,
    first_seen_at: seen_at,
    last_seen_at: seen_at,
  }
end

#set_random_avatar(user) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/discourse_dev/user.rb', line 56

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