Class: Decidim::UserBaseEntity

Inherits:
ApplicationRecord show all
Includes:
Followable, Loggable, Nicknamizable, Resourceable
Defined in:
app/models/decidim/user_base_entity.rb

Overview

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.

Direct Known Subclasses

User, UserGroup

Instance Method Summary collapse

Methods included from Followable

#followers

Instance Method Details

#followingObject

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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/models/decidim/user_base_entity.rb', line 31

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