Skip to content

Commit

Permalink
Merge pull request #208 from brycemcd/features/voting
Browse files Browse the repository at this point in the history
Allow authenticated user to vote/remove vote on card, add idMembersVoted to card
  • Loading branch information
Trung Lê committed Aug 11, 2016
2 parents 73e6e32 + 2952d38 commit 8c4a86d
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 1 deletion.
40 changes: 40 additions & 0 deletions lib/trello/card.rb
Expand Up @@ -219,6 +219,16 @@ def members
MultiAssociation.new(self, members).proxy
end

# Returns a list of members who have upvoted this card
# NOTE: this fetches a list each time it's called to avoid case where
# card is voted (or vote is removed) after card is fetched. Optimizing
# accuracy over network performance
#
# @return [Array<Trello::Member>]
def voters
Member.from_response client.get("/cards/#{id}/membersVoted")
end

# Saves a record.
#
# @raise [Trello::Error] if the card could not be saved
Expand Down Expand Up @@ -344,6 +354,30 @@ def remove_member(member)
client.delete("/cards/#{id}/members/#{member.id}")
end

# Current authenticated user upvotes a card
def upvote
begin
client.post("/cards/#{id}/membersVoted", {
value: me.id
})
rescue Trello::Error => e
fail e unless e.message =~ /has already voted/i
end

self
end

# Recind upvote. Noop if authenticated user hasn't previously voted
def remove_upvote
begin
client.delete("/cards/#{id}/membersVoted/#{me.id}")
rescue Trello::Error => e
fail e unless e.message =~ /has not voted/i
end

self
end

# Add a label
def add_label(label)
unless label.valid?
Expand Down Expand Up @@ -398,5 +432,11 @@ def request_prefix
def comments
comments = Comment.from_response client.get("/cards/#{id}/actions", filter: "commentCard")
end

private

def me
@me ||= Member.find(:me)
end
end
end
72 changes: 71 additions & 1 deletion spec/card_spec.rb
Expand Up @@ -94,7 +94,7 @@ module Trello

expect(card).to be_a Card
end

it 'creates a duplicate card with source due date and checklists and saves it on Trello', refactor: true do
payload = {
source_card_id: cards_details.first['id'],
Expand Down Expand Up @@ -402,6 +402,76 @@ module Trello
end
end

context "add/remove votes" do
let(:authenticated_member) { double(id: '4ee7df3ce582acdec80000b2') }

before do
allow(card)
.to receive(:me)
.and_return(authenticated_member)
end

it 'upvotes a card with the currently authenticated member' do
expect(client)
.to receive(:post)
.with("/cards/abcdef123456789123456789/membersVoted", {
value: authenticated_member.id
})

card.upvote
end

it 'returns the card even if the user has already upvoted' do
expect(client)
.to receive(:post)
.with("/cards/abcdef123456789123456789/membersVoted", {
value: authenticated_member.id
})
.and_raise(Trello::Error, 'member has already voted')
expect(card.upvote).to be_kind_of Trello::Card
end

it 'removes an upvote from a card' do
expect(client)
.to receive(:delete)
.with("/cards/abcdef123456789123456789/membersVoted/#{authenticated_member.id}")

card.remove_upvote
end

it 'returns card after remove_upvote even if the user has not previously upvoted it' do
expect(client)
.to receive(:delete)
.with("/cards/abcdef123456789123456789/membersVoted/#{authenticated_member.id}")
.and_raise(Trello::Error, 'member has not voted on the card')

card.remove_upvote
end

end

context "return all voters" do
it 'returns members that have voted for the card' do
no_voters = JSON.generate([])
expect(client)
.to receive(:get)
.with("/cards/#{card.id}/membersVoted")
.and_return(no_voters)

card.voters


voters = JSON.generate([user_details])
expect(client)
.to receive(:get)
.with("/cards/#{card.id}/membersVoted")
.and_return(voters)

expect(card.voters.first).to be_kind_of Trello::Member
end
end


context "comments" do
it "posts a comment" do
expect(client)
Expand Down

0 comments on commit 8c4a86d

Please sign in to comment.