Browse Source

Guard clauses for early returns

Maarten van den Berg 6 years ago
parent
commit
656cfda467
10 changed files with 56 additions and 70 deletions
  1. 2 17
      .rubocop.yml
  2. 4 4
      app/helpers/activities_helper.rb
  3. 36 34
      app/helpers/authentication_helper.rb
  4. 8 9
      app/helpers/groups_helper.rb
  5. 1 1
      bin/bundle
  6. 1 1
      bin/rails
  7. 1 1
      bin/rake
  8. 1 1
      bin/setup
  9. 1 1
      bin/update
  10. 1 1
      test/test_helper.rb

+ 2 - 17
.rubocop.yml

9
 AllCops:
9
 AllCops:
10
   Exclude:
10
   Exclude:
11
     - 'db/schema.rb'
11
     - 'db/schema.rb'
12
+    - 'bin/update'
13
+    - 'bin/setup'
12
 
14
 
13
 # Offense count: 30
15
 # Offense count: 30
14
 Metrics/AbcSize:
16
 Metrics/AbcSize:
144
 Style/Documentation:
146
 Style/Documentation:
145
   Enabled: false
147
   Enabled: false
146
 
148
 
147
-# Offense count: 6
148
-# Cop supports --auto-correct.
149
-Style/ExpandPathArguments:
150
-  Exclude:
151
-    - 'bin/bundle'
152
-    - 'bin/rails'
153
-    - 'bin/rake'
154
-    - 'bin/setup'
155
-    - 'bin/update'
156
-    - 'test/test_helper.rb'
157
-
158
 # Offense count: 140
149
 # Offense count: 140
159
 # Cop supports --auto-correct.
150
 # Cop supports --auto-correct.
160
 # Configuration parameters: EnforcedStyle.
151
 # Configuration parameters: EnforcedStyle.
170
   Exclude:
161
   Exclude:
171
     - 'lib/tasks/sessions.rake'
162
     - 'lib/tasks/sessions.rake'
172
 
163
 
173
-# Offense count: 2
174
-Style/MixinUsage:
175
-  Exclude:
176
-    - 'bin/setup'
177
-    - 'bin/update'
178
-
179
 # Offense count: 1
164
 # Offense count: 1
180
 Style/MultilineTernaryOperator:
165
 Style/MultilineTernaryOperator:
181
   Exclude:
166
   Exclude:

+ 4 - 4
app/helpers/activities_helper.rb

1
 module ActivitiesHelper
1
 module ActivitiesHelper
2
   def require_organizer!
2
   def require_organizer!
3
-    unless @activity.may_change?(current_person)
4
-      flash_message(:danger, I18n.t('authentication.organizer_required'))
5
-      redirect_to group_activity_path(@group, @activity)
6
-    end
3
+    return if @activity.may_change?(current_person)
4
+
5
+    flash_message(:danger, I18n.t('authentication.organizer_required'))
6
+    redirect_to group_activity_path(@group, @activity)
7
   end
7
   end
8
 end
8
 end

+ 36 - 34
app/helpers/authentication_helper.rb

1
 module AuthenticationHelper
1
 module AuthenticationHelper
2
   # Create a new Session and set the relevant cookies.
2
   # Create a new Session and set the relevant cookies.
3
-  def log_in(user, remember, new = true)
3
+  def log_in(user, remember, new_session = true)
4
     reset_session
4
     reset_session
5
 
5
 
6
     expiry = 6.hours.since
6
     expiry = 6.hours.since
7
     session[:user_id] = user.id
7
     session[:user_id] = user.id
8
     session[:expires] = expiry
8
     session[:expires] = expiry
9
 
9
 
10
-    if new
11
-      if remember == 1
12
-        token = Session.new_token
13
-        expiry = 1.years.since
14
-        cookies.signed.permanent[:remember_token] = {
15
-          value: token,
16
-          httponly: true
17
-        }
18
-        cookies.signed.permanent[:user_id] = {
19
-          value: user.id,
20
-          httponly: true
21
-        }
22
-      else
23
-        token = nil
24
-      end
25
-      s = Session.create!(
26
-        user: user,
27
-        ip: request.remote_ip,
28
-        expires: expiry,
29
-        remember_digest: token ? Session.digest(token) : nil
30
-      )
31
-      if remember
32
-        cookies.signed.permanent[:session_id] = {
33
-          value: s.id,
34
-          httponly: true
35
-        }
36
-      else
37
-        session[:session_id] = s.id
38
-      end
10
+    return unless new_session
11
+
12
+    if remember == 1
13
+      token = Session.new_token
14
+      expiry = 1.years.since
15
+      cookies.signed.permanent[:remember_token] = {
16
+        value: token,
17
+        httponly: true
18
+      }
19
+      cookies.signed.permanent[:user_id] = {
20
+        value: user.id,
21
+        httponly: true
22
+      }
23
+    else
24
+      token = nil
25
+    end
26
+
27
+    s = Session.create!(
28
+      user: user,
29
+      ip: request.remote_ip,
30
+      expires: expiry,
31
+      remember_digest: token ? Session.digest(token) : nil
32
+    )
33
+
34
+    if remember
35
+      cookies.signed.permanent[:session_id] = {
36
+        value: s.id,
37
+        httponly: true
38
+      }
39
+    else
40
+      session[:session_id] = s.id
39
     end
41
     end
40
   end
42
   end
41
 
43
 
127
   end
129
   end
128
 
130
 
129
   def require_admin!
131
   def require_admin!
130
-    unless current_person.is_admin?
131
-      flash_message(:danger, I18n.t('authentication.admin_required'))
132
-      redirect_to '/dashboard'
133
-    end
132
+    return if current_person.is_admin?
133
+
134
+    flash_message(:danger, I18n.t('authentication.admin_required'))
135
+    redirect_to '/dashboard'
134
   end
136
   end
135
 end
137
 end

+ 8 - 9
app/helpers/groups_helper.rb

2
   def require_membership!
2
   def require_membership!
3
     return unless require_login!
3
     return unless require_login!
4
 
4
 
5
-    unless @group.is_member?(current_person) || current_person.is_admin?
6
-      flash_message(:danger, I18n.t('groups.membership_required'))
7
-      redirect_to dashboard_home_path
8
-    end
5
+    return if @group.is_member?(current_person) || current_person.is_admin?
6
+
7
+    flash_message(:danger, I18n.t('groups.membership_required'))
8
+    redirect_to dashboard_home_path
9
   end
9
   end
10
 
10
 
11
   def require_leader!
11
   def require_leader!
12
     return unless require_login!
12
     return unless require_login!
13
 
13
 
14
-    unless @group.is_leader?(current_person) ||
15
-           current_person.is_admin?
16
-      flash_message(:danger, I18n.t('groups.leadership_required'))
17
-      redirect_to dashboard_home_path
18
-    end
14
+    return if @group.is_leader?(current_person) || current_person.is_admin?
15
+
16
+    flash_message(:danger, I18n.t('groups.leadership_required'))
17
+    redirect_to dashboard_home_path
19
   end
18
   end
20
 end
19
 end

+ 1 - 1
bin/bundle

1
 #!/usr/bin/env ruby
1
 #!/usr/bin/env ruby
2
-ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
2
+ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
3
 load Gem.bin_path('bundler', 'bundle')
3
 load Gem.bin_path('bundler', 'bundle')

+ 1 - 1
bin/rails

1
 #!/usr/bin/env ruby
1
 #!/usr/bin/env ruby
2
 begin
2
 begin
3
-  load File.expand_path('../spring', __FILE__)
3
+  load File.expand_path('spring', __dir__)
4
 rescue LoadError => e
4
 rescue LoadError => e
5
   raise unless e.message.include?('spring')
5
   raise unless e.message.include?('spring')
6
 end
6
 end

+ 1 - 1
bin/rake

1
 #!/usr/bin/env ruby
1
 #!/usr/bin/env ruby
2
 begin
2
 begin
3
-  load File.expand_path('../spring', __FILE__)
3
+  load File.expand_path('spring', __dir__)
4
 rescue LoadError => e
4
 rescue LoadError => e
5
   raise unless e.message.include?('spring')
5
   raise unless e.message.include?('spring')
6
 end
6
 end

+ 1 - 1
bin/setup

4
 include FileUtils
4
 include FileUtils
5
 
5
 
6
 # path to your application root.
6
 # path to your application root.
7
-APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
7
+APP_ROOT = Pathname.new File.expand_path('..', __dir__)
8
 
8
 
9
 def system!(*args)
9
 def system!(*args)
10
   system(*args) || abort("\n== Command #{args} failed ==")
10
   system(*args) || abort("\n== Command #{args} failed ==")

+ 1 - 1
bin/update

4
 include FileUtils
4
 include FileUtils
5
 
5
 
6
 # path to your application root.
6
 # path to your application root.
7
-APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
7
+APP_ROOT = Pathname.new File.expand_path('..', __dir__)
8
 
8
 
9
 def system!(*args)
9
 def system!(*args)
10
   system(*args) || abort("\n== Command #{args} failed ==")
10
   system(*args) || abort("\n== Command #{args} failed ==")

+ 1 - 1
test/test_helper.rb

1
 ENV['RAILS_ENV'] ||= 'test'
1
 ENV['RAILS_ENV'] ||= 'test'
2
-require File.expand_path('../../config/environment', __FILE__)
2
+require File.expand_path('../config/environment', __dir__)
3
 require 'rails/test_help'
3
 require 'rails/test_help'
4
 
4
 
5
 class ActiveSupport::TestCase
5
 class ActiveSupport::TestCase