Update API fields for Comment

This commit is contained in:
Alberto Miedes Garcés
2016-12-27 18:18:22 +01:00
parent 0e501e2edd
commit 90ff84b6ef
3 changed files with 49 additions and 1 deletions

View File

@@ -16,6 +16,7 @@ class Comment < ActiveRecord::Base
belongs_to :commentable, -> { with_hidden }, polymorphic: true, counter_cache: true belongs_to :commentable, -> { with_hidden }, polymorphic: true, counter_cache: true
belongs_to :user, -> { with_hidden } belongs_to :user, -> { with_hidden }
belongs_to :public_author, -> { with_public_activity }, class_name: 'User', foreign_key: 'user_id'
before_save :calculate_confidence_score 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 :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 :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_by_most_voted, -> { order(confidence_score: :desc, created_at: :desc) }
scope :sort_descendants_by_most_voted, -> { order(confidence_score: :desc, created_at: :asc) } scope :sort_descendants_by_most_voted, -> { order(confidence_score: :desc, created_at: :asc) }

View File

@@ -2,7 +2,7 @@ API_TYPE_DEFINITIONS = {
User => %I[ id username proposals ], 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 ], 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 ], 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 ] Geozone => %I[ id name ]
} }

View File

@@ -132,4 +132,45 @@ describe Comment do
end end
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 end