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/chapter05-user-products.adoc
+15-17Lines changed: 15 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,13 @@
1
1
[#chapter05-user-products]
2
2
= User products
3
3
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.
5
5
6
6
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 //.
7
7
8
8
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_.
9
9
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.
11
11
12
12
You can clone the project up to this point:
13
13
@@ -44,7 +44,7 @@ Running via Spring preloader in process 1476
44
44
----
45
45
46
46
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.
48
48
49
49
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.
rails test test/controllers/api/v1/users_controller_test.rb:43
89
89
----
90
90
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.
92
92
93
93
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:
94
94
@@ -104,7 +104,7 @@ one:
104
104
# ...
105
105
----
106
106
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.
108
108
109
109
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:
110
110
@@ -160,6 +160,8 @@ As we saw with the user, validations are an important part when building any kin
160
160
161
161
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.
162
162
163
+
//pas compris !
164
+
163
165
[source,ruby]
164
166
.test/models/product_test.rb
165
167
----
@@ -244,7 +246,7 @@ class Api::V1::ProductsControllerTest < ActionDispatch::IntegrationTest
244
246
end
245
247
----
246
248
247
-
We then add the code to make the test pass:
249
+
Then we add the code to make the test pass:
248
250
249
251
[source,ruby]
250
252
.app/controllers/api/v1/products_controller.rb
@@ -339,7 +341,7 @@ Rails.application.routes.draw do
339
341
end
340
342
----
341
343
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.
343
345
344
346
[source,bash]
345
347
----
@@ -452,7 +454,7 @@ $ rake test
452
454
453
455
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.
454
456
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:
456
458
457
459
[source,ruby]
458
460
.config/routes.rb
@@ -500,10 +502,6 @@ end
500
502
501
503
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.
502
504
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
-
507
505
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.
508
506
509
507
Now let’s implement the code to make our tests pass:
@@ -605,7 +603,7 @@ end
605
603
----
606
604
607
605
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:
609
607
610
608
[source,ruby]
611
609
.app/controllers/api/v1/products_controller.rb
@@ -626,7 +624,7 @@ class Api::V1::ProductsController < ApplicationController
626
624
end
627
625
----
628
626
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.
630
628
631
629
[source,bash]
632
630
----
@@ -747,9 +745,9 @@ Let's _commit_ changes:
747
745
$ git commit -am "Create a seed to populate database"
748
746
----
749
747
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_:
751
749
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:
753
751
[source,bash]
754
752
----
755
753
$ git checkout master
@@ -758,7 +756,7 @@ $ git merge chapter05
758
756
759
757
== Conclusion
760
758
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.
762
760
763
761
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.
0 commit comments