Skip to content
Permalink
Browse files

Fix: Duplicate results in Decidim::HasPrivateUsers::visible_for(user) (

…#5462)

* Fix: Duplicate results in Decidim::HasPrivateUsers::visible_for(user)

* Add CHANGELOG entry

* Fix PG::Error

I've run into the following problem multiple times since adding `.distinct`:
'ERROR: for SELECT DISTINCT, <SCOPE_EXPRESSION> expressions must appear in select list'.
  • Loading branch information...
aitorlb authored and tramuntanal committed Nov 5, 2019
1 parent 834c41e commit e4d26e7fd8c26a251dcd6e79646db688c06b22db
@@ -17,6 +17,7 @@

**Fixed**:

- **decidim-core**, **decidim-participatory_processes**: Fix: Duplicate results in `Decidim::HasPrivateUsers::visible_for(user)` [\#5462](https://github.com/decidim/decidim/pull/5462)
- **decidim-participatory_processes**: Fix: flaky test when mapping Rails timezone names to PostgreSQL [\#5472](https://github.com/decidim/decidim/pull/5472)
- **decidim-conferences**: Fix: Add pagination interface to some sections [\#5463](https://github.com/decidim/decidim/pull/5463)
- **decidim-sortitions**: Fix: Don't include drafts in sortitions [\#5434](https://github.com/decidim/decidim/pull/5434)
@@ -42,3 +42,4 @@
require "decidim/core/test/shared_examples/amendable/amendment_rejected_event_examples"
require "decidim/core/test/shared_examples/amendable/amendment_promoted_event_examples"
require "decidim/core/test/shared_examples/uncommentable_component_examples"
require "decidim/core/test/shared_examples/has_private_users"
@@ -0,0 +1,63 @@
# frozen_string_literal: true

require "spec_helper"

shared_examples_for "has private users" do
let(:factory_name) { described_class.name.demodulize.underscore.to_sym }

let!(:public_space) do
create(factory_name, private_space: false, published_at: Time.current)
end

let!(:private_space) do
create(factory_name, private_space: true, published_at: Time.current)
end

def create_space_private_user(space, user = create(:user, organization: space.organization))
Decidim::ParticipatorySpacePrivateUser.create(privatable_to: space, user: user)
end

describe ".public_spaces" do
let(:scope) { described_class.send(:public_spaces) }

it { expect(scope).to eq([public_space]) }
end

describe ".visible_for" do
let(:scope) { described_class.send(:visible_for, user) }

before { create_space_private_user(private_space) }

context "without user" do
let(:user) { nil }

it { expect(scope).to contain_exactly(public_space) }
end

context "with non-private user" do
let(:user) { create(:user) }

it { expect(scope).to contain_exactly(public_space) }
end

context "with private user" do
let(:user) { private_space.users.first }

it { expect(scope).to contain_exactly(public_space, private_space) }
end

context "when the space is both public and has private users" do
# Visible spaces for non-private user.
let(:user) { create(:user) }

before do
# Public space has multiple private users.
create_space_private_user(public_space)
create_space_private_user(public_space)
end

# Expect no duplicate results.
it { expect(scope).to contain_exactly(public_space) }
end
end
end
@@ -22,9 +22,11 @@ def self.visible_for(user)
if user
return all if user.admin?

left_outer_joins(:participatory_space_private_users).where(
%(private_space = false OR
decidim_participatory_space_private_users.decidim_user_id = ?), user.id
where(
id: public_spaces +
private_spaces
.joins(:participatory_space_private_users)
.where("decidim_participatory_space_private_users.decidim_user_id = ?", user.id)
)
else
public_spaces
@@ -12,7 +12,8 @@ module Decidim

it { is_expected.to be_versioned }

include_examples "publicable"
it_behaves_like "publicable"
it_behaves_like "has private users"

it "overwrites the log presenter" do
expect(described_class.log_presenter_class_for(:foo))

0 comments on commit e4d26e7

Please sign in to comment.
You can’t perform that action at this time.