Преглед на файлове

Guard clause, if-unless, parentheses

Maarten van den Berg преди 6 години
родител
ревизия
4eeccb1671

+ 0 - 33
.rubocop.yml

@@ -162,18 +162,6 @@ Style/ExpandPathArguments:
162 162
 Style/FrozenStringLiteralComment:
163 163
   Enabled: false
164 164
 
165
-# Offense count: 14
166
-# Configuration parameters: MinBodyLength.
167
-Style/GuardClause:
168
-  Exclude:
169
-    - 'app/controllers/people_controller.rb'
170
-    - 'app/helpers/activities_helper.rb'
171
-    - 'app/helpers/authentication_helper.rb'
172
-    - 'app/helpers/groups_helper.rb'
173
-    - 'app/models/activity.rb'
174
-    - 'app/models/person.rb'
175
-    - 'app/models/user.rb'
176
-
177 165
 # Offense count: 1
178 166
 # Cop supports --auto-correct.
179 167
 # Configuration parameters: EnforcedStyle, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols.
@@ -182,27 +170,6 @@ Style/HashSyntax:
182 170
   Exclude:
183 171
     - 'lib/tasks/sessions.rake'
184 172
 
185
-# Offense count: 20
186
-# Cop supports --auto-correct.
187
-Style/IfUnlessModifier:
188
-  Exclude:
189
-    - 'app/controllers/activities_controller.rb'
190
-    - 'app/controllers/authentication_controller.rb'
191
-    - 'app/controllers/people_controller.rb'
192
-    - 'app/helpers/authentication_helper.rb'
193
-    - 'app/models/activity.rb'
194
-    - 'app/models/person.rb'
195
-    - 'app/models/user.rb'
196
-    - 'config/puma.rb'
197
-
198
-# Offense count: 1
199
-# Cop supports --auto-correct.
200
-# Configuration parameters: EnforcedStyle.
201
-# SupportedStyles: require_parentheses, require_no_parentheses, require_no_parentheses_except_multiline
202
-Style/MethodDefParentheses:
203
-  Exclude:
204
-    - 'app/helpers/dashboard_helper.rb'
205
-
206 173
 # Offense count: 2
207 174
 Style/MixinUsage:
208 175
   Exclude:

+ 2 - 6
app/controllers/activities_controller.rb

@@ -127,9 +127,7 @@ class ActivitiesController < ApplicationController
127 127
 
128 128
   # POST /activities/1/immediate_subgroups
129 129
   def immediate_subgroups
130
-    if params[:overwrite]
131
-      @activity.clear_subgroups!
132
-    end
130
+    @activity.clear_subgroups! if params[:overwrite]
133 131
 
134 132
     @activity.assign_subgroups!
135 133
 
@@ -284,9 +282,7 @@ class ActivitiesController < ApplicationController
284 282
       return
285 283
     end
286 284
 
287
-    if params[:participant]
288
-      params[:notes] = params[:participant][:notes]
289
-    end
285
+    params[:notes] = params[:participant][:notes] if params[:participant]
290 286
     participant.update_attributes(params.permit(:notes, :attending))
291 287
     head :no_content
292 288
   end

+ 1 - 3
app/controllers/authentication_controller.rb

@@ -91,9 +91,7 @@ class AuthenticationController < ApplicationController
91 91
 
92 92
   def reset_password_form
93 93
     token = Token.find_by(token: params[:token], tokentype: Token::TYPES[:password_reset])
94
-    unless token_valid? token
95
-      return
96
-    end
94
+    return unless token_valid? token
97 95
 
98 96
     render layout: 'void'
99 97
   end

+ 1 - 3
app/controllers/people_controller.rb

@@ -13,9 +13,7 @@ class PeopleController < ApplicationController
13 13
   # GET /people/1
14 14
   # GET /people/1.json
15 15
   def show
16
-    if @person != current_person
17
-      require_admin!
18
-    end
16
+    require_admin! if @person != current_person
19 17
   end
20 18
 
21 19
   # GET /people/new

+ 2 - 6
app/helpers/authentication_helper.rb

@@ -74,9 +74,7 @@ module AuthenticationHelper
74 74
 
75 75
         get_user_session
76 76
 
77
-        if @user_session.nil? || @user_session.remember_digest.nil?
78
-          return false
79
-        end
77
+        return false if @user_session.nil? || @user_session.remember_digest.nil?
80 78
 
81 79
         session_password = BCrypt::Password.new @user_session.remember_digest
82 80
 
@@ -103,9 +101,7 @@ module AuthenticationHelper
103 101
     end
104 102
 
105 103
     # Edge case if a session no longer exists in the database
106
-    unless @user_session
107
-      log_out(session_broken: true)
108
-    end
104
+    log_out(session_broken: true) unless @user_session
109 105
   end
110 106
 
111 107
   def current_user

+ 1 - 1
app/helpers/dashboard_helper.rb

@@ -1,5 +1,5 @@
1 1
 module DashboardHelper
2
-  def xeditable? obj = nil
2
+  def xeditable?(obj = nil)
3 3
     obj.may_change?(current_person)
4 4
   end
5 5
 end

+ 8 - 24
app/models/activity.rb

@@ -137,9 +137,7 @@ class Activity < ApplicationRecord
137 137
   #  2. do not have Participants (and thus, no way to confirm) yet
138 138
   def create_missing_participants!
139 139
     people = self.group.people
140
-    unless self.participants.empty?
141
-      people = people.where('people.id NOT IN (?)', self.people.ids)
142
-    end
140
+    people = people.where('people.id NOT IN (?)', self.people.ids) unless self.participants.empty?
143 141
 
144 142
     people.each do |p|
145 143
       Participant.create(
@@ -198,13 +196,9 @@ class Activity < ApplicationRecord
198 196
         a.reminder_at = Time.zone.local(rd.year, rd.month, rd.day, rt.hour, rt.min)
199 197
       end
200 198
 
201
-      unless row['subgroup_division_enabled'].blank?
202
-        a.subgroup_division_enabled = row['subgroup_division_enabled'].casecmp('y').zero?
203
-      end
199
+      a.subgroup_division_enabled = row['subgroup_division_enabled'].casecmp('y').zero? unless row['subgroup_division_enabled'].blank?
204 200
 
205
-      unless row['no_response_action'].blank?
206
-        a.no_response_action = row['no_response_action'].casecmp('p').zero?
207
-      end
201
+      a.no_response_action = row['no_response_action'].casecmp('p').zero? unless row['no_response_action'].blank?
208 202
 
209 203
       result << a
210 204
     end
@@ -271,9 +265,7 @@ class Activity < ApplicationRecord
271 265
       groups.first[0] += 1
272 266
     end
273 267
 
274
-    if mail
275
-      self.notify_subgroups!
276
-    end
268
+    self.notify_subgroups! if mail
277 269
   end
278 270
 
279 271
   def clear_subgroups!(only_assignable = true)
@@ -310,30 +302,22 @@ class Activity < ApplicationRecord
310 302
   # Assert that the deadline for participants to change the deadline, if any,
311 303
   # is set before the event starts.
312 304
   def deadline_before_start
313
-    if self.deadline > self.start
314
-      errors.add(:deadline, I18n.t('activities.errors.must_be_before_start'))
315
-    end
305
+    errors.add(:deadline, I18n.t('activities.errors.must_be_before_start')) if self.deadline > self.start
316 306
   end
317 307
 
318 308
   # Assert that the activity's end, if any, occurs after the event's start.
319 309
   def end_after_start
320
-    if self.end < self.start
321
-      errors.add(:end, I18n.t('activities.errors.must_be_after_start'))
322
-    end
310
+    errors.add(:end, I18n.t('activities.errors.must_be_after_start')) if self.end < self.start
323 311
   end
324 312
 
325 313
   # Assert that the reminder for non-response is sent while participants still
326 314
   # can change their response.
327 315
   def reminder_before_deadline
328
-    if self.reminder_at > self.deadline
329
-      errors.add(:reminder_at, I18n.t('activities.errors.must_be_before_deadline'))
330
-    end
316
+    errors.add(:reminder_at, I18n.t('activities.errors.must_be_before_deadline')) if self.reminder_at > self.deadline
331 317
   end
332 318
 
333 319
   # Assert that there is at least one divisible subgroup.
334 320
   def subgroups_for_division_present
335
-    if self.subgroups.where(is_assignable: true).none? && subgroup_division_enabled?
336
-      errors.add(:subgroup_division_enabled, I18n.t('activities.errors.cannot_divide_without_subgroups'))
337
-    end
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?
338 322
   end
339 323
 end

+ 3 - 9
app/models/person.rb

@@ -135,9 +135,7 @@ class Person < ApplicationRecord
135 135
       # Response
136 136
       yourresponse = "#{I18n.t 'activities.participant.yourresponse'}: #{p.human_attending}"
137 137
 
138
-      if p.notes.present?
139
-        yourresponse << " (#{p.notes})"
140
-      end
138
+      yourresponse << " (#{p.notes})" if p.notes.present?
141 139
 
142 140
       description_items << yourresponse
143 141
 
@@ -157,9 +155,7 @@ class Person < ApplicationRecord
157 155
 
158 156
       # Subgroups
159 157
       if a.subgroups.any?
160
-        if p.subgroup
161
-          description_items << "#{I18n.t 'activities.participant.yoursubgroup'}: #{p.subgroup}"
162
-        end
158
+        description_items << "#{I18n.t 'activities.participant.yoursubgroup'}: #{p.subgroup}" if p.subgroup
163 159
 
164 160
         subgroup_names = a.subgroups.map(&:name).join ', '
165 161
         description_items << "#{I18n.t 'activerecord.models.subgroup.other'}: #{subgroup_names}"
@@ -194,9 +190,7 @@ class Person < ApplicationRecord
194 190
 
195 191
   # Assert that the person's birth date, if any, lies in the past.
196 192
   def birth_date_cannot_be_in_future
197
-    if self.birth_date && self.birth_date > Date.today
198
-      errors.add(:birth_date, I18n.t('person.errors.cannot_future'))
199
-    end
193
+    errors.add(:birth_date, I18n.t('person.errors.cannot_future')) if self.birth_date && self.birth_date > Date.today
200 194
   end
201 195
 
202 196
   # Explicitly force nil to false in the admin field.

+ 1 - 3
app/models/user.rb

@@ -30,8 +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
-    if self.person && (self.email != self.person.email)
34
-      errors.add(:email, I18n.t('authentication.user_person_mail_mismatch'))
35
-    end
33
+    errors.add(:email, I18n.t('authentication.user_person_mail_mismatch')) if self.person && (self.email != self.person.email)
36 34
   end
37 35
 end

+ 1 - 3
config/puma.rb

@@ -17,9 +17,7 @@ env = ENV.fetch("RAILS_ENV") { "development" }
17 17
 environment env
18 18
 
19 19
 state_path "#{ENV['AARDBEI_PATH']}/tmp/pids/puma.state"
20
-if env == "production"
21
-  stdout_redirect "#{ENV['AARDBEI_PATH']}/log/stdout", "#{ENV['AARDBEI_PATH']}/log/stderr", true
22
-end
20
+stdout_redirect "#{ENV['AARDBEI_PATH']}/log/stdout", "#{ENV['AARDBEI_PATH']}/log/stderr", true if env == "production"
23 21
 
24 22
 # Specifies the number of `workers` to boot in clustered mode.
25 23
 # Workers are forked webserver processes. If using threads and workers together