Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move current seeds.rb script to new DevStarter module (lib/seed/seeder.rb) #767

Merged
merged 9 commits into from
Mar 4, 2025
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ You can start your local development environment by running the following comman
docker compose up
```

Refer to `db/seeds.rb` for credentials on how to login using the seed users.
Refer to `lib/seed/constants.rb` for credentials on how to login using the seed users.

Below are some common commands you can use via `just`:

Expand Down
93 changes: 1 addition & 92 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,94 +1,3 @@
# rubocop:disable Rails/Output
puts "Seeding database..."

# Admin user
# ----------
ADMIN_FULL_NAME = "Admin User"
ADMIN_PREFERRED_NAME = "Admin"
ADMIN_EMAIL = "admin@tramline.app"
ADMIN_PASSWORD = "why aroma enclose startup"

admin_user = lambda do
email_authentication = Accounts::EmailAuthentication.find_or_initialize_by(email: ADMIN_EMAIL)
admin = true

unless email_authentication.persisted?
user = Accounts::User.find_or_create_by!(full_name: ADMIN_FULL_NAME, preferred_name: ADMIN_PREFERRED_NAME, admin:, unique_authn_id: ADMIN_EMAIL)
email_authentication.update!(password: ADMIN_PASSWORD, confirmed_at: DateTime.now, user:)
end

puts "Added/updated admin user."
end

# Owner user
# --------------
OWNER_FULL_NAME = "Owner User"
OWNER_PREFERRED_NAME = "Owner"
OWNER_EMAIL = "owner@tramline.app"
OWNER_PASSWORD = "why aroma enclose startup"

owner_user = lambda do
email_authentication = Accounts::EmailAuthentication.find_or_initialize_by(email: OWNER_EMAIL)

if email_authentication.persisted?
user = email_authentication.user
else
user = Accounts::User.find_or_create_by!(full_name: OWNER_FULL_NAME, preferred_name: OWNER_PREFERRED_NAME, unique_authn_id: OWNER_EMAIL)
email_authentication.update!(password: OWNER_PASSWORD, confirmed_at: DateTime.now, user:)
email_authentication.reload
end

organization = Accounts::Organization.find_or_create_by!(
name: "Tramline Test 1 (Owner)",
status: Accounts::Organization.statuses[:active],
created_by: email_authentication.email
)

Accounts::Membership.find_or_create_by!(
user:,
organization:,
role: Accounts::Membership.roles[:owner]
)

puts "Added/updated owner user."
end

# Developer user
# --------------
DEVELOPER_FULL_NAME = "Developer User"
DEVELOPER_PREFERRED_NAME = "Developer"
DEVELOPER_EMAIL = "developer@tramline.app"
DEVELOPER_PASSWORD = "why aroma enclose startup"

developer_user = lambda do
email_authentication = Accounts::EmailAuthentication.find_or_initialize_by(email: DEVELOPER_EMAIL)

if email_authentication.persisted?
user = email_authentication.user
else
user = Accounts::User.find_or_create_by!(full_name: DEVELOPER_FULL_NAME, preferred_name: DEVELOPER_PREFERRED_NAME, unique_authn_id: DEVELOPER_EMAIL)
email_authentication.update!(password: DEVELOPER_PASSWORD, confirmed_at: DateTime.now, user:)
email_authentication.reload
end

organization = Accounts::Organization.find_or_create_by!(
name: "Tramline Test 1 (Developer)",
status: Accounts::Organization.statuses[:active],
created_by: email_authentication.email
)

Accounts::Membership.find_or_create_by!(
user:,
organization:,
role: Accounts::Membership.roles[:developer]
)

puts "Added/updated developer user."
end

ActiveRecord::Base.transaction do
admin_user.call
owner_user.call
developer_user.call
end
Seed::DevStarter.call
# rubocop:enable Rails/Output
21 changes: 21 additions & 0 deletions lib/seed/constants.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Seed
module Constants
# admin constants
ADMIN_FULL_NAME = "Admin User"
ADMIN_PREFERRED_NAME = "Admin"
ADMIN_EMAIL = "admin@tramline.app"
ADMIN_PASSWORD = "why aroma enclose startup"

# owner constants
OWNER_FULL_NAME = "Owner User"
OWNER_PREFERRED_NAME = "Owner"
OWNER_EMAIL = "owner@tramline.app"
OWNER_PASSWORD = "why aroma enclose startup"

# developer constants
DEVELOPER_FULL_NAME = "Developer User"
DEVELOPER_PREFERRED_NAME = "Developer"
DEVELOPER_EMAIL = "developer@tramline.app"
DEVELOPER_PASSWORD = "why aroma enclose startup"
end
end
89 changes: 89 additions & 0 deletions lib/seed/dev_starter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# rubocop:disable Rails/Output

module Seed
class DevStarter
include Seed::Constants

def call
puts "Seeding database..."

ActiveRecord::Base.transaction do
self.class.send(:create_admin_user)
self.class.send(:create_owner_user)
self.class.send(:create_developer_user)
Comment on lines +11 to +13
Copy link
Member

@kitallis kitallis Feb 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue this needn't be called like this, we can just call it like self.class.create_admin_user and remove the private. But better yet, since this class doesn't have state yet, the point of the class is sort of very low value. Classes encapsulate state. But it may in the future, so I would recommend just do something like:

def self.call
  new.call
end

def call
  create_admin_user
  ..
end

And make all those class methods, instance methods (and you can keep them private).

end

puts "Completed seeding database"
end

def self.create_admin_user
email_authentication = Accounts::EmailAuthentication.find_or_initialize_by(email: ADMIN_EMAIL)
admin = true

unless email_authentication.persisted?
user = Accounts::User.find_or_create_by!(full_name: ADMIN_FULL_NAME, preferred_name: ADMIN_PREFERRED_NAME, admin:, unique_authn_id: ADMIN_EMAIL)
email_authentication.update!(password: ADMIN_PASSWORD, confirmed_at: DateTime.now, user:)
end

puts "Added/updated admin user."
end

def self.create_owner_user
email_authentication = Accounts::EmailAuthentication.find_or_initialize_by(email: OWNER_EMAIL)

if email_authentication.persisted?
user = email_authentication.user
else
user = Accounts::User.find_or_create_by!(full_name: OWNER_FULL_NAME, preferred_name: OWNER_PREFERRED_NAME, unique_authn_id: OWNER_EMAIL)
email_authentication.update!(password: OWNER_PASSWORD, confirmed_at: DateTime.now, user:)
email_authentication.reload
end

organization = Accounts::Organization.find_or_create_by!(
name: "Tramline Test 1 (Owner)",
status: Accounts::Organization.statuses[:active],
created_by: email_authentication.email
)

Accounts::Membership.find_or_create_by!(
user:,
organization:,
role: Accounts::Membership.roles[:owner]
)

puts "Added/updated owner user."
end

def self.create_developer_user
email_authentication = Accounts::EmailAuthentication.find_or_initialize_by(email: DEVELOPER_EMAIL)

if email_authentication.persisted?
user = email_authentication.user
else
user = Accounts::User.find_or_create_by!(full_name: DEVELOPER_FULL_NAME, preferred_name: DEVELOPER_PREFERRED_NAME, unique_authn_id: DEVELOPER_EMAIL)
email_authentication.update!(password: DEVELOPER_PASSWORD, confirmed_at: DateTime.now, user:)
email_authentication.reload
end

organization = Accounts::Organization.find_or_create_by!(
name: "Tramline Test 1 (Developer)",
status: Accounts::Organization.statuses[:active],
created_by: email_authentication.email
)

Accounts::Membership.find_or_create_by!(
user:,
organization:,
role: Accounts::Membership.roles[:developer]
)

puts "Added/updated developer user."
end

private_class_method :create_admin_user
private_class_method :create_owner_user
private_class_method :create_developer_user
end
end

# rubocop:enable Rails/Output
Loading