Sha256: 5ea48cf60257a8ed49a731d188a7dd5e9a8f834e475f46c6a5ee478f3256e8c2

Contents?: true

Size: 1.32 KB

Versions: 8

Compression:

Stored size: 1.32 KB

Contents

# 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,
               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

        users.include?(user)
      end

      def self.public_spaces
        where(private_space: false).published
      end

      def self.private_spaces
        where(private_space: true)
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
decidim-core-0.23.1 lib/decidim/has_private_users.rb
decidim-core-0.23.1.rc1 lib/decidim/has_private_users.rb
decidim-core-0.23.0 lib/decidim/has_private_users.rb
decidim-core-0.22.0 lib/decidim/has_private_users.rb
decidim-core-0.21.0 lib/decidim/has_private_users.rb
decidim-core-0.20.1 lib/decidim/has_private_users.rb
decidim-core-0.20.0 lib/decidim/has_private_users.rb
decidim-core-0.19.1 lib/decidim/has_private_users.rb