From c985af1f85b6fb38794c6ecdd245a736561588f3 Mon Sep 17 00:00:00 2001 From: cmrd Senya Date: Sun, 2 Apr 2017 20:18:04 +0300 Subject: [PATCH] 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. --- app/models/poll_participation.rb | 1 + lib/diaspora/exporter/others_relayables.rb | 42 +++++++++++++++++++ .../exporter/others_relayables_spec.rb | 35 ++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 lib/diaspora/exporter/others_relayables.rb create mode 100644 spec/lib/diaspora/exporter/others_relayables_spec.rb diff --git a/app/models/poll_participation.rb b/app/models/poll_participation.rb index 8dba27465..7e653bb65 100644 --- a/app/models/poll_participation.rb +++ b/app/models/poll_participation.rb @@ -6,6 +6,7 @@ class PollParticipation < ActiveRecord::Base belongs_to :poll belongs_to :poll_answer, counter_cache: :vote_count + has_one :status_message, through: :poll has_one :signature, class_name: "PollParticipationSignature", dependent: :delete diff --git a/lib/diaspora/exporter/others_relayables.rb b/lib/diaspora/exporter/others_relayables.rb new file mode 100644 index 000000000..5876c52cd --- /dev/null +++ b/lib/diaspora/exporter/others_relayables.rb @@ -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 diff --git a/spec/lib/diaspora/exporter/others_relayables_spec.rb b/spec/lib/diaspora/exporter/others_relayables_spec.rb new file mode 100644 index 000000000..95595739f --- /dev/null +++ b/spec/lib/diaspora/exporter/others_relayables_spec.rb @@ -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