Sprint 3: Setting up databases with draft:resource and Devise.
Published: 2024-02-25 23:44:45

Now that we have a better understanding of setting up our RCAVs (route, controller, actions, view template), we can worry about handling the back-end and displaying our desired data. 

Luckily for us, we can utilize a gem called "draft_generator" in order to generate the necessary SQL commands to build our databases. This is quite easy as long as we have done the necessary preparation beforehand to know the desired tables we want to utilize. 

(However, we don't have to be perfect. We can add or remove columns as needed if we find flaws in our ERD (entity relationship diagram). Honestly, we can even nuke the whole database if needed - I had to do it a couple of times...)

Using draft:resource, this is how we set up our desired database(s). In the terminal, follow this command FORMAT:

Format:
```
rails generate draft:resource <DATABASE_NAME> <COLUMN_NAME_1>:<DATA_TYPE_1> <COLUMN_NAME_2>:<DATA_TYPE_2> <COLUMN_NAME_3>:<DATA_TYPE_3> ...
```

Example 1:
```
rails generate draft:resource delivery description:string supposted_to_arrive_on:date details:text user_id:integer arrived:boolean
```

Example 2:
```
rake db:migrate
```

What is happening?

1. In the example above, we are creating a database called "Deliveries" (plural since it is the database itself). We refer to the single instances (rows) as a "Delivery". 

2. The rest of the command line specifies the desired columns we want and the data-type it should be stored as. BUT...

3. ...that is not the best part. With this single command, it also auto-generates the RCAVs related to this database. While it is boilerplate code, as long as we've done our due diligence, it should be easy to understand the CRUD commands defined by the generator. 

4. Do NOT forget to input the command: db:migrate. Without it, the SQL commands necessary to generate this database is pending!

------------------------------------------------------------------

Using Devise to generate our Users database: allows User authentication.

If you haven't already, go ahead and use the command: 

```
rails generate devise:install
```

to install Devise. There will be a prompt on how to initialize this gem so make sure to follow the steps. (I usually do 1-3).

Afterwards, we can use this command:

```
rails generate devise user
```

What is happening?

1. This command generates our Users database. By default, this will create the SQL commands to make the id, email, and password columns for Users. 

2. In addition, this will create the RCAVs for User related functionality. (i.e. /users/sign_up, /users/sign_in, /users/edit, /users/delete). The functionality for these routes are hidden behind the scenes and we just get to take advantage of it! 

Therefore, if anyone wants to take advantage of our web application, they would first need to create their own email account to start CRUDing.

There are other generators we can take advantage of to automate this process. The important thing is to know what is happening with the boilerplate code it generates. By doing so, we are able to adapt our web application to our specific needs without dealing with the "formulaic" part of setting it up. NEAT.

CY@.