From 98835efd1dfdeb27c097bb7fa7c1952dd2368713 Mon Sep 17 00:00:00 2001 From: ishowta Date: Fri, 26 Oct 2018 21:30:12 +0900 Subject: [PATCH] implement preload api --- app/controllers/api/v1/entries_controller.rb | 2 +- app/controllers/api/v1/ranking_controller.rb | 31 ++++++++++++-------- app/serializers/entry_serializer.rb | 4 +-- config/routes.rb | 1 + 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/app/controllers/api/v1/entries_controller.rb b/app/controllers/api/v1/entries_controller.rb index 72e7479..4f4424b 100644 --- a/app/controllers/api/v1/entries_controller.rb +++ b/app/controllers/api/v1/entries_controller.rb @@ -3,7 +3,7 @@ class Api::V1::EntriesController < ApplicationController # GET /entries/1 def show - render json: @entry, include: [ bookmarks: :user ] + render json: @entry, include: ['bookmarks', 'bookmarks.user'] end # PATCH/PUT /entries/1 diff --git a/app/controllers/api/v1/ranking_controller.rb b/app/controllers/api/v1/ranking_controller.rb index 1635a32..1415ced 100644 --- a/app/controllers/api/v1/ranking_controller.rb +++ b/app/controllers/api/v1/ranking_controller.rb @@ -1,22 +1,27 @@ class Api::V1::RankingController < ApplicationController - before_action :set_ranking, only: [:index] - - # GET /ranking + # GET /ranking/:page def index - render json: @ranking + render json: Entry + .page(ranking_params[:page]) + .includes(:bookmarks) + .each{|t| t[:num_of_bookmarked] = t.bookmarks.size}, + include: '', # this line means not include bookmarks + each_serializer: EntrySerializer end - private - # Use callbacks to share common setup or constraints between actions. - def set_ranking - @ranking = Entry.page(ranking_params[:page]) - @ranking.each do |entry| - entry[:num_of_bookmarked] = entry.count_bookmarks - end - end + # GET /ranking/:page/preload + def preload + render json: Entry + .page(ranking_params[:page]) + .includes(bookmarks: :user) + .each{|t| t[:num_of_bookmarked] = t.bookmarks.size}, + include: 'bookmarks.user', + each_serializer: EntrySerializer + end + private # Only allow a trusted parameter "white list" through. def ranking_params - params.fetch(:entry, {}).permit(:page) + params.permit(:page) end end diff --git a/app/serializers/entry_serializer.rb b/app/serializers/entry_serializer.rb index b618487..22f554f 100644 --- a/app/serializers/entry_serializer.rb +++ b/app/serializers/entry_serializer.rb @@ -1,4 +1,4 @@ class EntrySerializer < ActiveModel::Serializer - attributes :id, :url, :title, :thumbnail_url, :num_of_bookmarked, :bookmarks + attributes :id, :url, :title, :thumbnail_url, :num_of_bookmarked has_many :bookmarks -end \ No newline at end of file +end diff --git a/config/routes.rb b/config/routes.rb index 9b3f4d1..bf5e312 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,7 @@ resources :bookmarks resources :entries, :only => :show get "/ranking/:page", to: "ranking#index" + get "/ranking/:page/preload", to: "ranking#preload" post "/entries/:entry_id", to: "bookmarks#create_by_entry_id" mount_devise_token_auth_for 'User', at: 'auth',