From 143e3648123c6aa3f3db67412d8156639e1281ad Mon Sep 17 00:00:00 2001 From: Ari Timonen Date: Mon, 16 Mar 2020 22:46:27 +0200 Subject: [PATCH] Fix more scopes for rails 4 + add rubocop --- Gemfile | 1 + app/models/gatherer.rb | 34 ++++++++++++++++------------------ app/models/server.rb | 29 ++++++++++++++--------------- app/models/shoutmsg.rb | 2 +- 4 files changed, 32 insertions(+), 34 deletions(-) diff --git a/Gemfile b/Gemfile index 8659653..988e256 100644 --- a/Gemfile +++ b/Gemfile @@ -79,6 +79,7 @@ group :development do gem 'quiet_assets' gem 'spring', '2.0.2' gem 'web-console' + gem 'rubocop' end group :test do diff --git a/app/models/gatherer.rb b/app/models/gatherer.rb index 7fe6288..64f2462 100644 --- a/app/models/gatherer.rb +++ b/app/models/gatherer.rb @@ -30,7 +30,7 @@ class Gatherer < ActiveRecord::Base lambda { |team| {:conditions => {:team => team}} } scope :of_user, lambda { |user| {:conditions => {:user_id => user.id}} } - scope :lobby, :conditions => "team IS NULL" + scope :lobby, -> { where(team: nil) } scope :best, lambda { |gather| { :select => "u.id, u.username, (COUNT(*) / (SELECT COUNT(*) FROM gatherers g3 WHERE g3.user_id = u.id)) AS skill, g4.id", @@ -42,25 +42,23 @@ class Gatherer < ActiveRecord::Base :having => "g4.id IS NOT NULL", :order => "skill DESC", :limit => 15 } } - scope :with_kpd, - :select => "gatherers.*, SUM(kills)/SUM(deaths) as kpd, COUNT(rounders.id) as rounds", - :joins => "LEFT JOIN rounders ON rounders.user_id = gatherers.user_id", - :group => "rounders.user_id", - :order => "kpd DESC" - scope :lobby_team, - lambda { |team| { - :conditions => ["gatherers.team IS NULL OR gatherers.team = ?", team], - :order => "gatherers.team"} } - scope :most_voted, :order => "votes DESC, created_at DESC" + scope :with_kpd, -> { + select("gatherers.*, SUM(kills)/SUM(deaths) as kpd, COUNT(rounders.id) as rounds"). + joins("LEFT JOIN rounders ON rounders.user_id = gatherers.user_id"). + group("rounders.user_id"). + order("kpd DESC") } + scope :lobby_team, -> (team) { conditions("gatherers.team IS NULL OR gatherers.team = ?", team). + order("gatherers.team") } + scope :most_voted, -> { order("votes DESC, created_at DESC") } scope :not_user, lambda { |user| {:conditions => ["user_id != ?", user.id]} } - scope :eject_order, :order => "votes ASC" - scope :ordered, - :joins => "LEFT JOIN gathers ON captain1_id = gatherers.id OR captain2_id = gatherers.id", - :order => "captain1_id, captain2_id, gatherers.id" - scope :idle, - :joins => "LEFT JOIN users ON users.id = gatherers.user_id", - :conditions => ["lastvisit < ?", 30.minutes.ago.utc] + scope :eject_order, -> { order("votes ASC") } + scope :ordered, -> { + joins("LEFT JOIN gathers ON captain1_id = gatherers.id OR captain2_id = gatherers.id"). + order("captain1_id, captain2_id, gatherers.id") } + scope :idle, -> { + joins("LEFT JOIN users ON users.id = gatherers.user_id"). + where("lastvisit < ?", 30.minutes.ago.utc) } belongs_to :user belongs_to :gather diff --git a/app/models/server.rb b/app/models/server.rb index 129f684..507e6dc 100644 --- a/app/models/server.rb +++ b/app/models/server.rb @@ -50,21 +50,20 @@ class Server < ActiveRecord::Base validates_format_of :reservation, :with => /\A[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}:[0-9]{1,5}\z/, :allow_nil => true validates_format_of :pwd, :with => /\A[A-Za-z0-9_\-]*\z/, :allow_nil => true - scope :ordered, :order => "name" - scope :hlds, :conditions => ["domain = ?", DOMAIN_HLDS] - scope :ns2, :conditions => ["domain = ?", DOMAIN_NS2] - scope :hltvs, :conditions => ["domain = ?", DOMAIN_HLTV] - scope :active, :conditions => "active = 1" - scope :with_players, :conditions => "players > 0" - scope :reserved, :conditions => "reservation IS NOT NULL" - scope :unreserved_now, :conditions => "reservation IS NULL" - scope :unreserved_hltv_around, - lambda { |time| { - :select => "servers.*", - :joins => "LEFT JOIN matches ON servers.id = matches.hltv_id - AND match_time > '#{(time.ago(Match::MATCH_LENGTH).utc).strftime("%Y-%m-%d %H:%M:%S")}' - AND match_time < '#{(time.ago(-Match::MATCH_LENGTH).utc).strftime("%Y-%m-%d %H:%M:%S")}'", - :conditions => "matches.hltv_id IS NULL"} } + scope :ordered, -> { order("name") } + scope :hlds, -> { where("domain = ?", DOMAIN_HLDS) } + scope :ns2, -> { where("domain = ?", DOMAIN_NS2) } + scope :hltvs, -> { where("domain = ?", DOMAIN_HLTV) } + scope :active, -> { where("active = 1") } + scope :with_players, -> { where("players > 0") } + scope :reserved, -> { where("reservation IS NOT NULL") } + scope :unreserved_now, -> { where("reservation IS NULL") } + scope :unreserved_hltv_around, -> (time) { + select("servers.*"). + joins("LEFT JOIN matches ON servers.id = matches.hltv_id + AND match_time > '#{(time.ago(Match::MATCH_LENGTH).utc).strftime("%Y-%m-%d %H:%M:%S")}' + AND match_time < '#{(time.ago(-Match::MATCH_LENGTH).utc).strftime("%Y-%m-%d %H:%M:%S")}'"). + where("matches.hltv_id IS NULL") } has_many :logs has_many :matches diff --git a/app/models/shoutmsg.rb b/app/models/shoutmsg.rb index 2363364..2b2c192 100644 --- a/app/models/shoutmsg.rb +++ b/app/models/shoutmsg.rb @@ -28,7 +28,7 @@ class Shoutmsg < ActiveRecord::Base scope :typebox, -> { where(shoutable_type: nil, shoutable_id: nil) } scope :last500, -> { includes(:user).order("id DESC").limit(500) } scope :of_object, -> (object, id) { where(shoutable_type: object, shoutable_id: id) } - scope :ordered, order("id") + scope :ordered, -> { order("id") } def domain self[:shoutable_type] ? "shout_#{shoutable_type}_#{shoutable_id}" : "shoutbox"