From ce9797aed8009b93ccc7488f38e14f6fd19d0632 Mon Sep 17 00:00:00 2001 From: Ari Timonen Date: Mon, 3 Jun 2019 15:38:24 -0400 Subject: [PATCH] More progress on Rails 4.1 update Just fix scopes mostly. Add binstubs. --- .gitignore | 3 +- app/controllers/contests_controller.rb | 4 +- app/controllers/teams_controller.rb | 2 +- app/models/contest.rb | 38 ++++---- app/models/contester.rb | 20 +++-- app/models/forum.rb | 14 ++- app/models/gather.rb | 2 +- app/models/match.rb | 110 +++++++++-------------- app/models/movie.rb | 2 - app/models/poll.rb | 2 + app/models/team.rb | 45 +++++----- app/models/teamer.rb | 117 +++++++++++-------------- app/models/user.rb | 16 ++-- app/views/forums/index.html.erb | 4 +- app/views/teams/_list.html.erb | 2 - app/views/teams/show.html.erb | 8 +- app/views/widgets/_poll.html.erb | 2 +- bin/bundle | 3 + bin/rails | 4 + bin/rake | 4 + 20 files changed, 189 insertions(+), 213 deletions(-) create mode 100755 bin/bundle create mode 100755 bin/rails create mode 100755 bin/rake diff --git a/.gitignore b/.gitignore index e76151e..007efbb 100644 --- a/.gitignore +++ b/.gitignore @@ -20,6 +20,7 @@ /public/files/* /public/local /public/uploads +/public/assets/ # RubyMine /.idea @@ -43,4 +44,4 @@ rerun.txt pickle-email-*.html # Direnv -.envrc \ No newline at end of file +.envrc diff --git a/app/controllers/contests_controller.rb b/app/controllers/contests_controller.rb index 1b6731b..7155d21 100644 --- a/app/controllers/contests_controller.rb +++ b/app/controllers/contests_controller.rb @@ -10,9 +10,9 @@ class ContestsController < ApplicationController def historical case params[:id] when "NS1" - @contests = Contest.with_contesters.ordered.where ["name LIKE ? OR name LIKE ?", "S%:%", "%Night%"] + @contests = Contest.all.ordered.includes(:contesters).where("name LIKE ? OR name LIKE ?", "S%:%", "%Night%") else - @contests = Contest.with_contesters.ordered.where ["id > ?", "113"] + @contests = Contest.all.ordered.includes(:contesters).where("id > ?", "113") end end diff --git a/app/controllers/teams_controller.rb b/app/controllers/teams_controller.rb index eb7bfe8..868797e 100644 --- a/app/controllers/teams_controller.rb +++ b/app/controllers/teams_controller.rb @@ -2,7 +2,7 @@ class TeamsController < ApplicationController before_filter :get_team, only: [:show, :edit, :update, :destroy, :recover] def index - @teams = Team.with_teamers_num(0).search(params[:search]).paginate(per_page: 80, page: params[:page]).ordered + @teams = Team.non_empty_teams.search(params[:search]).paginate(per_page: 80, page: params[:page]).ordered end def show diff --git a/app/models/contest.rb b/app/models/contest.rb index 33da2c2..57d40a4 100644 --- a/app/models/contest.rb +++ b/app/models/contest.rb @@ -35,30 +35,30 @@ class Contest < ActiveRecord::Base attr_protected :id, :updated_at, :created_at - scope :active, :conditions => ["status != ?", STATUS_CLOSED] - scope :inactive, :conditions => {:status => STATUS_CLOSED} - scope :joinable, :conditions => {:status => STATUS_OPEN} - scope :with_contesters, :include => :contesters - scope :ordered, :order => "start DESC" - scope :nsls1, :conditions => ["name LIKE ?", "NSL S1:%"] - scope :nsls2, :conditions => ["name LIKE ?", "NSL S2:%"] - scope :ns1seasons, :conditions => ["name LIKE ?", "S%:%"] + scope :active, -> { where.not(status: STATUS_CLOSED) } + scope :inactive, -> { where(status: STATUS_CLOSED) } + scope :joinable, -> { where(status: STATUS_OPEN) } + scope :with_contesters, -> { includes(:contesters) } + scope :ordered, -> { order("start DESC") } + scope :nsls1, -> { where("name LIKE ?", "NSL S1:%") } + scope :nsls2, -> { where("name LIKE ?", "NSL S2:%") } + scope :ns1seasons, -> { where("name LIKE ?", "S%:%") } has_many :matches, :dependent => :destroy has_many :weeks, :dependent => :destroy - has_many :contesters, :dependent => :destroy, :include => :team + has_many :contesters, -> { includes(:team) }, :dependent => :destroy has_many :predictions, :through => :matches has_many :brackets - has_many :preds_with_score, - :source => :predictions, - :through => :matches, - :select => "predictions.id, predictions.user_id, - SUM(result) AS correct, - SUM(result)/COUNT(*)*100 AS score, - COUNT(*) AS total", - :conditions => "result IS NOT NULL", - :group => "predictions.user_id", - :order => "correct DESC" + has_many :preds_with_score, -> { + select("predictions.id, predictions.user_id + SUM(result) AS correct, + SUM(result)/COUNT(*)*100 AS score, + COUNT(*) AS total") + .where("result IS NOT NULL") + .group("predictions.user_id") + .order("correct DESC") }, + :source => :predictions, + :through => :matches has_and_belongs_to_many :maps belongs_to :demos, :class_name => "Directory" belongs_to :winner, :class_name => "Contester" diff --git a/app/models/contester.rb b/app/models/contester.rb index eafd233..980c3ca 100644 --- a/app/models/contester.rb +++ b/app/models/contester.rb @@ -26,22 +26,24 @@ class Contester < ActiveRecord::Base attr_protected :id, :updated_at, :created_at, :trend attr_accessor :user - scope :active, :include => :team, :conditions => {"contesters.active" => true} + belongs_to :team + belongs_to :contest + + scope :active, -> { includes(:team).where(active: true) } # ranked is used for ladder. lower score the higher the rank - scope :ranked, :select => "contesters.*", :order => "score ASC, win DESC, loss ASC" - scope :ordered, :select => "contesters.*, (score + extra) AS total_score", :order => "total_score DESC, score DESC, win DESC, loss ASC" - scope :chronological, :order => "created_at DESC" - scope :of_contest, lambda { |contest| {:conditions => {"contesters.contest_id" => contest.id}} } + scope :ranked, -> { order("score ASC, win DESC, loss ASC").select("contesters.*") } + scope :ordered, -> { select("contesters.*, (score + extra) AS total_score").order("total_score DESC, score DESC, win DESC, loss ASC") } + scope :chronological, -> { order("created_at DESC") } + scope :of_contest, -> (contest) { where("contesters.contest_id", contest.id) } has_many :challenges_sent, :class_name => "Challenge", :foreign_key => "contester1_id" has_many :challenges_received, :class_name => "Challenge", :foreign_key => "contester2_id" - has_many :matches, :through => :contest, :conditions => "(contester1_id = contesters.id OR contester2_id = contesters.id)" - belongs_to :team - belongs_to :contest + has_many :matches, -> { where("(contester1_id = contesters.id OR contester2_id = contesters.id)") }, :through => :contest validates_presence_of :team, :contest validates_inclusion_of [:score, :win, :loss, :draw, :extra], :in => 0..9999, :allow_nil => true validates_uniqueness_of :team_id, :scope => :contest_id, :message => "You can't join same contest twice." + #validate_on_create:validate_member_participation #validate_on_create:validate_contest #validate_on_create:validate_playernumber @@ -93,7 +95,7 @@ class Contester < ActiveRecord::Base end def validate_playernumber - if team.teamers.active.distinct.count < 6 + if team.teamers.active.unique_by_team.count < 6 errors.add :team, I18n.t(:contests_join_need6) end end diff --git a/app/models/forum.rb b/app/models/forum.rb index b402987..dff7494 100644 --- a/app/models/forum.rb +++ b/app/models/forum.rb @@ -19,13 +19,11 @@ class Forum < ActiveRecord::Base attr_protected :id, :updated_at, :created_at - scope :public, - :select => "forums.*", - :joins => "LEFT JOIN forumers ON forumers.forum_id = forums.id AND forumers.access = #{Forumer::ACCESS_READ}", - :conditions => "forumers.id IS NULL" - scope :of_forum, - lambda { |forum| {:conditions => {"forums.id" => forum.id}} } - scope :ordered, :order => "position" + scope :public_forums, -> { select("forums.*") + .joins("LEFT JOIN forumers ON forumers.forum_id = forums.id AND forumers.access = #{Forumer::ACCESS_READ}") + .where("forumers.id IS NULL") } + scope :of_forum, -> (forum) { where("forums.id", forum.id) } + scope :ordered, -> { order("position") } has_many :topics has_many :posts, :through => :topics @@ -50,7 +48,7 @@ class Forum < ActiveRecord::Base if cuser Forum.available_to(cuser, Forumer::ACCESS_READ).of_forum(self).first else - Forum.public.of_forum(self).first + Forum.public_forums.where(id: self.id).exists? end end diff --git a/app/models/gather.rb b/app/models/gather.rb index 920c42b..60b311e 100644 --- a/app/models/gather.rb +++ b/app/models/gather.rb @@ -30,7 +30,7 @@ class Gather < ActiveRecord::Base attr_accessor :admin scope :ordered, -> { order("id DESC") } - scope :basic, -> { include(:captain1, :captain2, :map1, :map2, :server) } + scope :basic, -> { includes(: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) } diff --git a/app/models/match.rb b/app/models/match.rb index 1059d07..b0de65f 100644 --- a/app/models/match.rb +++ b/app/models/match.rb @@ -41,11 +41,11 @@ class Match < ActiveRecord::Base has_many :matchers, :dependent => :destroy has_many :users, :through => :matchers has_many :predictions, :dependent => :destroy - has_many :comments, :as => :commentable, :order => "created_at", :dependent => :destroy + has_many :comments, -> { order("created_at") }, :as => :commentable, :dependent => :destroy belongs_to :challenge belongs_to :contest - belongs_to :contester1, :class_name => "Contester", :include => 'team' - belongs_to :contester2, :class_name => "Contester", :include => 'team' + belongs_to :contester1, -> { includes('team') }, :class_name => "Contester" + belongs_to :contester2, -> { includes('team') }, :class_name => "Contester" belongs_to :map1, :class_name => "Map" belongs_to :map2, :class_name => "Map" belongs_to :server @@ -57,65 +57,41 @@ class Match < ActiveRecord::Base belongs_to :stream, :class_name => "Movie" belongs_to :caster, :class_name => "User" - scope :future, :conditions => ["match_time > UTC_TIMESTAMP()"] - scope :future5, :conditions => ["match_time > UTC_TIMESTAMP()"], :limit => 5 - scope :finished, :conditions => ["score1 != 0 OR score2 != 0"] - scope :realfinished, :conditions => ["score1 IS NOT NULL AND score2 IS NOT NULL"] - scope :unfinished, :conditions => ["score1 IS NULL AND score2 IS NULL"] - scope :unreffed, :conditions => ["referee_id IS NULL"] - scope :ordered, :order => "match_time DESC" - scope :chrono, :order => "match_time ASC" - scope :recent, :limit => "8" - scope :bigrecent, :limit => "50" - scope :active, :conditions => ["contest_id IN (?)", Contest.active] - scope :on_day, - lambda { |day| { - :conditions => ["match_time > ? and match_time < ?", day.beginning_of_day, day.end_of_day]} } - scope :on_week, - lambda { |time| { - :conditions => ["match_time > ? and match_time < ?", time.beginning_of_week, time.end_of_week]} } - scope :of_contester, - lambda { |contester| { - :conditions => ["contester1_id = ? OR contester2_id = ?", contester.id, contester.id]} } - scope :of_user, - lambda { |user| { - :include => :matchers, - :conditions => ["matchers.user_id = ?", user.id]} } - scope :of_team, - lambda { |team| { - :include => {:contester1 => :team, - :contester2 => :team}, :conditions => ["teams.id = ? OR teams_contesters.id = ?", team.id, team.id]} } - scope :of_userteam, - lambda { |user, team| { - :include => {:matchers => {:contester => :team}}, - :conditions => ["teams.id = ? AND matchers.user_id = ?", team.id, user.id]} } - scope :within_time, - lambda { |from, to| { - :conditions => ["match_time > ? AND match_time < ?", from.utc, to.utc]} } - scope :around, - lambda { |time| { - :conditions => ["match_time > ? AND match_time < ?", time.ago(MATCH_LENGTH).utc, time.ago(-MATCH_LENGTH).utc]} } - scope :after, - lambda { |time| { - :conditions => ["match_time > ? AND match_time < ?", time.utc, time.ago(-MATCH_LENGTH).utc]} } - scope :map_stats, - :select => "map1_id, COUNT(*) as num, maps.name", - :joins => "LEFT JOIN maps ON maps.id = map1_id", - :group => "map1_id", - :having => "map1_id is not null", - :order => "num DESC" - scope :year_stats, - :select => "id, DATE_FORMAT(match_time, '%Y') as year, COUNT(*) as num", - :conditions => "match_time > '2000-01-01 01:01:01'", - :group => "year", - :order => "num DESC" - scope :month_stats, - :select => "id, DATE_FORMAT(match_time, '%m') as month_n, - DATE_FORMAT(match_time, '%M') as month, - COUNT(*) as num", - :conditions => "match_time > '2000-01-01 01:01:01'", - :group => "month", - :order => "month_n" + scope :future, -> { where("match_time > UTC_TIMESTAMP()") } + scope :future5, -> { where("match_time > UTC_TIMESTAMP()").limit(5) } + scope :finished, -> { where("score1 != 0 OR score2 != 0") } + scope :realfinished, -> { where("score1 IS NOT NULL AND score2 IS NOT NULL") } + scope :unfinished, -> { where("score1 IS NULL AND score2 IS NULL") } + scope :unreffed, -> { where.not(referee_id: nil) } + scope :ordered, -> { order("match_time DESC") } + scope :chrono, -> { order("match_time ASC") } + scope :recent, -> { limit("8") } + scope :bigrecent, -> { limit("50") } + scope :active, -> { where("contest_id IN (?)", Contest.active) } + scope :on_day, -> (day) { where("match_time > ? and match_time < ?", day.beginning_of_day, day.end_of_day) } + scope :on_week, -> (time) { where("match_time > ? and match_time < ?", time.beginning_of_week, time.end_of_week) } + scope :of_contester, -> (contester) { where("contester1_id = ? OR contester2_id = ?", contester.id, contester.id) } + scope :of_user, -> (user) { includes(:matchers).where("matchers.user_id = ?", user.id) } + scope :of_team, -> (team) { includes({:contester1 => :team, :contester2 => :team}).where("teams.id = ? OR teams_contesters.id = ?", team.id, team.id) } + scope :of_userteam, -> (user, team) { includes({:matchers => {:contester => :team}}).where("teams.id = ? AND matchers.user_id = ?", team.id, user.id) } + scope :within_time, -> (from, to) { where("match_time > ? AND match_time < ?", from.utc, to.utc) } + scope :around, -> (time) { where("match_time > ? AND match_time < ?", time.ago(MATCH_LENGTH).utc, time.ago(-MATCH_LENGTH).utc) } + scope :after, -> (time) { where("match_time > ? AND match_time < ?", time.utc, time.ago(-MATCH_LENGTH).utc) } + scope :map_stats, -> { select("map1_id, COUNT(*) as num, maps.name"). + joins("LEFT JOIN maps ON maps.id = map1_id"). + group("map1_id"). + having("map1_id is not null"). + order("num DESC") } + scope :year_stats, -> { select("id, DATE_FORMAT(match_time, '%Y') as year, COUNT(*) as num"). + where("match_time > '2000-01-01 01:01:01'"). + group("year"). + order("num DESC") } + scope :month_stats, -> { select("id, DATE_FORMAT(match_time, '%m') as month_n, + DATE_FORMAT(match_time, '%M') as month, + COUNT(*) as num"). + where("match_time > '2000-01-01 01:01:01'"). + group("month"). + order("month_n") } validates_presence_of :contester1, :contester2, :contest validates_format_of [:score1, :score2], :with => /\A[0-9]\z/, :allow_nil => true @@ -145,12 +121,12 @@ class Match < ActiveRecord::Base end def preds contester - perc = Prediction.count(:conditions => ["match_id = ? AND score#{contester} > 2", id]) + perc = Prediction.where("match_id = ? AND score#{contester} > 2", id).count() perc != 0 ? (perc/predictions.count.to_f*100).round : 0 end def mercs contester - matchers.all :conditions => {:merc => true, :contester_id => contester.id} + matchers.where(merc: true, contester_id: contester.id) end def get_hltv @@ -162,11 +138,11 @@ class Match < ActiveRecord::Base end def team1_lineup - matchers.all(:conditions => {:contester_id => contester1_id}) + matchers.where(contester_id: contester1_id) end def team2_lineup - matchers.all(:conditions => {:contester_id => contester2_id}) + matchers.where(contester_id: contester2_id) end def get_friendly param = nil @@ -194,7 +170,7 @@ class Match < ActiveRecord::Base end def send_notifications - Profile.all(:include => :user, :conditions => "notify_any_match = 1").each do |p| + Profile.where("notify_any_match", 1).includes(:user).each do |p| Notifications.match p.user, self if p.user end contester2.team.teamers.active.each do |teamer| diff --git a/app/models/movie.rb b/app/models/movie.rb index 77f005f..d834d0e 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -18,8 +18,6 @@ # category_id :integer # -require 'data_file' - class Movie < ActiveRecord::Base include Extra diff --git a/app/models/poll.rb b/app/models/poll.rb index 802235b..ee1e77d 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -14,6 +14,8 @@ class Poll < ActiveRecord::Base include Extra + default_scope -> { order("created_at DESC") } + attr_protected :id, :updated_at, :created_at, :votes, :user_id validates_length_of :question, :in => 1..50 diff --git a/app/models/team.rb b/app/models/team.rb index 3596264..c7b1e71 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -33,33 +33,36 @@ class Team < ActiveRecord::Base validates_format_of :country, :with => /\A[A-Z]{2}\z/, :allow_blank => true validates_length_of [:comment, :recruiting], :in => 0..75, :allow_blank => true - scope :with_teamers_num, - lambda { |num| { - :select => "teams.*, COUNT(T.id) AS teamers_num", - :joins => "LEFT JOIN teamers T ON T.team_id = teams.id AND T.rank >= #{Teamer::RANK_MEMBER}", - :group => "teams.id", - :having => ["teamers_num >= ?", num]} } - scope :with_teamers, :include => :teamers - scope :active, :conditions => {:active => true} - scope :inactive, :conditions => {:active => false} - scope :ordered, :order => "name" - scope :recruiting, :conditions => "recruiting IS NOT NULL AND recruiting != ''" + scope :with_teamers_num, -> (num) { + select("teams.*, COUNT(T.id) AS teamers_num"). + joins("LEFT JOIN teamers T ON T.team_id = teams.id AND T.rank >= #{Teamer::RANK_MEMBER}"). + group("teams.id"). + having("teamers_num >= ?", num) } + scope :non_empty_teams, -> { joins(:teamers).where("teamers.rank >= #{Teamer::RANK_MEMBER}").distinct } + scope :with_teamers, -> { includes(:teamers) } + scope :active, -> { where(active: true) } + scope :inactive, -> { where(active: false) } + scope :ordered, -> { order("name") } + scope :recruiting, -> { where("recruiting IS NOT NULL AND recruiting != ''") } belongs_to :founder, :class_name => "User" + + has_many :active_teamers, -> { where("rank >= ?", Teamer::RANK_MEMBER) } has_many :teamers, :dependent => :destroy - has_many :leaders, :class_name => "Teamer", :conditions => ["rank = ?", Teamer::RANK_LEADER] + has_many :leaders, -> { where("rank = ?", Teamer::RANK_LEADER) }, :class_name => "Teamer" has_many :contesters, :dependent => :destroy - has_many :contests, :through => :contesters, :conditions => {"contesters.active" => true} + has_many :contests, -> { where("contesters.active", true) }, :through => :contesters has_many :received_messages, :class_name => "Message", :as => "recipient" has_many :sent_messages, :class_name => "Message", :as => "sender" has_many :matches, :through => :contesters - has_many :matches_finished, :through => :contesters, :source => :matches, :conditions => "(score1 != 0 OR score2 != 0)" - has_many :matches_won, :through => :contesters, :source => :matches, - :conditions => "((score1 > score2 AND contester1_id = contesters.id) OR (score2 > score1 AND contester2_id = contesters.id)) AND (score1 != 0 OR score2 != 0)" - has_many :matches_lost, :through => :contesters, :source => :matches, - :conditions => "((score1 < score2 AND contester1_id = contesters.id) OR (score2 < score1 AND contester2_id = contesters.id)) AND (score1 != 0 OR score2 != 0)" - has_many :matches_draw, :through => :contesters, :source => :matches, - :conditions => "(score1 = score2 AND score1 > 0) AND (score1 != 0 OR score2 != 0)" + has_many :matches_finished, -> { where("(score1 != 0 OR score2 != 0)") }, + :through => :contesters, :source => :matches + has_many :matches_won, -> { where("((score1 > score2 AND contester1_id = contesters.id) OR (score2 > score1 AND contester2_id = contesters.id)) AND (score1 != 0 OR score2 != 0)") }, + :through => :contesters, :source => :matches + has_many :matches_lost, -> { where("((score1 < score2 AND contester1_id = contesters.id) OR (score2 < score1 AND contester2_id = contesters.id)) AND (score1 != 0 OR score2 != 0)") }, + :through => :contesters, :source => :matches + has_many :matches_draw, -> { where("(score1 = score2 AND score1 > 0) AND (score1 != 0 OR score2 != 0)") }, + :through => :contesters, :source => :matches mount_uploader :logo, TeamUploader @@ -89,7 +92,7 @@ class Team < ActiveRecord::Base end def self.search(search) - search ? where("LOWER(name) LIKE LOWER(?)", "%#{search}%") : scoped + search ? where("LOWER(name) LIKE LOWER(?)", "%#{search}%") : all end def destroy diff --git a/app/models/teamer.rb b/app/models/teamer.rb index 54361ca..0555d62 100644 --- a/app/models/teamer.rb +++ b/app/models/teamer.rb @@ -29,80 +29,67 @@ class Teamer < ActiveRecord::Base #validate_on_create:validate_contests validate :validate_team - scope :basic, - :include => :user, - :order => "rank DESC, created_at ASC" - scope :past, - :conditions => ["teamers.rank = ?", RANK_REMOVED] - scope :joining, - :conditions => ["teamers.rank = ?", RANK_JOINER] - scope :present, - :conditions => ["teamers.rank >= ?", RANK_JOINER] - scope :active, - :conditions => ["teamers.rank >= ?", RANK_MEMBER] - scope :leaders, - :conditions => ["teamers.rank >= ?", RANK_DEPUTEE] - scope :of_team, - lambda { |team| {:conditions => {"teamers.team_id" => team.id}} } - scope :active_teams, - :include => :team, - :conditions => ["teams.active = ?", true] - scope :distinct, - :group => "user_id, team_id" - scope :ordered, - :order => "rank DESC, created_at ASC" - scope :historic, - lambda { |user, time| - {:conditions => ["user_id = ? AND created_at < ? AND ((updated_at > ? AND rank = ?) OR rank >= ?)", - user.id, time.utc, time.utc, RANK_REMOVED, RANK_MEMBER]} } + scope :basic, -> { includes(:user).order("rank DESC, created_at ASC") } + scope :past, -> { where("teamers.rank = ?", RANK_REMOVED) } + scope :joining,-> { where("teamers.rank = ?", RANK_JOINER) } + scope :present, -> { where("teamers.rank >= ?", RANK_JOINER) } + scope :active, -> { where("teamers.rank >= ?", RANK_MEMBER) } + scope :leaders, -> { where("teamers.rank >= ?", RANK_DEPUTEE) } + scope :of_team, -> (team) { where("teamers.team_id" => team.id) } + scope :active_teams, -> { includes(:team).where("teams.active = ?", true) } + scope :unique_by_team, -> { group("user_id, team_id") } + scope :ordered, -> { order("rank DESC, created_at ASC") } + scope :historic, -> (user, time) { + where("user_id = ? AND created_at < ? AND ((updated_at > ? AND rank = ?) OR rank >= ?)", + user.id, time.utc, time.utc, RANK_REMOVED, RANK_MEMBER) } - belongs_to :user - belongs_to :team - has_many :other_teamers, :through => :user, :source => :teamers, :conditions => ["teamers.id != ?", object_id] - has_many :contesters, :through => :team + belongs_to :user + belongs_to :team + has_many :other_teamers, -> { where("teamers.id != ?", object_id) }, :through => :user, :source => :teamers + has_many :contesters, :through => :team - before_create :init_variables + before_create :init_variables - def to_s - user.to_s + def to_s + user.to_s + end + + def ranks + {RANK_JOINER => "Joining", RANK_MEMBER => "Member", RANK_DEPUTEE => "Deputee", RANK_LEADER => "Leader"} + end + + def validate_team + if user.teamers.of_team(team).present.count > 0 + errors.add :team, I18n.t(:teams_join_twice) end + end - def ranks - {RANK_JOINER => "Joining", RANK_MEMBER => "Member", RANK_DEPUTEE => "Deputee", RANK_LEADER => "Leader"} - end + def validate_contests + # TODO + end - def validate_team - if user.teamers.of_team(team).present.count > 0 - errors.add :team, I18n.t(:teams_join_twice) - end - end + def init_variables + self.rank = RANK_JOINER unless self.rank + end - def validate_contests - # TODO + def destroy + user.update_attribute :team, nil if user.team == team + if rank == Teamer::RANK_JOINER + super + else + update_attribute :rank, Teamer::RANK_REMOVED end + end - def init_variables - self.rank = RANK_JOINER unless self.rank - end + def can_create? cuser, params + cuser and Verification.contain params, [:user_id, :team_id] + end - def destroy - user.update_attribute :team, nil if user.team == team - if rank == Teamer::RANK_JOINER - super - else - update_attribute :rank, Teamer::RANK_REMOVED - end - end + def can_update? cuser + cuser and cuser.admin? + end - def can_create? cuser, params - cuser and Verification.contain params, [:user_id, :team_id] - end - - def can_update? cuser - cuser and cuser.admin? - end - - def can_destroy? cuser - cuser and (user == cuser or team.is_leader? cuser or cuser.admin?) - end + def can_destroy? cuser + cuser and (user == cuser or team.is_leader? cuser or cuser.admin?) + end end diff --git a/app/models/user.rb b/app/models/user.rb index 2afc580..fc2622f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -90,23 +90,23 @@ class User < ActiveRecord::Base .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()", - :conditions => "bans.id IS NOT NULL" - scope :idle, - :conditions => ["lastvisit < ?", 30.minutes.ago.utc] + scope :banned, -> { + joins("LEFT JOIN bans ON bans.user_id = users.id AND expiry > UTC_TIMESTAMP()") + .conditions("bans.id IS NOT NULL") } + scope :idle, -> { + joins("lastvisit < ?", 30.minutes.ago.utc) } validates_uniqueness_of :username, :email, :steamid validates_length_of :firstname, :in => 1..15, :allow_blank => true validates_length_of :lastname, :in => 1..25, :allow_blank => true validates_length_of :username, :in => 2..20 validates_format_of :username, :with => /\A[A-Za-z0-9_\-\+]{2,20}\Z/ - validates_presence_of :raw_password, :on => :create + validates_presence_of :raw_password, :on => :create validates_length_of :email, :maximum => 50 validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i validates_length_of :steamid, :maximum => 30 validates_format_of :steamid, :with => /\A([0-9]{1,10}:){2}[0-9]{1,10}\Z/ - validates_length_of :time_zone, :maximum => 100, :allow_blank => true, :allow_nil => true + validates_length_of :time_zone, :maximum => 100, :allow_blank => true, :allow_nil => true validates_inclusion_of [:public_email], :in => [true, false], :allow_nil => true validate :validate_team @@ -292,7 +292,7 @@ class User < ActiveRecord::Base end def self.search(search) - search ? where("LOWER(username) LIKE LOWER(?) OR steamid LIKE ?", "%#{search}%", "%#{search}%") : scoped + search ? where("LOWER(username) LIKE LOWER(?) OR steamid LIKE ?", "%#{search}%", "%#{search}%") : all end def self.refadmins diff --git a/app/views/forums/index.html.erb b/app/views/forums/index.html.erb index 353e7f9..eaedf73 100644 --- a/app/views/forums/index.html.erb +++ b/app/views/forums/index.html.erb @@ -2,7 +2,7 @@
<% @categories.each do |cat| %> - <% forums = cuser ? cat.forums.available_to(cuser, Forumer::ACCESS_READ).ordered : cat.forums.public.ordered %> + <% forums = cuser ? cat.forums.available_to(cuser, Forumer::ACCESS_READ).ordered : cat.forums.public_forums.ordered %> <% next if forums.length == 0 %>
@@ -58,7 +58,7 @@ <%= Topic.count %> topics, and <%= User.count %> users.

- Our newest member is <%= namelink User.last %> and most active member is <%= namelink User.posts_stats.first %>. + Our newest member is <%= namelink User.last %> and most active member is <%= namelink User.posts_stats(1).first %>.

diff --git a/app/views/teams/_list.html.erb b/app/views/teams/_list.html.erb index d754114..837fd43 100644 --- a/app/views/teams/_list.html.erb +++ b/app/views/teams/_list.html.erb @@ -10,7 +10,6 @@ <% for team in teams %> - <% if team.teamers_num > 0 %> <%= flag team.country %> <%= namelink team %> @@ -33,6 +32,5 @@ <% end %> - <% end %> <% end %> diff --git a/app/views/teams/show.html.erb b/app/views/teams/show.html.erb index f0e2deb..043f1d9 100644 --- a/app/views/teams/show.html.erb +++ b/app/views/teams/show.html.erb @@ -58,14 +58,14 @@
- <% if @team.teamers.active.ordered.distinct.length > 0 %> + <% if @team.teamers.active.ordered.unique_by_team.length > 0 %>

Current Members

- <%= render partial: "teamers/list", locals: { teamers: @team.teamers.active.ordered.distinct, blacklist: false, comment: true } %> + <%= render partial: "teamers/list", locals: { teamers: @team.teamers.active.ordered.unique_by_team, blacklist: false, comment: true } %> <% end %> - <% if @team.teamers.past.distinct.length > 0 %> + <% if @team.teamers.past.unique_by_team.length > 0 %>

Past Members

- <%= render partial: "teamers/list", locals: { teamers: @team.teamers.past.distinct, blacklist: @team.teamers.active.ordered.distinct, comment: false } %> + <%= render partial: "teamers/list", locals: { teamers: @team.teamers.past.unique_by_team, blacklist: @team.teamers.active.ordered.unique_by_team, comment: false } %> <% end %>
diff --git a/app/views/widgets/_poll.html.erb b/app/views/widgets/_poll.html.erb index 74907ce..a261804 100644 --- a/app/views/widgets/_poll.html.erb +++ b/app/views/widgets/_poll.html.erb @@ -2,7 +2,7 @@

<%= t('widget.poll') %>

- <% @poll = Poll.first(order: "created_at DESC") %> + <% @poll = Poll.first %> <%= render(partial: "polls/show") if @poll %>
<% end %> \ No newline at end of file diff --git a/bin/bundle b/bin/bundle new file mode 100755 index 0000000..66e9889 --- /dev/null +++ b/bin/bundle @@ -0,0 +1,3 @@ +#!/usr/bin/env ruby +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) +load Gem.bin_path('bundler', 'bundle') diff --git a/bin/rails b/bin/rails new file mode 100755 index 0000000..728cd85 --- /dev/null +++ b/bin/rails @@ -0,0 +1,4 @@ +#!/usr/bin/env ruby +APP_PATH = File.expand_path('../../config/application', __FILE__) +require_relative '../config/boot' +require 'rails/commands' diff --git a/bin/rake b/bin/rake new file mode 100755 index 0000000..1724048 --- /dev/null +++ b/bin/rake @@ -0,0 +1,4 @@ +#!/usr/bin/env ruby +require_relative '../config/boot' +require 'rake' +Rake.application.run