From 7fc1e2c5fcbfac6faf80477c41363f786b1cb06c Mon Sep 17 00:00:00 2001 From: junv Date: Tue, 6 Oct 2015 21:59:08 +0800 Subject: [PATCH] #4 Add user serializer --- app/controllers/api/v1/application_controller.rb | 2 +- app/controllers/api/v1/users_controller.rb | 13 +++++++++++++ app/controllers/application_controller.rb | 2 +- app/models/user.rb | 4 ++++ app/serializers/user_serializer.rb | 9 +++++++++ config/routes.rb | 2 ++ 6 files changed, 30 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v1/application_controller.rb b/app/controllers/api/v1/application_controller.rb index e03c127..c82fa55 100644 --- a/app/controllers/api/v1/application_controller.rb +++ b/app/controllers/api/v1/application_controller.rb @@ -3,7 +3,7 @@ class Api::V1::ApplicationController < ActionController::Base skip_before_action :verify_authenticity_token - serialization_scope :current_user! + serialization_scope :view_context before_action :current_user! before_action :authenticate_user_from_token diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb index ffecf4d..f44b6d9 100644 --- a/app/controllers/api/v1/users_controller.rb +++ b/app/controllers/api/v1/users_controller.rb @@ -1,8 +1,21 @@ class Api::V1::UsersController < Api::V1::ApplicationController + before_action :load_user, except: [:me] def show + render json: @user, serializer: UserSerializer + + end + + def me render json: @current_user end + + private + + def load_user + @user = User.find_by id: params[:id] + render json: {msg: '不能找到该用户'}, status: 404 unless @user + end end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index a0c6a28..90c13b8 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,7 +1,7 @@ class ApplicationController < ActionController::Base include ActionController::Serialization - serialization_scope :current_user! + serialization_scope :view_context before_action :current_user! def current_user! diff --git a/app/models/user.rb b/app/models/user.rb index 1521fde..85345b1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -21,6 +21,10 @@ class User < ActiveRecord::Base User.where(id: user_ids) end + def followers_ids + FollowRelation.where(star_id: self.id).pluck(:user_id) + end + def active_auth_token if auth_tokens.empty? generate_auth_key diff --git a/app/serializers/user_serializer.rb b/app/serializers/user_serializer.rb index fa31ae2..bcbe98c 100644 --- a/app/serializers/user_serializer.rb +++ b/app/serializers/user_serializer.rb @@ -1,4 +1,13 @@ class UserSerializer < ActiveModel::Serializer + attributes :id, :name, :email, :followed + + def followed + return true if object == scope.current_user + object.followers_ids.include? scope.current_user + end + + + end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index e621fe5..c3d1e65 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -78,6 +78,8 @@ Rails.application.routes.draw do get '/top/:type', to: 'dashboard#top' get '/top', to: 'dashboard#top_all' + get '/users/me', to: 'users#me' + resources :users, only:[:show] do resources :follow, only: [:create] post :unfollow, to: 'follow#destroy'