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

Commit

Permalink
FEATURE: add users to random groups while creating.
Browse files Browse the repository at this point in the history
  • Loading branch information
vinothkannans committed Mar 4, 2021
1 parent 62b015d commit 662f2f1
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
11 changes: 5 additions & 6 deletions lib/discourse_dev/category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,12 @@ def permissions
end

def create!
category = ::Category.new(data)
category.set_permissions(permissions)
category.save!
super do |category|
category.set_permissions(permissions)
category.save!

@parent_category_ids << category.id if category.parent_category_id.blank?

putc "."
@parent_category_ids << category.id if category.parent_category_id.blank?
end
end
end
end
8 changes: 6 additions & 2 deletions lib/discourse_dev/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ def data
name = Faker::Discourse.group

while @existing_names.include? name
name = Faker::Company.profession
name = Faker::Company.profession.gsub(" ", "-")
end

@existing_names << name

{
name: name
name: name,
allow_membership_requests: Faker::Boolean.boolean,
public_exit: Faker::Boolean.boolean,
public_admission: Faker::Boolean.boolean,
primary_group: Faker::Boolean.boolean
}
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/discourse_dev/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def initialize(model, count = DEFAULT_COUNT)
end

def create!
model.create!(data)
record = model.create!(data)
yield(record)
putc "."
end

Expand Down
1 change: 1 addition & 0 deletions lib/discourse_dev/tasks/dev.rake
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ end

desc 'Populate sample content for development environment'
task 'dev:populate' => ['db:load_config'] do |_, args|
Rake::Task['groups:populate'].invoke
Rake::Task['users:populate'].invoke
Rake::Task['categories:populate'].invoke
Rake::Task['topics:populate'].invoke
Expand Down
14 changes: 14 additions & 0 deletions lib/discourse_dev/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class User < Record

def initialize(count = DEFAULT_COUNT)
super(::User, count)
@groups = ::Group.where(automatic: false)
@group_count = @groups.count
end

def data
Expand All @@ -22,8 +24,20 @@ def data
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)
}
end

def create!
super do |user|
Faker::Number.between(from: 0, to: 2).times do
offset = rand(@group_count)
group = @groups.offset(offset).first

group.add(user)
end
end
end
end
end

0 comments on commit 662f2f1

Please sign in to comment.