Skip to content

Commit

Permalink
Filter internal valuation comments from public api
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bertocq committed Jan 30, 2018
1 parent de615a5 commit 09a6174
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
5 changes: 4 additions & 1 deletion app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class Comment < ActiveRecord::Base
end
scope :sort_by_flags, -> { order(flags_count: :desc, updated_at: :desc) }
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 = 'Poll' and comments.commentable_id in (?))},
Debate.public_for_api.pluck(:id),
Expand All @@ -50,6 +51,8 @@ class Comment < ActiveRecord::Base
scope :sort_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

def self.build(commentable, user, body, p_id = nil)
Expand Down
4 changes: 4 additions & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,10 @@
trait :with_confidence_score do
before(:save) { |d| d.calculate_confidence_score }
end

trait :valuation do
valuation true
end
end

factory :legacy_legislation do
Expand Down
6 changes: 6 additions & 0 deletions spec/models/comment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,11 @@

expect(described_class.public_for_api).not_to include(comment)
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

0 comments on commit 09a6174

Please sign in to comment.