From 203cd8ffda785e328b3fef6ebd51b82211917850 Mon Sep 17 00:00:00 2001 From: Joonas Date: Fri, 14 Jun 2024 11:20:41 +0300 Subject: [PATCH] Fix private user private process block --- lib/decidim/has_private_users.rb | 53 ++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 lib/decidim/has_private_users.rb diff --git a/lib/decidim/has_private_users.rb b/lib/decidim/has_private_users.rb new file mode 100644 index 0000000..97f076a --- /dev/null +++ b/lib/decidim/has_private_users.rb @@ -0,0 +1,53 @@ +# frozen_string_literal: true + +require "active_support/concern" + +module Decidim + # A concern with the features needed when you want a model to be able to create + # private users + module HasPrivateUsers + extend ActiveSupport::Concern + + included do + has_many :participatory_space_private_users, + class_name: "Decidim::ParticipatorySpacePrivateUser", + as: :privatable_to, + dependent: :destroy + has_many :users, + -> { entire_collection }, + through: :participatory_space_private_users, + class_name: "Decidim::User", + foreign_key: "private_user_to_id" + + def self.visible_for(user) + if user + return all if user.admin? + + 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 + end + end + + def can_participate?(user) + return true unless private_space? + return false unless user + + participatory_space_private_users.exists?(decidim_user_id: user.id) + end + + def self.public_spaces + where(private_space: false).published + end + + def self.private_spaces + where(private_space: true) + end + end + end +end