Browse Source

Redundant self

Maarten van den Berg 6 years ago
parent
commit
3dacb5cb07

+ 0 - 19
.rubocop.yml

56
 Style/FrozenStringLiteralComment:
56
 Style/FrozenStringLiteralComment:
57
   Enabled: false
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
 # Offense count: 215
59
 # Offense count: 215
79
 # Cop supports --auto-correct.
60
 # Cop supports --auto-correct.
80
 # Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline.
61
 # Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline.

+ 44 - 44
app/models/activity.rb

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

+ 2 - 2
app/models/group.rb

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

+ 6 - 6
app/models/member.rb

20
   # Create Participants for this Member for all the group's future activities, where the member isn't enrolled yet.
20
   # Create Participants for this Member for all the group's future activities, where the member isn't enrolled yet.
21
   # Intended to be called after the member is added to the group.
21
   # Intended to be called after the member is added to the group.
22
   def create_future_participants!
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
       activities = activities.where(
26
       activities = activities.where(
27
-        'activities.id NOT IN (?)', self.person.activities.ids
27
+        'activities.id NOT IN (?)', person.activities.ids
28
       )
28
       )
29
     end
29
     end
30
 
30
 
31
     activities.each do |a|
31
     activities.each do |a|
32
       Participant.create!(
32
       Participant.create!(
33
         activity: a,
33
         activity: a,
34
-        person: self.person
34
+        person: person
35
       )
35
       )
36
     end
36
     end
37
   end
37
   end
40
   # Intended to be called before the member is deleted.
40
   # Intended to be called before the member is deleted.
41
   def delete_future_participants!
41
   def delete_future_participants!
42
     participants = Participant.where(
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
     participants.each(&:destroy!)
47
     participants.each(&:destroy!)

+ 12 - 12
app/models/participant.rb

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

+ 14 - 14
app/models/person.rb

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

+ 1 - 2
app/models/session.rb

28
 
28
 
29
   # @return [String] a BCrypt digest of the given string.
29
   # @return [String] a BCrypt digest of the given string.
30
   def self.digest(string)
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
     BCrypt::Password.create(string, cost: cost)
32
     BCrypt::Password.create(string, cost: cost)
34
   end
33
   end
35
 end
34
 end

+ 1 - 2
app/models/subgroup.rb

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

+ 1 - 1
app/models/token.rb

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

+ 1 - 1
app/models/user.rb

30
   # Assert that the user's email address is the same as the email address of
30
   # Assert that the user's email address is the same as the email address of
31
   # the associated Person.
31
   # the associated Person.
32
   def email_same_as_person
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
   end
34
   end
35
 end
35
 end