adds poll comments to API

This commit is contained in:
Juanjo Bazán
2017-11-21 14:31:03 +01:00
parent ae71f4960b
commit 21b2a11339
3 changed files with 32 additions and 4 deletions

View File

@@ -30,9 +30,11 @@ class Comment < ActiveRecord::Base
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 where(%{(comments.commentable_type = 'Debate' and comments.commentable_id in (?)) or
(comments.commentable_type = 'Proposal' and comments.commentable_id in (?))}, (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), Debate.public_for_api.pluck(:id),
Proposal.public_for_api.pluck(:id)) Proposal.public_for_api.pluck(:id),
Poll.public_for_api.pluck(:id))
end 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) }

View File

@@ -28,6 +28,7 @@ class Poll < ActiveRecord::Base
scope :recounting, -> { Poll.where(ends_at: (Date.current.beginning_of_day - RECOUNT_DURATION)..Date.current.beginning_of_day) } scope :recounting, -> { Poll.where(ends_at: (Date.current.beginning_of_day - RECOUNT_DURATION)..Date.current.beginning_of_day) }
scope :published, -> { where('published = ?', true) } scope :published, -> { where('published = ?', true) }
scope :by_geozone_id, ->(geozone_id) { where(geozones: {id: geozone_id}.joins(:geozones)) } scope :by_geozone_id, ->(geozone_id) { where(geozones: {id: geozone_id}.joins(:geozones)) }
scope :public_for_api, -> { all }
scope :sort_for_list, -> { order(:geozone_restricted, :starts_at, :name) } scope :sort_for_list, -> { order(:geozone_restricted, :starts_at, :name) }

View File

@@ -282,15 +282,16 @@ describe 'ConsulSchema' do
end end
describe 'Comments' do describe 'Comments' do
it 'only returns comments from proposals and debates' do it 'only returns comments from proposals, debates and polls' do
proposal_comment = create(:comment, commentable: create(:proposal)) proposal_comment = create(:comment, commentable: create(:proposal))
debate_comment = create(:comment, commentable: create(:debate)) debate_comment = create(:comment, commentable: create(:debate))
poll_comment = create(:comment, commentable: create(:poll))
spending_proposal_comment = build(:comment, commentable: create(:spending_proposal)).save(skip_validation: true) spending_proposal_comment = build(:comment, commentable: create(:spending_proposal)).save(skip_validation: true)
response = execute('{ comments { edges { node { commentable_type } } } }') response = execute('{ comments { edges { node { commentable_type } } } }')
received_commentables = extract_fields(response, 'comments', 'commentable_type') received_commentables = extract_fields(response, 'comments', 'commentable_type')
expect(received_commentables).to match_array ['Proposal', 'Debate'] expect(received_commentables).to match_array ['Proposal', 'Debate', 'Poll']
end end
it 'displays comments of authors even if public activity is set to false' do it 'displays comments of authors even if public activity is set to false' do
@@ -355,6 +356,19 @@ describe 'ConsulSchema' do
expect(received_comments).to match_array [visible_debate_comment.body] expect(received_comments).to match_array [visible_debate_comment.body]
end end
it 'does not include comments from hidden polls' do
visible_poll = create(:poll)
hidden_poll = create(:poll, hidden_at: Time.current)
visible_poll_comment = create(:comment, commentable: visible_poll)
hidden_poll_comment = create(:comment, commentable: hidden_poll)
response = execute('{ comments { edges { node { body } } } }')
received_comments = extract_fields(response, 'comments', 'body')
expect(received_comments).to match_array [visible_poll_comment.body]
end
it 'does not include comments of debates that are not public' do it 'does not include comments of debates that are not public' do
not_public_debate = create(:debate, :hidden) not_public_debate = create(:debate, :hidden)
not_public_debate_comment = create(:comment, commentable: not_public_debate) not_public_debate_comment = create(:comment, commentable: not_public_debate)
@@ -377,6 +391,17 @@ describe 'ConsulSchema' do
expect(received_comments).to_not include(not_public_proposal_comment.body) expect(received_comments).to_not include(not_public_proposal_comment.body)
end end
it 'does not include comments of polls that are not public' do
not_public_poll = create(:poll)
not_public_poll_comment = create(:comment, commentable: not_public_poll)
allow(Comment).to receive(:public_for_api).and_return([])
response = execute('{ comments { edges { node { body } } } }')
received_comments = extract_fields(response, 'comments', 'body')
expect(received_comments).to_not include(not_public_poll_comment.body)
end
it 'only returns date and hour for created_at' do it 'only returns date and hour for created_at' do
created_at = Time.zone.parse("2017-12-31 9:30:15") created_at = Time.zone.parse("2017-12-31 9:30:15")
create(:comment, created_at: created_at) create(:comment, created_at: created_at)