Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from angusmcleod/vote_count_to_custom_fields
Store vote count, vote user and vote history in post custom fields && Integrate custom storage into a functionality consistent vote post action
- Loading branch information
Showing
13 changed files
with
487 additions
and
118 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
assets/javascripts/discourse/connectors/category-custom-settings/enable-qa.js.es6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default { | ||
setupComponent(attrs) { | ||
if (!attrs.category.custom_fields) { | ||
attrs.category.custom_fields = {}; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { popupAjaxError } from 'discourse/lib/ajax-error'; | ||
import { ajax } from 'discourse/lib/ajax'; | ||
|
||
const voteActionId = 100; | ||
|
||
const vote = function(type, data) { | ||
return ajax('/qa/vote', { | ||
type, | ||
data | ||
}).catch(popupAjaxError) | ||
} | ||
|
||
const undoVote = function(data) { | ||
return vote('DELETE', data); | ||
} | ||
|
||
const castVote = function(data) { | ||
return vote('POST', data); | ||
} | ||
|
||
const whoVoted = function(data) { | ||
return ajax('/qa/voters', { | ||
type: 'GET', | ||
data | ||
}).catch(popupAjaxError) | ||
} | ||
|
||
export { undoVote, castVote, voteActionId, whoVoted }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
db/migrate/20180712004204_save_existing_post_vote_counts_to_custom_fields.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
class SaveExistingPostVoteCountsToCustomFields < ActiveRecord::Migration[5.2] | ||
def up | ||
vote_totals = {} | ||
|
||
PostAction.where(post_action_type_id: 5).each do |action| | ||
if post = Post.find_by(id: action[:post_id]) | ||
votes = post.vote_history | ||
|
||
votes.push( | ||
"direction": QuestionAnswer::Vote::UP, | ||
"action": QuestionAnswer::Vote::CREATE, | ||
"user_id": action[:user_id].to_s, | ||
"created_at": action[:created_at] | ||
) | ||
|
||
post.custom_fields['vote_history'] = votes.to_json | ||
post.save_custom_fields(true) | ||
end | ||
|
||
total = vote_totals[action[:post_id]] | ||
total = { count: 0, voted: [] } if total == nil | ||
|
||
total[:count] += 1 | ||
|
||
voted = total[:voted] | ||
voted.push(action[:user_id]) | ||
total[:voted] = voted | ||
|
||
vote_totals[action[:post_id]] = total | ||
end | ||
|
||
if vote_totals.any? | ||
vote_totals.each do |k, v| | ||
if post = Post.find_by(id: k) | ||
post.custom_fields['vote_history'] = post.vote_history.to_json | ||
post.custom_fields['vote_count'] = v[:count].to_i | ||
post.custom_fields['voted'] = v[:voted] | ||
post.save_custom_fields(true) | ||
end | ||
end | ||
end | ||
end | ||
|
||
def down | ||
raise ActiveRecord::IrreversibleMigration | ||
end | ||
end |
Oops, something went wrong.