## Day04 Week03 - Using AASM Status to Manage Orders

Today we began introducing order status management using AASM gem. This allowed for order status to be managed more efficiently and effectively.

In order to change order status, an event (keyword) must be invoked in the controller. The event is triggered by a post request from views. This completes the MVC loop.

How to add a new :post action:

1. Create post path in route and rake db:migrate
2. Define post action in controller, linking another "action!"
3. Create "action!" in model
4. Create button in views, linking to path in route, remember to add :method => :post

I also jot down some important notes and learning points that I found useful:

How to change AASM state manually in terminal console:

[recommend first install *gem awesome_railsconsole* to better view console results]_

1. See all orders by using Order.all
2. Find the one you want by using Order.find(#) with # = the ID number
3. Define a variable for this order AAA = Order.find(#)
4. Check the AASM state of the order AAA.aasm_state
5. Assign new AASM state (in this case we'll use "paid") AAA.aasm_state = "paid"
6. Save AAA.save! (Must add "!" here to force save into the Order, otherwise only the variable AAA's aasm state will be saved but not when you check again in Order)

Important Distinction Between = vs. ===

When I tried to build an if... end statement using = to check value, it didn't work. This is because = assigns value to a variable, not checking if there is an equality relationship. I changed this to === and it worked. This is a very juvenile mistake that I don't think will happen again.

Important Distinction Between Array and Single Variable

When I tried to pull the admin user's email address to send a notification email, the email pull could not work. In the console, I was told the "email" method was undefined.

So I checked order.user vs. User.where(is_admin: true):

Initially they looked the same to me. Then I noticed the [0] in front of the user details. I know that [ ] indicates an array, and 0 would be the position of the value in the array. So this is an array with just 1 value. Which meant that I had to specify which value in the array I wanted to call in order for the .email method to work. So I added .first at the end of the variable and pulled the email address successfully.

How to correctly use Roadie Rails automatically

I checked the gem roadie-rails website, for whatever reason they didn't mention how to include the stylesheet inside the email template.

This is a key step and without it the CSS would obviously not show up. For a more complete step-by-step simple installation, please see here.

I have completed everything in the development stage at this point. The next step will be deployment onto Heroku, which will require an email client (MailGun), a password manager (figaro), and an image host, in addition to a number of issues that may come up along the way. I now understand why it is critical to leave out 1/3 of the time for deployment and user testing.