From f20274f44aac8bf3207320b8686b020a33450507 Mon Sep 17 00:00:00 2001 From: Bianca Nenciu Date: Thu, 26 Aug 2021 16:16:22 +0300 Subject: [PATCH] FIX: Correctly serialize accepted_answers (#158) --- plugin.rb | 2 +- spec/serializers/user_card_serializer_spec.rb | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 spec/serializers/user_card_serializer_spec.rb diff --git a/plugin.rb b/plugin.rb index 75d3d92..6f714e5 100644 --- a/plugin.rb +++ b/plugin.rb @@ -689,7 +689,7 @@ SQL add_to_serializer(:user_card, :accepted_answers) do Post - .where(user: User.find_by_username('bar3')) + .where(user_id: object.id) .joins(:_custom_fields) .where(_custom_fields: { name: 'is_accepted_answer', value: 'true' }) .count diff --git a/spec/serializers/user_card_serializer_spec.rb b/spec/serializers/user_card_serializer_spec.rb new file mode 100644 index 0000000..56eafcd --- /dev/null +++ b/spec/serializers/user_card_serializer_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe UserCardSerializer do + let(:user) { Fabricate(:user) } + let(:serializer) { described_class.new(user, scope: Guardian.new, root: false) } + let(:json) { serializer.as_json } + + it "accepted_answers serializes number of accepted answers" do + post = Fabricate(:post, user: user) + post.upsert_custom_fields(is_accepted_answer: 'true') + expect(serializer.as_json[:accepted_answers]).to eq(1) + + post = Fabricate(:post, user: user) + post.upsert_custom_fields(is_accepted_answer: 'true') + expect(serializer.as_json[:accepted_answers]).to eq(2) + end +end