Class: Decidim::UserGroup

Inherits:
UserBaseEntity show all
Includes:
ActsAsAuthor, DownloadYourData, Searchable, Traceable, UserReportable
Defined in:
app/models/decidim/user_group.rb

Overview

A UserGroup is an organization of participants

Constant Summary

Constants inherited from UserBaseEntity

Decidim::UserBaseEntity::REGEXP_NAME

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Searchable

searchable_resources, searchable_resources_of_type_comment, searchable_resources_of_type_component, searchable_resources_of_type_participant, searchable_resources_of_type_participatory_space

Methods inherited from UserBaseEntity

#public_followings

Methods included from HasUploadValidations

#attached_uploader, #maximum_avatar_size, #maximum_upload_size

Methods included from Followable

#followers

Class Method Details

.export_serializerObject



89
90
91
# File 'app/models/decidim/user_group.rb', line 89

def self.export_serializer
  Decidim::DownloadYourDataSerializers::DownloadYourDataUserGroupSerializer
end

.log_presenter_class_for(_log) ⇒ Object



61
62
63
# File 'app/models/decidim/user_group.rb', line 61

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

.ransackable_scopes(_auth = nil) ⇒ Object



148
149
150
# File 'app/models/decidim/user_group.rb', line 148

def self.ransackable_scopes(_auth = nil)
  [:state_eq]
end

.sort_by_document_number_ascObject



160
161
162
# File 'app/models/decidim/user_group.rb', line 160

def self.sort_by_document_number_asc
  order(Arel.sql("extended_data->>'document_number' ASC"))
end

.sort_by_document_number_descObject



164
165
166
# File 'app/models/decidim/user_group.rb', line 164

def self.sort_by_document_number_desc
  order(Arel.sql("extended_data->>'document_number' DESC"))
end

.sort_by_phone_ascObject



168
169
170
# File 'app/models/decidim/user_group.rb', line 168

def self.sort_by_phone_asc
  order(Arel.sql("extended_data->>'phone' ASC"))
end

.sort_by_phone_descObject



172
173
174
# File 'app/models/decidim/user_group.rb', line 172

def self.sort_by_phone_desc
  order(Arel.sql("extended_data->>'phone' DESC"))
end

.sort_by_state_ascObject



176
177
178
# File 'app/models/decidim/user_group.rb', line 176

def self.sort_by_state_asc
  order(Arel.sql("extended_data->>'rejected_at' ASC, extended_data->>'verified_at' ASC, deleted_at ASC"))
end

.sort_by_state_descObject



180
181
182
# File 'app/models/decidim/user_group.rb', line 180

def self.sort_by_state_desc
  order(Arel.sql("extended_data->>'rejected_at' DESC, extended_data->>'verified_at' DESC, deleted_at DESC"))
end

.state_eq(value) ⇒ Object



144
145
146
# File 'app/models/decidim/user_group.rb', line 144

def self.state_eq(value)
  send(value.to_sym) if %w(all pending rejected verified).include?(value)
end

.user_collection(user) ⇒ Object



85
86
87
# File 'app/models/decidim/user_group.rb', line 85

def self.user_collection(user)
  user.user_groups
end

.with_document_number(organization, number) ⇒ Object



46
47
48
49
# File 'app/models/decidim/user_group.rb', line 46

def self.with_document_number(organization, number)
  where(decidim_organization_id: organization.id)
    .where("extended_data->>'document_number' = ?", number)
end

Instance Method Details

#accepted_membershipsObject

sugar for the memberships query



122
123
124
# File 'app/models/decidim/user_group.rb', line 122

def accepted_memberships
  UserGroups::AcceptedMemberships.for(self)
end

#accepted_usersObject

easy way to return all the users accepted in this group



127
128
129
# File 'app/models/decidim/user_group.rb', line 127

def accepted_users
  accepted_memberships.map(&:user)
end

#accepts_conversation?(_user) ⇒ Boolean

Currently, groups always accept conversations from anyone. This may change in the future in case the desired behaviour is to check if all (or any) of the administrators accepts conversations or there’s simply and option for this in the group preferences.

Returns:

  • (Boolean)


135
136
137
# File 'app/models/decidim/user_group.rb', line 135

def accepts_conversation?(_user)
  true
end

#deleted?Boolean

Check if the group has been deleted or not

Returns:

  • (Boolean)


81
82
83
# File 'app/models/decidim/user_group.rb', line 81

def deleted?
  deleted_at.present?
end

#document_numberObject



93
94
95
# File 'app/models/decidim/user_group.rb', line 93

def document_number
  extended_data["document_number"]
end

#non_deleted_membershipsObject



51
52
53
# File 'app/models/decidim/user_group.rb', line 51

def non_deleted_memberships
  memberships.where(decidim_users: { deleted_at: nil })
end

#pending?Boolean

Public: Checks if the user group is pending.

Returns:

  • (Boolean)


76
77
78
# File 'app/models/decidim/user_group.rb', line 76

def pending?
  verified_at.blank? && rejected_at.blank?
end

#phoneObject



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

def phone
  extended_data["phone"]
end

#presenterObject

Returns the presenter for this author, to be used in the views. Required by ActsAsAuthor.



57
58
59
# File 'app/models/decidim/user_group.rb', line 57

def presenter
  Decidim::UserGroupPresenter.new(self)
end

#reject!Object



109
110
111
112
113
# File 'app/models/decidim/user_group.rb', line 109

def reject!
  extended_data["verified_at"] = nil
  extended_data["rejected_at"] = Time.current
  save!
end

#rejected?Boolean

Public: Checks if the user group is rejected.

Returns:

  • (Boolean)


71
72
73
# File 'app/models/decidim/user_group.rb', line 71

def rejected?
  rejected_at.present?
end

#rejected_atObject



101
102
103
# File 'app/models/decidim/user_group.rb', line 101

def rejected_at
  extended_data["rejected_at"]
end

#unread_messages_count_for(user) ⇒ Object



139
140
141
142
# File 'app/models/decidim/user_group.rb', line 139

def unread_messages_count_for(user)
  @unread_messages_count_for ||= {}
  @unread_messages_count_for[user.id] ||= Decidim::Messaging::Conversation.user_collection(self).unread_messages_by(user).count
end

#verified?Boolean

Public: Checks if the user group is verified.

Returns:

  • (Boolean)


66
67
68
# File 'app/models/decidim/user_group.rb', line 66

def verified?
  verified_at.present?
end

#verified_atObject



105
106
107
# File 'app/models/decidim/user_group.rb', line 105

def verified_at
  extended_data["verified_at"]
end

#verify!Object



115
116
117
118
119
# File 'app/models/decidim/user_group.rb', line 115

def verify!
  extended_data["verified_at"] = Time.current
  extended_data["rejected_at"] = nil
  save!
end