Update scopes for rails 4

This commit is contained in:
Ari Timonen 2019-06-02 04:26:36 +03:00
parent 0b53968d3d
commit 768d3faa1b
14 changed files with 75 additions and 92 deletions

View file

@ -2,7 +2,7 @@ class ShoutmsgsController < ApplicationController
respond_to :html, :js
def index
@shoutmsgs = Shoutmsg.lastXXX.typebox
@shoutmsgs = Shoutmsg.last.typebox
end
def show

View file

@ -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

View file

@ -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

View file

@ -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") }

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -29,11 +29,10 @@ 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"
@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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").
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 :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

View file

@ -1,5 +1,5 @@
<ol>
<% Comment.recent3.filtered.each do |comment| %>
<% Comment.recent(3).filtered.each do |comment| %>
<li>
<b>
<%= namelink comment.commentable, 15 %>

View file

@ -13,7 +13,7 @@
<div class="separator">Latest comments</div>
<ol>
<% Comment.recent.filtered.each do |comment| %>
<% Comment.recent(3).each do |comment| %>
<li>
<%= namelink comment.commentable, 15 %>
by <%= namelink comment.user, 10 %>