Commit 8b069d33fcb9025e4ce6925a0a11a7119a174393
1 parent
f89b6066
Exists in
master
and in
1 other branch
Rubocop: return early instead of wrapping in guard clause
Based on https://github.com/bbatsov/ruby-style-guide#no-nested-conditionals This handles a few of the cases.
Showing
6 changed files
with
65 additions
and
57 deletions
Show diff stats
.rubocop.yml
@@ -23,6 +23,9 @@ Style/IndentationConsistency: | @@ -23,6 +23,9 @@ Style/IndentationConsistency: | ||
23 | # modifiers. | 23 | # modifiers. |
24 | EnforcedStyle: rails | 24 | EnforcedStyle: rails |
25 | 25 | ||
26 | +Style/GuardClause: | ||
27 | + MinBodyLength: 10 | ||
28 | + | ||
26 | Style/AccessModifierIndentation: | 29 | Style/AccessModifierIndentation: |
27 | EnforcedStyle: outdent | 30 | EnforcedStyle: outdent |
28 | 31 |
.rubocop_todo.yml
@@ -75,11 +75,6 @@ Style/EachWithObject: | @@ -75,11 +75,6 @@ Style/EachWithObject: | ||
75 | - 'app/models/notice_fingerprinter.rb' | 75 | - 'app/models/notice_fingerprinter.rb' |
76 | - 'lib/recurse.rb' | 76 | - 'lib/recurse.rb' |
77 | 77 | ||
78 | -# Offense count: 27 | ||
79 | -# Configuration parameters: MinBodyLength. | ||
80 | -Style/GuardClause: | ||
81 | - Enabled: false | ||
82 | - | ||
83 | # Offense count: 873 | 78 | # Offense count: 873 |
84 | # Cop supports --auto-correct. | 79 | # Cop supports --auto-correct. |
85 | # Configuration parameters: EnforcedStyle, SupportedStyles. | 80 | # Configuration parameters: EnforcedStyle, SupportedStyles. |
app/controllers/application_controller.rb
@@ -23,10 +23,10 @@ protected | @@ -23,10 +23,10 @@ protected | ||
23 | # Check if the current_user is admin or not and redirect to root url if not | 23 | # Check if the current_user is admin or not and redirect to root url if not |
24 | # | 24 | # |
25 | def require_admin! | 25 | def require_admin! |
26 | - unless user_signed_in? && current_user.admin? | ||
27 | - flash[:error] = "Sorry, you don't have permission to do that" | ||
28 | - redirect_to_root | ||
29 | - end | 26 | + return if user_signed_in? && current_user.admin? |
27 | + | ||
28 | + flash[:error] = "Sorry, you don't have permission to do that" | ||
29 | + redirect_to_root | ||
30 | end | 30 | end |
31 | 31 | ||
32 | def redirect_to_root | 32 | def redirect_to_root |
app/controllers/apps_controller.rb
@@ -93,13 +93,16 @@ class AppsController < ApplicationController | @@ -93,13 +93,16 @@ class AppsController < ApplicationController | ||
93 | protected | 93 | protected |
94 | 94 | ||
95 | def initialize_subclassed_notification_service | 95 | def initialize_subclassed_notification_service |
96 | + notification_type = params[:app]. | ||
97 | + fetch(:notification_service_attributes, {}). | ||
98 | + fetch(:type, nil) | ||
99 | + return if notification_type.blank? | ||
100 | + | ||
96 | # set the app's notification service | 101 | # set the app's notification service |
97 | - if params[:app][:notification_service_attributes] && (notification_type = params[:app][:notification_service_attributes][:type]) | ||
98 | - available_notification_classes = [NotificationService] + NotificationService.subclasses | ||
99 | - notification_class = available_notification_classes.detect { |c| c.name == notification_type } | ||
100 | - if notification_class.present? | ||
101 | - app.notification_service = notification_class.new(params[:app][:notification_service_attributes]) | ||
102 | - end | 102 | + available_notification_classes = [NotificationService] + NotificationService.subclasses |
103 | + notification_class = available_notification_classes.detect { |c| c.name == notification_type } | ||
104 | + if notification_class.present? | ||
105 | + app.notification_service = notification_class.new(params[:app][:notification_service_attributes]) | ||
103 | end | 106 | end |
104 | end | 107 | end |
105 | 108 | ||
@@ -112,32 +115,38 @@ protected | @@ -112,32 +115,38 @@ protected | ||
112 | 115 | ||
113 | # email_at_notices is edited as a string, and stored as an array. | 116 | # email_at_notices is edited as a string, and stored as an array. |
114 | def parse_email_at_notices_or_set_default | 117 | def parse_email_at_notices_or_set_default |
115 | - if params[:app] && (val = params[:app][:email_at_notices]) | ||
116 | - # Sanitize negative values, split on comma, | ||
117 | - # strip, parse as integer, remove all '0's. | ||
118 | - # If empty, set as default and show an error message. | ||
119 | - email_at_notices = val.gsub(/-\d+/, "").split(",").map { |v| v.strip.to_i }.reject { |v| v == 0 } | ||
120 | - if email_at_notices.any? | ||
121 | - params[:app][:email_at_notices] = email_at_notices | ||
122 | - else | ||
123 | - default_array = params[:app][:email_at_notices] = Errbit::Config.email_at_notices | ||
124 | - flash[:error] = "Couldn't parse your notification frequency. Value was reset to default (#{default_array.join(', ')})." | ||
125 | - end | 118 | + return if params[:app].blank? |
119 | + | ||
120 | + val = params[:app][:email_at_notices] | ||
121 | + return if val.blank? | ||
122 | + | ||
123 | + # Sanitize negative values, split on comma, | ||
124 | + # strip, parse as integer, remove all '0's. | ||
125 | + # If empty, set as default and show an error message. | ||
126 | + email_at_notices = val.gsub(/-\d+/, "").split(",").map { |v| v.strip.to_i }.reject { |v| v == 0 } | ||
127 | + if email_at_notices.any? | ||
128 | + params[:app][:email_at_notices] = email_at_notices | ||
129 | + else | ||
130 | + default_array = params[:app][:email_at_notices] = Errbit::Config.email_at_notices | ||
131 | + flash[:error] = "Couldn't parse your notification frequency. Value was reset to default (#{default_array.join(', ')})." | ||
126 | end | 132 | end |
127 | end | 133 | end |
128 | 134 | ||
129 | def parse_notice_at_notices_or_set_default | 135 | def parse_notice_at_notices_or_set_default |
130 | - if params[:app][:notification_service_attributes] && (val = params[:app][:notification_service_attributes][:notify_at_notices]) | ||
131 | - # Sanitize negative values, split on comma, | ||
132 | - # strip, parse as integer, remove all '0's. | ||
133 | - # If empty, set as default and show an error message. | ||
134 | - notify_at_notices = val.gsub(/-\d+/, "").split(",").map { |v| v.strip.to_i } | ||
135 | - if notify_at_notices.any? | ||
136 | - params[:app][:notification_service_attributes][:notify_at_notices] = notify_at_notices | ||
137 | - else | ||
138 | - default_array = params[:app][:notification_service_attributes][:notify_at_notices] = Errbit::Config.notify_at_notices | ||
139 | - flash[:error] = "Couldn't parse your notification frequency. Value was reset to default (#{default_array.join(', ')})." | ||
140 | - end | 136 | + return if params[:app][:notification_service_attributes].blank? |
137 | + | ||
138 | + val = params[:app][:notification_service_attributes][:notify_at_notices] | ||
139 | + return if val.blank? | ||
140 | + | ||
141 | + # Sanitize negative values, split on comma, | ||
142 | + # strip, parse as integer, remove all '0's. | ||
143 | + # If empty, set as default and show an error message. | ||
144 | + notify_at_notices = val.gsub(/-\d+/, "").split(",").map { |v| v.strip.to_i } | ||
145 | + if notify_at_notices.any? | ||
146 | + params[:app][:notification_service_attributes][:notify_at_notices] = notify_at_notices | ||
147 | + else | ||
148 | + default_array = params[:app][:notification_service_attributes][:notify_at_notices] = Errbit::Config.notify_at_notices | ||
149 | + flash[:error] = "Couldn't parse your notification frequency. Value was reset to default (#{default_array.join(', ')})." | ||
141 | end | 150 | end |
142 | end | 151 | end |
143 | 152 |
app/models/app.rb
@@ -152,16 +152,17 @@ class App | @@ -152,16 +152,17 @@ class App | ||
152 | 152 | ||
153 | # Copy app attributes from another app. | 153 | # Copy app attributes from another app. |
154 | def copy_attributes_from(app_id) | 154 | def copy_attributes_from(app_id) |
155 | - if (copy_app = App.where(_id: app_id).first) | ||
156 | - # Copy fields | ||
157 | - (copy_app.fields.keys - %w(_id name created_at updated_at)).each do |k| | ||
158 | - send("#{k}=", copy_app.send(k)) | ||
159 | - end | ||
160 | - # Clone the embedded objects that can be changed via apps/edit (ignore errs & deploys, etc.) | ||
161 | - %w(watchers issue_tracker notification_service).each do |relation| | ||
162 | - if (obj = copy_app.send(relation)) | ||
163 | - send("#{relation}=", obj.is_a?(Array) ? obj.map(&:clone) : obj.clone) | ||
164 | - end | 155 | + copy_app = App.where(_id: app_id).first |
156 | + return if copy_app.blank? | ||
157 | + | ||
158 | + # Copy fields | ||
159 | + (copy_app.fields.keys - %w(_id name created_at updated_at)).each do |k| | ||
160 | + send("#{k}=", copy_app.send(k)) | ||
161 | + end | ||
162 | + # Clone the embedded objects that can be changed via apps/edit (ignore errs & deploys, etc.) | ||
163 | + %w(watchers issue_tracker notification_service).each do |relation| | ||
164 | + if (obj = copy_app.send(relation)) | ||
165 | + send("#{relation}=", obj.is_a?(Array) ? obj.map(&:clone) : obj.clone) | ||
165 | end | 166 | end |
166 | end | 167 | end |
167 | end | 168 | end |
lib/overrides/hoptoad_notifier/hoptoad_notifier.rb
@@ -6,18 +6,18 @@ HoptoadNotifier.module_eval do | @@ -6,18 +6,18 @@ HoptoadNotifier.module_eval do | ||
6 | class << self | 6 | class << self |
7 | private def send_notice(notice) | 7 | private def send_notice(notice) |
8 | # Log the error internally if we are not in a development environment. | 8 | # Log the error internally if we are not in a development environment. |
9 | - if configuration.public? | ||
10 | - app = App.find_or_initialize_by(name: "Self.Errbit") | ||
11 | - app.github_repo = "errbit/errbit" | ||
12 | - app.save! | ||
13 | - notice.send("api_key=", app.api_key) | 9 | + return unless configuration.public? |
14 | 10 | ||
15 | - # Create notice internally. | ||
16 | - report = ErrorReport.new(notice.to_xml) | ||
17 | - report.generate_notice! | 11 | + app = App.find_or_initialize_by(name: "Self.Errbit") |
12 | + app.github_repo = "errbit/errbit" | ||
13 | + app.save! | ||
14 | + notice.send("api_key=", app.api_key) | ||
18 | 15 | ||
19 | - logger.info "Internal error was logged to 'Self.Errbit' app." | ||
20 | - end | 16 | + # Create notice internally. |
17 | + report = ErrorReport.new(notice.to_xml) | ||
18 | + report.generate_notice! | ||
19 | + | ||
20 | + logger.info "Internal error was logged to 'Self.Errbit' app." | ||
21 | end | 21 | end |
22 | end | 22 | end |
23 | end | 23 | end |