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:
parent
7db4f825a6
commit
fca6121c6a
3 changed files with 78 additions and 0 deletions
|
|
@ -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
|
||||
|
|
|
|||
54
lib/diaspora/exporter/posts_with_activity.rb
Normal file
54
lib/diaspora/exporter/posts_with_activity.rb
Normal 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
|
||||
23
spec/lib/diaspora/exporter/posts_with_activity_spec.rb
Normal file
23
spec/lib/diaspora/exporter/posts_with_activity_spec.rb
Normal 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
|
||||
Loading…
Reference in a new issue