Skip to content

Commit bd602a5

Browse files
authored
Chapter 05 corrigé Lorène
1 parent 82f4526 commit bd602a5

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

rails6/en/chapter05-user-products.adoc

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
[#chapter05-user-products]
22
= User products
33

4-
In the previous chapter, we implemented the authentication mechanism that we will use throughout the application.
4+
In the previous chapter, we implemented the authentication mechanism we will use throughout the application.
55

66
For the moment we have a very simple implementation of the `User` model but the moment of truth has come. We will customize the JSON output and add a second resource: the user's products. These are the elements that the user will sell in the application and therefore will be directly linked //.
77

88
If you are familiar with Rails, you may already know what I am talking about. But for those who don't know, we will associate the `User` model with the `Product` model using the `has_many` and `belongs_to` methods of _Active Record_.
99

10-
In this chapter, we will build the `Product` model from scratch, associate it with the user and create the necessary entries so that any customer can access the information.
10+
In this chapter, we will build the `Product` model from scratch, associate it with the user and create the necessary entries so any customer can access the information.
1111

1212
You can clone the project up to this point:
1313

@@ -44,7 +44,7 @@ Running via Spring preloader in process 1476
4444
----
4545

4646

47-
As you can see, we used the `belongs_to` type for the attribute. This is a shortcut that will create a `user_id` column of type `int` and also add a foreign key to the `users.id` field.
47+
As you can see, we used the `belongs_to` type for the attribute. This is a shortcut that will create an `user_id` column of type `int` and also add a foreign key to the `users.id` field.
4848

4949
In addition, `user_id` will also be defined as an `index`. This is a good practice for association keys because it optimizes database queries. It is not mandatory, but I highly recommend it.
5050

@@ -88,7 +88,7 @@ ActiveRecord::InvalidForeignKey: SQLite3::ConstraintException: FOREIGN KEY const
8888
rails test test/controllers/api/v1/users_controller_test.rb:43
8989
----
9090

91-
You certainly said "What?! But I didn't touch the users!". What I have seen in the code of other developers, when they work with associations, is that they forget about destroying dependencies between models. What I mean by that is that if a user is deleted, so should the user's products.
91+
You certainly said "What?! But I didn't touch the users!". What I have seen in the code of other developers, when they work with associations, is that they forget about destroying dependencies between models. What I mean by that is if a user is deleted, so should the user's products.
9292

9393
So to test this interaction between the models, we need a user with one of the products. Then, we will delete this user in the hope that the products will disappear with him. Rails has already generated this for us. Take a look at the _fixture_ of the products:
9494

@@ -104,7 +104,7 @@ one:
104104
# ...
105105
----
106106

107-
You can see that this _fixture_ does not use the attribute `user_id` but `user`. This means that the `one` product will have an `user_id` attribute corresponding to the `one` user ID.
107+
You can see this _fixture_ does not use the attribute `user_id` but `user`. This means that the `one` product will have an `user_id` attribute corresponding to the `one` user ID.
108108

109109
It is therefore necessary to specify a cascading deletion in order to delete the `one` product when the `one` user is deleted. Let's start with the unit test:
110110

@@ -160,6 +160,8 @@ As we saw with the user, validations are an important part when building any kin
160160

161161
Also an important thing about validation when working with associations, is in this case to validate that every product has a user, so in this case we need to validate the presence of the `user_id`. You can see what I’m talking about in next code snippet.
162162

163+
//pas compris !
164+
163165
[source,ruby]
164166
.test/models/product_test.rb
165167
----
@@ -244,7 +246,7 @@ class Api::V1::ProductsControllerTest < ActionDispatch::IntegrationTest
244246
end
245247
----
246248

247-
We then add the code to make the test pass:
249+
Then we add the code to make the test pass:
248250

249251
[source,ruby]
250252
.app/controllers/api/v1/products_controller.rb
@@ -339,7 +341,7 @@ Rails.application.routes.draw do
339341
end
340342
----
341343

342-
We are done for now with the public product endpoints. In the sections to come we will focus on building the actions that require a user to be logged in to access them. Said that we are committing this changes and continue.
344+
We are done for now with the public product endpoints. In the next sections we will focus on building the actions requiring a user to be logged in to access them. Said that we are committing this changes and continue.
343345

344346
[source,bash]
345347
----
@@ -452,7 +454,7 @@ $ rake test
452454
453455
Hopefully by now you understand the logic to build the upcoming actions, in this section we will focus on the `update` action, which will work similarly to the `create` one, we just need to fetch the product from the database and the update it.
454456
455-
We are first add the action to the routes, so we don’t forget later:
457+
We are first adding the action to the routes, so we don’t forget later:
456458
457459
[source,ruby]
458460
.config/routes.rb
@@ -500,10 +502,6 @@ end
500502
501503
NOTE: I have added a _fixture_ corresponding to a second user in order to verify that the second user cannot modify the first user's product.
502504
503-
The tests may seem complex, but if you look at them, they are almost identical to those of the users.
504-
505-
Now let's implement the code to pass our tests successfully:
506-
507505
The tests may look complex but take a second peek. They are almost the same we built for users. The only difference here is the nested routes as we saw on previous section, which in this case we need to send the `user_id` as a parameter.
508506
509507
Now let’s implement the code to make our tests pass:
@@ -605,7 +603,7 @@ end
605603
----
606604
607605
608-
Now we simply add the necessary code to make the tests pass:
606+
Now we simply add the necessary code to make tests pass:
609607
610608
[source,ruby]
611609
.app/controllers/api/v1/products_controller.rb
@@ -626,7 +624,7 @@ class Api::V1::ProductsController < ApplicationController
626624
end
627625
----
628626
629-
As you can see the three-line implementation does the job. We can run the tests to make sure everything is good and after that we will commit the changes as we added a bunch of new code. Also make sure you hook this action to the `before_action` callback as with the `update` action.
627+
As you can see the three-line implementation does the job. We can run tests to make sure everything is good and then we will commit the changes as we added a bunch of new code. Also make sure you hook this action to the `before_action` callback as with the `update` action.
630628
631629
[source,bash]
632630
----
@@ -747,9 +745,9 @@ Let's _commit_ changes:
747745
$ git commit -am "Create a seed to populate database"
748746
----
749747
750-
And as we get to the end of our chapter, it is time to apply all our modifications to the master branch by making a _merge_:
748+
And as we get to the end of our chapter, it's time to apply all our modifications to the master branch by making a _merge_:
751749
752-
We then create a bunch of product objects with the `FactoryBot` gem:
750+
Then we create a bunch of product objects with the `FactoryBot` gem:
753751
[source,bash]
754752
----
755753
$ git checkout master
@@ -758,7 +756,7 @@ $ git merge chapter05
758756
759757
== Conclusion
760758
761-
I hope you have enjoyed this chapter. It is a long one but the code we put together is an excellent base for the core app.
759+
I hope you have enjoyed this chapter. It's a long one but the code we put together is an excellent base for the core app.
762760
763761
In the next chapter, we will focus on customizing the output of user and product models using the gem https://github.com/Netflix/fast_jsonapi_jsonapi[fast_jsonapi]. It will allow us to easily filter the attributes to display and manage associations such as embedded objects for example.
764762

0 commit comments

Comments
 (0)