Class: Group

Inherits:
ApplicationRecord show all
Defined in:
app/models/group.rb

Overview

rubocop:disable Metrics/ClassLength

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_defaultsObject



70
71
72
73
74
75
# File 'app/models/group.rb', line 70

def self.create_defaults
  return if where(code: "RD").count > 0
  Rails.logger.info "Creating default Groups"
  create(title: "Princeton Research Data Service (PRDS)", code: "RD")
  create(title: "Princeton Plasma Physics Lab (PPPL)", code: "PPPL")
end

.defaultObject

Returns the default group. Used when we don’t have anything else to determine a more specific Group for a user.



79
80
81
82
# File 'app/models/group.rb', line 79

def self.default
  create_defaults
  research_data
end

.default_for_department(department_number) ⇒ Object

Returns the default group for a given department number. Reference: docs.google.com/spreadsheets/d/1_Elxs3Ex-2wCbbKUzD4ii3k16zx36sYf/edit#gid=1484576831



86
87
88
89
90
91
92
# File 'app/models/group.rb', line 86

def self.default_for_department(department_number)
  if department_number.present? && department_number >= "31000" && department_number <= "31027"
    plasma_laboratory
  else
    default
  end
end

.plasma_laboratoryObject



99
100
101
102
# File 'app/models/group.rb', line 99

def self.plasma_laboratory
  create_defaults
  where(code: "PPPL").first
end

.research_dataObject



94
95
96
97
# File 'app/models/group.rb', line 94

def self.research_data
  create_defaults
  where(code: "RD").first
end

Instance Method Details

#add_administrator(current_user, admin_user) ⇒ Object

The current user adds a new admin user to a group. For use in the UI - we need to check whether the current_user has the right permissions to add someone as an admin_user.

Parameters:

  • current_user (User)
    • the currently logged in user

  • admin_user (User)
    • the user who will be granted admin rights on this group



114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/models/group.rb', line 114

def add_administrator(current_user, admin_user)
  if current_user.has_role?(:super_admin) || current_user.has_role?(:group_admin, self)
    if admin_user.has_role? :group_admin, self
      errors.add(:admin, "User has already been added")
    else
      errors.delete(:admin)
      admin_user.add_role :group_admin, self
    end
  else
    errors.add(:admin, "Unauthorized")
  end
end

#add_submitter(current_user, additional_user) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/models/group.rb', line 127

def add_submitter(current_user, additional_user)
  if current_user.has_role?(:super_admin) || current_user.has_role?(:group_admin, self)
    return if (self == additional_user.default_group) && additional_user.just_created

    if additional_user.has_role? :submitter, self
      errors.add(:submitter, "User has already been added")
    else
      errors.delete(:submitter)
      additional_user.add_role :submitter, self
    end
  else
    errors.add(:submitter, "Unauthorized")
  end
end

#administratorsObject



104
105
106
# File 'app/models/group.rb', line 104

def administrators
  User.with_role(:group_admin, self)
end

#communitiesObject



158
159
160
161
162
163
164
165
166
167
# File 'app/models/group.rb', line 158

def communities
  if code == "PPPL"
    ["Princeton Plasma Physics Laboratory"]
  else
    ["Princeton Neuroscience Institute", "Geosciences", "Mechanical and Aerospace Engineering",
     "Astrophysical Sciences", "Civil and Environmental Engineering", "Chemical and Biological Engineering",
     "Digital Humanities", "Music and Arts", "Princeton School of Public and International Affairs",
     "Chemistry", "Lewis Seigler Genomics", "Architecture", "Ecology and Evolutionary Biology", "Economics"].sort
  end
end

#datasetsObject



35
36
37
# File 'app/models/group.rb', line 35

def datasets
  Work.where(group_id: id)
end

#default_admins_listObject



20
21
22
23
24
# File 'app/models/group.rb', line 20

def default_admins_list
  return [] if code.blank?
  key = code.downcase.to_sym
  Rails.configuration.group_defaults.dig(key, :admin) || []
end

#default_communityObject



204
205
206
# File 'app/models/group.rb', line 204

def default_community
  return communities.first if code == "PPPL"
end

#default_submitters_listObject



26
27
28
29
# File 'app/models/group.rb', line 26

def default_submitters_list
  key = code.downcase.to_sym
  Rails.configuration.group_defaults.dig(key, :submit) || []
end

#default_user(uid) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'app/models/group.rb', line 208

def default_user(uid)
  user = User.find_by(uid:)
  return user if user.present?

  begin
    user = User.new(uid:, default_group_id: id)
    user.save!
    user
  rescue ActiveRecord::RecordNotUnique => unique_error
    # If adding a submitter to an existing group
    Rails.logger.error("Failed to created a new user for #{self}: #{unique_error}")
    user = User.new(uid:)
    user.default_group_id = id
    user.save!
    user
  end
end

#delete_permission(current_user, removed_user) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/models/group.rb', line 142

def delete_permission(current_user, removed_user)
  if current_user.has_role?(:super_admin) || current_user.has_role?(:group_admin, self)
    if removed_user.nil?
      errors.add(:delete_permission, "User was not found")
    elsif removed_user == current_user
      errors.add(:delete_permission, "Cannot remove yourself from a group. Contact a super-admin for help.")
    else
      errors.delete(:delete_permission)
      removed_user.remove_role :group_admin, self
      removed_user.remove_role :submitter, self
    end
  else
    errors.add(:delete_permission, "Unauthorized")
  end
end

#disable_messages_for(user:, subcommunity: nil) ⇒ Object

Disable a User from receiving notification messages for members of this Group

Parameters:

Raises:

  • (ArgumentError)


54
55
56
57
58
59
# File 'app/models/group.rb', line 54

def disable_messages_for(user:, subcommunity: nil)
  raise(ArgumentError, "User #{user.uid} is not an administrator or submitter for this group #{title}") unless user.can_admin?(self) || user.can_submit?(self)
  group = GroupOption.find_or_initialize_by(option_type: GroupOption::EMAIL_MESSAGES, user:, group: self, subcommunity:)
  group.enabled = false
  group.save
end

#enable_messages_for(user:, subcommunity: nil) ⇒ Object

Permit a User to receive notification messages for members of this Group

Parameters:

Raises:

  • (ArgumentError)


45
46
47
48
49
50
# File 'app/models/group.rb', line 45

def enable_messages_for(user:, subcommunity: nil)
  raise(ArgumentError, "User #{user.uid} is not an administrator or submitter for this group #{title}") unless user.can_admin?(self) || user.can_submit?(self)
  group = GroupOption.find_or_initialize_by(option_type: GroupOption::EMAIL_MESSAGES, user:, group: self, subcommunity:)
  group.enabled = true
  group.save
end

#messages_enabled_for?(user:, subcommunity: nil) ⇒ Boolean

Returns true if a given user has notification e-mails enabled for this Group

Parameters:

Returns:

  • (Boolean)


64
65
66
67
68
# File 'app/models/group.rb', line 64

def messages_enabled_for?(user:, subcommunity: nil)
  group_option = group_messaging_options.find_by(user:, group: self, subcommunity:)
  group_option ||= GroupOption.new(enabled: true)
  group_option.enabled
end

#publisherObject

rubocop:enable Metrics/MethodLength



196
197
198
199
200
201
202
# File 'app/models/group.rb', line 196

def publisher
  if code == "PPPL"
    "Princeton Plasma Physics Laboratory, Princeton University"
  else
    "Princeton University"
  end
end

#subcommunitiesObject

rubocop:disable Metrics/MethodLength



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'app/models/group.rb', line 170

def subcommunities
  values = []
  if code == "PPPL"
    values << "Spherical Torus"
    values << "Advanced Projects"
    values << "ITER and Tokamaks"
    values << "Theory"
    values << "NSTX-U"
    values << "NSTX"
    values << "Discovery Plasma Science"
    values << "Theory and Computation"
    values << "Stellarators"
    values << "PPPL Collaborations"
    values << "MAST-U"
    values << "Other Projects"
    values << "System Studies"
    values << "Applied Materials and Sustainability Sciences"
    values << "Computational Science"
    values << "DIII-D"
    values << "Tokamak Experimental Sciences"

  end
  values.sort
end

#submittersObject



39
40
41
# File 'app/models/group.rb', line 39

def submitters
  User.with_role(:submitter, self)
end

#super_administratorsObject



31
32
33
# File 'app/models/group.rb', line 31

def super_administrators
  User.with_role(:super_admin)
end