From 56945d9bbef41ebf7cccc69c393cefe3ecbb5e2d Mon Sep 17 00:00:00 2001 From: rgarcia Date: Fri, 15 Dec 2017 19:21:02 +0100 Subject: [PATCH] allows querying for a proposal's votes --- app/models/vote.rb | 12 ------------ config/initializers/vote_extensions.rb | 11 +++++++++++ spec/lib/graphql_spec.rb | 10 ++++++++++ 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/app/models/vote.rb b/app/models/vote.rb index 14b11a68d..626d71879 100644 --- a/app/models/vote.rb +++ b/app/models/vote.rb @@ -1,14 +1,2 @@ class Vote < ActsAsVotable::Vote - - include Graphqlable - - scope :public_for_api, -> do - where(%{(votes.votable_type = 'Debate' and votes.votable_id in (?)) or - (votes.votable_type = 'Proposal' and votes.votable_id in (?)) or - (votes.votable_type = 'Comment' and votes.votable_id in (?))}, - Debate.public_for_api.pluck(:id), - Proposal.public_for_api.pluck(:id), - Comment.public_for_api.pluck(:id)) - end - end diff --git a/config/initializers/vote_extensions.rb b/config/initializers/vote_extensions.rb index effec12ea..4df338752 100644 --- a/config/initializers/vote_extensions.rb +++ b/config/initializers/vote_extensions.rb @@ -1,6 +1,17 @@ ActsAsVotable::Vote.class_eval do + include Graphqlable + belongs_to :signature + scope :public_for_api, -> do + where(%{(votes.votable_type = 'Debate' and votes.votable_id in (?)) or + (votes.votable_type = 'Proposal' and votes.votable_id in (?)) or + (votes.votable_type = 'Comment' and votes.votable_id in (?))}, + Debate.public_for_api.pluck(:id), + Proposal.public_for_api.pluck(:id), + Comment.public_for_api.pluck(:id)) + end + def self.for_debates(debates) where(votable_type: 'Debate', votable_id: debates) end diff --git a/spec/lib/graphql_spec.rb b/spec/lib/graphql_spec.rb index 4f03e0f6a..ce9cd9052 100644 --- a/spec/lib/graphql_spec.rb +++ b/spec/lib/graphql_spec.rb @@ -218,6 +218,16 @@ describe 'ConsulSchema' do expect(received_tags).to match_array ['Parks', 'Health'] end + it 'returns nested votes for a proposal' do + proposal = create(:proposal) + 2.times { create(:vote, votable: proposal) } + + response = execute("{ proposal(id: #{proposal.id}) { votes_for { edges { node { public_created_at } } } } }") + + votes = response["data"]["proposal"]["votes_for"]["edges"] + expect(votes.count).to eq(2) + end + end describe 'Debates' do