Class: Decidim::User

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

Overview

A User is a citizen that wants to join the platform to participate.

Constant Summary collapse

OMNIAUTH_PROVIDERS =
[:facebook, :twitter, :google_oauth2, (:developer if Rails.env.development?)].compact
ROLES =
%w(admin user_manager).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Searchable

searchable_resources

Instance Attribute Details

#invitation_instructionsObject

Public: Allows customizing the invitation instruction email content when inviting a user.

Returns a String.



75
76
77
# File 'app/models/decidim/user.rb', line 75

def invitation_instructions
  @invitation_instructions
end

#newsletter_notificationsObject

Returns the value of attribute newsletter_notifications.



56
57
58
# File 'app/models/decidim/user.rb', line 56

def newsletter_notifications
  @newsletter_notifications
end

Class Method Details

.data_portability_images(user) ⇒ Object



169
170
171
# File 'app/models/decidim/user.rb', line 169

def self.data_portability_images(user)
  user_collection(user).map(&:avatar)
end

.export_serializerObject



165
166
167
# File 'app/models/decidim/user.rb', line 165

def self.export_serializer
  Decidim::DataPortabilitySerializers::DataPortabilityUserSerializer
end

.find_for_authentication(warden_conditions) ⇒ Object

Check if the user exists with the given email and the current organization

warden_conditions - A hash with the authentication conditions

* email - a String that represents user's email.
* env - A Hash containing environment variables.

Returns a User.



153
154
155
156
157
158
159
# File 'app/models/decidim/user.rb', line 153

def self.find_for_authentication(warden_conditions)
  organization = warden_conditions.dig(:env, "decidim.current_organization")
  find_by(
    email: warden_conditions[:email],
    decidim_organization_id: organization.id
  )
end

.log_presenter_class_for(_log) ⇒ Object



77
78
79
# File 'app/models/decidim/user.rb', line 77

def self.log_presenter_class_for(_log)
  Decidim::AdminLog::UserPresenter
end

.user_collection(user) ⇒ Object



161
162
163
# File 'app/models/decidim/user.rb', line 161

def self.user_collection(user)
  where(id: user.id)
end

Instance Method Details

#active_roleObject

Public: Returns the active role of the user



92
93
94
# File 'app/models/decidim/user.rb', line 92

def active_role
  admin ? "admin" : roles.first
end

#deleted?Boolean

Check if the user account has been deleted or not

Returns:

  • (Boolean)


102
103
104
# File 'app/models/decidim/user.rb', line 102

def deleted?
  deleted_at.present?
end

#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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/models/decidim/user.rb', line 122

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

#following_usersObject



137
138
139
140
141
# File 'app/models/decidim/user.rb', line 137

def following_users
  @following_users ||= following.select do |f|
    f.is_a?(Decidim::User)
  end
end

#follows?(followable) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
# File 'app/models/decidim/user.rb', line 111

def follows?(followable)
  Decidim::Follow.where(user: self, followable: followable).any?
end

#nameObject

Public: returns the user’s name or the default one



97
98
99
# File 'app/models/decidim/user.rb', line 97

def name
  super || I18n.t("decidim.anonymous_user")
end

#officialized?Boolean

Public: whether the user has been officialized or not

Returns:

  • (Boolean)


107
108
109
# File 'app/models/decidim/user.rb', line 107

def officialized?
  !officialized_at.nil?
end

#role?(role) ⇒ Boolean

Checks if the user has the given ‘role` or not.

role - a String or a Symbol that represents the role that is being

checked

Returns a boolean.

Returns:

  • (Boolean)


87
88
89
# File 'app/models/decidim/user.rb', line 87

def role?(role)
  roles.include?(role.to_s)
end

#tos_accepted?Boolean

Returns:

  • (Boolean)


173
174
175
176
177
# File 'app/models/decidim/user.rb', line 173

def tos_accepted?
  return true if managed || organization.tos_version.nil?
  return false if accepted_tos_version.nil?
  accepted_tos_version >= organization.tos_version
end

#unread_conversationsObject



143
144
145
# File 'app/models/decidim/user.rb', line 143

def unread_conversations
  Decidim::Messaging::Conversation.unread_by(self)
end

#user_invited?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'app/models/decidim/user.rb', line 67

def user_invited?
  invitation_token_changed? && invitation_accepted_at_changed?
end