From 160d29a56957439268daa396f7cfae5a300a8729 Mon Sep 17 00:00:00 2001 From: Ari Timonen Date: Mon, 16 Mar 2020 22:57:41 +0200 Subject: [PATCH] Fix more scopes --- Gemfile.lock | 18 ++++++++++++++++++ app/models/gather_server.rb | 2 +- app/models/map.rb | 6 +++--- app/models/matcher.rb | 17 ++++++++--------- app/models/message.rb | 4 +++- app/models/movie.rb | 9 ++++----- app/models/prediction.rb | 2 +- app/models/week.rb | 2 +- 8 files changed, 39 insertions(+), 21 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index ec738bf..1ad2d88 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -54,6 +54,7 @@ GEM activerecord (>= 3.2, < 7.0) rake (>= 10.4, < 14.0) arel (6.0.4) + ast (2.4.0) bbcoder (1.0.1) better_errors (2.6.0) coderay (>= 1.0.0) @@ -163,6 +164,7 @@ GEM i18n-country-translations (~> 1.0, >= 1.3.0) unicode_utils (~> 1.0, >= 1.0.0) i18n_data (0.10.0) + jaro_winkler (1.5.4) jquery-rails (2.0.3) railties (>= 3.1.0, < 5.0) thor (~> 0.14) @@ -197,6 +199,9 @@ GEM nokogiri (1.9.1) mini_portile2 (~> 2.4.0) os (0.9.6) + parallel (1.19.1) + parser (2.7.0.4) + ast (~> 2.4.0) phantomjs (2.1.1.0) poltergeist (1.6.0) capybara (~> 2.1) @@ -246,6 +251,7 @@ GEM activesupport (= 4.2.11.1) rake (>= 0.8.7) thor (>= 0.18.1, < 2.0) + rainbow (3.0.0) rake (10.5.0) rb-fsevent (0.10.3) rb-inotify (0.10.1) @@ -258,6 +264,7 @@ GEM actionpack (>= 4.2.0, < 6.0) railties (>= 4.2.0, < 6.0) retriable (3.1.2) + rexml (3.2.4) rmagick (4.0.0) rspec (3.3.0) rspec-core (~> 3.3.0) @@ -280,6 +287,15 @@ GEM rspec-mocks (~> 3.3.0) rspec-support (~> 3.3.0) rspec-support (3.3.0) + rubocop (0.80.1) + jaro_winkler (~> 1.5.1) + parallel (~> 1.10) + parser (>= 2.7.0.1) + rainbow (>= 2.2.2, < 4.0) + rexml + ruby-progressbar (~> 1.7) + unicode-display_width (>= 1.4.0, < 1.7) + ruby-progressbar (1.10.1) rubyzip (1.3.0) sanitize (2.1.1) nokogiri (>= 1.4.4) @@ -334,6 +350,7 @@ GEM uglifier (2.5.3) execjs (>= 0.3.0) json (>= 1.8.0) + unicode-display_width (1.6.1) unicode_utils (1.4.0) unread (0.10.1) activerecord (>= 3) @@ -397,6 +414,7 @@ DEPENDENCIES rmagick rspec rspec-rails (~> 3.3.3) + rubocop sanitize (~> 2.1.0) sass-rails (~> 5.0.3) selenium-webdriver diff --git a/app/models/gather_server.rb b/app/models/gather_server.rb index 48f7362..c7ea73f 100644 --- a/app/models/gather_server.rb +++ b/app/models/gather_server.rb @@ -11,7 +11,7 @@ # class GatherServer < ActiveRecord::Base - scope :ordered, :order => "votes DESC" + scope :ordered, -> { order("votes DESC") } belongs_to :gather belongs_to :server diff --git a/app/models/map.rb b/app/models/map.rb index 870b7b2..ac80446 100644 --- a/app/models/map.rb +++ b/app/models/map.rb @@ -19,9 +19,9 @@ class Map < ActiveRecord::Base has_and_belongs_to_many :contests - scope :basic, :conditions => {:deleted => false}, :order => "name" - scope :with_name, lambda { |name| {:conditions => {:name => name}} } - scope :classic, :conditions => "name LIKE 'ns_%'" + scope :basic, -> { where(deleted: false).order("name") } + scope :with_name, -> (name) { where(name: name) } + scope :classic, -> { where("name LIKE 'ns_%'") } scope :of_category, lambda { |category| { :conditions => {:category_id => category.id} }} diff --git a/app/models/matcher.rb b/app/models/matcher.rb index 21da011..defabec 100644 --- a/app/models/matcher.rb +++ b/app/models/matcher.rb @@ -21,15 +21,14 @@ class Matcher < ActiveRecord::Base belongs_to :contester has_many :teams, :through => :contester - scope :stats, - :select => "user_id, COUNT(*) as num, users.username", - :joins => "LEFT JOIN users ON users.id = user_id", - :group => "user_id", - :having => "num > 20", - :order => "num DESC" - scope :mercs, :conditions => {:merc => true} - scope :of_contester, - lambda { |contester| {:conditions => {:contester_id => contester.id}} } + scope :stats, -> { + select("user_id, COUNT(*) as num, users.username"). + joins("LEFT JOIN users ON users.id = user_id"). + group("user_id"). + having("num > 20"). + order("num DESC") } + scope :mercs, -> { where(merc: true) } + scope :of_contester, -> (contester) { where(contester_id: contester.id) } validates_presence_of :match, :user validates_uniqueness_of :user_id, :scope => :match_id diff --git a/app/models/message.rb b/app/models/message.rb index c91aeb2..389b44c 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -23,7 +23,9 @@ class Message < ActiveRecord::Base validates_length_of :title, :in => 1..100 validates_length_of :text, :in => 1..65000 - scope :ordered, :order => "created_at DESC" + scope :ordered, -> { order("created_at DESC") } + + # FIXME: check before removing, provided by unread #scope :read_by, # lambda { |user| {:include => :readings, :conditions => ["readings.user_id = ?", user.id]} } #scope :unread_by, diff --git a/app/models/movie.rb b/app/models/movie.rb index 28e70ae..b0a3f4b 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -32,10 +32,9 @@ class Movie < ActiveRecord::Base attr_protected :id, :updated_at, :created_at attr_accessor :user_name, :name, :stream_ip, :stream_port - scope :recent, :limit => 5 - scope :ordered, - :include => "file", - :order => "data_files.created_at DESC" + scope :recent, -> { limit(5) } + scope :ordered, -> { include("file"). + order("data_files.created_at DESC") } scope :index, -> { select("movies.*, users.username, AVG(rates.score) as total_ratings") .joins("LEFT JOIN data_files ON movies.file_id = data_files.id @@ -43,7 +42,7 @@ class Movie < ActiveRecord::Base LEFT JOIN ratings ON rateable_id = data_files.id AND rateable_type = 'DataFile' LEFT JOIN rates ON ratings.rate_id = rates.id") .group("movies.id") } - scope :active_streams, :conditions => "status > 0" + scope :active_streams, -> { where("status > 0") } belongs_to :user belongs_to :file, :class_name => "DataFile" diff --git a/app/models/prediction.rb b/app/models/prediction.rb index 8153665..da20516 100644 --- a/app/models/prediction.rb +++ b/app/models/prediction.rb @@ -22,7 +22,7 @@ class Prediction < ActiveRecord::Base validates_inclusion_of :score2, :in => 0..99, :message => "Invalid score" validates_uniqueness_of :match_id, :scope => :user_id - scope :with_contest, :include => {:match => :contest} + scope :with_contest, -> { include({:match => :contest}) } belongs_to :match belongs_to :user diff --git a/app/models/week.rb b/app/models/week.rb index 993b202..a67d847 100644 --- a/app/models/week.rb +++ b/app/models/week.rb @@ -20,7 +20,7 @@ class Week < ActiveRecord::Base validates_presence_of :contest, :map1, :map2 validates_length_of :name, :in => 1..30 - scope :ordered, :order => "start_date ASC" + scope :ordered, -> { order("start_date ASC") } belongs_to :contest belongs_to :map1, :class_name => "Map"