-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix private user private process block
- Loading branch information
1 parent
1303772
commit 203cd8f
Showing
1 changed file
with
53 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |