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

pulibrary / pdc_describe / cace366a-ffad-45f1-9b60-678e607fa527

14 May 2024 02:21PM UTC coverage: 60.862% (-35.0%) from 95.908%
cace366a-ffad-45f1-9b60-678e607fa527

push

circleci

jrgriffiniii
wip

1 of 3 new or added lines in 2 files covered. (33.33%)

1194 existing lines in 57 files now uncovered.

2076 of 3411 relevant lines covered (60.86%)

22.71 hits per line

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

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

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

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

11
  has_many :users_with_options, through: :group_options, source: :user
1✔
12
  has_many :users_with_messaging, through: :group_messaging_options, source: :user
1✔
13

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

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

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

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

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

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

43
  # Permit a User to receive notification messages for members of this Group
44
  # @param user [User]
45
  def enable_messages_for(user:, subcommunity: nil)
1✔
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)
70✔
47
    group = GroupOption.find_or_initialize_by(option_type: GroupOption::EMAIL_MESSAGES, user:, group: self, subcommunity:)
70✔
48
    group.enabled = true
70✔
49
    group.save
70✔
50
  end
51

52
  # Disable a User from receiving notification messages for members of this Group
53
  # @param user [User]
54
  def disable_messages_for(user:, subcommunity: nil)
1✔
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)
91✔
56
    group = GroupOption.find_or_initialize_by(option_type: GroupOption::EMAIL_MESSAGES, user:, group: self, subcommunity:)
91✔
57
    group.enabled = false
91✔
58
    group.save
91✔
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]
64
  def messages_enabled_for?(user:, subcommunity: nil)
1✔
65
    group_option = group_messaging_options.find_by(user:, group: self, subcommunity:)
140✔
66
    group_option ||= GroupOption.new(enabled: true)
140✔
67
    group_option.enabled
140✔
68
  end
69

70
  def self.create_defaults
1✔
71
    return if where(code: "RD").count > 0
436✔
72
    Rails.logger.info "Creating default Groups"
51✔
73
    create(title: "Princeton Research Data Service (PRDS)", code: "RD")
51✔
74
    create(title: "Princeton Plasma Physics Lab (PPPL)", code: "PPPL")
51✔
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.
79
  def self.default
1✔
80
    create_defaults
153✔
81
    research_data
153✔
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
86
  def self.default_for_department(department_number)
1✔
87
    if department_number.present? && department_number >= "31000" && department_number <= "31027"
21✔
88
      plasma_laboratory
2✔
89
    else
90
      default
19✔
91
    end
92
  end
93

94
  def self.research_data
1✔
95
    create_defaults
229✔
96
    where(code: "RD").first
229✔
97
  end
98

99
  def self.plasma_laboratory
1✔
100
    create_defaults
54✔
101
    where(code: "PPPL").first
54✔
102
  end
103

104
  def administrators
1✔
105
    User.with_role(:group_admin, self)
8✔
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
114
  def add_administrator(current_user, admin_user)
1✔
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")
×
118
      else
UNCOV
119
        errors.delete(:admin)
×
UNCOV
120
        admin_user.add_role :group_admin, self
×
121
      end
122
    else
UNCOV
123
      errors.add(:admin, "Unauthorized")
×
124
    end
125
  end
126

127
  def add_submitter(current_user, additional_user)
1✔
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")
×
133
      else
UNCOV
134
        errors.delete(:submitter)
×
UNCOV
135
        additional_user.add_role :submitter, self
×
136
      end
137
    else
UNCOV
138
      errors.add(:submitter, "Unauthorized")
×
139
    end
140
  end
141

142
  def delete_permission(current_user, removed_user)
1✔
UNCOV
143
    if current_user.has_role?(:super_admin) || current_user.has_role?(:group_admin, self)
×
UNCOV
144
      if removed_user.nil?
×
UNCOV
145
        errors.add(:delete_permission, "User was not found")
×
UNCOV
146
      elsif removed_user == current_user
×
UNCOV
147
        errors.add(:delete_permission, "Cannot remove yourself from a group. Contact a super-admin for help.")
×
148
      else
UNCOV
149
        errors.delete(:delete_permission)
×
UNCOV
150
        removed_user.remove_role :group_admin, self
×
UNCOV
151
        removed_user.remove_role :submitter, self
×
152
      end
153
    else
UNCOV
154
      errors.add(:delete_permission, "Unauthorized")
×
155
    end
156
  end
157

158
  def communities
1✔
159
    if code == "PPPL"
40✔
160
      ["Princeton Plasma Physics Laboratory"]
4✔
161
    else
162
      ["Princeton Neuroscience Institute", "Department of Geosciences", "Mechanical and Aerospace Engineering",
36✔
163
       "Astrophysical Sciences", "Civil and Environmental Engineering", "Chemical and Biological Engineering",
164
       "Digital Humanities", "Music and Arts", "Princeton School of Public and International Affairs"].sort
165
    end
166
  end
167

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

190
    end
191
    values.sort
39✔
192
  end
193
  # rubocop:enable Metrics/MethodLength
194

195
  def publisher
1✔
196
    if code == "PPPL"
13✔
197
      "Princeton Plasma Physics Laboratory, Princeton University"
2✔
198
    else
199
      "Princeton University"
11✔
200
    end
201
  end
202

203
  def default_community
1✔
204
    return communities.first if code == "PPPL"
×
205
  end
206

207
  def default_user(uid)
1✔
UNCOV
208
    user = User.find_by(uid:)
×
UNCOV
209
    return user if user.present?
×
210

UNCOV
211
    user = User.new(uid:, default_group_id: id)
×
UNCOV
212
    user.save!
×
UNCOV
213
    user
×
214
  end
215
end
216
# 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