mirror of
https://github.com/discourse/discourse-solved.git
synced 2025-05-27 10:29:15 +00:00
PERF: Use UserAction to count accepted answers (#162)
The previous query which used custom fields performed a much more complex and slower search.
This commit is contained in:
parent
2a774f0d6f
commit
b558caff37
13
plugin.rb
13
plugin.rb
@ -687,12 +687,13 @@ SQL
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
add_to_serializer(:user_card, :accepted_answers) do
|
if defined?(UserAction::SOLVED)
|
||||||
Post
|
add_to_serializer(:user_card, :accepted_answers) do
|
||||||
.where(user_id: object.id)
|
UserAction
|
||||||
.joins(:_custom_fields)
|
.where(user_id: object.id)
|
||||||
.where(_custom_fields: { name: 'is_accepted_answer', value: 'true' })
|
.where(action_type: UserAction::SOLVED)
|
||||||
.count
|
.count
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if respond_to?(:register_topic_list_preload_user_ids)
|
if respond_to?(:register_topic_list_preload_user_ids)
|
||||||
|
@ -8,12 +8,15 @@ describe UserCardSerializer do
|
|||||||
let(:json) { serializer.as_json }
|
let(:json) { serializer.as_json }
|
||||||
|
|
||||||
it "accepted_answers serializes number of accepted answers" do
|
it "accepted_answers serializes number of accepted answers" do
|
||||||
post = Fabricate(:post, user: user)
|
post1 = Fabricate(:post, user: user)
|
||||||
post.upsert_custom_fields(is_accepted_answer: 'true')
|
DiscourseSolved.accept_answer!(post1, Discourse.system_user)
|
||||||
expect(serializer.as_json[:accepted_answers]).to eq(1)
|
expect(serializer.as_json[:accepted_answers]).to eq(1)
|
||||||
|
|
||||||
post = Fabricate(:post, user: user)
|
post2 = Fabricate(:post, user: user)
|
||||||
post.upsert_custom_fields(is_accepted_answer: 'true')
|
DiscourseSolved.accept_answer!(post2, Discourse.system_user)
|
||||||
expect(serializer.as_json[:accepted_answers]).to eq(2)
|
expect(serializer.as_json[:accepted_answers]).to eq(2)
|
||||||
|
|
||||||
|
DiscourseSolved.unaccept_answer!(post1)
|
||||||
|
expect(serializer.as_json[:accepted_answers]).to eq(1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user