You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: rails6/en/chapter07-placing-orders.adoc
+14-15Lines changed: 14 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,15 +1,15 @@
1
1
[#chapter07-placing-orders]
2
2
= Placing Orders
3
3
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.
5
5
6
6
In this chapter we will make several things which I list below:
7
7
8
8
* Create an `Order` model with its corresponding specs
9
9
* Handle JSON output association between the order user and product models
10
10
* Send a confirmation email with the order summary
11
11
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:
13
13
14
14
[source,bash]
15
15
----
@@ -24,7 +24,7 @@ $ git checkout -b chapter07
24
24
25
25
== Modeling order
26
26
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.
28
28
29
29
Let’s start by creating the order model, with a special form:
30
30
@@ -79,8 +79,7 @@ class User < ApplicationRecord
79
79
# ...
80
80
end
81
81
----
82
-
83
-
Les tests devraient passer:
82
+
Tests should pass:
84
83
85
84
[source,bash]
86
85
----
@@ -218,7 +217,7 @@ class Api::V1::OrdersControllerTest < ActionDispatch::IntegrationTest
218
217
end
219
218
----
220
219
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:
222
221
223
222
[source,ruby]
224
223
.config/routes.rb
@@ -287,7 +286,7 @@ $ git add . && git commit -m "Adds the show action for order"
287
286
288
287
=== Render a single order
289
288
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.
291
290
292
291
Let's start by adding some tests:
293
292
@@ -307,7 +306,7 @@ class Api::V1::OrdersControllerTest < ActionDispatch::IntegrationTest
307
306
end
308
307
----
309
308
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.
311
310
312
311
Let's add the implementation to run our tests. On the `routes.rb` file add the `show` action to the command routes:
313
312
@@ -324,7 +323,7 @@ Rails.application.routes.draw do
324
323
end
325
324
----
326
325
327
-
And the the implementation should look like this:
326
+
And the implementation should look like this:
328
327
329
328
[source,ruby]
330
329
.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
374
373
375
374
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.
376
375
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:
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.
466
465
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!
468
467
469
468
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.
470
469
@@ -611,7 +610,7 @@ class OrderMailer < ApplicationMailer
611
610
end
612
611
----
613
612
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.
615
614
616
615
617
616
[source,erb]
@@ -692,9 +691,9 @@ $ git merge chapter07
692
691
693
692
== Conclusion
694
693
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.
696
695
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:
698
697
699
698
* What happens when products are not available?
700
699
* Decrease the quantity of the product in progress when placing an order
0 commit comments