API: return own vote state in polls
This commit is contained in:
parent
b921b71b97
commit
8cae234f45
3 changed files with 22 additions and 10 deletions
|
|
@ -1,18 +1,19 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class PollPresenter < BasePresenter
|
class PollPresenter < BasePresenter
|
||||||
def initialize(poll, participant_user=nil)
|
def initialize(poll, current_user=nil)
|
||||||
@poll = poll
|
super(poll, current_user)
|
||||||
@user = participant_user
|
|
||||||
|
@participation = participation_answer(current_user) if current_user
|
||||||
end
|
end
|
||||||
|
|
||||||
def as_api_json
|
def as_api_json
|
||||||
{
|
{
|
||||||
guid: @poll.guid,
|
guid: guid,
|
||||||
participation_count: @poll.participation_count,
|
participation_count: participation_count,
|
||||||
question: @poll.question,
|
question: question,
|
||||||
already_participated: @user && @poll.participation_answer(@user) ? true : false,
|
already_participated: @participation.present?,
|
||||||
poll_answers: answers_collection_as_api_json(@poll.poll_answers)
|
poll_answers: answers_collection_as_api_json(poll_answers)
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -23,10 +24,12 @@ class PollPresenter < BasePresenter
|
||||||
end
|
end
|
||||||
|
|
||||||
def answer_as_api_json(answer)
|
def answer_as_api_json(answer)
|
||||||
{
|
base = {
|
||||||
id: answer.id,
|
id: answer.id,
|
||||||
answer: answer.answer,
|
answer: answer.answer,
|
||||||
vote_count: answer.vote_count
|
vote_count: answer.vote_count
|
||||||
}
|
}
|
||||||
|
base[:own_answer] = @participation.try(:poll_answer_id) == answer.id if current_user
|
||||||
|
base
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -295,7 +295,8 @@
|
||||||
"properties": {
|
"properties": {
|
||||||
"id": { "type": "integer" },
|
"id": { "type": "integer" },
|
||||||
"answer": { "type": "string" },
|
"answer": { "type": "string" },
|
||||||
"vote_count": { "type": "integer" }
|
"vote_count": { "type": "integer" },
|
||||||
|
"own_answer": { "type": "boolean" }
|
||||||
},
|
},
|
||||||
"required": ["id", "answer", "vote_count"],
|
"required": ["id", "answer", "vote_count"],
|
||||||
"additionalProperties": false
|
"additionalProperties": false
|
||||||
|
|
|
||||||
|
|
@ -13,12 +13,20 @@ describe PollPresenter do
|
||||||
it "works with user" do
|
it "works with user" do
|
||||||
presenter = PollPresenter.new(poll, alice)
|
presenter = PollPresenter.new(poll, alice)
|
||||||
confirm_poll_api_json_format(presenter.as_api_json, 0, false)
|
confirm_poll_api_json_format(presenter.as_api_json, 0, false)
|
||||||
|
expect(presenter.as_api_json[:poll_answers]).to all(include(own_answer: false))
|
||||||
|
|
||||||
poll.poll_participations.create(poll_answer: poll_answer, author: alice.person)
|
poll.poll_participations.create(poll_answer: poll_answer, author: alice.person)
|
||||||
|
presenter = PollPresenter.new(poll, alice)
|
||||||
confirm_poll_api_json_format(presenter.as_api_json, 1, true)
|
confirm_poll_api_json_format(presenter.as_api_json, 1, true)
|
||||||
|
expect(presenter.as_api_json[:poll_answers]).to include(include(id: poll_answer.id, own_answer: true))
|
||||||
|
|
||||||
presenter = PollPresenter.new(poll, eve)
|
presenter = PollPresenter.new(poll, eve)
|
||||||
confirm_poll_api_json_format(presenter.as_api_json, 1, false)
|
confirm_poll_api_json_format(presenter.as_api_json, 1, false)
|
||||||
|
expect(presenter.as_api_json[:poll_answers]).to all(include(own_answer: false))
|
||||||
|
|
||||||
presenter = PollPresenter.new(poll)
|
presenter = PollPresenter.new(poll)
|
||||||
confirm_poll_api_json_format(presenter.as_api_json, 1, false)
|
confirm_poll_api_json_format(presenter.as_api_json, 1, false)
|
||||||
|
expect(presenter.as_api_json[:poll_answers]).to_not include(include(:own_answer))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue