From 4383339ddf5abd6dac48496fbeff66375c98ee06 Mon Sep 17 00:00:00 2001 From: Junv Zhao Date: Tue, 8 Dec 2015 22:55:10 +0800 Subject: [PATCH] #12 update the serialzier of search result --- app/controllers/api/v1/search_controller.rb | 4 ++-- app/models/post.rb | 5 +++++ app/models/user.rb | 4 ++++ app/serializers/em_source_serializer.rb | 1 - 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/v1/search_controller.rb b/app/controllers/api/v1/search_controller.rb index 32173aa..59458bc 100644 --- a/app/controllers/api/v1/search_controller.rb +++ b/app/controllers/api/v1/search_controller.rb @@ -4,9 +4,9 @@ class Api::V1::SearchController < ApplicationController def index #.pluck(:id, :title, :url, :type, :good_format, :summary) - posts = Post.where('title LIKE ?',"%#{params[:keywords]}%").limit(5) + posts = Post.select(Post.json_attrs).where('title LIKE ?',"%#{params[:keywords]}%").limit(5) #.pluck(:id, :email, :name, :avatar) - users = User.where('name LIKE ?', "%#{params[:keywords]}%").limit(5) + users = User.select(User.json_attrs).where('name LIKE ?', "%#{params[:keywords]}%").limit(5) render json: { posts: posts, users: users diff --git a/app/models/post.rb b/app/models/post.rb index 496b3cf..80fb4d5 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -19,4 +19,9 @@ class Post < ActiveRecord::Base def first_sharer share_instances.first.user_name end + + def self.json_attrs + [:id, :url, :title, :type, :summary] + end + end diff --git a/app/models/user.rb b/app/models/user.rb index 26388e6..b99a844 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -23,6 +23,10 @@ class User < ActiveRecord::Base acts_as_paranoid + def self.json_attrs + [:id, :email, :name] + end + def followers user_ids = FollowRelation.where(star_id: self.id).pluck(:user_id) User.where(id: user_ids) diff --git a/app/serializers/em_source_serializer.rb b/app/serializers/em_source_serializer.rb index 7161c52..2572124 100644 --- a/app/serializers/em_source_serializer.rb +++ b/app/serializers/em_source_serializer.rb @@ -1,4 +1,3 @@ - class EMSourceSerializer < ActiveModel::Serializer include Rails.application.routes.url_helpers