Skip to content
Snippets Groups Projects
Commit 97745c37 authored by Israel Barreto Sant'Anna's avatar Israel Barreto Sant'Anna
Browse files

Implemented FeedController

parent 892e997f
No related branches found
No related tags found
No related merge requests found
......@@ -3,7 +3,8 @@ module Paginator
def paginate (model)
set_header model
model.limit(limit).offset(offset)
return model.limit(limit).offset(offset) if model.respond_to?('limit')
return model[offset..limit]
end
private
......
class V1::FeedController < ApplicationController
include ::Paginator
before_action :authenticate_user!
# GET v1/feed
# GET v1/feed.json
# Render all activities that logged user can see
def index
activities = paginate activities_followed
render json: activities
end
private
#TODO: Tests
def activities_followed
activities = []
types.each do |type|
model = type.classify.constantize
current_user.watching(type).each do |follow|
followed = model.find(follow.followable_id)
activities.push(*followed.activities.to_a)
end
end
p "--- BEGIN ---"
p activities
p "--- END ---"
activities
end
def types
['User', 'Collection']
end
end
......@@ -10,4 +10,11 @@ module Followable
Follow.where(followable: self)
end
## get all activities from user
# return an array of PublicActivity::Activity
def activities
condition = '(owner_type = :type AND owner_id = :id) OR (recipient_type = :type AND recipient_id = :id)'
PublicActivity::Activity.order('created_at DESC').where(condition, {type: self.class.to_s, id: self.id}).all
end
end
......@@ -138,13 +138,6 @@ class User < ApplicationRecord
# ~~~~ end followable actions ~~~~ #
## get all activities from user
# return an array of PublicActivity::Activity
def activities
condition = '(owner_type = :type AND owner_id = :id) OR (recipient_type = :type AND recipient_id = :id)'
PublicActivity::Activity.order('created_at DESC').where(condition, {type: self.class.to_s, id: self.id}).all
end
private
def default_role
......
......@@ -61,6 +61,7 @@ Rails.application.routes.draw do
namespace :v1 do
resources :activities, only: :index
resources :feed, only: [:index]
resources :users, concerns: [:followable, :deletable] do
member do
......
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