Ver Código Fonte

Redundant self

Maarten van den Berg 6 anos atrás
pai
commit
3dacb5cb07

+ 0 - 19
.rubocop.yml

@@ -56,25 +56,6 @@ Style/Documentation:
56 56
 Style/FrozenStringLiteralComment:
57 57
   Enabled: false
58 58
 
59
-# Offense count: 1
60
-# Cop supports --auto-correct.
61
-# Configuration parameters: Strict.
62
-Style/NumericLiterals:
63
-  MinDigits: 15
64
-
65
-# Offense count: 85
66
-# Cop supports --auto-correct.
67
-Style/RedundantSelf:
68
-  Exclude:
69
-    - 'app/models/activity.rb'
70
-    - 'app/models/group.rb'
71
-    - 'app/models/member.rb'
72
-    - 'app/models/participant.rb'
73
-    - 'app/models/person.rb'
74
-    - 'app/models/subgroup.rb'
75
-    - 'app/models/token.rb'
76
-    - 'app/models/user.rb'
77
-
78 59
 # Offense count: 215
79 60
 # Cop supports --auto-correct.
80 61
 # Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline.

+ 44 - 44
app/models/activity.rb

@@ -87,17 +87,17 @@ class Activity < ApplicationRecord
87 87
   # Get all people (not participants) that are organizers. Does not include
88 88
   # group leaders, although they may modify the activity as well.
89 89
   def organizers
90
-    self.participants.includes(:person).where(is_organizer: true)
90
+    participants.includes(:person).where(is_organizer: true)
91 91
   end
92 92
 
93 93
   def organizer_names
94
-    self.organizers.map { |o| o.person.full_name }
94
+    organizers.map { |o| o.person.full_name }
95 95
   end
96 96
 
97 97
   # Determine whether the passed Person participates in the activity.
98 98
   def participant?(person)
99 99
     Participant.exists?(
100
-      activity_id: self.id,
100
+      activity_id: id,
101 101
       person_id: person.id
102 102
     )
103 103
   end
@@ -106,19 +106,19 @@ class Activity < ApplicationRecord
106 106
   def organizer?(person)
107 107
     Participant.exists?(
108 108
       person_id: person.id,
109
-      activity_id: self.id,
109
+      activity_id: id,
110 110
       is_organizer: true
111 111
     )
112 112
   end
113 113
 
114 114
   # Query the database to determine the amount of participants that are present/absent/unknown
115 115
   def state_counts
116
-    self.participants.group(:attending).count
116
+    participants.group(:attending).count
117 117
   end
118 118
 
119 119
   # Return participants attending, absent, unknown
120 120
   def human_state_counts
121
-    c = self.state_counts
121
+    c = state_counts
122 122
     p = c[true]
123 123
     a = c[false]
124 124
     u = c[nil]
@@ -128,16 +128,16 @@ class Activity < ApplicationRecord
128 128
   # Determine whether the passed Person may change this activity.
129 129
   def may_change?(person)
130 130
     person.is_admin ||
131
-      self.organizer?(person) ||
132
-      self.group.leader?(person)
131
+      organizer?(person) ||
132
+      group.leader?(person)
133 133
   end
134 134
 
135 135
   # Create Participants for all People that
136 136
   #  1. are members of the group
137 137
   #  2. do not have Participants (and thus, no way to confirm) yet
138 138
   def create_missing_participants!
139
-    people = self.group.people
140
-    people = people.where('people.id NOT IN (?)', self.people.ids) unless self.participants.empty?
139
+    people = group.people
140
+    people = people.where('people.id NOT IN (?)', self.people.ids) unless participants.empty?
141 141
 
142 142
     people.each do |p|
143 143
       Participant.create(
@@ -149,10 +149,10 @@ class Activity < ApplicationRecord
149 149
 
150 150
   # Create Subgroups from the defaults set using DefaultSubgroups
151 151
   def copy_default_subgroups!
152
-    defaults = self.group.default_subgroups
152
+    defaults = group.default_subgroups
153 153
 
154 154
     # If there are no subgroups, there cannot be subgroup division.
155
-    self.update_attribute(:subgroup_division_enabled, false) if defaults.none?
155
+    update_attribute(:subgroup_division_enabled, false) if defaults.none?
156 156
 
157 157
     defaults.each do |dsg|
158 158
       sg = Subgroup.new(activity: self)
@@ -210,48 +210,48 @@ class Activity < ApplicationRecord
210 210
   # response to 'attending'.
211 211
   def send_reminder
212 212
     # Sanity check that the reminder date didn't change while queued.
213
-    return unless !self.reminder_done && self.reminder_at
214
-    return if self.reminder_at > Time.zone.now
213
+    return unless !reminder_done && reminder_at
214
+    return if reminder_at > Time.zone.now
215 215
 
216 216
     participants = self.participants.where(attending: nil)
217 217
     participants.each(&:send_reminder)
218 218
 
219 219
     self.reminder_done = true
220
-    self.save
220
+    save
221 221
   end
222 222
 
223 223
   def schedule_reminder
224
-    return if self.reminder_at.nil? || self.reminder_done
224
+    return if reminder_at.nil? || reminder_done
225 225
 
226
-    self.delay(run_at: self.reminder_at).send_reminder
226
+    delay(run_at: reminder_at).send_reminder
227 227
   end
228 228
 
229 229
   def schedule_subgroup_division
230
-    return if self.deadline.nil? || self.subgroup_division_done
230
+    return if deadline.nil? || subgroup_division_done
231 231
 
232
-    self.delay(run_at: self.deadline).assign_subgroups!(mail: true)
232
+    delay(run_at: deadline).assign_subgroups!(mail: true)
233 233
   end
234 234
 
235 235
   # Assign a subgroup to all attending participants without one.
236 236
   def assign_subgroups!(mail = false)
237 237
     # Sanity check: we need subgroups to divide into.
238
-    return unless self.subgroups.any?
238
+    return unless subgroups.any?
239 239
 
240 240
     # Get participants in random order
241
-    ps = self
242
-         .participants
243
-         .where(attending: true)
244
-         .where(subgroup: nil)
245
-         .to_a
241
+    ps =
242
+      participants
243
+      .where(attending: true)
244
+      .where(subgroup: nil)
245
+      .to_a
246 246
 
247 247
     ps.shuffle!
248 248
 
249 249
     # Get groups, link to participant count
250
-    groups = self
251
-             .subgroups
252
-             .where(is_assignable: true)
253
-             .to_a
254
-             .map { |sg| [sg.participants.count, sg] }
250
+    groups =
251
+      subgroups
252
+      .where(is_assignable: true)
253
+      .to_a
254
+      .map { |sg| [sg.participants.count, sg] }
255 255
 
256 256
     ps.each do |p|
257 257
       # Sort groups so the group with the least participants gets the following participant
@@ -265,21 +265,21 @@ class Activity < ApplicationRecord
265 265
       groups.first[0] += 1
266 266
     end
267 267
 
268
-    self.notify_subgroups! if mail
268
+    notify_subgroups! if mail
269 269
   end
270 270
 
271 271
   def clear_subgroups!(only_assignable = true)
272
-    sgs = self
273
-          .subgroups
272
+    sgs =
273
+      subgroups
274 274
 
275 275
     if only_assignable
276 276
       sgs = sgs
277 277
             .where(is_assignable: true)
278 278
     end
279 279
 
280
-    ps = self
281
-         .participants
282
-         .where(subgroup: sgs)
280
+    ps =
281
+      participants
282
+      .where(subgroup: sgs)
283 283
 
284 284
     ps.each do |p|
285 285
       p.subgroup = nil
@@ -289,10 +289,10 @@ class Activity < ApplicationRecord
289 289
 
290 290
   # Notify participants of the current subgroups, if any.
291 291
   def notify_subgroups!
292
-    ps = self
293
-         .participants
294
-         .joins(:person)
295
-         .where.not(subgroup: nil)
292
+    ps =
293
+      participants
294
+      .joins(:person)
295
+      .where.not(subgroup: nil)
296 296
 
297 297
     ps.each(&:send_subgroup_notification)
298 298
   end
@@ -302,22 +302,22 @@ class Activity < ApplicationRecord
302 302
   # Assert that the deadline for participants to change the deadline, if any,
303 303
   # is set before the event starts.
304 304
   def deadline_before_start
305
-    errors.add(:deadline, I18n.t('activities.errors.must_be_before_start')) if self.deadline > self.start
305
+    errors.add(:deadline, I18n.t('activities.errors.must_be_before_start')) if deadline > start
306 306
   end
307 307
 
308 308
   # Assert that the activity's end, if any, occurs after the event's start.
309 309
   def end_after_start
310
-    errors.add(:end, I18n.t('activities.errors.must_be_after_start')) if self.end < self.start
310
+    errors.add(:end, I18n.t('activities.errors.must_be_after_start')) if self.end < start
311 311
   end
312 312
 
313 313
   # Assert that the reminder for non-response is sent while participants still
314 314
   # can change their response.
315 315
   def reminder_before_deadline
316
-    errors.add(:reminder_at, I18n.t('activities.errors.must_be_before_deadline')) if self.reminder_at > self.deadline
316
+    errors.add(:reminder_at, I18n.t('activities.errors.must_be_before_deadline')) if reminder_at > deadline
317 317
   end
318 318
 
319 319
   # Assert that there is at least one divisible subgroup.
320 320
   def subgroups_for_division_present
321
-    errors.add(:subgroup_division_enabled, I18n.t('activities.errors.cannot_divide_without_subgroups')) if self.subgroups.where(is_assignable: true).none? && subgroup_division_enabled?
321
+    errors.add(:subgroup_division_enabled, I18n.t('activities.errors.cannot_divide_without_subgroups')) if subgroups.where(is_assignable: true).none? && subgroup_division_enabled?
322 322
   end
323 323
 end

+ 2 - 2
app/models/group.rb

@@ -25,12 +25,12 @@ class Group < ApplicationRecord
25 25
 
26 26
   # @return [Array<Member>] the members in the group who are also group leaders.
27 27
   def leaders
28
-    self.members.includes(:person).where(is_leader: true)
28
+    members.includes(:person).where(is_leader: true)
29 29
   end
30 30
 
31 31
   # @return [Array<Activity>] the activities that haven't started yet.
32 32
   def future_activities
33
-    self.activities.where('start > ?', Time.now)
33
+    activities.where('start > ?', Time.now)
34 34
   end
35 35
 
36 36
   # @return [Array<Activity>]

+ 6 - 6
app/models/member.rb

@@ -20,18 +20,18 @@ class Member < ApplicationRecord
20 20
   # Create Participants for this Member for all the group's future activities, where the member isn't enrolled yet.
21 21
   # Intended to be called after the member is added to the group.
22 22
   def create_future_participants!
23
-    activities = self.group.future_activities
23
+    activities = group.future_activities
24 24
 
25
-    unless self.person.activities.empty?
25
+    unless person.activities.empty?
26 26
       activities = activities.where(
27
-        'activities.id NOT IN (?)', self.person.activities.ids
27
+        'activities.id NOT IN (?)', person.activities.ids
28 28
       )
29 29
     end
30 30
 
31 31
     activities.each do |a|
32 32
       Participant.create!(
33 33
         activity: a,
34
-        person: self.person
34
+        person: person
35 35
       )
36 36
     end
37 37
   end
@@ -40,8 +40,8 @@ class Member < ApplicationRecord
40 40
   # Intended to be called before the member is deleted.
41 41
   def delete_future_participants!
42 42
     participants = Participant.where(
43
-      person_id: self.person.id,
44
-      activity: self.group.future_activities
43
+      person_id: person.id,
44
+      activity: group.future_activities
45 45
     )
46 46
 
47 47
     participants.each(&:destroy!)

+ 12 - 12
app/models/participant.rb

@@ -36,7 +36,7 @@ class Participant < ApplicationRecord
36 36
   # @return [String]
37 37
   #   the name for the Participant's current state in the current locale.
38 38
   def human_attending
39
-    HUMAN_ATTENDING[self.attending]
39
+    HUMAN_ATTENDING[attending]
40 40
   end
41 41
 
42 42
   ICAL_ATTENDING = {
@@ -48,16 +48,16 @@ class Participant < ApplicationRecord
48 48
   # @return [String]
49 49
   #   the ICal attending response.
50 50
   def ical_attending
51
-    ICAL_ATTENDING[self.attending]
51
+    ICAL_ATTENDING[attending]
52 52
   end
53 53
 
54 54
   # TODO: Move to a more appropriate place
55 55
   # @return [String]
56 56
   #   the class for a row containing this activity.
57 57
   def row_class
58
-    if self.attending
58
+    if attending
59 59
       "success"
60
-    elsif self.attending == false
60
+    elsif attending == false
61 61
       "danger"
62 62
     else
63 63
       "warning"
@@ -65,30 +65,30 @@ class Participant < ApplicationRecord
65 65
   end
66 66
 
67 67
   def may_change?(person)
68
-    self.activity.may_change?(person) ||
68
+    activity.may_change?(person) ||
69 69
       self.person == person
70 70
   end
71 71
 
72 72
   # Set attending to true if nil, and notify the Person via email.
73 73
   def send_reminder
74
-    return unless self.attending.nil?
74
+    return unless attending.nil?
75 75
 
76
-    self.attending = self.activity.no_response_action
76
+    self.attending = activity.no_response_action
77 77
     notes = self.notes || ""
78 78
     notes << '[auto]'
79 79
     self.notes = notes
80
-    self.save
80
+    save
81 81
 
82
-    return unless self.person.send_attendance_reminder
82
+    return unless person.send_attendance_reminder
83 83
 
84
-    ParticipantMailer.attendance_reminder(self.person, self.activity).deliver_later
84
+    ParticipantMailer.attendance_reminder(person, activity).deliver_later
85 85
   end
86 86
 
87 87
   # Send subgroup information email if person is attending.
88 88
   def send_subgroup_notification
89
-    return unless self.attending && self.subgroup
89
+    return unless attending && subgroup
90 90
 
91
-    ParticipantMailer.subgroup_notification(self.person, self.activity, self).deliver_later
91
+    ParticipantMailer.subgroup_notification(person, activity, self).deliver_later
92 92
   end
93 93
 
94 94
   # Clear subgroup if person is set to 'not attending'.

+ 14 - 14
app/models/person.rb

@@ -54,25 +54,25 @@ class Person < ApplicationRecord
54 54
 
55 55
   # The person's full name.
56 56
   def full_name
57
-    if self.infix&.present?
58
-      [self.first_name, self.infix, self.last_name].join(' ')
57
+    if infix&.present?
58
+      [first_name, infix, last_name].join(' ')
59 59
     else
60
-      [self.first_name, self.last_name].join(' ')
60
+      [first_name, last_name].join(' ')
61 61
     end
62 62
   end
63 63
 
64 64
   # The person's reversed name, to sort by surname.
65 65
   def reversed_name
66
-    if self.infix
67
-      [self.last_name, self.infix, self.first_name].join(', ')
66
+    if infix
67
+      [last_name, infix, first_name].join(', ')
68 68
     else
69
-      [self.last_name, self.first_name].join(', ')
69
+      [last_name, first_name].join(', ')
70 70
     end
71 71
   end
72 72
 
73 73
   # All activities where this person is an organizer.
74 74
   def organized_activities
75
-    self.participants.includes(:activity).where(is_organizer: true)
75
+    participants.includes(:activity).where(is_organizer: true)
76 76
   end
77 77
 
78 78
   # Create multiple Persons from data found in a csv file, return those.
@@ -100,7 +100,7 @@ class Person < ApplicationRecord
100 100
   # @return [String]
101 101
   #   the URL to access this person's calendar.
102 102
   def calendar_url
103
-    person_calendar_url self.calendar_token
103
+    person_calendar_url calendar_token
104 104
   end
105 105
 
106 106
   # @return [Icalendar::Calendar]
@@ -111,10 +111,10 @@ class Person < ApplicationRecord
111 111
 
112 112
     tzid = 1.seconds.since.time_zone.tzinfo.name
113 113
 
114
-    selection = self
115
-                .participants
116
-                .joins(:activity)
117
-                .where('"end" > ?', 3.months.ago)
114
+    selection =
115
+      participants
116
+      .joins(:activity)
117
+      .where('"end" > ?', 3.months.ago)
118 118
 
119 119
     if skip_absent
120 120
       selection = selection
@@ -190,7 +190,7 @@ class Person < ApplicationRecord
190 190
 
191 191
   # Assert that the person's birth date, if any, lies in the past.
192 192
   def birth_date_cannot_be_in_future
193
-    errors.add(:birth_date, I18n.t('person.errors.cannot_future')) if self.birth_date && self.birth_date > Date.today
193
+    errors.add(:birth_date, I18n.t('person.errors.cannot_future')) if birth_date && birth_date > Date.today
194 194
   end
195 195
 
196 196
   # Explicitly force nil to false in the admin field.
@@ -201,6 +201,6 @@ class Person < ApplicationRecord
201 201
   # Ensure the person's user email is updated at the same time as the person's
202 202
   # email.
203 203
   def update_user_email
204
-    self.user&.update!(email: self.email)
204
+    user&.update!(email: email)
205 205
   end
206 206
 end

+ 1 - 2
app/models/session.rb

@@ -28,8 +28,7 @@ class Session < ApplicationRecord
28 28
 
29 29
   # @return [String] a BCrypt digest of the given string.
30 30
   def self.digest(string)
31
-    cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
32
-                                                  BCrypt::Engine.cost
31
+    cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost
33 32
     BCrypt::Password.create(string, cost: cost)
34 33
   end
35 34
 end

+ 1 - 2
app/models/subgroup.rb

@@ -6,8 +6,7 @@ class Subgroup < ApplicationRecord
6 6
   validates :activity, presence: true
7 7
 
8 8
   def participant_names
9
-    self
10
-      .participants
9
+    participants
11 10
       .joins(:person)
12 11
       .map { |p| p.person.full_name }
13 12
       .sort

+ 1 - 1
app/models/token.rb

@@ -53,7 +53,7 @@ class Token < ApplicationRecord
53 53
 
54 54
   # Defines the default expiry for the expiring tokens.
55 55
   def generate_expiry
56
-    case self.tokentype
56
+    case tokentype
57 57
     when TYPES[:password_reset]
58 58
       self.expires = 1.days.since
59 59
     when TYPES[:account_confirmation]

+ 1 - 1
app/models/user.rb

@@ -30,6 +30,6 @@ class User < ApplicationRecord
30 30
   # Assert that the user's email address is the same as the email address of
31 31
   # the associated Person.
32 32
   def email_same_as_person
33
-    errors.add(:email, I18n.t('authentication.user_person_mail_mismatch')) if self.person && (self.email != self.person.email)
33
+    errors.add(:email, I18n.t('authentication.user_person_mail_mismatch')) if person && (email != person.email)
34 34
   end
35 35
 end