Skip to content

Commit

Permalink
refactor: return boolean 'live' instead of response_count
Browse files Browse the repository at this point in the history
  • Loading branch information
m-rios committed Mar 15, 2022
1 parent 4bda1af commit 3c1a4fb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions app/serializers/api/questionnaire_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ class QuestionnaireSerializer < ActiveModel::Serializer
:key,
:name,
:content,
:response_count
:live

def response_count
object.responses.count
def live
object.responses.count.positive?
end
end
end
12 changes: 6 additions & 6 deletions spec/serializers/api/questionnaire_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@
describe 'renders the correct json' do
it 'contains the correct variables' do
expect(json).not_to be_nil
expect(json.keys).to eq(%w[title key name content response_count])
expect(json.keys).to eq(%w[title key name content live])
expect(json[:title]).to eq questionnaire.title
expect(json[:key]).to eq questionnaire.key
expect(json[:name]).to eq questionnaire.name
expect(json[:content].as_json).to eq questionnaire.content.as_json
end

describe 'response_count' do
describe 'live' do
context 'when the questionnaire has no responses' do
it 'returns 0' do
expect(json['response_count']).to eq 0
it 'returns false' do
expect(json['live']).to be_falsey
end
end

context 'when the questionnaire has responses' do
let(:measurement) { FactoryBot.create :measurement, questionnaire: questionnaire }
let!(:response) { FactoryBot.create_list :response, 2, measurement: measurement }
it 'returns the correct number of responses' do
expect(json['response_count']).to eq 2
it 'returns true' do
expect(json['live']).to be_truthy
end
end
end
Expand Down

0 comments on commit 3c1a4fb

Please sign in to comment.