• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pulibrary / pdc_describe / 4e4e59fc-9df4-4838-9fd4-6c7ea33cdb7c

07 Apr 2025 06:36PM UTC coverage: 1.283% (-94.6%) from 95.862%
4e4e59fc-9df4-4838-9fd4-6c7ea33cdb7c

Pull #1994

circleci

hectorcorrea
Switched to use the autocomplete that we aleady use for ROR. Integrated it with the existing logic for creators
Pull Request #1994: Started adding auto complete to contributors

0 of 46 new or added lines in 2 files covered. (0.0%)

4806 existing lines in 74 files now uncovered.

65 of 5065 relevant lines covered (1.28%)

0.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/app/models/group.rb
1
# frozen_string_literal: true
2

3
# rubocop:disable Metrics/ClassLength
UNCOV
4
class Group < ApplicationRecord
×
UNCOV
5
  resourcify
×
6

7
  # GroupOptions model extensible options set for Groups and Users
UNCOV
8
  has_many :group_options, dependent: :destroy, class_name: "GroupOption"
×
UNCOV
9
  has_many :group_messaging_options, -> { where(option_type: GroupOption::EMAIL_MESSAGES) }, class_name: "GroupOption", dependent: :destroy
×
10

UNCOV
11
  has_many :users_with_options, through: :group_options, source: :user
×
UNCOV
12
  has_many :users_with_messaging, through: :group_messaging_options, source: :user
×
13

UNCOV
14
  validate do |group|
×
UNCOV
15
    if group.title.blank?
×
UNCOV
16
      group.errors.add :base, "Title cannot be empty"
×
UNCOV
17
    end
×
UNCOV
18
  end
×
19

UNCOV
20
  def default_admins_list
×
UNCOV
21
    return [] if code.blank?
×
UNCOV
22
    key = code.downcase.to_sym
×
UNCOV
23
    Rails.configuration.group_defaults.dig(key, :admin) || []
×
UNCOV
24
  end
×
25

UNCOV
26
  def default_submitters_list
×
UNCOV
27
    key = code.downcase.to_sym
×
UNCOV
28
    Rails.configuration.group_defaults.dig(key, :submit) || []
×
UNCOV
29
  end
×
30

UNCOV
31
  def super_administrators
×
UNCOV
32
    User.with_role(:super_admin)
×
UNCOV
33
  end
×
34

UNCOV
35
  def datasets
×
UNCOV
36
    Work.where(group_id: id)
×
UNCOV
37
  end
×
38

UNCOV
39
  def submitters
×
UNCOV
40
    User.with_role(:submitter, self)
×
UNCOV
41
  end
×
42

43
  # Permit a User to receive notification messages for members of this Group
44
  # @param user [User]
UNCOV
45
  def enable_messages_for(user:, subcommunity: nil)
×
UNCOV
46
    raise(ArgumentError, "User #{user.uid} is not an administrator or submitter for this group #{title}") unless user.can_admin?(self) || user.can_submit?(self)
×
UNCOV
47
    group = GroupOption.find_or_initialize_by(option_type: GroupOption::EMAIL_MESSAGES, user:, group: self, subcommunity:)
×
UNCOV
48
    group.enabled = true
×
UNCOV
49
    group.save
×
UNCOV
50
  end
×
51

52
  # Disable a User from receiving notification messages for members of this Group
53
  # @param user [User]
UNCOV
54
  def disable_messages_for(user:, subcommunity: nil)
×
UNCOV
55
    raise(ArgumentError, "User #{user.uid} is not an administrator or submitter for this group #{title}") unless user.can_admin?(self) || user.can_submit?(self)
×
UNCOV
56
    group = GroupOption.find_or_initialize_by(option_type: GroupOption::EMAIL_MESSAGES, user:, group: self, subcommunity:)
×
UNCOV
57
    group.enabled = false
×
UNCOV
58
    group.save
×
UNCOV
59
  end
×
60

61
  # Returns true if a given user has notification e-mails enabled for this Group
62
  # @param user [User]
63
  # @return [Boolean]
UNCOV
64
  def messages_enabled_for?(user:, subcommunity: nil)
×
UNCOV
65
    group_option = group_messaging_options.find_by(user:, group: self, subcommunity:)
×
UNCOV
66
    group_option ||= GroupOption.new(enabled: true)
×
UNCOV
67
    group_option.enabled
×
UNCOV
68
  end
×
69

UNCOV
70
  def self.create_defaults
×
UNCOV
71
    return if where(code: "RD").count > 0
×
UNCOV
72
    Rails.logger.info "Creating default Groups"
×
UNCOV
73
    create(title: "Princeton Research Data Service (PRDS)", code: "RD")
×
UNCOV
74
    create(title: "Princeton Plasma Physics Lab (PPPL)", code: "PPPL")
×
UNCOV
75
  end
×
76

77
  # Returns the default group.
78
  # Used when we don't have anything else to determine a more specific Group for a user.
UNCOV
79
  def self.default
×
UNCOV
80
    create_defaults
×
UNCOV
81
    research_data
×
UNCOV
82
  end
×
83

84
  # Returns the default group for a given department number.
85
  # Reference: https://docs.google.com/spreadsheets/d/1_Elxs3Ex-2wCbbKUzD4ii3k16zx36sYf/edit#gid=1484576831
UNCOV
86
  def self.default_for_department(department_number)
×
UNCOV
87
    if department_number.present? && department_number >= "31000" && department_number <= "31027"
×
UNCOV
88
      plasma_laboratory
×
UNCOV
89
    else
×
UNCOV
90
      default
×
UNCOV
91
    end
×
UNCOV
92
  end
×
93

UNCOV
94
  def self.research_data
×
UNCOV
95
    create_defaults
×
UNCOV
96
    where(code: "RD").first
×
UNCOV
97
  end
×
98

UNCOV
99
  def self.plasma_laboratory
×
UNCOV
100
    create_defaults
×
UNCOV
101
    where(code: "PPPL").first
×
UNCOV
102
  end
×
103

UNCOV
104
  def administrators
×
UNCOV
105
    User.with_role(:group_admin, self)
×
UNCOV
106
  end
×
107

108
  ##
109
  # The current user adds a new admin user to a group.
110
  # For use in the UI - we need to check whether the current_user
111
  # has the right permissions to add someone as an admin_user.
112
  # @param [User] current_user - the currently logged in user
113
  # @param [User] admin_user - the user who will be granted admin rights on this group
UNCOV
114
  def add_administrator(current_user, admin_user)
×
UNCOV
115
    if current_user.has_role?(:super_admin) || current_user.has_role?(:group_admin, self)
×
UNCOV
116
      if admin_user.has_role? :group_admin, self
×
UNCOV
117
        errors.add(:admin, "User has already been added")
×
UNCOV
118
      else
×
UNCOV
119
        errors.delete(:admin)
×
UNCOV
120
        admin_user.add_role :group_admin, self
×
UNCOV
121
      end
×
UNCOV
122
    else
×
UNCOV
123
      errors.add(:admin, "Unauthorized")
×
UNCOV
124
    end
×
UNCOV
125
  end
×
126

UNCOV
127
  def add_submitter(current_user, additional_user)
×
UNCOV
128
    if current_user.has_role?(:super_admin) || current_user.has_role?(:group_admin, self)
×
UNCOV
129
      return if (self == additional_user.default_group) && additional_user.just_created
×
130

UNCOV
131
      if additional_user.has_role? :submitter, self
×
UNCOV
132
        errors.add(:submitter, "User has already been added")
×
UNCOV
133
      else
×
UNCOV
134
        errors.delete(:submitter)
×
UNCOV
135
        additional_user.add_role :submitter, self
×
UNCOV
136
      end
×
UNCOV
137
    else
×
UNCOV
138
      errors.add(:submitter, "Unauthorized")
×
UNCOV
139
    end
×
UNCOV
140
  end
×
141

142
  # @param [User] current_user - the currently logged in user
143
  # @param [User] admin_user - the user who will be granted admin rights on this group
144
  # @param [String] role to be deleted
UNCOV
145
  def delete_permission(current_user, removed_user, role)
×
UNCOV
146
    if current_user.has_role?(:super_admin) || current_user.has_role?(:group_admin, self)
×
UNCOV
147
      if removed_user.nil?
×
UNCOV
148
        errors.add(:delete_permission, "User was not found")
×
UNCOV
149
      elsif removed_user == current_user
×
UNCOV
150
        errors.add(:delete_permission, "Cannot remove yourself from a group. Contact a super-admin for help.")
×
UNCOV
151
      else
×
UNCOV
152
        errors.delete(:delete_permission)
×
UNCOV
153
        removed_user.remove_role role, self
×
UNCOV
154
      end
×
UNCOV
155
    else
×
UNCOV
156
      errors.add(:delete_permission, "Unauthorized")
×
UNCOV
157
    end
×
UNCOV
158
  end
×
159

UNCOV
160
  def communities
×
UNCOV
161
    if code == "PPPL"
×
UNCOV
162
      ["Princeton Plasma Physics Laboratory"]
×
UNCOV
163
    else
×
UNCOV
164
      ["Princeton Neuroscience Institute", "Geosciences", "Mechanical and Aerospace Engineering",
×
UNCOV
165
       "Astrophysical Sciences", "Civil and Environmental Engineering", "Chemical and Biological Engineering",
×
UNCOV
166
       "Digital Humanities", "Music and Arts", "Princeton School of Public and International Affairs",
×
UNCOV
167
       "Chemistry", "Lewis Seigler Genomics", "Architecture", "Ecology and Evolutionary Biology", "Economics"].sort
×
UNCOV
168
    end
×
UNCOV
169
  end
×
170

171
  # rubocop:disable Metrics/MethodLength
UNCOV
172
  def subcommunities
×
UNCOV
173
    values = []
×
UNCOV
174
    if code == "PPPL"
×
UNCOV
175
      values << "Spherical Torus"
×
UNCOV
176
      values << "Advanced Projects"
×
UNCOV
177
      values << "ITER and Tokamaks"
×
UNCOV
178
      values << "Theory"
×
UNCOV
179
      values << "NSTX-U"
×
UNCOV
180
      values << "NSTX"
×
UNCOV
181
      values << "Discovery Plasma Science"
×
UNCOV
182
      values << "Theory and Computation"
×
UNCOV
183
      values << "Stellarators"
×
UNCOV
184
      values << "PPPL Collaborations"
×
UNCOV
185
      values << "MAST-U"
×
UNCOV
186
      values << "Other Projects"
×
UNCOV
187
      values << "System Studies"
×
UNCOV
188
      values << "Applied Materials and Sustainability Sciences"
×
UNCOV
189
      values << "Computational Science"
×
UNCOV
190
      values << "DIII-D"
×
UNCOV
191
      values << "Tokamak Experimental Sciences"
×
192

UNCOV
193
    end
×
UNCOV
194
    values.sort
×
UNCOV
195
  end
×
196
  # rubocop:enable Metrics/MethodLength
197

UNCOV
198
  def publisher
×
UNCOV
199
    if code == "PPPL"
×
UNCOV
200
      "Princeton Plasma Physics Laboratory, Princeton University"
×
UNCOV
201
    else
×
UNCOV
202
      "Princeton University"
×
UNCOV
203
    end
×
UNCOV
204
  end
×
205

UNCOV
206
  def default_community
×
207
    return communities.first if code == "PPPL"
×
UNCOV
208
  end
×
209

UNCOV
210
  def default_user(uid)
×
UNCOV
211
    user = User.find_by(uid:)
×
UNCOV
212
    return user if user.present?
×
213

UNCOV
214
    begin
×
UNCOV
215
      user = User.new(uid:, default_group_id: id)
×
UNCOV
216
      user.save!
×
UNCOV
217
      user
×
UNCOV
218
    rescue ActiveRecord::RecordNotUnique => unique_error
×
219
      # If adding a submitter to an existing group
UNCOV
220
      Rails.logger.error("Failed to created a new user for #{self}: #{unique_error}")
×
UNCOV
221
      user = User.new(uid:)
×
UNCOV
222
      user.default_group_id = id
×
UNCOV
223
      user.save!
×
UNCOV
224
      user
×
UNCOV
225
    end
×
UNCOV
226
  end
×
UNCOV
227
end
×
228
# rubocop:enable Metrics/ClassLength
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc