Skip to content

Commit df2d7f0

Browse files
style: code cleanup (nanego#322)
1 parent efe9d53 commit df2d7f0

23 files changed

+107
-228
lines changed

.rubocop_todo.yml

-144
Original file line numberDiff line numberDiff line change
@@ -96,25 +96,6 @@ Naming/PredicateName:
9696
- 'app/models/port_type.rb'
9797
- 'app/models/server.rb'
9898

99-
# Offense count: 1
100-
# This cop supports safe autocorrection (--autocorrect).
101-
# Configuration parameters: PreferredName.
102-
Naming/RescuedExceptionsVariableName:
103-
Exclude:
104-
- 'app/controllers/servers_grids_controller.rb'
105-
106-
# Offense count: 1
107-
# Configuration parameters: MinSize.
108-
Performance/CollectionLiteralInLoop:
109-
Exclude:
110-
- 'lib/tasks/update_pdu_lines.rake'
111-
112-
# Offense count: 1
113-
# This cop supports safe autocorrection (--autocorrect).
114-
Performance/CompareWithBlock:
115-
Exclude:
116-
- 'app/models/frame.rb'
117-
11899
# Offense count: 2
119100
# This cop supports unsafe autocorrection (--autocorrect-all).
120101
Performance/Detect:
@@ -128,13 +109,6 @@ Performance/MapCompact:
128109
Exclude:
129110
- 'app/models/server.rb'
130111

131-
# Offense count: 1
132-
# This cop supports unsafe autocorrection (--autocorrect-all).
133-
# Configuration parameters: MaxKeyValuePairs.
134-
Performance/RedundantMerge:
135-
Exclude:
136-
- 'app/controllers/servers_controller.rb'
137-
138112
# Offense count: 3
139113
# This cop supports safe autocorrection (--autocorrect).
140114
Performance/StringReplacement:
@@ -252,14 +226,6 @@ Rails/DynamicFindBy:
252226
- 'db/migrate/20180110152701_add_attributes_to_servers.rb'
253227
- 'lib/tasks/update_pdu_lines.rake'
254228

255-
# Offense count: 1
256-
# This cop supports safe autocorrection (--autocorrect).
257-
# Configuration parameters: Include.
258-
# Include: app/models/**/*.rb
259-
Rails/EnumHash:
260-
Exclude:
261-
- 'app/models/user.rb'
262-
263229
# Offense count: 4
264230
# This cop supports safe autocorrection (--autocorrect).
265231
# Configuration parameters: EnforcedStyle.
@@ -319,13 +285,6 @@ Rails/InverseOf:
319285
- 'app/models/move.rb'
320286
- 'app/models/server.rb'
321287

322-
# Offense count: 1
323-
# Configuration parameters: Include.
324-
# Include: app/controllers/**/*.rb, app/mailers/**/*.rb
325-
Rails/LexicallyScopedActionFilter:
326-
Exclude:
327-
- 'app/controllers/users/sessions_controller.rb'
328-
329288
# Offense count: 19
330289
# This cop supports unsafe autocorrection (--autocorrect-all).
331290
# Configuration parameters: Include.
@@ -362,12 +321,6 @@ Rails/RedundantActiveRecordAllMethod:
362321
- 'db/migrate/20170803155630_init_cables_from_current_ports.rb'
363322
- 'db/migrate/20180110152701_add_attributes_to_servers.rb'
364323

365-
# Offense count: 1
366-
# This cop supports safe autocorrection (--autocorrect).
367-
Rails/RedundantForeignKey:
368-
Exclude:
369-
- 'app/models/move.rb'
370-
371324
# Offense count: 6
372325
# This cop supports safe autocorrection (--autocorrect).
373326
# Configuration parameters: EnforcedStyle, Include.
@@ -430,23 +383,11 @@ Rails/WhereNot:
430383
- 'app/models/server.rb'
431384
- 'lib/tasks/update_pdu_lines.rake'
432385

433-
# Offense count: 1
434-
# Configuration parameters: Severity.
435-
Rails/WhereNotWithMultipleConditions:
436-
Exclude:
437-
- 'app/controllers/connections_controller.rb'
438-
439386
# Offense count: 2
440387
Rake/ClassDefinitionInTask:
441388
Exclude:
442389
- 'lib/tasks/convert_activities_to_changelog_entries.rake'
443390

444-
# Offense count: 1
445-
# This cop supports safe autocorrection (--autocorrect).
446-
Rake/Desc:
447-
Exclude:
448-
- 'lib/tasks/convert_activities_to_changelog_entries.rake'
449-
450391
# Offense count: 2
451392
Rake/MethodDefinitionInTask:
452393
Exclude:
@@ -477,25 +418,11 @@ Style/ConditionalAssignment:
477418
Style/Documentation:
478419
Enabled: false
479420

480-
# Offense count: 1
481-
# This cop supports safe autocorrection (--autocorrect).
482-
Style/EmptyLambdaParameter:
483-
Exclude:
484-
- 'app/models/card.rb'
485-
486421
# Offense count: 37
487422
# This cop supports safe autocorrection (--autocorrect).
488423
Style/ExpandPathArguments:
489424
Enabled: false
490425

491-
# Offense count: 1
492-
# This cop supports safe autocorrection (--autocorrect).
493-
# Configuration parameters: EnforcedStyle.
494-
# SupportedStyles: format, sprintf, percent
495-
Style/FormatString:
496-
Exclude:
497-
- 'app/helpers/modeles_helper.rb'
498-
499426
# Offense count: 3
500427
# This cop supports safe autocorrection (--autocorrect).
501428
# Configuration parameters: MaxUnannotatedPlaceholdersAllowed, AllowedMethods, AllowedPatterns.
@@ -585,20 +512,6 @@ Style/IfUnlessModifier:
585512
- 'app/services/import_equipment_by_csv.rb'
586513
- 'db/migrate/20221220143609_add_foreign_key_constraint_to_active_storage_attachments_for_blob_id.active_storage.rb'
587514

588-
# Offense count: 1
589-
# This cop supports safe autocorrection (--autocorrect).
590-
Style/KeywordParametersOrder:
591-
Exclude:
592-
- 'spec/support/shared_examples/models/changelogable.rb'
593-
594-
# Offense count: 1
595-
# This cop supports safe autocorrection (--autocorrect).
596-
# Configuration parameters: EnforcedStyle.
597-
# SupportedStyles: line_count_dependent, lambda, literal
598-
Style/Lambda:
599-
Exclude:
600-
- 'test/services/omniauth_dynamic_full_host_test.rb'
601-
602515
# Offense count: 5
603516
# This cop supports safe autocorrection (--autocorrect).
604517
Style/MultilineIfModifier:
@@ -623,12 +536,6 @@ Style/MutableConstant:
623536
- 'app/models/pdu.rb'
624537
- 'app/models/server.rb'
625538

626-
# Offense count: 1
627-
# This cop supports safe autocorrection (--autocorrect).
628-
Style/NestedTernaryOperator:
629-
Exclude:
630-
- 'app/helpers/servers_helper.rb'
631-
632539
# Offense count: 9
633540
# This cop supports safe autocorrection (--autocorrect).
634541
# Configuration parameters: EnforcedStyle, MinBodyLength.
@@ -660,19 +567,6 @@ Style/NumericPredicate:
660567
- 'app/controllers/ports_controller.rb'
661568
- 'app/helpers/servers_helper.rb'
662569

663-
# Offense count: 1
664-
# This cop supports safe autocorrection (--autocorrect).
665-
Style/OrAssignment:
666-
Exclude:
667-
- 'app/controllers/servers_controller.rb'
668-
669-
# Offense count: 1
670-
# This cop supports safe autocorrection (--autocorrect).
671-
# Configuration parameters: AllowSafeAssignment, AllowInMultilineConditions.
672-
Style/ParenthesesAroundCondition:
673-
Exclude:
674-
- 'app/services/import_equipment_by_csv.rb'
675-
676570
# Offense count: 2
677571
# This cop supports safe autocorrection (--autocorrect).
678572
# Configuration parameters: PreferredDelimiters.
@@ -681,36 +575,6 @@ Style/PercentLiteralDelimiters:
681575
- 'app/controllers/servers_grids_controller.rb'
682576
- 'app/helpers/servers_helper.rb'
683577

684-
# Offense count: 1
685-
# This cop supports safe autocorrection (--autocorrect).
686-
Style/Proc:
687-
Exclude:
688-
- 'lib/omniauth/dynamic_full_host.rb'
689-
690-
# Offense count: 1
691-
# This cop supports safe autocorrection (--autocorrect).
692-
Style/RedundantBegin:
693-
Exclude:
694-
- 'app/services/import_equipment_by_csv.rb'
695-
696-
# Offense count: 1
697-
# This cop supports safe autocorrection (--autocorrect).
698-
Style/RedundantCapitalW:
699-
Exclude:
700-
- 'app/controllers/servers_grids_controller.rb'
701-
702-
# Offense count: 1
703-
# This cop supports safe autocorrection (--autocorrect).
704-
Style/RedundantCondition:
705-
Exclude:
706-
- 'app/helpers/servers_helper.rb'
707-
708-
# Offense count: 1
709-
# This cop supports safe autocorrection (--autocorrect).
710-
Style/RedundantConstantBase:
711-
Exclude:
712-
- 'config/environments/production.rb'
713-
714578
# Offense count: 13
715579
# This cop supports safe autocorrection (--autocorrect).
716580
Style/RedundantFreeze:
@@ -771,14 +635,6 @@ Style/RescueStandardError:
771635
- 'app/controllers/servers_grids_controller.rb'
772636
- 'lib/tasks/convert_activities_to_changelog_entries.rake'
773637

774-
# Offense count: 1
775-
# This cop supports unsafe autocorrection (--autocorrect-all).
776-
# Configuration parameters: ConvertCodeThatCanStartToReturnNil, AllowedMethods, MaxChainLength.
777-
# AllowedMethods: present?, blank?, presence, try, try!
778-
Style/SafeNavigation:
779-
Exclude:
780-
- 'app/models/server.rb'
781-
782638
# Offense count: 2
783639
# This cop supports safe autocorrection (--autocorrect).
784640
# Configuration parameters: AllowAsExpressionSeparator.

Gemfile.lock

+5-5
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ GEM
364364
orm_adapter (0.5.0)
365365
pagy (9.3.3)
366366
parallel (1.26.3)
367-
parser (3.3.7.0)
367+
parser (3.3.7.1)
368368
ast (~> 2.4.1)
369369
racc
370370
passenger (6.0.24)
@@ -486,14 +486,14 @@ GEM
486486
rspec-support (~> 3.13)
487487
rspec-support (3.13.2)
488488
rubanok (0.5.1)
489-
rubocop (1.71.0)
489+
rubocop (1.71.2)
490490
json (~> 2.3)
491491
language_server-protocol (>= 3.17.0)
492492
parallel (~> 1.10)
493493
parser (>= 3.3.0.2)
494494
rainbow (>= 2.2.2, < 4.0)
495495
regexp_parser (>= 2.9.3, < 3.0)
496-
rubocop-ast (>= 1.36.2, < 2.0)
496+
rubocop-ast (>= 1.38.0, < 2.0)
497497
ruby-progressbar (~> 1.7)
498498
unicode-display_width (>= 2.4.0, < 4.0)
499499
rubocop-ast (1.38.0)
@@ -810,7 +810,7 @@ CHECKSUMS
810810
orm_adapter (0.5.0) sha256=aa5d0be5d540cbb46d3a93e88061f4ece6a25f6e97d6a47122beb84fe595e9b9
811811
pagy (9.3.3) sha256=4831418eb4ec7cc5658ea57de559cc3a960286db36f98d4c1c65e19db0e2bb60
812812
parallel (1.26.3) sha256=d86babb7a2b814be9f4b81587bf0b6ce2da7d45969fab24d8ae4bf2bb4d4c7ef
813-
parser (3.3.7.0) sha256=7449011771e3e7881297859b849de26a6f4fccd515bece9520a87e7d2116119b
813+
parser (3.3.7.1) sha256=7dbe61618025519024ac72402a6677ead02099587a5538e84371b76659e6aca1
814814
passenger (6.0.24) sha256=acd635c0aa850af364eb3caec02d367c37e37676288d82a90650d9827413714c
815815
pg (1.5.9) sha256=761efbdf73b66516f0c26fcbe6515dc7500c3f0aa1a1b853feae245433c64fdc
816816
popper_js (2.11.8) sha256=f4b0be717fc0d50bdb3dbbc55788525a9e0e8f640b76c9971fc34ee609eadbd2
@@ -854,7 +854,7 @@ CHECKSUMS
854854
rspec-rails (7.1.0) sha256=94585b69c4086ca79afae5cc8d2c5e314f6ad32a88c927f9c065b99596e3ee47
855855
rspec-support (3.13.2) sha256=cea3a2463fd9b84b9dcc9685efd80ea701aa8f7b3decb3b3ce795ed67737dbec
856856
rubanok (0.5.1) sha256=4c496d0fe2510c47a084c556018bfd95d388e7553b741ec9138b7139c801f3ec
857-
rubocop (1.71.0) sha256=e19679efd447346ac476122313d3788ae23c38214790bcf660e984c747608bf0
857+
rubocop (1.71.2) sha256=9a7b7501aac661a338ed7ff2a5eba78e581759e1f0d3c82362b2ca217ed3f97f
858858
rubocop-ast (1.38.0) sha256=4fdf6792fe443a9a18acb12dbc8225d0d64cd1654e41fedb30e79c18edbb26ae
859859
rubocop-capybara (2.21.0) sha256=5d264efdd8b6c7081a3d4889decf1451a1cfaaec204d81534e236bc825b280ab
860860
rubocop-performance (1.23.1) sha256=f22f86a795f5e6a6180aac2c6fc172534b173a068d6ed3396d6460523e051b82

app/controllers/connections_controller.rb

+6-3
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@ def edit
3535
@to_port = @cable.connections.reject { |conn| conn.port_id.to_i == @from_port.id }.first.try(:port) if @cable.present?
3636

3737
# Destination server
38-
if @from_port.is_power_input?
39-
@to_server = @to_port.present? ? @to_port.server : @frame.pdus.first
38+
if @to_port.present?
39+
@to_server = @to_port.server
40+
elsif @from_port.is_power_input?
41+
@to_server = @frame.pdus.first
4042
else
41-
@to_server = @to_port.present? ? @to_port.server : @frame.servers.where.not(position: nil, modele_id: nil).order(:position).first
43+
@to_server = @frame.servers.where("position NOT NULL AND modele_id NOT NULL").order(:position).first
4244
end
45+
4346
if @to_server
4447
@to_server.create_missing_ports
4548
@to_server.reload

app/controllers/servers_controller.rb

+2-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def sort
4040
if positions[index].present?
4141
server = Server.find_by_id(id)
4242
new_params = { position: positions[index] }
43-
new_params.merge!({ frame_id: frame.id }) if frame.present?
43+
new_params[:frame_id] = frame.id if frame.present?
4444

4545
server.update(new_params)
4646
end
@@ -127,8 +127,7 @@ def destroy_connections
127127

128128
# Use callbacks to share common setup or constraints between actions.
129129
def set_server
130-
@server = Server.where('lower(numero) = ?', params[:id].to_s.downcase).includes(:cards => [:ports => %i[connection cable], :card_type => [:port_type]]).first
131-
@server = Server.friendly.find(params[:id].to_s.downcase) unless @server
130+
@server = Server.friendly_find_by_numero_or_name(params[:id])
132131
end
133132

134133
# Never trust parameters from the scary internet, only allow the white list through.

app/controllers/servers_grids_controller.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ def index
2020

2121
begin
2222
@servers = ServersGrid.new(@merged_params[:servers_grid])
23-
rescue => ex
23+
rescue => e
2424
@servers = ServersGrid.new(params[:servers_grid])
25-
logger.error ex.message
25+
logger.error e.message
2626
end
2727
end
2828
format.csv do

app/controllers/users/sessions_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
module Users
44
class SessionsController < Devise::SessionsController
5-
skip_before_action :verify_authenticity_token, :only => :create
5+
skip_before_action :verify_authenticity_token, only: :create # rubocop:disable Rails/LexicallyScopedActionFilter
66
end
77
end

app/helpers/modeles_helper.rb

+7-8
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
module ModelesHelper
44
def lighten_color(hex_color, amount = 0.6)
5-
if hex_color
6-
hex_color = hex_color.gsub('#', '')
7-
rgb = hex_color.scan(/../).map { |color| color.hex }
8-
rgb[0] = [(rgb[0].to_i + (255 * amount)).round, 255].min
9-
rgb[1] = [(rgb[1].to_i + (255 * amount)).round, 255].min
10-
rgb[2] = [(rgb[2].to_i + (255 * amount)).round, 255].min
11-
"#%02x%02x%02x" % rgb
12-
end
5+
hex_color = hex_color.gsub('#', '')
6+
rgb = hex_color.scan(/../).map { |color| color.hex }
7+
rgb[0] = [(rgb[0].to_i + (255 * amount)).round, 255].min
8+
rgb[1] = [(rgb[1].to_i + (255 * amount)).round, 255].min
9+
rgb[2] = [(rgb[2].to_i + (255 * amount)).round, 255].min
10+
11+
format("#%02x%02x%02x", *rgb)
1312
end
1413
end

app/helpers/servers_helper.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ def slot_label(server, component)
1414
else
1515
cards_names.join('-')
1616
end
17+
elsif component.name.present?
18+
"#{component.name.include?('SL') ? component.name[2] : component.name}"
1719
else
18-
component.name.present? ? "#{component.name.include?('SL') ? component.name[2] : component.name}" : component.position
20+
component.position
1921
end
2022
end
2123

@@ -141,7 +143,7 @@ def link_to_port_by_type(label, type, port_data, position, card_id, port_id)
141143
end
142144

143145
link_to label.to_s, edit_port_url, id: port_id, title: (port_data.present? ? "#{port_data.vlans}" : ""),
144-
class: "border border-secondary port port#{port_class} #{port_data.try(:cable_color) ? port_data.try(:cable_color) : "empty"}",
146+
class: "border border-secondary port port#{port_class} #{port_data.try(:cable_color) || "empty"}",
145147
data: { url: edit_port_url, position:, type:, controller: 'tooltip', bs_placement: 'top' }, target: :_top
146148
end
147149

app/models/card.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Card < ApplicationRecord
2020

2121
after_commit :set_twin_card
2222

23-
scope :on_patch_panels, -> () { joins(:server => { :modele => :category }).where("categories.name = 'Patch Panel'") }
23+
scope :on_patch_panels, -> { joins(:server => { :modele => :category }).where("categories.name = 'Patch Panel'") }
2424

2525
def to_s
2626
"Carte #{server} / #{card_type} / #{composant}"

app/models/frame.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def to_s
3737
end
3838

3939
def self.all_sorted
40-
Frame.includes(:islet => :room, :bay => :islet).sort { |f1, f2| f1.name_with_room_and_islet <=> f2.name_with_room_and_islet }
40+
Frame.includes(:islet => :room, :bay => :islet).sort_by(&:name_with_room_and_islet)
4141
end
4242

4343
def should_generate_new_friendly_id?

0 commit comments

Comments
 (0)