Fix more scopes for rails 4 + add rubocop

This commit is contained in:
Ari Timonen 2020-03-16 22:46:27 +02:00
parent aa207ac578
commit 143e364812
4 changed files with 32 additions and 34 deletions

View file

@ -79,6 +79,7 @@ group :development do
gem 'quiet_assets' gem 'quiet_assets'
gem 'spring', '2.0.2' gem 'spring', '2.0.2'
gem 'web-console' gem 'web-console'
gem 'rubocop'
end end
group :test do group :test do

View file

@ -30,7 +30,7 @@ class Gatherer < ActiveRecord::Base
lambda { |team| {:conditions => {:team => team}} } lambda { |team| {:conditions => {:team => team}} }
scope :of_user, scope :of_user,
lambda { |user| {:conditions => {:user_id => user.id}} } lambda { |user| {:conditions => {:user_id => user.id}} }
scope :lobby, :conditions => "team IS NULL" scope :lobby, -> { where(team: nil) }
scope :best, scope :best,
lambda { |gather| { lambda { |gather| {
:select => "u.id, u.username, (COUNT(*) / (SELECT COUNT(*) FROM gatherers g3 WHERE g3.user_id = u.id)) AS skill, g4.id", :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", :having => "g4.id IS NOT NULL",
:order => "skill DESC", :order => "skill DESC",
:limit => 15 } } :limit => 15 } }
scope :with_kpd, scope :with_kpd, -> {
:select => "gatherers.*, SUM(kills)/SUM(deaths) as kpd, COUNT(rounders.id) as rounds", select("gatherers.*, SUM(kills)/SUM(deaths) as kpd, COUNT(rounders.id) as rounds").
:joins => "LEFT JOIN rounders ON rounders.user_id = gatherers.user_id", joins("LEFT JOIN rounders ON rounders.user_id = gatherers.user_id").
:group => "rounders.user_id", group("rounders.user_id").
:order => "kpd DESC" order("kpd DESC") }
scope :lobby_team, scope :lobby_team, -> (team) { conditions("gatherers.team IS NULL OR gatherers.team = ?", team).
lambda { |team| { order("gatherers.team") }
:conditions => ["gatherers.team IS NULL OR gatherers.team = ?", team], scope :most_voted, -> { order("votes DESC, created_at DESC") }
:order => "gatherers.team"} }
scope :most_voted, :order => "votes DESC, created_at DESC"
scope :not_user, scope :not_user,
lambda { |user| {:conditions => ["user_id != ?", user.id]} } lambda { |user| {:conditions => ["user_id != ?", user.id]} }
scope :eject_order, :order => "votes ASC" scope :eject_order, -> { order("votes ASC") }
scope :ordered, scope :ordered, -> {
:joins => "LEFT JOIN gathers ON captain1_id = gatherers.id OR captain2_id = gatherers.id", joins("LEFT JOIN gathers ON captain1_id = gatherers.id OR captain2_id = gatherers.id").
:order => "captain1_id, captain2_id, gatherers.id" order("captain1_id, captain2_id, gatherers.id") }
scope :idle, scope :idle, -> {
:joins => "LEFT JOIN users ON users.id = gatherers.user_id", joins("LEFT JOIN users ON users.id = gatherers.user_id").
:conditions => ["lastvisit < ?", 30.minutes.ago.utc] where("lastvisit < ?", 30.minutes.ago.utc) }
belongs_to :user belongs_to :user
belongs_to :gather belongs_to :gather

View file

@ -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 :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 validates_format_of :pwd, :with => /\A[A-Za-z0-9_\-]*\z/, :allow_nil => true
scope :ordered, :order => "name" scope :ordered, -> { order("name") }
scope :hlds, :conditions => ["domain = ?", DOMAIN_HLDS] scope :hlds, -> { where("domain = ?", DOMAIN_HLDS) }
scope :ns2, :conditions => ["domain = ?", DOMAIN_NS2] scope :ns2, -> { where("domain = ?", DOMAIN_NS2) }
scope :hltvs, :conditions => ["domain = ?", DOMAIN_HLTV] scope :hltvs, -> { where("domain = ?", DOMAIN_HLTV) }
scope :active, :conditions => "active = 1" scope :active, -> { where("active = 1") }
scope :with_players, :conditions => "players > 0" scope :with_players, -> { where("players > 0") }
scope :reserved, :conditions => "reservation IS NOT NULL" scope :reserved, -> { where("reservation IS NOT NULL") }
scope :unreserved_now, :conditions => "reservation IS NULL" scope :unreserved_now, -> { where("reservation IS NULL") }
scope :unreserved_hltv_around, scope :unreserved_hltv_around, -> (time) {
lambda { |time| { select("servers.*").
:select => "servers.*", joins("LEFT JOIN matches ON servers.id = matches.hltv_id
: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")}' 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") }
:conditions => "matches.hltv_id IS NULL"} }
has_many :logs has_many :logs
has_many :matches has_many :matches

View file

@ -28,7 +28,7 @@ class Shoutmsg < ActiveRecord::Base
scope :typebox, -> { where(shoutable_type: nil, shoutable_id: nil) } scope :typebox, -> { where(shoutable_type: nil, shoutable_id: nil) }
scope :last500, -> { includes(:user).order("id DESC").limit(500) } scope :last500, -> { includes(:user).order("id DESC").limit(500) }
scope :of_object, -> (object, id) { where(shoutable_type: object, shoutable_id: id) } scope :of_object, -> (object, id) { where(shoutable_type: object, shoutable_id: id) }
scope :ordered, order("id") scope :ordered, -> { order("id") }
def domain def domain
self[:shoutable_type] ? "shout_#{shoutable_type}_#{shoutable_id}" : "shoutbox" self[:shoutable_type] ? "shout_#{shoutable_type}_#{shoutable_id}" : "shoutbox"