From 55428843a1fd74cd5e4267abd1b8b913b4665948 Mon Sep 17 00:00:00 2001 From: Benjamin Schaaf Date: Tue, 18 Jun 2024 00:15:15 +1000 Subject: [PATCH] Rubocop update --- .rubocop.yml | 10 +++ Gemfile | 8 +- Gemfile.lock | 75 +++++++++++++------ Rakefile | 4 +- .../concerns/forums/permissions.rb | 4 +- .../concerns/leagues/match_permissions.rb | 4 +- .../concerns/leagues/roster_permissions.rb | 2 +- app/controllers/concerns/team_permissions.rb | 2 +- app/controllers/concerns/users_permissions.rb | 2 +- .../leagues/transfers_controller.rb | 4 +- app/controllers/permissions_controller.rb | 6 +- app/helpers/forums/topics_helper.rb | 6 +- app/models/api_key.rb | 2 +- app/models/concerns/paths.rb | 4 +- app/models/forums/post.rb | 2 +- app/models/forums/thread.rb | 5 +- app/models/league.rb | 6 +- app/models/league/match.rb | 5 +- app/models/league/match/pick_ban.rb | 4 +- app/models/league/roster.rb | 7 +- app/models/league/roster/transfer.rb | 2 +- app/models/league/tiebreaker.rb | 4 +- app/models/team.rb | 5 +- app/models/user.rb | 4 +- app/models/user/log.rb | 2 +- app/models/user/name_change.rb | 4 +- app/presenters/base_presenter.rb | 2 - app/presenters/league/match_presenter.rb | 2 +- .../roster/transfer_request_presenter.rb | 6 +- app/presenters/team/player_presenter.rb | 4 +- app/presenters/user/ban_presenter.rb | 2 +- app/services/forums/posts/editing_service.rb | 2 +- .../leagues/matches/comms/creation_service.rb | 2 +- .../leagues/matches/generation_service.rb | 2 +- .../leagues/rosters/creation_service.rb | 6 +- .../leagues/rosters/score_updating_service.rb | 4 +- .../rosters/transfers/cancellation_service.rb | 2 +- .../rosters/transfers/creation_service.rb | 4 +- .../forums/threads/_subscribe_btn.html.haml | 2 +- .../forums/topics/_subscribe_btn.html.haml | 2 +- app/views/leagues/matches/generate.html.haml | 2 +- app/views/leagues/rosters/new.html.haml | 2 +- config.ru | 2 +- lib/auth/model.rb | 2 +- lib/steam_id.rb | 2 +- .../controllers/users/bans_controller_spec.rb | 4 +- spec/controllers/users_controller_spec.rb | 2 +- spec/helpers/admin_helper_spec.rb | 4 - spec/helpers/leagues_helper_spec.rb | 4 - spec/helpers/meta/formats_helper_spec.rb | 4 - spec/helpers/meta/games_helper_spec.rb | 4 - spec/helpers/teams_helper_spec.rb | 4 - spec/helpers/users_helper_spec.rb | 4 - spec/models/user_spec.rb | 6 +- spec/rails_helper.rb | 4 +- spec/support/carrierwave.rb | 4 +- .../leagues/matches/show.html.haml_spec.rb | 4 +- spec/views/users/bans/index.html.haml_spec.rb | 2 +- 58 files changed, 147 insertions(+), 138 deletions(-) delete mode 100644 spec/helpers/admin_helper_spec.rb delete mode 100644 spec/helpers/leagues_helper_spec.rb delete mode 100644 spec/helpers/meta/formats_helper_spec.rb delete mode 100644 spec/helpers/meta/games_helper_spec.rb delete mode 100644 spec/helpers/teams_helper_spec.rb delete mode 100644 spec/helpers/users_helper_spec.rb diff --git a/.rubocop.yml b/.rubocop.yml index b87e75704..cc3c4a45f 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -2,6 +2,12 @@ require: rubocop-rails Rails: Enabled: true +Rails/ActionOrder: + Enabled: false +Rails/I18nLocaleTexts: + Enabled: false +Rails/UniqueValidationWithoutIndex: + Enabled: false Rails/ApplicationRecord: Exclude: - 'lib/**/*' @@ -45,6 +51,8 @@ Style/FormatStringToken: EnforcedStyle: template Style/StringConcatenation: Enabled: false +Style/NegatedIfElseCondition: + Enabled: false Style/MixinUsage: Exclude: - 'bin/setup' @@ -56,6 +64,8 @@ Naming/MethodParameterName: MinNameLength: 2 AllowedNames: - _ +Naming/VariableNumber: + Enabled: false # 'old' style Style/EmptyMethod: diff --git a/Gemfile b/Gemfile index b8d47f08d..eb6946c8b 100644 --- a/Gemfile +++ b/Gemfile @@ -1,8 +1,8 @@ source 'https://rubygems.org' gem 'rails', '~> 7.1.0' -gem 'sprockets-rails' gem 'rake', '~> 12.0' +gem 'sprockets-rails' # Use postgres gem 'pg', '~> 1.0' # Fast loading @@ -79,9 +79,9 @@ group :test do # Keep codebase clean gem 'haml_lint', require: false gem 'rails_best_practices', require: false - gem 'reek', '~> 5', require: false - gem 'rubocop', '~> 0.58', require: false - gem 'rubocop-rails', '~> 2.2.1', require: false + gem 'reek', '~> 6', require: false + gem 'rubocop', '~> 1.64.1', require: false + gem 'rubocop-rails', '~> 2.25.0', require: false gem 'codecov', require: false end diff --git a/Gemfile.lock b/Gemfile.lock index 5003a94d8..0671f7b76 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -144,7 +144,6 @@ GEM cocoon (1.2.15) code_analyzer (0.5.5) sexp_processor - codeclimate-engine-rb (0.4.2) codecov (0.6.0) simplecov (>= 0.15, < 0.22) coffee-rails (5.0.0) @@ -176,6 +175,33 @@ GEM diff-lcs (1.5.1) docile (1.4.0) drb (2.2.1) + dry-configurable (1.1.0) + dry-core (~> 1.0, < 2) + zeitwerk (~> 2.6) + dry-core (1.0.1) + concurrent-ruby (~> 1.0) + zeitwerk (~> 2.6) + dry-inflector (1.0.0) + dry-initializer (3.1.1) + dry-logic (1.5.0) + concurrent-ruby (~> 1.0) + dry-core (~> 1.0, < 2) + zeitwerk (~> 2.6) + dry-schema (1.13.4) + concurrent-ruby (~> 1.0) + dry-configurable (~> 1.0, >= 1.0.1) + dry-core (~> 1.0, < 2) + dry-initializer (~> 3.0) + dry-logic (>= 1.4, < 2) + dry-types (>= 1.7, < 2) + zeitwerk (~> 2.6) + dry-types (1.7.2) + bigdecimal (~> 3.0) + concurrent-ruby (~> 1.0) + dry-core (~> 1.0) + dry-inflector (~> 1.0) + dry-logic (~> 1.4) + zeitwerk (~> 2.6) erubi (1.12.0) erubis (2.7.0) execjs (2.9.1) @@ -224,7 +250,7 @@ GEM thor (>= 0.14, < 2.0) json (2.7.2) jsonapi-renderer (0.2.2) - kwalify (0.7.2) + language_server-protocol (3.17.0.3) launchy (3.0.1) addressable (~> 2.8) childprocess (~> 5.0) @@ -283,11 +309,11 @@ GEM parallel (1.24.0) parallel_tests (4.7.1) parallel - parser (2.7.2.0) + parser (3.3.3.0) ast (~> 2.4.1) + racc pg (1.5.6) popper_js (1.16.1) - psych (3.1.0) public_suffix (5.0.5) racc (1.8.0) rack (2.2.9) @@ -342,12 +368,11 @@ GEM rake (12.3.3) rdoc (6.3.4.1) redcarpet (3.6.0) - reek (5.6.0) - codeclimate-engine-rb (~> 0.4.0) - kwalify (~> 0.7.0) - parser (>= 2.5.0.0, < 2.8, != 2.5.1.1) - psych (~> 3.1.0) + reek (6.3.0) + dry-schema (~> 1.13.0) + parser (~> 3.3.0) rainbow (>= 2.0, < 4.0) + rexml (~> 3.1) regexp_parser (2.9.2) reline (0.5.7) io-console (~> 0.5) @@ -378,20 +403,24 @@ GEM rspec-mocks (~> 3.13) rspec-support (~> 3.13) rspec-support (3.13.1) - rubocop (0.93.1) + rubocop (1.64.1) + json (~> 2.3) + language_server-protocol (>= 3.17.0) parallel (~> 1.10) - parser (>= 2.7.1.5) + parser (>= 3.3.0.2) rainbow (>= 2.2.2, < 4.0) - regexp_parser (>= 1.8) - rexml - rubocop-ast (>= 0.6.0) + regexp_parser (>= 1.8, < 3.0) + rexml (>= 3.2.5, < 4.0) + rubocop-ast (>= 1.31.1, < 2.0) ruby-progressbar (~> 1.7) - unicode-display_width (>= 1.4.0, < 2.0) - rubocop-ast (1.4.1) - parser (>= 2.7.1.5) - rubocop-rails (2.2.1) + unicode-display_width (>= 2.4.0, < 3.0) + rubocop-ast (1.31.3) + parser (>= 3.3.1.0) + rubocop-rails (2.25.0) + activesupport (>= 4.2.0) rack (>= 1.1) - rubocop (>= 0.72.0) + rubocop (>= 1.33.0, < 2.0) + rubocop-ast (>= 1.31.1, < 2.0) ruby-openid (2.9.2) ruby-progressbar (1.13.0) ruby-vips (2.2.1) @@ -444,7 +473,7 @@ GEM concurrent-ruby (~> 1.0) uglifier (4.2.0) execjs (>= 0.3.0, < 3) - unicode-display_width (1.8.0) + unicode-display_width (2.5.0) uniform_notifier (1.16.0) warden (1.2.9) rack (>= 2.0.9) @@ -501,10 +530,10 @@ DEPENDENCIES rails_best_practices rake (~> 12.0) redcarpet - reek (~> 5) + reek (~> 6) rspec-rails (~> 6) - rubocop (~> 0.58) - rubocop-rails (~> 2.2.1) + rubocop (~> 1.64.1) + rubocop-rails (~> 2.25.0) sass-rails (~> 6) scanf shoulda-matchers diff --git a/Rakefile b/Rakefile index eb3b819d0..7880967d5 100644 --- a/Rakefile +++ b/Rakefile @@ -17,7 +17,7 @@ begin RuboCop::RakeTask.new Reek::Rake::Task.new - task :rbp do + task rbp: [:environment] do require 'rails_best_practices' app_root = Rake.application.original_dir @@ -34,7 +34,7 @@ rescue LoadError puts 'Test tasks not available' end -task :log do +task log: [:environment] do ActiveRecord::Base.logger = Logger.new($stdout) end diff --git a/app/controllers/concerns/forums/permissions.rb b/app/controllers/concerns/forums/permissions.rb index 7f470fe4f..30ca5511a 100644 --- a/app/controllers/concerns/forums/permissions.rb +++ b/app/controllers/concerns/forums/permissions.rb @@ -11,7 +11,7 @@ def user_can_manage_topic?(topic = nil) return user_can_manage_forums? unless topic - user_signed_in? && (topic.not_isolated? && user_can_manage_forums? || + user_signed_in? && ((topic.not_isolated? && user_can_manage_forums?) || user_can_manage_topics?(topic.ancestors) || current_user.can?(:manage, topic)) end @@ -41,7 +41,7 @@ def user_can_manage_thread?(thread = nil) return user_can_manage_forums? unless thread - user_signed_in? && (thread.not_isolated? && user_can_manage_forums? || + user_signed_in? && ((thread.not_isolated? && user_can_manage_forums?) || user_can_manage_topics?(thread.ancestors) || current_user.can?(:manage, thread)) end diff --git a/app/controllers/concerns/leagues/match_permissions.rb b/app/controllers/concerns/leagues/match_permissions.rb index 8760d9bc1..ad83e2205 100644 --- a/app/controllers/concerns/leagues/match_permissions.rb +++ b/app/controllers/concerns/leagues/match_permissions.rb @@ -18,8 +18,8 @@ def user_can_either_teams? def user_can_confirm_score? status = @match.status - user_can_home_team? && status == 'submitted_by_away_team' || - user_can_away_team? && status == 'submitted_by_home_team' + (user_can_home_team? && status == 'submitted_by_away_team') || + (user_can_away_team? && status == 'submitted_by_home_team') end def user_can_submit_team_score? diff --git a/app/controllers/concerns/leagues/roster_permissions.rb b/app/controllers/concerns/leagues/roster_permissions.rb index 390213677..d6a6f5ee1 100644 --- a/app/controllers/concerns/leagues/roster_permissions.rb +++ b/app/controllers/concerns/leagues/roster_permissions.rb @@ -16,7 +16,7 @@ def user_can_edit_roster?(roster = nil) disbanded = roster&.disbanded? user_can_edit_league?(roster.league) || - user_signed_in? && current_user.can?(:edit, roster.team) && user_not_banned? && !disbanded + (user_signed_in? && current_user.can?(:edit, roster.team) && user_not_banned? && !disbanded) end def user_can_disband_roster?(roster = nil) diff --git a/app/controllers/concerns/team_permissions.rb b/app/controllers/concerns/team_permissions.rb index 891de9f06..61e8626d0 100644 --- a/app/controllers/concerns/team_permissions.rb +++ b/app/controllers/concerns/team_permissions.rb @@ -8,7 +8,7 @@ def user_can_create_team? def user_can_edit_team?(team = nil) team ||= @team - user_signed_in? && current_user.can?(:edit, team) && current_user.can?(:use, :teams) || + (user_signed_in? && current_user.can?(:edit, team) && current_user.can?(:use, :teams)) || user_can_edit_teams? end diff --git a/app/controllers/concerns/users_permissions.rb b/app/controllers/concerns/users_permissions.rb index 7c9d6d2b6..b4ffd59a5 100644 --- a/app/controllers/concerns/users_permissions.rb +++ b/app/controllers/concerns/users_permissions.rb @@ -2,7 +2,7 @@ module UsersPermissions extend ActiveSupport::Concern def user_can_edit_user? - user_signed_in? && current_user == @user && current_user.can?(:use, :users) || + (user_signed_in? && current_user == @user && current_user.can?(:use, :users)) || user_can_edit_users? end diff --git a/app/controllers/leagues/transfers_controller.rb b/app/controllers/leagues/transfers_controller.rb index 5eda87c21..15129e288 100644 --- a/app/controllers/leagues/transfers_controller.rb +++ b/app/controllers/leagues/transfers_controller.rb @@ -28,7 +28,7 @@ def index def update Transfers::ApprovalService.call(@transfer_request, current_user) - flash[:error] = @transfer_request.errors.full_messages.first + flash.now[:error] = @transfer_request.errors.full_messages.first index render :index end @@ -36,7 +36,7 @@ def update def destroy Transfers::DenialService.call(@transfer_request, current_user) - flash[:error] = @transfer_request.errors.full_messages.first + flash.now[:error] = @transfer_request.errors.full_messages.first index render :index end diff --git a/app/controllers/permissions_controller.rb b/app/controllers/permissions_controller.rb index 04301da49..55bebd327 100644 --- a/app/controllers/permissions_controller.rb +++ b/app/controllers/permissions_controller.rb @@ -28,7 +28,7 @@ def users excluded_users = users_which_can.select(:id) @users_without_permission = target_users.search(params[:q]) - .where('users.id NOT IN (?)', excluded_users) + .where.not(users: { id: excluded_users }) .paginate(page: params[:page]) end @@ -85,10 +85,10 @@ def user_can_edit_permissions? end def ensure_valid_target - redirect_back if subject? && ![:team, :forums_topic].include?(@subject) + redirect_back if subject? && [:team, :forums_topic].exclude?(@subject) end def redirect_back(options = {}) - super(**{ fallback_location: permissions_path }.merge(options)) + super(fallback_location: permissions_path, **options) end end diff --git a/app/helpers/forums/topics_helper.rb b/app/helpers/forums/topics_helper.rb index c54dcee54..1fbb1d503 100644 --- a/app/helpers/forums/topics_helper.rb +++ b/app/helpers/forums/topics_helper.rb @@ -2,11 +2,11 @@ module Forums module TopicsHelper include Forums::Permissions - ISOLATE_CONFIRM_MESSAGE = "Are you sure you want to unisolate this Topic?\n"\ + ISOLATE_CONFIRM_MESSAGE = "Are you sure you want to unisolate this Topic?\n" \ 'This means any admin will be able to manage this Topic.'.freeze - UNISOLATE_CONFIRM_MESSAGE = "Are you sure you want to isolate this Topic?\n"\ - 'This means only you and anyone you give access '\ + UNISOLATE_CONFIRM_MESSAGE = "Are you sure you want to isolate this Topic?\n" \ + 'This means only you and anyone you give access ' \ 'to will be able to manage this Topic.'.freeze end end diff --git a/app/models/api_key.rb b/app/models/api_key.rb index 7cbcb0177..9f2e25eb2 100644 --- a/app/models/api_key.rb +++ b/app/models/api_key.rb @@ -2,7 +2,7 @@ class APIKey < ApplicationRecord belongs_to :user, optional: true validates :name, allow_blank: true, length: { maximum: 64 } - validates :user, allow_nil: true, uniqueness: true + # validates :user, allow_nil: true, uniqueness: true validates :key, presence: true before_validation :generate_unique_key, on: :create diff --git a/app/models/concerns/paths.rb b/app/models/concerns/paths.rb index 146f70c1b..dc186c6cf 100644 --- a/app/models/concerns/paths.rb +++ b/app/models/concerns/paths.rb @@ -8,7 +8,7 @@ def paths class_methods do attr_reader :paths_class - # rubocop:disable Metrics/MethodLength, Lint/MissingSuper + # rubocop:disable Metrics/MethodLength def paths(&block) @paths_class = Class.new do include Rails.application.routes.url_helpers @@ -40,6 +40,6 @@ def method_missing(method, *args, &block) @paths_class.class_eval(&block) end - # rubocop:enable Metrics/MethodLength, Lint/MissingSuper + # rubocop:enable Metrics/MethodLength end end diff --git a/app/models/forums/post.rb b/app/models/forums/post.rb index 7d7f24fd5..5c47d4151 100644 --- a/app/models/forums/post.rb +++ b/app/models/forums/post.rb @@ -42,7 +42,7 @@ def self.page_of(post) return 1 unless post post_index = post.thread.posts.where('created_at < ?', post.created_at).count - post_index / Post.per_page + 1 + (post_index / Post.per_page) + 1 end end end diff --git a/app/models/forums/thread.rb b/app/models/forums/thread.rb index bb5a754da..8c4c6a35d 100644 --- a/app/models/forums/thread.rb +++ b/app/models/forums/thread.rb @@ -27,15 +27,14 @@ class Thread < ApplicationRecord 'forums_threads.hidden' => nil, } + after_initialize :set_defaults, unless: :persisted? before_create :update_depth before_update :update_depth, if: :topic_id_changed? - after_initialize :set_defaults, unless: :persisted? - before_update do if hidden_changed? sign = hidden? ? :- : :+ - ActiveRecord::Base.connection.exec_update(<<-SQL, 'SQL', [id]) + ActiveRecord::Base.connection.exec_update(<<-SQL.squish, 'SQL', [id]) UPDATE users SET public_forums_posts_count = public_forums_posts_count #{sign} ( SELECT COUNT(1) FROM forums_posts WHERE thread_id = $1 AND created_by_id = users.id) diff --git a/app/models/league.rb b/app/models/league.rb index 0024a6589..fd7fdc6ba 100644 --- a/app/models/league.rb +++ b/app/models/league.rb @@ -21,7 +21,7 @@ class League < ApplicationRecord has_many :titles, class_name: 'User::Title', dependent: :destroy - enum status: [:hidden, :running, :completed] + enum status: { hidden: 0, running: 1, completed: 2 } validates :name, presence: true, length: { in: 1..64 } validates :description, presence: true @@ -52,7 +52,7 @@ class League < ApplicationRecord validates :points_per_forfeit_loss, presence: true # Scheduling - enum schedule: [:manual, :weeklies] + enum schedule: { manual: 0, weeklies: 1 } has_one :weekly_scheduler, inverse_of: :league, class_name: 'League::Schedulers::Weekly', dependent: :destroy accepts_nested_attributes_for :weekly_scheduler @@ -76,7 +76,7 @@ class League < ApplicationRecord end) def entered?(user) - players.where(user: user).exists? + players.exists?(user: user) end def roster_for(user) diff --git a/app/models/league/match.rb b/app/models/league/match.rb index 59e4c7f88..cd79e8f24 100644 --- a/app/models/league/match.rb +++ b/app/models/league/match.rb @@ -20,13 +20,14 @@ class Match < ApplicationRecord validates :rounds, associated: true # Make *really* sure all rounds are valid - enum status: [:pending, :submitted_by_home_team, :submitted_by_away_team, :confirmed] + enum status: { pending: 0, submitted_by_home_team: 1, submitted_by_away_team: 2, confirmed: 3 } validates :status, presence: true validates :has_winner, inclusion: { in: [true, false] } validates :allow_round_draws, inclusion: { in: [true, false] } - enum forfeit_by: [:no_forfeit, :home_team_forfeit, :away_team_forfeit, :mutual_forfeit, :technical_forfeit] + enum forfeit_by: { no_forfeit: 0, home_team_forfeit: 1, away_team_forfeit: 2, mutual_forfeit: 3, +technical_forfeit: 4 } validates :forfeit_by, presence: true validates :round_name, presence: true, allow_blank: true validates :round_number, presence: true, numericality: { greater_than_or_equal_to: 0 } diff --git a/app/models/league/match/pick_ban.rb b/app/models/league/match/pick_ban.rb index 61ce5ce8d..e89142027 100644 --- a/app/models/league/match/pick_ban.rb +++ b/app/models/league/match/pick_ban.rb @@ -7,8 +7,8 @@ class PickBan < ApplicationRecord belongs_to :picked_by, class_name: 'User', optional: true belongs_to :map, class_name: 'Map', optional: true - enum kind: [:pick, :ban, :deferred], _prefix: :kind - enum team: [:home_team, :away_team] + enum kind: { pick: 0, ban: 1, deferred: 2 }, _prefix: :kind + enum team: { home_team: 0, away_team: 1 } validates :deferrable, inclusion: { in: [true, false] } diff --git a/app/models/league/roster.rb b/app/models/league/roster.rb index 07a31e1f5..f7ff4301a 100644 --- a/app/models/league/roster.rb +++ b/app/models/league/roster.rb @@ -54,13 +54,12 @@ class Roster < ApplicationRecord after_destroy { League.decrement_counter(:rosters_count, league.id) } # rubocop:enable Rails/SkipsModelValidations + after_initialize :set_defaults, unless: :persisted? after_create :trigger_score_update!, if: :approved? after_save do trigger_score_update! if [:ranking, :seeding, :approved, :disbanded].any? { |a| saved_change_to_attribute?(a) } end - after_initialize :set_defaults, unless: :persisted? - def self.matches Match.for_roster(all.map(&:id)) end @@ -95,7 +94,7 @@ def remove_player!(user) end def on_roster?(user) - players.where(user: user).exists? + players.exists?(user: user) end def tentative_player_count @@ -149,7 +148,7 @@ def within_roster_size_limits def unique_within_league return if league.blank? - errors.add(:base, 'Can only sign up once') if league.rosters.where(team: team).exists? + errors.add(:base, 'Can only sign up once') if league.rosters.exists?(team: team) end def validate_schedule diff --git a/app/models/league/roster/transfer.rb b/app/models/league/roster/transfer.rb index 8b3c5f22f..50617a2b0 100644 --- a/app/models/league/roster/transfer.rb +++ b/app/models/league/roster/transfer.rb @@ -2,7 +2,7 @@ class League class Roster class Transfer < ApplicationRecord belongs_to :user - belongs_to :roster, class_name: 'Roster', foreign_key: 'roster_id' + belongs_to :roster, class_name: 'Roster' delegate :team, to: :roster, allow_nil: true delegate :division, to: :roster, allow_nil: true delegate :league, to: :division, allow_nil: true diff --git a/app/models/league/tiebreaker.rb b/app/models/league/tiebreaker.rb index dabb450ce..f028ee969 100644 --- a/app/models/league/tiebreaker.rb +++ b/app/models/league/tiebreaker.rb @@ -2,8 +2,8 @@ class League class Tiebreaker < ApplicationRecord belongs_to :league, inverse_of: :tiebreakers - enum kind: [:round_wins, :round_score_sum, :round_wins_against_tied_rosters, :normalized_round_score, - :round_score_difference, :buchholz, :median_buchholz] + enum kind: { round_wins: 0, round_score_sum: 1, round_wins_against_tied_rosters: 2, +normalized_round_score: 3, round_score_difference: 4, buchholz: 5, median_buchholz: 6 } def value_for(roster) send("#{kind}_value_for", roster) diff --git a/app/models/team.rb b/app/models/team.rb index a226e4b8f..a2f04b62d 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -59,13 +59,12 @@ def remove_player!(user) end def on_roster?(user) - players.where(user: user).exists? + players.exists?(user: user) end def entered?(comp) rosters.joins(:division) - .where(league_divisions: { league_id: comp.id }) - .exists? + .exists?(league_divisions: { league_id: comp.id }) end def reset_query_cache! diff --git a/app/models/user.rb b/app/models/user.rb index 9718bc3a7..6f25524b3 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -17,7 +17,7 @@ class User < ApplicationRecord private :team_players, :team_players= has_many :teams, through: :team_players has_many :team_invites, class_name: 'Team::Invite', dependent: :destroy - has_many :team_transfers, -> { order(created_at: :desc) }, class_name: 'Team::Transfer' + has_many :team_transfers, -> { order(created_at: :desc) }, class_name: 'Team::Transfer', dependent: :destroy has_many :roster_players, class_name: 'League::Roster::Player', dependent: :restrict_with_exception private :roster_players, :roster_players= @@ -30,7 +30,7 @@ class User < ApplicationRecord has_many :forums_posts, class_name: 'Forums::Post', inverse_of: :created_by, foreign_key: :created_by, dependent: :destroy has_many :public_forums_posts, -> { publicly_viewable }, class_name: 'Forums::Post', inverse_of: :created_by, - foreign_key: :created_by + foreign_key: :created_by, dependent: :destroy has_many :logs, class_name: 'User::Log', dependent: :destroy diff --git a/app/models/user/log.rb b/app/models/user/log.rb index 8b57033b5..7b5334871 100644 --- a/app/models/user/log.rb +++ b/app/models/user/log.rb @@ -3,7 +3,7 @@ class Log < ApplicationRecord belongs_to :user def self.log_user!(user, ip) - connection.exec_query(<<-SQL, 'log_user', [user.id, ip, Time.now.utc]) + connection.exec_query(<<-SQL.squish, 'log_user', [user.id, ip, Time.now.utc]) INSERT INTO user_logs (user_id, ip, first_seen_at, last_seen_at) VALUES ($1, $2::inet, $3, $3) ON CONFLICT (user_id, ip) DO UPDATE SET last_seen_at = $3 diff --git a/app/models/user/name_change.rb b/app/models/user/name_change.rb index de9ff2b92..3beb9125b 100644 --- a/app/models/user/name_change.rb +++ b/app/models/user/name_change.rb @@ -44,8 +44,8 @@ def only_one_request_per_user def name_not_already_used return if name.blank? - if pending? && (User.where(name: name).exists? || - NameChange.pending.where(name: name).exists?) + if pending? && (User.exists?(name: name) || + NameChange.pending.exists?(name: name)) errors.add(:name, 'Must be unique') end end diff --git a/app/presenters/base_presenter.rb b/app/presenters/base_presenter.rb index 991a1a81c..62762da36 100644 --- a/app/presenters/base_presenter.rb +++ b/app/presenters/base_presenter.rb @@ -18,7 +18,6 @@ def initialize(object, view_context) private - # rubocop:disable Lint/MissingSuper def respond_to_missing?(method, _include_private = false) view_context.respond_to?(method) end @@ -30,5 +29,4 @@ def method_missing(method, *args, &block) super end end - # rubocop:enable Lint/MissingSuper end diff --git a/app/presenters/league/match_presenter.rb b/app/presenters/league/match_presenter.rb index 0724b69c5..c2a16f972 100644 --- a/app/presenters/league/match_presenter.rb +++ b/app/presenters/league/match_presenter.rb @@ -110,7 +110,7 @@ def match_name def team_score(symbol) if match.no_forfeit? - match.rounds.map(&"#{symbol}_score".to_sym).join(':') + match.rounds.map(&:"#{symbol}_score").join(':') elsif match.send("#{symbol}_forfeit?") || match.mutual_forfeit? 'ff' else diff --git a/app/presenters/league/roster/transfer_request_presenter.rb b/app/presenters/league/roster/transfer_request_presenter.rb index 578ec095f..841bbb159 100644 --- a/app/presenters/league/roster/transfer_request_presenter.rb +++ b/app/presenters/league/roster/transfer_request_presenter.rb @@ -4,13 +4,13 @@ class TransferRequestPresenter < BasePresenter presents :transfer_request # rubocop:disable Rails/OutputSafety - PENDING_MESSAGE = '%{created_by} requested %{user} be transferred %{direction} '\ + PENDING_MESSAGE = '%{created_by} requested %{user} be transferred %{direction} ' \ '%{roster}%{leaving_message}'.html_safe - APPROVED_MESSAGE = '%{approved_by} approved %{user}\'s transfer %{direction} '\ + APPROVED_MESSAGE = '%{approved_by} approved %{user}\'s transfer %{direction} ' \ '%{roster}%{leaving_message} (requested by %{created_by})'.html_safe - DENIED_MESSAGE = '%{denied_by} denied %{user}\'s transfer %{direction} '\ + DENIED_MESSAGE = '%{denied_by} denied %{user}\'s transfer %{direction} ' \ '%{roster}%{leaving_message} (requested by %{created_by})'.html_safe LEAVING_MESSAGE = ', out of %{leaving_roster} in %{division}'.html_safe diff --git a/app/presenters/team/player_presenter.rb b/app/presenters/team/player_presenter.rb index ecc320468..dbc1ccef7 100644 --- a/app/presenters/team/player_presenter.rb +++ b/app/presenters/team/player_presenter.rb @@ -7,14 +7,14 @@ def user end def leave_button - content = safe_join(['Leave', tag(:span, class: 'glyphicon glyphicon-remove')]) + content = safe_join(['Leave', tag.span(class: 'glyphicon glyphicon-remove')]) link_to(content, leave_team_path(player.team), method: :patch, class: 'btn btn-danger', data: { confirm: leave_confirm_message }) end def kick_button - content = safe_join(['Kick', tag(:span, class: 'glyphicon glyphicon-remove')]) + content = safe_join(['Kick', tag.span(class: 'glyphicon glyphicon-remove')]) link_to(content, kick_team_path(player.team, user_id: player.user.id), method: :patch, class: 'btn btn-danger', data: { confirm: kick_confirm_message }) diff --git a/app/presenters/user/ban_presenter.rb b/app/presenters/user/ban_presenter.rb index 774540c82..57cfef40b 100644 --- a/app/presenters/user/ban_presenter.rb +++ b/app/presenters/user/ban_presenter.rb @@ -11,7 +11,7 @@ def from end def title_text - safe_join([expire_s, reason_s], tag(:br)) + safe_join([expire_s, reason_s], tag.br) end def started_at diff --git a/app/services/forums/posts/editing_service.rb b/app/services/forums/posts/editing_service.rb index 3c843e309..7b708a80a 100644 --- a/app/services/forums/posts/editing_service.rb +++ b/app/services/forums/posts/editing_service.rb @@ -6,7 +6,7 @@ module EditingService def call(user, post, params) post.transaction do post.assign_attributes(params) - return post unless post.changed? + next unless post.changed? post.save || rollback! diff --git a/app/services/leagues/matches/comms/creation_service.rb b/app/services/leagues/matches/comms/creation_service.rb index 6db657880..b766993d0 100644 --- a/app/services/leagues/matches/comms/creation_service.rb +++ b/app/services/leagues/matches/comms/creation_service.rb @@ -21,7 +21,7 @@ def call(creator, match, params) private def notify_captains!(user, match) - message = "'#{user.name}' posted a message on the match: "\ + message = "'#{user.name}' posted a message on the match: " \ "'#{match.home_team.name}' vs '#{match.away_team.name}'" link = match_path(match) diff --git a/app/services/leagues/matches/generation_service.rb b/app/services/leagues/matches/generation_service.rb index 192b7abae..3ee93bc6d 100644 --- a/app/services/leagues/matches/generation_service.rb +++ b/app/services/leagues/matches/generation_service.rb @@ -13,7 +13,7 @@ def call(division, match_params, tournament_system, tournament_options) create_notifications_for_matches(matches) rescue League::Division::GenerationError => e - return invalid_match(match_params, e) + invalid_match = invalid_match(match_params, e) end invalid_match diff --git a/app/services/leagues/rosters/creation_service.rb b/app/services/leagues/rosters/creation_service.rb index 9637a831b..3ea1eaac0 100644 --- a/app/services/leagues/rosters/creation_service.rb +++ b/app/services/leagues/rosters/creation_service.rb @@ -19,9 +19,9 @@ def call(league, team, params) private def notify_players(roster, league) - msg = 'You have been entered in '\ - "#{league.name.truncate(48, separator: ' ')} "\ - 'with '\ + msg = 'You have been entered in ' \ + "#{league.name.truncate(48, separator: ' ')} " \ + 'with ' \ "#{roster.name.truncate(48, separator: ' ')}." link = team_path(roster.team) diff --git a/app/services/leagues/rosters/score_updating_service.rb b/app/services/leagues/rosters/score_updating_service.rb index 93925572b..3b9c38dd6 100644 --- a/app/services/leagues/rosters/score_updating_service.rb +++ b/app/services/leagues/rosters/score_updating_service.rb @@ -20,7 +20,7 @@ def call_each_division(league) def call(league, division) rosters = division.rosters.select(*SCORE_ATTRIBUTES).to_a - roster_id_map = rosters.map { |roster| [roster.id, roster] }.to_h + roster_id_map = rosters.index_by(&:id) # Gather stats for later calculations roster_results = Hash.new { |h, k| h[k] = [] } @@ -119,7 +119,7 @@ def update_rosters(league, rosters, roster_results, opponents_map) ) roster.points = calculate_points(league, roster) points_map[roster.points].push roster - roster.normalized_round_score = roster.won_rounds_count * 1.0 + roster.drawn_rounds_count * 0.5 + roster.normalized_round_score = (roster.won_rounds_count * 1.0) + (roster.drawn_rounds_count * 0.5) end # Update scores based on opponents diff --git a/app/services/leagues/rosters/transfers/cancellation_service.rb b/app/services/leagues/rosters/transfers/cancellation_service.rb index d999876bb..505ce81b5 100644 --- a/app/services/leagues/rosters/transfers/cancellation_service.rb +++ b/app/services/leagues/rosters/transfers/cancellation_service.rb @@ -25,7 +25,7 @@ def request_msg(request) end def notify_user(request, user, roster) - msg = "The request for you to transfer #{request_msg(request)} "\ + msg = "The request for you to transfer #{request_msg(request)} " \ "#{roster.name} for #{roster.league.name} has been cancelled" link = team_path(roster.team) diff --git a/app/services/leagues/rosters/transfers/creation_service.rb b/app/services/leagues/rosters/transfers/creation_service.rb index 49a699ad8..fc13e6006 100644 --- a/app/services/leagues/rosters/transfers/creation_service.rb +++ b/app/services/leagues/rosters/transfers/creation_service.rb @@ -49,7 +49,7 @@ def request_msg(request) end def request_notify_user(request, user, roster) - msg = "It has been requested for you to transfer #{request_msg(request)} "\ + msg = "It has been requested for you to transfer #{request_msg(request)} " \ "#{roster.name} for #{roster.league.name}" link = team_path(roster.team) @@ -57,7 +57,7 @@ def request_notify_user(request, user, roster) end def transfer_notify_user(request, user, roster) - msg = "You have been transferred #{request_msg(request)} "\ + msg = "You have been transferred #{request_msg(request)} " \ "#{roster.name} for #{roster.league.name}" link = team_path(roster.team) diff --git a/app/views/forums/threads/_subscribe_btn.html.haml b/app/views/forums/threads/_subscribe_btn.html.haml index 4804c7e7b..535231a88 100644 --- a/app/views/forums/threads/_subscribe_btn.html.haml +++ b/app/views/forums/threads/_subscribe_btn.html.haml @@ -1,4 +1,4 @@ -- subscribed = current_user.forums_subscriptions.where(thread: thread).exists? +- subscribed = current_user.forums_subscriptions.exists?(thread: thread) = button_to toggle_subscription_for_forums_thread_path(thread), method: :patch, class: "btn btn-sm #{subscribed ? 'btn-danger' : 'btn-primary'}" do diff --git a/app/views/forums/topics/_subscribe_btn.html.haml b/app/views/forums/topics/_subscribe_btn.html.haml index 9caa1905d..f5ce03e80 100644 --- a/app/views/forums/topics/_subscribe_btn.html.haml +++ b/app/views/forums/topics/_subscribe_btn.html.haml @@ -1,4 +1,4 @@ -- subscribed = current_user.forums_subscriptions.where(topic: topic).exists? +- subscribed = current_user.forums_subscriptions.exists?(topic: topic) = button_to toggle_subscription_for_forums_topic_path(topic), method: :patch, class: "btn btn-sm #{subscribed ? 'btn-danger' : 'btn-primary'}" do diff --git a/app/views/leagues/matches/generate.html.haml b/app/views/leagues/matches/generate.html.haml index ece22ce37..65144d28a 100644 --- a/app/views/leagues/matches/generate.html.haml +++ b/app/views/leagues/matches/generate.html.haml @@ -26,7 +26,7 @@ - tournament_systems.each do |system| - klass = system == @tournament_system ? 'active' : '' .tab-pane{ id: system, class: klass } - - data_name = "#{system}_tournament".to_sym + - data_name = :"#{system}_tournament" - value = instance_variable_get("@#{data_name}") = render "leagues/matches/tournament_systems/#{system}_form", f: f, data_name => value diff --git a/app/views/leagues/rosters/new.html.haml b/app/views/leagues/rosters/new.html.haml index c73c6aade..29c1f0317 100644 --- a/app/views/leagues/rosters/new.html.haml +++ b/app/views/leagues/rosters/new.html.haml @@ -54,7 +54,7 @@ / Manually display errors. - if player && has_errors - - player.errors.each do |_key_, error| + - player.errors.each_value do |error| %span.help-block= error .d-flex.justify-content-end diff --git a/config.ru b/config.ru index 9797be5ae..81559b870 100644 --- a/config.ru +++ b/config.ru @@ -1,4 +1,4 @@ # This file is used by Rack-based servers to start the application. -require ::File.expand_path('config/environment', __dir__) +require File.expand_path('config/environment', __dir__) run Rails.application diff --git a/lib/auth/model.rb b/lib/auth/model.rb index 3ed08842a..f29147bf3 100644 --- a/lib/auth/model.rb +++ b/lib/auth/model.rb @@ -99,7 +99,7 @@ def actor_name end def actor_id - "#{name.underscore}_id".to_sym + :"#{name.underscore}_id" end def permissions(collection = nil) diff --git a/lib/steam_id.rb b/lib/steam_id.rb index b2fe0df92..d27f7b47d 100644 --- a/lib/steam_id.rb +++ b/lib/steam_id.rb @@ -46,7 +46,7 @@ def to_id3(value) def from_32_to_64(value) account_lower, account_higher = value.scanf('STEAM_0:%d:%d') - account_lower + account_higher * 2 + UNIVERSE_0_TYPE_PUBLIC + account_lower + (account_higher * 2) + UNIVERSE_0_TYPE_PUBLIC end def from_id3_to_64(value) diff --git a/spec/controllers/users/bans_controller_spec.rb b/spec/controllers/users/bans_controller_spec.rb index 04755cbbd..7f65ea42f 100644 --- a/spec/controllers/users/bans_controller_spec.rb +++ b/spec/controllers/users/bans_controller_spec.rb @@ -31,7 +31,7 @@ describe 'POST #create' do # Need to round off to the nearest second - let(:terminated_at) { Time.zone.at((Time.zone.now + 2.days).to_i) } + let(:terminated_at) { Time.zone.at(2.days.from_now.to_i) } it 'succeeds for authorized user' do admin.grant(:edit, :users) @@ -53,7 +53,7 @@ admin.grant(:edit, :users) sign_in admin - terminated_at = Time.zone.now - 2.days + terminated_at = 2.days.ago post :create, params: { user_id: user.id, ban: { reason: 'foo', terminated_at: terminated_at }, action_: :use, subject: :leagues } diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index 4786fbe48..e156402ed 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -329,7 +329,7 @@ user.save! # Jump an hour into the future - allow(Time).to receive(:current).and_return(Time.current + 1.hour) + allow(Time).to receive(:current).and_return(1.hour.from_now) patch :confirm_email, params: { token: token } diff --git a/spec/helpers/admin_helper_spec.rb b/spec/helpers/admin_helper_spec.rb deleted file mode 100644 index 2249ff837..000000000 --- a/spec/helpers/admin_helper_spec.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'rails_helper' - -describe AdminHelper do -end diff --git a/spec/helpers/leagues_helper_spec.rb b/spec/helpers/leagues_helper_spec.rb deleted file mode 100644 index 00086dda2..000000000 --- a/spec/helpers/leagues_helper_spec.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'rails_helper' - -describe LeaguesHelper do -end diff --git a/spec/helpers/meta/formats_helper_spec.rb b/spec/helpers/meta/formats_helper_spec.rb deleted file mode 100644 index 4ecf0f686..000000000 --- a/spec/helpers/meta/formats_helper_spec.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'rails_helper' - -describe Meta::FormatsHelper do -end diff --git a/spec/helpers/meta/games_helper_spec.rb b/spec/helpers/meta/games_helper_spec.rb deleted file mode 100644 index c9f83650c..000000000 --- a/spec/helpers/meta/games_helper_spec.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'rails_helper' - -describe Meta::GamesHelper do -end diff --git a/spec/helpers/teams_helper_spec.rb b/spec/helpers/teams_helper_spec.rb deleted file mode 100644 index 5a6cac749..000000000 --- a/spec/helpers/teams_helper_spec.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'rails_helper' - -describe TeamsHelper do -end diff --git a/spec/helpers/users_helper_spec.rb b/spec/helpers/users_helper_spec.rb deleted file mode 100644 index cd96b9e8c..000000000 --- a/spec/helpers/users_helper_spec.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'rails_helper' - -describe UsersHelper do -end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 4cdd7886e..c10e3319a 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -54,7 +54,7 @@ end it 'has avatar' do - image = File.open(Rails.root.join('spec', 'support', 'avatar.png')) + image = Rails.root.join('spec/support/avatar.png').open expect(build(:user, avatar: image)).to be_valid end @@ -160,12 +160,12 @@ user.ban(:use, :users) expect(user.can?(:use, :users)).to be(false) - allow(Time.zone).to receive(:now).and_return(Time.zone.now + 10.years) + allow(Time.zone).to receive(:now).and_return(10.years.from_now) expect(user.can?(:use, :users)).to be(false) end it "can't ban with negative duration" do - time = Time.zone.now - 1.minute + time = 1.minute.ago expect do user.ban(:use, :users, terminated_at: time) diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index deb88ae1c..6610215a8 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -23,7 +23,7 @@ # directory. Alternatively, in the individual `*_spec.rb` files, manually # require only the support files necessary. # -Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f } +Dir[Rails.root.join('spec/support/**/*.rb')].sort.each { |f| require f } # Checks for pending migration and applies them before tests are run. # If you are not using ActiveRecord, you can remove this line. @@ -31,7 +31,7 @@ RSpec.configure do |config| # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures - config.fixture_path = ["#{::Rails.root}/spec/fixtures"] + config.fixture_path = [Rails.root.join('spec/fixtures').to_s] config.before(type: :view) do view.lookup_context.prefixes = %w[application] diff --git a/spec/support/carrierwave.rb b/spec/support/carrierwave.rb index b2f43614f..214480cfb 100644 --- a/spec/support/carrierwave.rb +++ b/spec/support/carrierwave.rb @@ -1,9 +1,9 @@ RSpec.configure do |config| config.before(:suite) do - CarrierWave.root = Rails.root.join('spec', 'carrierwave') + CarrierWave.root = Rails.root.join('spec/carrierwave') end config.after(:suite) do - FileUtils.rm_rf(Rails.root.join('spec', 'carrierwave')) + FileUtils.rm_rf(Rails.root.join('spec/carrierwave')) end end diff --git a/spec/views/leagues/matches/show.html.haml_spec.rb b/spec/views/leagues/matches/show.html.haml_spec.rb index 46de7db00..a243f66d7 100644 --- a/spec/views/leagues/matches/show.html.haml_spec.rb +++ b/spec/views/leagues/matches/show.html.haml_spec.rb @@ -116,9 +116,7 @@ after do comms.each do |comm| - if view.user_can_edit_league? - expect(rendered).to include(comm.created_by.name) - elsif comm.exists? + if view.user_can_edit_league? || comm.exists? expect(rendered).to include(comm.created_by.name) else expect(rendered).to_not include(comm.created_by.name) diff --git a/spec/views/users/bans/index.html.haml_spec.rb b/spec/views/users/bans/index.html.haml_spec.rb index 38776397e..3b374da72 100644 --- a/spec/views/users/bans/index.html.haml_spec.rb +++ b/spec/views/users/bans/index.html.haml_spec.rb @@ -7,7 +7,7 @@ [ user.bans_for(:use, :teams).new(id: 1, user: user, created_at: Time.zone.now), user.bans_for(:use, :users).new(id: 2, user: user, created_at: Time.zone.now, duration: 1.hour), - user.bans_for(:use, :leagues).new(id: 3, user: user, created_at: Time.zone.now - 2.hours, duration: 1.hour), + user.bans_for(:use, :leagues).new(id: 3, user: user, created_at: 2.hours.ago, duration: 1.hour), ] end