Add Votes info to api

This commit is contained in:
Alberto Miedes Garcés
2016-12-29 12:03:00 +01:00
parent 80c3108a18
commit 90130f20e7
4 changed files with 73 additions and 11 deletions

View File

@@ -46,15 +46,17 @@ class User < ActiveRecord::Base
attr_accessor :skip_password_validation
attr_accessor :use_redeemable_code
scope :administrators, -> { joins(:administrators) }
scope :moderators, -> { joins(:moderator) }
scope :organizations, -> { joins(:organization) }
scope :officials, -> { where("official_level > 0") }
scope :for_render, -> { includes(:organization) }
scope :by_document, -> (document_type, document_number) { where(document_type: document_type, document_number: document_number) }
scope :email_digest, -> { where(email_digest: true) }
scope :active, -> { where(erased_at: nil) }
scope :administrators, -> { joins(:administrators) }
scope :moderators, -> { joins(:moderator) }
scope :organizations, -> { joins(:organization) }
scope :officials, -> { where("official_level > 0") }
scope :for_render, -> { includes(:organization) }
scope :by_document, -> (document_type, document_number) { where(document_type: document_type, document_number: document_number) }
scope :email_digest, -> { where(email_digest: true) }
scope :active, -> { where(erased_at: nil) }
scope :with_public_activity, -> { where(public_activity: true) }
scope :public_for_api, -> { select("id, username") }
scope :for_votes, -> { select("users.gender, users.geozone_id") }
before_validation :clean_document_number

View File

@@ -1,2 +1,10 @@
class Vote < ActsAsVotable::Vote
end
belongs_to :public_voter, -> { for_votes }, class_name: 'User', foreign_key: :voter_id
scope :public_for_api, -> do
joins("FULL OUTER JOIN debates ON votable_type = 'Debate' AND votable_id = debates.id").
joins("FULL OUTER JOIN proposals ON votable_type = 'Proposal' AND votable_id = proposals.id").
joins("FULL OUTER JOIN comments ON votable_type = 'Comment' AND votable_id = comments.id").
where("votable_type = 'Proposal' AND proposals.hidden_at IS NULL OR votable_type = 'Debate' AND debates.hidden_at IS NULL OR votable_type = 'Comment' AND comments.hidden_at IS NULL")
end
end

View File

@@ -1,11 +1,12 @@
API_TYPE_DEFINITIONS = {
User => %I[ id username proposals ],
User => %I[ id username gender geozone_id geozone ],
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 proposal_notifications public_author ],
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 ],
ProposalNotification => %I[ title body proposal_id created_at proposal ],
Tag => %I[ id name taggings_count kind ],
Vote => %I[ votable_id votable_type created_at public_voter ]
}
type_creator = GraphQL::TypeCreator.new

View File

@@ -40,4 +40,55 @@ describe 'Vote' do
expect(vote.value).to eq(false)
end
end
describe 'public_for_api scope' do
it 'returns votes on debates' do
debate = create(:debate)
vote = create(:vote, votable: debate)
expect(Vote.public_for_api).to include(vote)
end
it 'blocks votes on hidden debates' do
debate = create(:debate, :hidden)
vote = create(:vote, votable: debate)
expect(Vote.public_for_api).not_to include(vote)
end
it 'returns votes on proposals' do
proposal = create(:proposal)
vote = create(:vote, votable: proposal)
expect(Vote.public_for_api).to include(vote)
end
it 'blocks votes on hidden proposals' do
proposal = create(:proposal, :hidden)
vote = create(:vote, votable: proposal)
expect(Vote.public_for_api).not_to include(vote)
end
it 'returns votes on comments' do
comment = create(:comment)
vote = create(:vote, votable: comment)
expect(Vote.public_for_api).to include(vote)
end
it 'blocks votes on hidden comments' do
comment = create(:comment, :hidden)
vote = create(:vote, votable: comment)
expect(Vote.public_for_api).not_to include(vote)
end
it 'blocks any other kind of votes' do
spending_proposal = create(:spending_proposal)
vote = create(:vote, votable: spending_proposal)
expect(Vote.public_for_api).not_to include(vote)
end
end
end