Filter internal valuation comments from public api

Why:

Internal valuation comments are only for admins and valuators,
not for the public view.

How:

Adding a `not_valuations` scope and use it at the `public_for_api` one
This commit is contained in:
Bertocq
2018-01-29 21:53:38 +01:00
parent c84b2f0704
commit 56fc5c9583
3 changed files with 14 additions and 1 deletions

View File

@@ -33,7 +33,8 @@ class Comment < ActiveRecord::Base
end end
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 scope :public_for_api, -> do
where(%{(comments.commentable_type = 'Debate' and comments.commentable_id in (?)) or not_valuations
.where(%{(comments.commentable_type = 'Debate' and comments.commentable_id in (?)) or
(comments.commentable_type = 'Proposal' and comments.commentable_id in (?)) or (comments.commentable_type = 'Proposal' and comments.commentable_id in (?)) or
(comments.commentable_type = 'Poll' and comments.commentable_id in (?))}, (comments.commentable_type = 'Poll' and comments.commentable_id in (?))},
Debate.public_for_api.pluck(:id), Debate.public_for_api.pluck(:id),
@@ -50,6 +51,8 @@ class Comment < ActiveRecord::Base
scope :sort_by_oldest, -> { order(created_at: :asc) } scope :sort_by_oldest, -> { order(created_at: :asc) }
scope :sort_descendants_by_oldest, -> { order(created_at: :asc) } scope :sort_descendants_by_oldest, -> { order(created_at: :asc) }
scope :not_valuations, -> { where(valuation: false) }
after_create :call_after_commented after_create :call_after_commented
def self.build(commentable, user, body, p_id = nil) def self.build(commentable, user, body, p_id = nil)

View File

@@ -465,6 +465,10 @@ FactoryBot.define do
trait :with_confidence_score do trait :with_confidence_score do
before(:save) { |d| d.calculate_confidence_score } before(:save) { |d| d.calculate_confidence_score }
end end
trait :valuation do
valuation true
end
end end
factory :legacy_legislation do factory :legacy_legislation do

View File

@@ -187,5 +187,11 @@ describe Comment do
expect(described_class.public_for_api).not_to include(comment) expect(described_class.public_for_api).not_to include(comment)
end end
it "does not return internal valuation comments" do
valuation_comment = create(:comment, :valuation)
expect(described_class.public_for_api).not_to include(valuation_comment)
end
end end
end end