Skip to content

Commit f19abf0

Browse files
authored
Merge pull request #1224 from codidact/0valt/1207/update-votes-tooltip
Dynamically update post score tooltip (#1207)
2 parents 8df636c + 8f111a3 commit f19abf0

4 files changed

Lines changed: 27 additions & 6 deletions

File tree

app/assets/javascripts/votes.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,29 @@ $(() => {
22
$(document).on('click', '.vote-button', async evt => {
33
const $tgt = $(evt.target).is('button') ? $(evt.target) : $(evt.target).parents('button');
44
const $post = $tgt.parents('.post');
5-
const $up = $post.find('.post--votes').find('.js-upvote-count');
6-
const $down = $post.find('.post--votes').find('.js-downvote-count');
5+
6+
const $container = $post.find(".post--votes");
7+
8+
const $up = $container.find('.js-upvote-count');
9+
const $down = $container.find('.js-downvote-count');
710
const voteType = $tgt.data('vote-type');
811
const voted = $tgt.hasClass('is-active');
912

1013
if (voted) {
1114
const voteId = $tgt.attr('data-vote-id');
15+
1216
const resp = await fetch(`/votes/${voteId}`, {
1317
method: 'DELETE',
1418
credentials: 'include',
1519
headers: { 'X-CSRF-Token': QPixel.csrfToken() }
1620
});
21+
1722
const data = await resp.json();
23+
1824
if (data.status === 'OK') {
1925
$up.text(`+${data.upvotes}`);
2026
$down.html(`−${data.downvotes}`);
27+
$container.attr("title", `Score: ${data.score}`);
2128
$tgt.removeClass('is-active')
2229
.removeAttr('data-vote-id');
2330
}
@@ -34,10 +41,13 @@ $(() => {
3441
headers: { 'Content-Type': 'application/json', 'X-CSRF-Token': QPixel.csrfToken() },
3542
body: JSON.stringify({post_id: $post.data('post-id'), vote_type: voteType})
3643
});
44+
3745
const data = await resp.json();
46+
3847
if (data.status === 'modified' || data.status === 'OK') {
3948
$up.text(`+${data.upvotes}`);
4049
$down.html(`−${data.downvotes}`);
50+
$container.attr("title", `Score: ${data.score}`);
4151
$tgt.addClass('is-active')
4252
.attr('data-vote-id', data.vote_id);
4353

app/controllers/votes_controller.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,11 @@ def create
4242
AbilityQueue.add(post.user, "Vote Change on ##{post.id}")
4343

4444
modified = !destroyed.empty?
45-
state = { status: (modified ? 'modified' : 'OK'), vote_id: vote.id, upvotes: post.upvote_count,
46-
downvotes: post.downvote_count }
45+
state = { status: (modified ? 'modified' : 'OK'),
46+
vote_id: vote.id,
47+
upvotes: post.upvote_count,
48+
downvotes: post.downvote_count,
49+
score: post.score }
4750

4851
render json: state
4952
end
@@ -59,7 +62,10 @@ def destroy
5962

6063
if vote.destroy
6164
AbilityQueue.add(post.user, "Vote Change on ##{post.id}")
62-
render json: { status: 'OK', upvotes: post.upvote_count, downvotes: post.downvote_count }
65+
render json: { status: 'OK',
66+
upvotes: post.upvote_count,
67+
downvotes: post.downvote_count,
68+
score: post.score }
6369
else
6470
render json: { status: 'failed', message: vote.errors.full_messages.join('. ') }, status: :forbidden
6571
end

app/models/post.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ def recalc_score
164164
sql = 'UPDATE posts SET score = (upvote_count + ?) / (upvote_count + downvote_count + (2 * ?)) WHERE id = ?'
165165
sanitized = ActiveRecord::Base.sanitize_sql_array([sql, variable, variable, id])
166166
ActiveRecord::Base.connection.execute sanitized
167+
168+
# ensures the updated score is immediately available
169+
self.score = (upvote_count + variable).to_f / (upvote_count + downvote_count + (2 * variable))
170+
# prevents AR from accidentally saving the dirty state
171+
clear_attribute_changes([:score])
167172
end
168173

169174
# This method will update the locked status of this post if locked_until is in the past.

app/views/posts/_expanded.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
<% if post_type.has_votes || (user_signed_in? && post.post_type.has_reactions && post.post_type.reactions.any?) %>
4040
<div>
4141
<% if post_type.has_votes %>
42-
<div class="post--votes has-text-align-center" title="Score : <%= post.score %>">
42+
<div class="post--votes has-text-align-center" title="Score: <%= post.score %>">
4343
<% existing_vote = my_vote(post) %>
4444
<% unless post.locked? %>
4545
<button class="vote-button button is-icon-only-button <%= (existing_vote&.vote_type == 1) ? 'is-active' : '' %>"

0 commit comments

Comments
 (0)