Exporter::PostsWithActivity class

This class allows to query posts where a person made any activity (submitted comments,
likes, participations or poll participations).
This commit is contained in:
cmrd Senya 2017-04-02 19:37:47 +03:00
parent 7db4f825a6
commit fca6121c6a
No known key found for this signature in database
GPG key ID: 5FCC5BA680E67BFE
3 changed files with 78 additions and 0 deletions

View file

@ -19,6 +19,7 @@ class StatusMessage < Post
has_one :location
has_one :poll, autosave: true
has_many :poll_participations, through: :poll
attr_accessor :oembed_url
attr_accessor :open_graph_url

View file

@ -0,0 +1,54 @@
module Diaspora
class Exporter
# This class allows to query posts where a person made any activity (submitted comments,
# likes, participations or poll participations).
class PostsWithActivity
# TODO: docs
def initialize(user)
@user = user
end
# TODO: docs
def query
Post.from("(#{sql_union_all_activities}) AS posts")
end
private
attr_reader :user
def person
user.person
end
def sql_union_all_activities
all_activities.map(&:to_sql).join(" UNION ")
end
def all_activities
[comments_activity, likes_activity, subscriptions, polls_activity].compact
end
def likes_activity
other_people_posts.liked_by(person)
end
def comments_activity
other_people_posts.commented_by(person)
end
def subscriptions
other_people_posts.subscribed_by(user)
end
def polls_activity
StatusMessage.where.not(author_id: person.id).joins(:poll_participations)
.where(poll_participations: {author_id: person.id})
end
def other_people_posts
Post.where.not(author_id: person.id)
end
end
end
end

View file

@ -0,0 +1,23 @@
describe Diaspora::Exporter::PostsWithActivity do
let(:user) { FactoryGirl.create(:user) }
let(:instance) { Diaspora::Exporter::PostsWithActivity.new(user) }
describe "#query" do
let(:activity) {
[
user.person.likes.first.target,
user.person.comments.first.parent,
user.person.poll_participations.first.parent.status_message,
user.person.participations.first.target
]
}
before do
DataGenerator.create(user, %i[activity participation])
end
it "returns all posts with person's activity" do
expect(instance.query).to match_array(activity)
end
end
end