-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Record delegated votes #67
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
578ee57
Extract VoteDelegation command out of monkey patch
sauloperez 990a1d9
Track vote records with PaperTrail
sauloperez f6f6bea
Track who performed the vote
sauloperez f06f3c6
Tiny refactor of conditional
sauloperez 37e5b5d
Add controller tests to vote destroy
sauloperez da3fd07
Store delegation_id in versions when available
sauloperez 1f7b14f
Track unvotes with PaperTrail
sauloperez 6fd66c1
Track who performed the unvote
sauloperez 653ddaa
Add query object to get versions of a consultation
sauloperez 053654b
Version which delegation caused the vote/unvote
sauloperez a0e45d5
Do not track regular votes
sauloperez 831bd17
Doc feature in README
sauloperez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 @@ | ||
# frozen_string_literal: true | ||
|
||
module Decidim | ||
module ActionDelegator | ||
class VoteDelegation | ||
def initialize(form) | ||
@context = form.context | ||
@response = form.response | ||
end | ||
|
||
def call | ||
PaperTrail.request.controller_info = { decidim_action_delegator_delegation_id: context.delegation.id } | ||
WhodunnitVote.new(build_vote, context.current_user) | ||
end | ||
|
||
private | ||
|
||
attr_reader :context, :response | ||
|
||
def build_vote | ||
context.current_question.votes.build( | ||
author: context.delegation.granter, | ||
response: response | ||
) | ||
end | ||
end | ||
end | ||
end |
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,13 @@ | ||
# frozen_string_literal: true | ||
|
||
module Decidim | ||
module ActionDelegator | ||
class UnversionedVote < SimpleDelegator | ||
def save | ||
PaperTrail.request(enabled: false) do | ||
super | ||
end | ||
end | ||
end | ||
end | ||
end |
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,22 @@ | ||
# frozen_string_literal: true | ||
|
||
module Decidim | ||
module ActionDelegator | ||
class WhodunnitVote < DelegateClass(Decidim::Consultations::Vote) | ||
def initialize(vote, user) | ||
@user = user | ||
super(vote) | ||
end | ||
|
||
def save | ||
PaperTrail.request(whodunnit: user.id) do | ||
super | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :user | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
Decidim::Consultations::Vote.class_eval do | ||
has_paper_trail | ||
end |
31 changes: 31 additions & 0 deletions
31
app/queries/decidim/action_delegator/delegated_votes_versions.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,31 @@ | ||
# frozen_string_literal: true | ||
|
||
module Decidim | ||
module ActionDelegator | ||
# Returns all PaperTrail versions of a consultation's delegated votes for auditing purposes. | ||
# It is intended to be used to easily fetch this data when a judge ask us so. | ||
class DelegatedVotesVersions | ||
def initialize(consultation) | ||
@consultation = consultation | ||
end | ||
|
||
def query | ||
statement = <<-SQL.strip_heredoc | ||
SELECT * | ||
FROM versions | ||
INNER JOIN decidim_action_delegator_delegations | ||
ON decidim_action_delegator_delegations.id = versions.decidim_action_delegator_delegation_id | ||
INNER JOIN decidim_action_delegator_settings | ||
ON decidim_action_delegator_settings.id = decidim_action_delegator_delegations.decidim_action_delegator_setting_id | ||
WHERE decidim_action_delegator_settings.decidim_consultation_id = #{consultation.id} | ||
SQL | ||
|
||
ActiveRecord::Base.connection.execute(statement).to_a | ||
end | ||
|
||
private | ||
|
||
attr_reader :consultation | ||
end | ||
end | ||
end |
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,8 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddDelegationIdToVersions < ActiveRecord::Migration[5.2] | ||
def change | ||
add_column :versions, :decidim_action_delegator_delegation_id, :integer, null: true, default: nil | ||
sauloperez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
add_index :versions, :decidim_action_delegator_delegation_id | ||
end | ||
end | ||
sauloperez marked this conversation as resolved.
Show resolved
Hide resolved
|
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
51 changes: 51 additions & 0 deletions
51
spec/commands/decidim/action_delegator/vote_delegation_spec.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,51 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
describe Decidim::ActionDelegator::VoteDelegation do | ||
subject { described_class.new(form) } | ||
|
||
let(:organization) { build(:organization) } | ||
let(:consultation) { build(:consultation, :active, organization: organization) } | ||
let(:setting) { build(:setting, consultation: consultation) } | ||
let(:delegation) { build(:delegation, setting: setting) } | ||
let(:question) { build(:question, :published, consultation: consultation) } | ||
let(:response) { build(:response, question: question) } | ||
|
||
let(:context) { double(:context, delegation: delegation, current_question: question, current_user: delegation.grantee) } | ||
let(:form) { instance_double(Decidim::Consultations::VoteForm, context: context, response: response) } | ||
|
||
describe "#call" do | ||
it "builds a vote with the granter as author" do | ||
vote = subject.call | ||
expect(vote.author).to eq(delegation.granter) | ||
end | ||
|
||
it "builds a vote with the response taken from the form" do | ||
vote = subject.call | ||
expect(vote.response).to eq(form.response) | ||
end | ||
|
||
it "builds a valid vote" do | ||
vote = subject.call | ||
expect(vote).to be_valid | ||
end | ||
|
||
it "tracks who performed the vote", versioning: true do | ||
vote = subject.call | ||
vote.save | ||
|
||
expect(vote.versions.last.whodunnit).to eq(context.current_user.id.to_s) | ||
end | ||
|
||
it "tracks the delegation the vote is related to", versioning: true do | ||
delegation.save | ||
question.save | ||
|
||
vote = subject.call | ||
vote.save | ||
|
||
expect(vote.versions.last.decidim_action_delegator_delegation_id).to eq(delegation.id) | ||
end | ||
end | ||
end |
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
67 changes: 67 additions & 0 deletions
67
spec/controllers/decidim/consultations/question_votes_controller_spec.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,67 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
module Decidim | ||
module Consultations | ||
describe QuestionVotesController, type: :controller do | ||
routes { Decidim::Consultations::Engine.routes } | ||
|
||
let(:organization) { create :organization } | ||
let(:user) { create(:user, :confirmed, organization: organization) } | ||
|
||
before do | ||
request.env["decidim.current_organization"] = organization | ||
sign_in user | ||
end | ||
|
||
describe "#destroy" do | ||
let(:consultation) { create(:consultation, organization: organization) } | ||
let(:question) { create(:question, consultation: consultation) } | ||
let(:setting) { create(:setting, consultation: consultation) } | ||
|
||
context "when a delegation is specified", versioning: true do | ||
let(:delegation) { create(:delegation, setting: setting, grantee: user) } | ||
let!(:vote) { create(:vote, author: delegation.granter, question: question) } | ||
|
||
it "destroys the vote" do | ||
delete :destroy, params: { question_slug: question.slug, decidim_consultations_delegation_id: delegation.id }, format: :js | ||
expect(response).to render_template(:update_vote_button) | ||
end | ||
|
||
it "creates a new version" do | ||
expect do | ||
delete :destroy, params: { question_slug: question.slug, decidim_consultations_delegation_id: delegation.id }, format: :js | ||
end.to change(PaperTrail::Version, :count) | ||
end | ||
|
||
it "tracks who performed the unvote" do | ||
delete :destroy, params: { question_slug: question.slug, decidim_consultations_delegation_id: delegation.id }, format: :js | ||
version = vote.versions.last | ||
expect(version.whodunnit).to eq(user.id.to_s) | ||
end | ||
|
||
it "tracks the delegation the unvote is related to" do | ||
delete :destroy, params: { question_slug: question.slug, decidim_consultations_delegation_id: delegation.id }, format: :js | ||
version = vote.versions.last | ||
expect(version.decidim_action_delegator_delegation_id).to eq(delegation.id) | ||
end | ||
end | ||
|
||
context "when no delegation is specified", versioning: true do | ||
before { create(:vote, author: user, question: question) } | ||
|
||
it "destroys the vote" do | ||
delete :destroy, params: { question_slug: question.slug }, format: :js | ||
expect(response).to render_template(:update_vote_button) | ||
end | ||
|
||
it "does not create a new version" do | ||
expect { delete :destroy, params: { question_slug: question.slug }, format: :js } | ||
.not_to change(PaperTrail::Version, :count) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
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
19 changes: 19 additions & 0 deletions
19
spec/models/decidim/action_delegator/unversioned_vote_spec.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,19 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
module Decidim | ||
module ActionDelegator | ||
describe UnversionedVote do | ||
subject(:unversioned_vote) { described_class.new(vote) } | ||
|
||
let(:vote) { build(:vote) } | ||
|
||
it "disables PaperTrail", versioning: true do | ||
subject.save | ||
|
||
expect(unversioned_vote.versions).to be_empty | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs serious refactoring extracting UnvoteDelegation to start with. We also should give it a crack at extracting the DelegationVotesController