From 90ff84b6efb4db78d18b74b10f84fbd5c6971089 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Miedes=20Garc=C3=A9s?= Date: Tue, 27 Dec 2016 18:18:22 +0100 Subject: [PATCH] Update API fields for Comment --- app/models/comment.rb | 7 ++++++ config/initializers/graphql.rb | 2 +- spec/models/comment_spec.rb | 41 ++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/app/models/comment.rb b/app/models/comment.rb index 0bfb6a320..a1d89a33f 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -16,6 +16,7 @@ class Comment < ActiveRecord::Base belongs_to :commentable, -> { with_hidden }, polymorphic: true, counter_cache: true belongs_to :user, -> { with_hidden } + belongs_to :public_author, -> { with_public_activity }, class_name: 'User', foreign_key: 'user_id' before_save :calculate_confidence_score @@ -24,6 +25,12 @@ class Comment < ActiveRecord::Base scope :not_as_admin_or_moderator, -> { where("administrator_id IS NULL").where("moderator_id IS NULL")} scope :sort_by_flags, -> { order(flags_count: :desc, updated_at: :desc) } + scope :public_for_api, -> do + joins("FULL OUTER JOIN debates ON commentable_type = 'Debate' AND commentable_id = debates.id"). + joins("FULL OUTER JOIN proposals ON commentable_type = 'Proposal' AND commentable_id = proposals.id"). + where("commentable_type = 'Proposal' AND proposals.hidden_at IS NULL OR commentable_type = 'Debate' AND debates.hidden_at IS NULL") + end + scope :sort_by_most_voted, -> { order(confidence_score: :desc, created_at: :desc) } scope :sort_descendants_by_most_voted, -> { order(confidence_score: :desc, created_at: :asc) } diff --git a/config/initializers/graphql.rb b/config/initializers/graphql.rb index bf5d8d8f9..b6d98562b 100644 --- a/config/initializers/graphql.rb +++ b/config/initializers/graphql.rb @@ -2,7 +2,7 @@ API_TYPE_DEFINITIONS = { User => %I[ id username proposals ], Debate => %I[ id title description created_at cached_votes_total cached_votes_up cached_votes_down comments_count hot_score confidence_score geozone_id geozone comments public_author ], Proposal => %I[ id title description external_url cached_votes_up comments_count hot_score confidence_score created_at summary video_url geozone_id retired_at retired_reason retired_explanation geozone comments public_author ], - Comment => %I[ id body user_id user commentable_id ], + Comment => %I[ id commentable_id commentable_type body created_at cached_votes_total cached_votes_up cached_votes_down ancestry confidence_score public_author ], Geozone => %I[ id name ] } diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index b899b4e89..ecd167237 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -132,4 +132,45 @@ describe Comment do end end + describe "public_for_api" do + it "returns comments" do + comment = create(:comment) + + expect(Comment.public_for_api).to include(comment) + end + + it "does not return hidden comments" do + hidden_comment = create(:comment, :hidden) + + expect(Comment.public_for_api).not_to include(hidden_comment) + end + + it "returns comments on debates" do + debate = create(:debate) + comment = create(:comment, commentable: debate) + + expect(Comment.public_for_api).to include(comment) + end + + it "does not return comments on hidden debates" do + hidden_debate = create(:debate, :hidden) + comment = create(:comment, commentable: hidden_debate) + + expect(Comment.public_for_api).not_to include(comment) + end + + it "returns comments on proposals" do + proposal = create(:proposal) + comment = create(:comment, commentable: proposal) + + expect(Comment.public_for_api).to include(comment) + end + + it "does not return comments on hidden proposals" do + hidden_proposal = create(:proposal, :hidden) + comment = create(:comment, commentable: hidden_proposal) + + expect(Comment.public_for_api).not_to include(comment) + end + end end