Skip to content
Snippets Groups Projects
Commit 1d8fdb3c authored by Mateus Rambo Strey's avatar Mateus Rambo Strey
Browse files

Merge branch 'list-following' into 'master'

feat(List following): get what user is following



`(Pode apagar a branch)`

See merge request !257
parents 3b5f48b0 58e56fcd
No related branches found
No related tags found
No related merge requests found
...@@ -28,5 +28,4 @@ module FollowableController ...@@ -28,5 +28,4 @@ module FollowableController
render nothing: true, status: :forbidden render nothing: true, status: :forbidden
end end
end end
end
end
\ No newline at end of file
class V1::UsersController < ApplicationController class V1::UsersController < ApplicationController
include ::FollowableController include ::FollowableController
before_action :set_user, only: [:show, :update, :destroy] before_action :set_user, only: [:show, :update, :destroy, :watching]
before_action :authenticate_user!, only: [:create, :update, :destroy] before_action :authenticate_user!, only: [:create, :update, :destroy, :watching]
# GET /v1/users # GET /v1/users
# GET /v1/users.json # GET /v1/users.json
...@@ -50,6 +50,17 @@ class V1::UsersController < ApplicationController ...@@ -50,6 +50,17 @@ class V1::UsersController < ApplicationController
end end
end end
def watching
type = params[:object_type]
is_current = (@user.id == current_user.id) unless current_user.nil?
render nothing: true, status: :bad_request unless type.in? %w(User Collection)
w = @user.watching(type, is_current)
render json: w, root: 'follows', status: :ok
end
private private
def followable def followable
...@@ -64,5 +75,4 @@ class V1::UsersController < ApplicationController ...@@ -64,5 +75,4 @@ class V1::UsersController < ApplicationController
def user_params def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation, :terms_of_service, role_ids: []) params.require(:user).permit(:name, :email, :password, :password_confirmation, :terms_of_service, role_ids: [])
end end
end end
...@@ -119,6 +119,25 @@ class User < ActiveRecord::Base ...@@ -119,6 +119,25 @@ class User < ActiveRecord::Base
!follows.where(followable_id: followable.id, followable_type: followable.class.name).blank? !follows.where(followable_id: followable.id, followable_type: followable.class.name).blank?
end end
# This function permits see what person is following.
# For current user, list all users and all collections. But,
# if, isn't current user, list only users, and public collections which this person follows.
def watching(followable_type, is_current_user = true)
f = Follow.where(user_id: id, followable_type: followable_type).all
# If type collection, and isn't current_user, so should list only public collections
if followable_type.to_s == 'Collection' && !is_current_user
f = Follow.joins('INNER JOIN collections ON follows.followable_id = collections.id').where(
"follows.user_id = #{id} " \
'AND follows.followable_type = \'Collection\' ' \
'AND collections.id = follows.followable_id ' \
'AND collections.privacy = \'public\''
)
end
f
end
# ~~~~ end followable actions ~~~~ # # ~~~~ end followable actions ~~~~ #
private private
......
class FollowSerializer < ActiveModel::Serializer
has_one :followable
attributes :id, :user_id
end
...@@ -42,6 +42,8 @@ Rails.application.routes.draw do ...@@ -42,6 +42,8 @@ Rails.application.routes.draw do
resources :users, concerns: :followable do resources :users, concerns: :followable do
member do member do
resources :bookmarks, module: 'users', only: [:index, :create, :destroy] resources :bookmarks, module: 'users', only: [:index, :create, :destroy]
get 'watching/:object_type', to: 'users#watching'
end end
end end
get :search, to: 'search#index' get :search, to: 'search#index'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment