Sha256: 5c3e5de8c8b0bfb7f0af4c737a3d5d2aebde4a047857d13d85d99763e47995d0

Contents?: true

Size: 1.97 KB

Versions: 13

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

module Decidim
  # This class serves as a base class for `Decidim::User` and `Decidim::UserGroup`
  # so that we can set some shared logic.
  # This class is not supposed to be used directly.
  class UserBaseEntity < ApplicationRecord
    self.table_name = "decidim_users"

    include Nicknamizable
    include Resourceable
    include Decidim::Followable
    include Decidim::Loggable

    belongs_to :organization, foreign_key: "decidim_organization_id", class_name: "Decidim::Organization"
    has_many :notifications, foreign_key: "decidim_user_id", class_name: "Decidim::Notification", dependent: :destroy
    has_many :following_follows, foreign_key: "decidim_user_id", class_name: "Decidim::Follow", dependent: :destroy

    validates :avatar, file_size: { less_than_or_equal_to: ->(_record) { Decidim.maximum_avatar_size } }
    mount_uploader :avatar, Decidim::AvatarUploader

    validates :name, :nickname, format: { with: /\A(?!.*[<>?%&\^*#@\(\)\[\]\=\+\:\;\"\{\}\\\|])/ }

    # Public: Returns a collection with all the entities this user is following.
    #
    # This can't be done as with a `has_many :following, through: :following_follows`
    # since it's a polymorphic relation and Rails doesn't know how to load it. With
    # this implementation we only query the database once for each kind of following.
    #
    # Returns an Array of Decidim::Followable
    def following
      @following ||= begin
                       followings = following_follows.pluck(:decidim_followable_type, :decidim_followable_id)
                       grouped_followings = followings.each_with_object({}) do |(type, following_id), all|
                         all[type] ||= []
                         all[type] << following_id
                         all
                       end

                       grouped_followings.flat_map do |type, ids|
                         type.constantize.where(id: ids)
                       end
                     end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
decidim-core-0.22.0 app/models/decidim/user_base_entity.rb
decidim-core-0.21.0 app/models/decidim/user_base_entity.rb
decidim-core-0.20.1 app/models/decidim/user_base_entity.rb
decidim-core-0.20.0 app/models/decidim/user_base_entity.rb
decidim-core-0.19.1 app/models/decidim/user_base_entity.rb
decidim-core-0.18.1 app/models/decidim/user_base_entity.rb
decidim-core-0.19.0 app/models/decidim/user_base_entity.rb
decidim-core-0.17.2 app/models/decidim/user_base_entity.rb
decidim-core-0.18.0 app/models/decidim/user_base_entity.rb
decidim-core-0.17.1 app/models/decidim/user_base_entity.rb
decidim-core-0.16.1 app/models/decidim/user_base_entity.rb
decidim-core-0.17.0 app/models/decidim/user_base_entity.rb
decidim-core-0.16.0 app/models/decidim/user_base_entity.rb