Skip to content

Commit d7451ac

Browse files
authored
Chapter 07 corrigé
1 parent c6d2448 commit d7451ac

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

rails6/en/chapter07-placing-orders.adoc

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
[#chapter07-placing-orders]
22
= Placing Orders
33

4-
Back in previous chapter we handle associations between the product and user models, and how to serialize them in order to scale fast and easy. Now it is time to start placing orders which is going to be a more complex situation. We will handle associations between three models and we have to be smart enough to handle the JSON output we are delivering.
4+
In previous chapter we handle associations between product and user models, and how to serialize them in order to scale fast and easy. Now it is time to start placing orders which is going to be a more complex situation. We will handle associations between three models and we have to be smart enough to handle the JSON output we are delivering.
55

66
In this chapter we will make several things which I list below:
77

88
* Create an `Order` model with its corresponding specs
99
* Handle JSON output association between the order user and product models
1010
* Send a confirmation email with the order summary
1111
12-
So now that we have everything clear, we can get our hands dirty. You can clone the project up to this point with:
12+
So now everything is clear, we can get our hands dirty. You can clone the project up to this point with:
1313

1414
[source,bash]
1515
----
@@ -24,7 +24,7 @@ $ git checkout -b chapter07
2424

2525
== Modeling order
2626

27-
If you remember associations model, the `Order` model is associated with users and products at the same time. It is actually really simply to achieve this in Rails. The tricky part is whens comes to serializing this objects. I talk about more about this in a next section.
27+
If you remember associations model, the `Order` model is associated with users and products at the same time. It is actually really simply to achieve this in Rails. The tricky part is when comes to serializing this objects. I'll talk more about this in a next section.
2828

2929
Let’s start by creating the order model, with a special form:
3030

@@ -79,8 +79,7 @@ class User < ApplicationRecord
7979
# ...
8080
end
8181
----
82-
83-
Les tests devraient passer:
82+
Tests should pass:
8483

8584
[source,bash]
8685
----
@@ -218,7 +217,7 @@ class Api::V1::OrdersControllerTest < ActionDispatch::IntegrationTest
218217
end
219218
----
220219

221-
If we run the test suite now, as you may expect, both tests will fail, because have not even set the correct routes, nor the action. So let’s start by adding the routes:
220+
If we run the test suite now, as you may expect, both tests will fail, because they have not even set the correct routes, nor the action. So let’s start by adding the routes:
222221

223222
[source,ruby]
224223
.config/routes.rb
@@ -287,7 +286,7 @@ $ git add . && git commit -m "Adds the show action for order"
287286

288287
=== Render a single order
289288

290-
As you can already imagine, this route is very easy. We only have to set up a few configurations (routes, controller action) and that will be all for this section. We will also include the products related to this order in the output JSON.
289+
As you can already imagine, this route is very easy. We only have to set up a few configurations (routes, controller action) and this section will be over. We will also include products related to this order in the output JSON.
291290

292291
Let's start by adding some tests:
293292

@@ -307,7 +306,7 @@ class Api::V1::OrdersControllerTest < ActionDispatch::IntegrationTest
307306
end
308307
----
309308

310-
As you can see, the second part of the test verifies that the product is included in the JSON.
309+
As you can see, the second part of the test verifies the product is included in the JSON.
311310

312311
Let's add the implementation to run our tests. On the `routes.rb` file add the `show` action to the command routes:
313312

@@ -324,7 +323,7 @@ Rails.application.routes.draw do
324323
end
325324
----
326325

327-
And the the implementation should look like this:
326+
And the implementation should look like this:
328327

329328
[source,ruby]
330329
.app/controllers/api/v1/orders_controller.rb
@@ -374,7 +373,7 @@ Before launching this feature, let's take the time to think about the implicatio
374373

375374
It seems like there's still a lot to do, but believe me: you're closer than you think and it's not as hard as it looks. For now, let's keep it simple and assume that we still have enough products to place any number of orders. We're just concerned about the server's response at the moment.
376375

377-
If you remember order model, we need three things: a total for the order, the user who places the order and the products for the order. Based on this information, we can start adding some tests:
376+
If you remember order model, we need three things: a total for the order, user who places the order and products for the order. Based on this information, we can start adding some tests:
378377

379378
[source,ruby]
380379
.test/controllers/api/v1/orders_controller_test.rb
@@ -462,9 +461,9 @@ $ rake test
462461
----
463462

464463

465-
Ok, so we have everything nice and green. We now should move on to the next chapter right? Let me stop you right there. We have some serious errors on the app, and they are not related to the code itself but on the business part.
464+
Ok, so we have everything nice and green. Now we should move on to the next chapter right? Let me stop you right there. We have some serious errors on the app, and they are not related to the code itself but on the business part.
466465

467-
Not because the tests are green, it means the app is filling the business part of the app. I wanted to bring this up because in many cases is super easy to just receive params and build objects from those params thinking that we are always receiving the correct data. In this particular case we cannot rely on that, and the easiest way to see this, is that we are letting the client to set the order total, yeah crazy!
466+
Not because the tests are green, it means the app is filling the business part of the app. I wanted to bring this up because in many cases that's super easy just receiving params and building objects from those params thinking that we are always receiving the correct data. In this particular case we cannot rely on that, and the easiest way to see this, is that we are letting the client to set the order total, yeah crazy!
468467

469468
We have to add some validations or a callback to calculate the order total an set it through the model. This way we don’t longer receive that total attribute and have complete control on this attribute. So let’s do that.
470469

@@ -611,7 +610,7 @@ class OrderMailer < ApplicationMailer
611610
end
612611
----
613612

614-
After adding this code, we must now add the corresponding views. It is a good practice to include a text version in addition to the HTML version.
613+
After adding this code, we must now add the corresponding views. It is a good practice including a text version in addition to the HTML version.
615614

616615

617616
[source,erb]
@@ -692,9 +691,9 @@ $ git merge chapter07
692691

693692
== Conclusion
694693

695-
That's it! That's it! You did it! You did it! You can applaud yourself. I know it's been a long time, but it's almost over, believe me.
694+
That's it! You did it! You can applaud yourself. I know it's been a long time, but it's almost over, believe me.
696695

697-
In future chapters, we will continue to work on the order template to add validations when placing an order. Some scenarios are:
696+
In next chapters, we will continue working on the order template to add validations when placing an order. Some scenarios are:
698697

699698
* What happens when products are not available?
700699
* Decrease the quantity of the product in progress when placing an order

0 commit comments

Comments
 (0)