Skip to content
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

Fix duplicated endorsements #11853

Merged
merged 7 commits into from Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions decidim-core/app/commands/decidim/endorse_resource.rb
Expand Up @@ -31,6 +31,8 @@ def call
else
broadcast(:invalid)
end
rescue ActiveRecord::RecordNotUnique
broadcast(:invalid)
end

private
Expand Down
2 changes: 1 addition & 1 deletion decidim-core/app/commands/decidim/unendorse_resource.rb
Expand Up @@ -31,7 +31,7 @@ def destroy_resource_endorsement
query = if @current_group.present?
@resource.endorsements.where(decidim_user_group_id: @current_group&.id)
else
@resource.endorsements.where(author: @current_user, decidim_user_group_id: nil)
@resource.endorsements.where(author: @current_user, decidim_user_group_id: 0)
end
query.destroy_all
end
Expand Down
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class ChangeDefaultValueForDecidimEndorsements < ActiveRecord::Migration[6.1]
def up
change_column_default :decidim_endorsements, :decidim_user_group_id, 0
end

def down
change_column_default :decidim_endorsements, :decidim_user_group_id, nil
end
end
2 changes: 1 addition & 1 deletion decidim-core/lib/decidim/endorsable.rb
Expand Up @@ -21,7 +21,7 @@ def endorsed_by?(user, user_group = nil)
if user_group
endorsements.where(user_group:).any?
else
endorsements.where(author: user, user_group: nil).any?
endorsements.where(author: user, user_group: 0).any?
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions decidim-core/spec/models/decidim/endorsement_spec.rb
Expand Up @@ -86,8 +86,8 @@ module Decidim

it "sorts user_grup endorsements first and then by created_at" do
alecslupu marked this conversation as resolved.
Show resolved Hide resolved
expected_sorting = [
endorsement.id, other_endorsement2.id,
other_endorsement1.id
other_endorsement1.id, endorsement.id,
other_endorsement2.id,
]
expect(resource.endorsements.for_listing.pluck(:id)).to eq(expected_sorting)
end
Expand Down