This repository has been archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from enpitut2018/feature/preload
implement #44 backend API
- Loading branch information
Showing
4 changed files
with
22 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,29 @@ | ||
class Api::V1::TrendController < ApplicationController | ||
before_action :set_trend, only: [:index] | ||
before_action :set_trend, only: [:index, :preload] | ||
|
||
# GET /trend | ||
# GET /trend/:page | ||
def index | ||
render json: @trend | ||
#p @trend | ||
render json: @trend, include: '' | ||
end | ||
|
||
# GET /trend/:page/preload | ||
def preload | ||
render json: @trend, include: 'bookmarks.user' | ||
end | ||
|
||
private | ||
# Use callbacks to share common setup or constraints between actions. | ||
def set_trend | ||
|
||
Entry.find_each do |entry| | ||
entry[:num_of_bookmarked] = entry.count_bookmarks | ||
end | ||
@trend=Entry.all.reorder!(num_of_bookmarked: :desc) | ||
@trend = Entry | ||
.page(trend_params[:page]) | ||
.includes(bookmarks: :user) | ||
.each{|e| e[:num_of_bookmarked] = e.bookmarks.size} | ||
.sort_by(&:num_of_bookmarked) | ||
.reverse | ||
end | ||
|
||
# Only allow a trusted parameter "white list" through. | ||
#def trend_params | ||
# params.fetch(:entry, {}).permit(:page) | ||
#end | ||
def trend_params | ||
params.permit(:page) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters