Sha256: 3fe181b25a78e95deb04cac124947f7dba88d316eb2d148056e63be5196a5fc3

Contents?: true

Size: 1.36 KB

Versions: 50

Compression:

Stored size: 1.36 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

        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

Version data entries

50 entries across 50 versions & 1 rubygems

Version Path
decidim-core-0.29.1 lib/decidim/has_private_users.rb
decidim-core-0.28.4 lib/decidim/has_private_users.rb
decidim-core-0.27.9 lib/decidim/has_private_users.rb
decidim-core-0.29.0 lib/decidim/has_private_users.rb
decidim-core-0.28.3 lib/decidim/has_private_users.rb
decidim-core-0.27.8 lib/decidim/has_private_users.rb
decidim-core-0.29.0.rc4 lib/decidim/has_private_users.rb
decidim-core-0.29.0.rc3 lib/decidim/has_private_users.rb
decidim-core-0.29.0.rc2 lib/decidim/has_private_users.rb
decidim-core-0.29.0.rc1 lib/decidim/has_private_users.rb
decidim-core-0.28.2 lib/decidim/has_private_users.rb
decidim-core-0.27.7 lib/decidim/has_private_users.rb
decidim-core-0.28.1 lib/decidim/has_private_users.rb
decidim-core-0.27.6 lib/decidim/has_private_users.rb
decidim-core-0.26.10 lib/decidim/has_private_users.rb
decidim-core-0.26.9 lib/decidim/has_private_users.rb
decidim-core-0.28.0 lib/decidim/has_private_users.rb
decidim-core-0.27.5 lib/decidim/has_private_users.rb
decidim-core-0.28.0.rc5 lib/decidim/has_private_users.rb
decidim-core-0.28.0.rc4 lib/decidim/has_private_users.rb