Skip to content

Commit 0a1045e

Browse files
franklinjosmellmadeindjs
authored andcommitted
Adds some corrections in chapter 06 (#26)
1 parent f3f94f8 commit 0a1045e

File tree

1 file changed

+12
-22
lines changed

1 file changed

+12
-22
lines changed

rails6/en/chapter06-improve-json.adoc

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ The content of the `data` key is also quite strict:
5858
* properties of the objects must be placed in an `attributes` key
5959
* links of the objects must be placed in a `relationships` key
6060

61-
In this chapter we will customize the JSON output using Netflix's gem: https://github.com/Netflix/fast_jsonapi_jsonapi[fast_jsonapi]. Luckily for use they already implements all https://jsonapi.org/[JSON:API] specifications.
61+
In this chapter we will customize the JSON output using Netflix's gem: https://github.com/Netflix/fast_jsonapi[fast_jsonapi]. Luckily for use they already implements all https://jsonapi.org/[JSON:API] specifications.
6262

6363
So let's install the gem `fast_jsonapi`:
6464

@@ -202,7 +202,7 @@ end
202202
There you go. It's no more complicated than that. Let's change our controller a little bit.
203203

204204
[source,ruby]
205-
.test/controllers/api/v1/products_controller_test.rb
205+
.app/controllers/api/v1/products_controller.rb
206206
----
207207
class Api::V1::ProductsController < ApplicationController
208208
# ...
@@ -238,7 +238,7 @@ end
238238
And we're updating our functional test:
239239

240240
[source,ruby]
241-
.app/controllers/api/v1/products_controller.rb
241+
.test/controllers/api/v1/products_controller_test.rb
242242
----
243243
# ...
244244
class Api::V1::ProductsControllerTest < ActionDispatch::IntegrationTest
@@ -559,17 +559,6 @@ There you go. Now this is what the JSON should look like:
559559
}
560560
----
561561

562-
Implementation is very simple: just add a line to the product serializer.
563-
564-
[source,ruby]
565-
.app/serializers/product_serializer.rb
566-
----
567-
class ProductSerializer < ActiveModel::Serializer
568-
attributes :id, :title, :price, :published
569-
has_one :user
570-
end
571-
----
572-
573562
Now all tests should pass:
574563

575564
[source,bash]
@@ -582,9 +571,10 @@ Let's make a _commit_ to celebrate:
582571

583572
[source,bash]
584573
----
585-
$ git commit -am "Add user relationship to product"
574+
$ git commit -am "Add user relationship to product serializer"
586575
----
587576

577+
<<<
588578

589579
=== Retrieve user's products
590580

@@ -694,7 +684,7 @@ $ git commit -am "Add products relationship to user#show"
694684

695685
In this last section we will continue to strengthen the `Products#index` action by setting up a very simple search mechanism allowing any customer to filter the results. This section is optional as it will have no impact on the application modules. But if you want to practice more with the TDD I recommend that you to complete this last step.
696686

697-
I use https://github.com/activerecord-hackery/ransack[Ransack] or https://github.com/casecommons/pg_search_search[pg_search] to build advanced search forms extremely quickly. But since the goal is learning and searching we are going to do is very simple. I think we can build a search engine from scratch. We simply have to consider the criteria by which we will filter the attributes. Hang on to your seats it's going to be a tough trip.
687+
I use https://github.com/activerecord-hackery/ransack[Ransack] or https://github.com/casecommons/pg_search[pg_search] to build advanced search forms extremely quickly. But since the goal is learning and searching we are going to do is very simple. I think we can build a search engine from scratch. We simply have to consider the criteria by which we will filter the attributes. Hang on to your seats it's going to be a tough trip.
698688

699689
We will therefore filter the products according to the following criteria:
700690

@@ -741,11 +731,11 @@ And now we can build some tests:
741731
class ProductTest < ActiveSupport::TestCase
742732
# ...
743733
test "should filter products by name" do
744-
assert_equal 2, Product.filter_by_title('TV').count
734+
assert_equal 2, Product.filter_by_title('tv').count
745735
end
746736
747737
test 'should filter products by name and sort them' do
748-
assert_equal [products(:another_tv), products(:one)], Product.filter_by_title('TV').sort
738+
assert_equal [products(:another_tv), products(:one)], Product.filter_by_title('tv').sort
749739
end
750740
end
751741
----
@@ -915,12 +905,12 @@ class ProductTest < ActiveSupport::TestCase
915905
assert Product.search(search_hash).empty?
916906
end
917907
918-
test 'search should fin cheap TV' do
908+
test 'search should find cheap TV' do
919909
search_hash = { keyword: 'tv', min_price: 50, max_price: 150 }
920910
assert_equal [products(:another_tv)], Product.search(search_hash)
921911
end
922912
923-
test 'should get all product when no parameters' do
913+
test 'should get all products when no parameters' do
924914
assert_equal Product.all.to_a, Product.search({})
925915
end
926916
@@ -939,12 +929,12 @@ We have added a lot of code but I assure you that the implementation is very eas
939929
class Product < ApplicationRecord
940930
# ...
941931
def self.search(params = {})
942-
products = params[:product_ids].present? ? Product.find(params[:product_ids]) : Product.all
932+
products = params[:product_ids].present? ? Product.where(id: params[:product_ids]) : Product.all
943933
944934
products = products.filter_by_title(params[:keyword]) if params[:keyword]
945935
products = products.above_or_equal_to_price(params[:min_price].to_f) if params[:min_price]
946936
products = products.below_or_equal_to_price(params[:max_price].to_f) if params[:max_price]
947-
products = products.recent(params[:recent]) if params[:recent].present?
937+
products = products.recent if params[:recent]
948938
949939
products
950940
end

0 commit comments

Comments
 (0)