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

script to create a user #27

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Check out a live demo at [linkarooie.com](https://linkarooie.com).

### Prerequisites

- Ruby 3.2.2
- Ruby 3.3.0 or higher
- Rails 7.1.3 or higher
- SQLite3
- Node.js and npm (for asset compilation)
Expand Down Expand Up @@ -60,6 +60,50 @@ Check out a live demo at [linkarooie.com](https://linkarooie.com).

5. Visit `http://localhost:3000` in your browser.

### Creating a New User

This section explains how to create a new user using an interactive Ruby script. This method allows you to quickly add users to your Rails application with the required and optional attributes.

#### Using the Interactive Ruby Script

The Ruby script offers a user-friendly and interactive way to create a new user by prompting you for the required and optional details.

##### Usage

Run the script by navigating to your Rails project directory and executing the following command:

```bash
ruby create_user.rb
```

You will be prompted to enter various details for the new user:

```bash
Enter email: [email protected]
Enter password: AnotherSuperSecretPassword
Enter username (or leave blank to use email prefix): bob_j
Enter full name: Bob Johnson
Enter tags (comma-separated): Marketing,SEO,Content Strategy
Enter avatar URL: https://pbs.twimg.com/profile_images/1756873036220059648/zc13kjbX_400x400.jpg
Enter banner URL: https://pbs.twimg.com/profile_banners/1192091185/1719830949/1500x500
Enter description: Digital marketing expert focused on driving growth through innovative SEO and content strategies.
```

##### Example

The details entered above will create a user with the following attributes:

* **Email:** `[email protected]`
* **Password:** `AnotherSuperSecretPassword`
* **Username:** `bob_j`
* **Full Name:** `Bob Johnson`
* **Tags:** `Marketing`, `SEO`, `Content Strategy`
* **Avatar:** Same as provided
* **Banner:** Same as provided
* **Description:** "Digital marketing expert focused on driving growth through innovative SEO and content strategies."

After entering all the details, the script will create the user and confirm whether the process was successful.

### Docker Deployment

1. Build and start the Docker containers:
Expand Down
37 changes: 37 additions & 0 deletions create_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# create_user.rb

require_relative 'config/environment'

def prompt(message)
print "#{message}: "
gets.chomp
end

email = prompt("Enter email")
password = prompt("Enter password")
username = prompt("Enter username (or leave blank to use email prefix)")
full_name = prompt("Enter full name")
tags = prompt("Enter tags (comma-separated)")
avatar = prompt("Enter avatar URL")
banner = prompt("Enter banner URL")
description = prompt("Enter description")

username = email.split('@').first if username.strip.empty?
tags = tags.split(',').map(&:strip).to_json unless tags.strip.empty?

user = User.new(
email: email,
password: password,
username: username,
full_name: full_name,
tags: tags || '[]',
avatar: avatar,
banner: banner,
description: description
)

if user.save
puts "User #{user.username} created successfully."
else
puts "Error creating user: #{user.errors.full_messages.join(', ')}"
end
Loading