Add default admin to seeds file (#10620)
* Add default admin to seeds file * Docs
This commit is contained in:
parent
d444ef3397
commit
c0272357c0
2 changed files with 44 additions and 2 deletions
29
db/seeds.rb
29
db/seeds.rb
|
|
@ -21,6 +21,16 @@ class Seeder
|
|||
puts " #{@counter}. #{plural} already exist. Skipping."
|
||||
end
|
||||
end
|
||||
|
||||
def create_if_doesnt_exist(klass, attribute_name, attribute_value)
|
||||
record = klass.find_by("#{attribute_name}": attribute_value)
|
||||
if record.nil?
|
||||
puts " #{klass} with #{attribute_name} = #{attribute_value} not found, proceeding..."
|
||||
yield
|
||||
else
|
||||
puts " #{klass} with #{attribute_name} = #{attribute_value} found, skipping."
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# we use this to be able to increase the size of the seeded DB at will
|
||||
|
|
@ -136,6 +146,25 @@ users_in_random_order = seeder.create_if_none(User, num_users) do
|
|||
User.order(Arel.sql("RANDOM()"))
|
||||
end
|
||||
|
||||
seeder.create_if_doesnt_exist(User, "email", "admin@forem.local") do
|
||||
user = User.create!(
|
||||
name: "Admin McAdmin",
|
||||
email: "admin@forem.local",
|
||||
username: "Admin_McAdmin",
|
||||
summary: Faker::Lorem.paragraph_by_chars(number: 199, supplemental: false),
|
||||
profile_image: File.open(Rails.root.join("app/assets/images/#{rand(1..40)}.png")),
|
||||
website_url: Faker::Internet.url,
|
||||
email_comment_notifications: false,
|
||||
email_follower_notifications: false,
|
||||
confirmed_at: Time.current,
|
||||
password: "password",
|
||||
password_confirmation: "password",
|
||||
)
|
||||
|
||||
user.add_role(:admin)
|
||||
user.add_role(:single_resource_admin)
|
||||
end
|
||||
|
||||
##############################################################################
|
||||
|
||||
seeder.create_if_none(Tag) do
|
||||
|
|
|
|||
|
|
@ -46,10 +46,23 @@ It's currently used only for `articles` and `users`.
|
|||
|
||||
It can also be used for `rails db:seed` and `rails db:reset`.
|
||||
|
||||
## Default Admin User
|
||||
|
||||
Seed data creates a handful of regular users, and a single admin user that can
|
||||
be used to log into the application with the Email login option:
|
||||
|
||||
```
|
||||
email: admin@forem.local
|
||||
password: admin
|
||||
```
|
||||
|
||||
### Other seed modes
|
||||
|
||||
To put your local forem into "starter mode", as it would be for a new creator, use `MODE=STARTER` i.e...
|
||||
To put your local forem into "starter mode", as it would be for a new creator,
|
||||
use `MODE=STARTER` i.e...
|
||||
|
||||
```shell
|
||||
MODE=STARTER rails db:setup
|
||||
```
|
||||
```
|
||||
|
||||
This mode skips creation of all sample data.
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue