Class: Decidim::User
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Decidim::User
- 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
-
#invitation_instructions ⇒ Object
Public: Allows customizing the invitation instruction email content when inviting a user.
-
#newsletter_notifications ⇒ Object
Returns the value of attribute newsletter_notifications.
Class Method Summary collapse
- .data_portability_images(user) ⇒ Object
- .export_serializer ⇒ Object
-
.find_for_authentication(warden_conditions) ⇒ Object
Check if the user exists with the given email and the current organization.
- .log_presenter_class_for(_log) ⇒ Object
- .user_collection(user) ⇒ Object
Instance Method Summary collapse
-
#active_role ⇒ Object
Public: Returns the active role of the user.
-
#deleted? ⇒ Boolean
Check if the user account has been deleted or not.
-
#following ⇒ Object
Public: Returns a collection with all the entities this user is following.
- #following_users ⇒ Object
- #follows?(followable) ⇒ Boolean
-
#name ⇒ Object
Public: returns the user’s name or the default one.
-
#officialized? ⇒ Boolean
Public: whether the user has been officialized or not.
-
#role?(role) ⇒ Boolean
Checks if the user has the given ‘role` or not.
- #tos_accepted? ⇒ Boolean
- #unread_conversations ⇒ Object
- #user_invited? ⇒ Boolean
Methods included from Searchable
Instance Attribute Details
#invitation_instructions ⇒ Object
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_notifications ⇒ Object
Returns the value of attribute newsletter_notifications.
56 57 58 |
# File 'app/models/decidim/user.rb', line 56 def @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_serializer ⇒ Object
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_role ⇒ Object
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
102 103 104 |
# File 'app/models/decidim/user.rb', line 102 def deleted? deleted_at.present? end |
#following ⇒ Object
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_users ⇒ Object
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
111 112 113 |
# File 'app/models/decidim/user.rb', line 111 def follows?(followable) Decidim::Follow.where(user: self, followable: followable).any? end |
#name ⇒ Object
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
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.
87 88 89 |
# File 'app/models/decidim/user.rb', line 87 def role?(role) roles.include?(role.to_s) end |
#tos_accepted? ⇒ 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_conversations ⇒ Object
143 144 145 |
# File 'app/models/decidim/user.rb', line 143 def unread_conversations Decidim::Messaging::Conversation.unread_by(self) end |
#user_invited? ⇒ Boolean
67 68 69 |
# File 'app/models/decidim/user.rb', line 67 def user_invited? invitation_token_changed? && invitation_accepted_at_changed? end |