diff --git a/app/controllers/shoutmsgs_controller.rb b/app/controllers/shoutmsgs_controller.rb index 041116c..5ca3e36 100644 --- a/app/controllers/shoutmsgs_controller.rb +++ b/app/controllers/shoutmsgs_controller.rb @@ -2,7 +2,7 @@ class ShoutmsgsController < ApplicationController respond_to :html, :js def index - @shoutmsgs = Shoutmsg.lastXXX.typebox + @shoutmsgs = Shoutmsg.last.typebox end def show diff --git a/app/models/article.rb b/app/models/article.rb index e4a1aaf..9724f0b 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -80,7 +80,7 @@ class Article < ActiveRecord::Base end def previous_article - category.articles.nodrafts.first(conditions: ["id < ?", self.id], order: "id DESC") + category.articles.nodrafts.first.where("id < ?", self.id).order("id DESC") end def next_article diff --git a/app/models/bracketer.rb b/app/models/bracketer.rb index cb5c4ea..b79c609 100644 --- a/app/models/bracketer.rb +++ b/app/models/bracketer.rb @@ -22,7 +22,7 @@ class Bracketer < ActiveRecord::Base belongs_to :match belongs_to :contester, :foreign_key => "team_id" - scope :pos, lambda { |row, col| {:conditions => {"row" => row, "column" => col}} } + scope :pos, -> (row, col) { where(row: row, column: col) } def to_s if self.match_id diff --git a/app/models/category.rb b/app/models/category.rb index 4d9d4e9..3591978 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -36,9 +36,9 @@ class Category < ActiveRecord::Base scope :ordered, -> { order("sort ASC, created_at DESC") } scope :domain, -> (domain) { where(domain: domain) } scope :nospecial, -> { where.not(name: 'Special') } - scope :newest, -> { include(:articles).order("articles.created_at DESC") } + scope :newest, -> { includes(:articles).order("articles.created_at DESC") } # scope :page, lambda { |page| {:limit => "#{(page-1)*PER_PAGE}, #{(page-1)*PER_PAGE+PER_PAGE}"} } - #scope :of_user, lambda { |user| {:conditions => {"articles.user_id" => user.id}, :include => :articles} } + scope :of_user, -> (user) { where("articles.user_id", user.id).includes(:articles) } has_many :articles, -> { order("created_at DESC") } has_many :issues, -> { order("created_at DESC") } diff --git a/app/models/comment.rb b/app/models/comment.rb index b596079..2733f92 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -17,12 +17,11 @@ class Comment < ActiveRecord::Base attr_protected :id, :updated_at, :created_at, :user_id - scope :with_userteam, :include => {:user => :team} - scope :recent, :order => "id DESC", :limit => 10 - scope :recent3, :order => "id DESC", :limit => 3 - scope :recent5, :order => "id DESC", :limit => 5, :group => "commentable_id, commentable_type" - scope :filtered, :conditions => ["commentable_type != 'Issue'"] - scope :ordered, :order => "id ASC" + scope :with_userteam, -> { includes({:user => :team}) } + scope :recent, -> (n) { order("id DESC").limit(n) } + scope :recent5, -> { order("id DESC").limit(5).group("commentable_id, commentable_type") } + scope :filtered, -> { where.not({"commentable_type" => 'Issue'}) } + scope :ordered, -> { order("id ASC") } belongs_to :user belongs_to :commentable, :polymorphic => true diff --git a/app/models/data_file.rb b/app/models/data_file.rb index 2368f91..a1b5fa4 100644 --- a/app/models/data_file.rb +++ b/app/models/data_file.rb @@ -25,12 +25,12 @@ class DataFile < ActiveRecord::Base attr_accessor :related_id attr_protected :id, :updated_at, :created_at, :path, :size, :md5 - scope :recent, :order => "created_at DESC", :limit => 8 - scope :demos, :order => "created_at DESC", :conditions => ["directory_id IN (SELECT id FROM directories WHERE parent_id = ?)", Directory::DEMOS] - scope :ordered, :order => "created_at DESC" - scope :movies, :order => "created_at DESC", :conditions => {:directory_id => Directory::MOVIES} - scope :not, lambda { |file| {:conditions => ["id != ?", file.id]} } - scope :unrelated, :conditions => "related_id is null" + scope :recent, -> { order("created_at DESC").limit(8) } + scope :demos, -> { order("created_at DESC").where("directory_id IN (SELECT id FROM directories WHERE parent_id = ?)", Directory::DEMOS) } + scope :ordered, -> { order("created_at DESC") } + scope :movies, -> { order("created_at DESC", :conditions => {:directory_id => Directory::MOVIES} + scope :not, -> (file) { where.not(id: file.id) } + scope :unrelated, -> { where("related_id is null") } has_many :related_files, :class_name => "DataFile", :foreign_key => :related_id has_many :comments, :as => :commentable diff --git a/app/models/directory.rb b/app/models/directory.rb index 3f6ca86..1a91de8 100644 --- a/app/models/directory.rb +++ b/app/models/directory.rb @@ -26,11 +26,11 @@ class Directory < ActiveRecord::Base belongs_to :parent, :class_name => "Directory" has_many :subdirs, :class_name => "Directory", :foreign_key => :parent_id - has_many :files, :class_name => "DataFile", :order => "name" + has_many :files, -> { order("name") }, :class_name => "DataFile" - scope :ordered, :order => "name ASC" - scope :filtered, :conditions => {:hidden => false} - scope :of_parent, lambda { |parent| {:conditions => {:parent_id => parent.id}} } + scope :ordered, -> { order("name ASC") } + scope :filtered, -> { where(hidden: true) } + scope :of_parent, -> (parent) { where(parent_id: parent.id) } validates_length_of [:name, :path], :in => 1..255 validates_format_of :name, :with => /\A[A-Za-z0-9]{1,20}\z/, :on => :create diff --git a/app/models/gather.rb b/app/models/gather.rb index 44b774f..920c42b 100644 --- a/app/models/gather.rb +++ b/app/models/gather.rb @@ -29,12 +29,11 @@ class Gather < ActiveRecord::Base attr_accessor :admin - scope :ordered, :order => "id DESC" - scope :basic, :include => [:captain1, :captain2, :map1, :map2, :server] - scope :active, - :conditions => ["gathers.status IN (?, ?, ?) AND gathers.updated_at > ?", - STATE_VOTING, STATE_PICKING, STATE_RUNNING, 12.hours.ago.utc] - + scope :ordered, -> { order("id DESC") } + scope :basic, -> { include(:captain1, :captain2, :map1, :map2, :server) } + scope :active, -> { where("gathers.status IN (?, ?, ?) AND gathers.updated_at > ?", + STATE_VOTING, STATE_PICKING, STATE_RUNNING, 12.hours.ago.utc) } + belongs_to :server belongs_to :captain1, :class_name => "Gatherer" belongs_to :captain2, :class_name => "Gatherer" @@ -94,11 +93,11 @@ class Gather < ActiveRecord::Base end def previous_gather - Gather.first(:conditions => ["id < ? AND category_id = ?", self.id, category_id], :order => "id DESC") + Gather.first.where("id < ? AND category_id = ?", self.id, category_id).order("id DESC") end def next_gather - Gather.first(:conditions => ["id > ? AND category_id = ?", self.id, category_id], :order => "id ASC") + Gather.first.where("id > ? AND category_id = ?", self.id, category_id).order("id ASC") end def last diff --git a/app/models/gather_map.rb b/app/models/gather_map.rb index 5aba394..3b10f2c 100644 --- a/app/models/gather_map.rb +++ b/app/models/gather_map.rb @@ -9,7 +9,7 @@ # class GatherMap < ActiveRecord::Base - scope :ordered, :order => "votes DESC, id DESC" + scope :ordered, -> { order("votes DESC, id DESC") } belongs_to :gather belongs_to :map @@ -19,7 +19,7 @@ class GatherMap < ActiveRecord::Base def to_s self.map.to_s - end + endD def init_variables self.votes = 0 diff --git a/app/models/shoutmsg.rb b/app/models/shoutmsg.rb index 60e0d59..a4085ef 100644 --- a/app/models/shoutmsg.rb +++ b/app/models/shoutmsg.rb @@ -19,26 +19,16 @@ class Shoutmsg < ActiveRecord::Base validates_length_of :text, :in => 1..100 validates_presence_of :user - scope :recent, - :include => :user, - :order => "id DESC", - :limit => 8 - scope :box, - :conditions => "shoutable_type IS NULL AND shoutable_id IS NULL", - :limit => 8 - scope :typebox, - :conditions => "shoutable_type IS NULL AND shoutable_id IS NULL" - scope :lastXXX, - :include => :user, - :order => "id DESC", - :limit => 500 - scope :of_object, - lambda { |object, id| {:conditions => {:shoutable_type => object, :shoutable_id => id}} } - scope :ordered, :order => "id" - belongs_to :user belongs_to :shoutable, :polymorphic => true + scope :recent, -> { includes(:user).order("id DESC").limit(8) } + scope :box, -> { where(shoutable_type: nil, shoutable_id: nil).limit(8) } + scope :typebox, -> { where(shoutable_type: NULL, shoutable_id: NULL) } + 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") + def domain self[:shoutable_type] ? "shout_#{shoutable_type}_#{shoutable_id}" : "shoutbox" end diff --git a/app/models/topic.rb b/app/models/topic.rb index 845d87b..ff279b8 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -26,12 +26,12 @@ class Topic < ActiveRecord::Base belongs_to :user belongs_to :forum has_one :lock, :as => :lockable - has_one :latest, :class_name => "Post", :order => "id DESC" - has_many :posts, :order => "id ASC", :dependent => :destroy + has_one :latest, -> { order("id DESC") }, :class_name => "Post" + has_many :posts, -> { order("id ASC") }, :dependent => :destroy has_many :view_counts, :as => :viewable, :dependent => :destroy - scope :basic, :include => [:latest, { forum: :forumer }, :user] - scope :ordered, :order => "state DESC, posts.id DESC" + scope :basic, -> { includes([:latest, { forum: :forumer }, :user]) } + scope :ordered, -> { order("state DESC, posts.id DESC") } validates_presence_of :user_id, :forum_id validates_length_of :title, :in => 1..50 diff --git a/app/models/user.rb b/app/models/user.rb index b3410b0..2afc580 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -45,51 +45,50 @@ class User < ActiveRecord::Base has_many :groups, :through => :groupers has_many :shoutmsgs, :dependent => :destroy has_many :issues, :foreign_key => "author_id", :dependent => :destroy - has_many :assigned_issues, :class_name => "Issue", :foreign_key => "assigned_id", + has_many :assigned_issues, :class_name => "Issue", :foreign_key => "assigned_id" has_many :posted_comments, :dependent => :destroy, :class_name => "Comment" - has_many :comments, :class_name => "Comment", :as => :commentable, :order => "created_at ASC", :dependent => :destroy + has_many :comments, -> { order("created_at ASC") }, :class_name => "Comment", :as => :commentable, :dependent => :destroy has_many :teamers, :dependent => :destroy - has_many :active_teams, :through => :teamers, :source => "team", -> - { where("teamers.rank >= ? AND teams.active = ?", Teamer::RANK_MEMBER, true) } - has_many :active_contesters, :through => :active_teams, :source => "contesters", -> - { where({"contesters.active = ?", true} } - has_many :active_contests, :through => :active_contesters, :source => "contest", -> - { where("contests.status != ?", Contest::STATUS_CLOSED) } - has_many :past_teams, :through => :teamers, :source => "team", :group => "user_id, team_id" + has_many :active_teams, -> { where("teamers.rank >= ? AND teams.active = ?", Teamer::RANK_MEMBER, true) }, \ + :through => :teamers, :source => "team" + has_many :active_contesters, -> { where("contesters.active = ?", true) }, \ + :through => :active_teams, :source => "contesters" + has_many :active_contests, -> { where("contests.status != ?", Contest::STATUS_CLOSED) }, \ + :through => :active_contesters, :source => "contest" has_many :matchers, :dependent => :destroy has_many :matches, :through => :matchers has_many :predictions, :dependent => :destroy has_many :challenges_received, :through => :active_contesters, :source => "challenges_received" has_many :challenges_sent, :through => :active_contesters, :source => "challenges_sent" - has_many :upcoming_team_matches, :through => :active_contesters, :source => "matches", - :conditions => "match_time > UTC_TIMESTAMP()" - has_many :upcoming_ref_matches, :class_name => "Match", :foreign_key => "referee_id", - :conditions => "match_time > UTC_TIMESTAMP()" - has_many :past_team_matches, :through => :active_contesters, :source => "matches", - :conditions => "match_time < UTC_TIMESTAMP()" - has_many :past_ref_matches, :class_name => "Match", :foreign_key => "referee_id", - :conditions => "match_time < UTC_TIMESTAMP()" + has_many :upcoming_team_matches, -> { where("match_time > UTC_TIMESTAMP()") }, + :through => :active_teams, :source => "matches" + has_many :upcoming_ref_matches, -> { where("match_time > UTC_TIMESTAMP()") }, + :class_name => "Match", :foreign_key => "referee_id" + has_many :past_team_matches, -> { where("match_time < UTC_TIMESTAMP()") }, + :through => :active_contesters, :source => "matches" + has_many :past_ref_matches, -> { where("match_time < UTC_TIMESTAMP()") }, + :class_name => "Match", :foreign_key => "referee_id" has_many :received_personal_messages, :class_name => "Message", :as => "recipient", :dependent => :destroy has_many :received_team_messages, :through => :active_teams, :source => :received_messages has_many :sent_personal_messages, :class_name => "Message", :as => "sender", :dependent => :destroy has_many :sent_team_messages, :through => :active_teams, :source => :sent_messages - has_many :match_teams, :through => :matchers, :source => :teams, :uniq => true + has_many :match_teams, :through => :matchers, :source => :teams scope :active, -> { where(banned: false) } - scope :with_age, -> - {where("DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(birthdate)), '%Y')+0 AS aged, COUNT(*) as num, username"). - .group("aged"). - .having("num > 8 AND aged > 0")} - scope :country_stats, -> - {select("country, COUNT(*) as num"). - .where("country is not null and country != '' and country != '--'"). - .group("country"). - .having("num > 15"). - .order("num DESC") } - scope :posts_stats, -> - { select("users.id, username, COUNT(posts.id) as num"). - .joins("LEFT JOIN posts ON posts.user_id = users.id"). - .group("users.id"). + scope :with_age, -> { + where("DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(birthdate)), '%Y')+0 AS aged, COUNT(*) as num, username") + .group("aged") + .having("num > 8 AND aged > 0") } + scope :country_stats, -> { + select("country, COUNT(*) as num") + .where("country is not null and country != '' and country != '--'") + .group("country") + .having("num > 15") + .order("num DESC") } + scope :posts_stats, -> { + select("users.id, username, COUNT(posts.id) as num") + .joins("LEFT JOIN posts ON posts.user_id = users.id") + .group("users.id") .order("num DESC") } scope :banned, :joins => "LEFT JOIN bans ON bans.user_id = users.id AND expiry > UTC_TIMESTAMP()", @@ -200,18 +199,14 @@ class User < ActiveRecord::Base end def caster? - groups.exists? :id SL/ensl.org - + groups.exists? :id => Group::CASTERS end def verified? - # created_at < DaSL/ensl.org -N_TIME) true end - def has_access? groupSL/ensl.org - + def has_access? groups admin? or groups.exists?(:id => group) end diff --git a/app/views/widgets/_comments.html.erb b/app/views/widgets/_comments.html.erb index 62b6610..b36f96b 100644 --- a/app/views/widgets/_comments.html.erb +++ b/app/views/widgets/_comments.html.erb @@ -1,5 +1,5 @@
    - <% Comment.recent3.filtered.each do |comment| %> + <% Comment.recent(3).filtered.each do |comment| %>
  1. <%= namelink comment.commentable, 15 %> diff --git a/app/views/widgets/_posts.html.erb b/app/views/widgets/_posts.html.erb index 6639986..af4a6b1 100644 --- a/app/views/widgets/_posts.html.erb +++ b/app/views/widgets/_posts.html.erb @@ -13,7 +13,7 @@
    Latest comments
      - <% Comment.recent.filtered.each do |comment| %> + <% Comment.recent(3).each do |comment| %>
    1. <%= namelink comment.commentable, 15 %> by <%= namelink comment.user, 10 %>