New Exporter::OthersRelayables class

This class implements methods that allow to query relayables (comments, likes, participations,
poll_participations) of other people for posts of the given person.
This commit is contained in:
cmrd Senya 2017-04-02 20:18:04 +03:00
parent fca6121c6a
commit c985af1f85
No known key found for this signature in database
GPG key ID: 5FCC5BA680E67BFE
3 changed files with 78 additions and 0 deletions

View file

@ -6,6 +6,7 @@ class PollParticipation < ActiveRecord::Base
belongs_to :poll belongs_to :poll
belongs_to :poll_answer, counter_cache: :vote_count belongs_to :poll_answer, counter_cache: :vote_count
has_one :status_message, through: :poll
has_one :signature, class_name: "PollParticipationSignature", dependent: :delete has_one :signature, class_name: "PollParticipationSignature", dependent: :delete

View file

@ -0,0 +1,42 @@
module Diaspora
class Exporter
# This class implements methods that allow to query relayables (comments, likes, participations,
# poll_participations) of other people for posts of the given person.
class OthersRelayables
# @param person_id [Integer] Database id of a person for whom we want to request relayalbes
def initialize(person_id)
@person_id = person_id
end
# Comments of other people to the person's post
# @return [Comment::ActiveRecord_Relation]
def comments
Comment
.where.not(author_id: person_id)
.joins("INNER JOIN posts ON (commentable_type = 'Post' AND posts.id = commentable_id)")
.where("posts.author_id = ?", person_id)
end
# Likes of other people to the person's post
# @return [Like::ActiveRecord_Relation]
def likes
Like
.where.not(author_id: person_id)
.joins("INNER JOIN posts ON (target_type = 'Post' AND posts.id = target_id)")
.where("posts.author_id = ?", person_id)
end
# Poll participations of other people to the person's polls
# @return [PollParticipation::ActiveRecord_Relation]
def poll_participations
PollParticipation
.where.not(author_id: person_id).joins(:status_message)
.where("posts.author_id = ?", person_id)
end
private
attr_reader :person_id
end
end
end

View file

@ -0,0 +1,35 @@
describe Diaspora::Exporter::OthersRelayables do
let(:status_message) { FactoryGirl.create(:status_message) }
let(:person) { status_message.author }
let(:instance) { Diaspora::Exporter::OthersRelayables.new(person.id) }
describe "#comments" do
let(:comment) { FactoryGirl.create(:comment, post: status_message) }
it "has a comment in the data set" do
expect(instance.comments).to eq([comment])
end
end
describe "#likes" do
let(:like) { FactoryGirl.create(:like, target: status_message) }
it "has a like in the data set" do
expect(instance.likes).to eq([like])
end
end
describe "#poll_participations" do
let(:status_message) { FactoryGirl.create(:status_message_with_poll) }
let(:poll_participation) {
FactoryGirl.create(
:poll_participation,
poll_answer: status_message.poll.poll_answers.first
)
}
it "has a poll participation in the data set" do
expect(instance.poll_participations).to eq([poll_participation])
end
end
end