use activerecord query interface instead of deprecated finder methods

This commit is contained in:
Serge Paquet 2013-12-23 22:20:06 -05:00
parent bf0dd25bfd
commit 8fbf76b484
22 changed files with 102 additions and 84 deletions

View file

@ -9,7 +9,7 @@ class ApplicationController < ActionController::Base
def authenticate_user
if session[:u]
@user = User.find_by_session_token(session[:u].to_s)
@user = User.where(:session_token => session[:u].to_s).first
end
true
@ -59,8 +59,9 @@ class ApplicationController < ActionController::Base
@_tags_filtered = nil
def tags_filtered_by_cookie
@_tags_filtered ||= cookies[TAG_FILTER_COOKIE].to_s.split(",").map{|t|
Tag.find_by_tag(t) }.reject{|tf| !tf }
@_tags_filtered ||= Tag.where(
:tag => cookies[TAG_FILTER_COOKIE].to_s.split(",")
)
end
def user_is_spider?
@ -69,7 +70,7 @@ class ApplicationController < ActionController::Base
def find_user_from_rss_token
if !@user && request[:format] == "rss" && params[:token].to_s.present?
@user = User.find_by_rss_token(params[:token].to_s)
@user = User.where(:rss_token => params[:token].to_s).first
end
end
end

View file

@ -8,7 +8,7 @@ class CommentsController < ApplicationController
before_filter :find_user_from_rss_token, :only => [ :index ]
def create
if !(story = Story.find_by_short_id(params[:story_id])) || story.is_gone?
if !(story = Story.where(:short_id => params[:story_id]).first) || story.is_gone?
return render :text => "can't find story", :status => 400
end
@ -18,8 +18,8 @@ class CommentsController < ApplicationController
comment.user_id = @user.id
if params[:parent_comment_short_id].present?
if pc = Comment.find_by_story_id_and_short_id(story.id,
params[:parent_comment_short_id])
if pc = Comment.where(:story_id => story.id, :short_id =>
params[:parent_comment_short_id]).first
comment.parent_comment_id = pc.id
# needed for carryng along in comment preview form
comment.parent_comment_short_id = params[:parent_comment_short_id]
@ -31,8 +31,8 @@ class CommentsController < ApplicationController
# prevent double-clicks of the post button
if !params[:preview].present? &&
(pc = Comment.find_by_story_id_and_user_id_and_parent_comment_id(story.id,
@user.id, comment.parent_comment_id))
(pc = Comment.where(:story_id => story.id, :user_id => @user.id,
:parent_comment_id => comment.parent_comment_id).first)
if (Time.now - pc.created_at) < 5.minutes
comment.errors.add(:comment, "^You have already posted a comment " <<
"here recently.")
@ -72,8 +72,7 @@ class CommentsController < ApplicationController
end
def edit
if !((comment = Comment.find_by_short_id(params[:comment_id])) &&
comment.is_editable_by_user?(@user))
if !((comment = find_comment) && comment.is_editable_by_user?(@user))
return render :text => "can't find comment", :status => 400
end
@ -83,8 +82,7 @@ class CommentsController < ApplicationController
end
def delete
if !((comment = Comment.find_by_short_id(params[:comment_id])) &&
comment.is_deletable_by_user?(@user))
if !((comment = find_comment) && comment.is_deletable_by_user?(@user))
return render :text => "can't find comment", :status => 400
end
@ -96,8 +94,7 @@ class CommentsController < ApplicationController
end
def undelete
if !((comment = Comment.find_by_short_id(params[:comment_id])) &&
comment.is_undeletable_by_user?(@user))
if !((comment = find_comment) && comment.is_undeletable_by_user?(@user))
return render :text => "can't find comment", :status => 400
end
@ -109,8 +106,7 @@ class CommentsController < ApplicationController
end
def update
if !((comment = Comment.find_by_short_id(params[:comment_id])) &&
comment.is_editable_by_user?(@user))
if !((comment = find_comment) && comment.is_editable_by_user?(@user))
return render :text => "can't find comment", :status => 400
end
@ -133,8 +129,7 @@ class CommentsController < ApplicationController
end
def preview
if !((comment = Comment.find_by_short_id(params[:comment_id])) &&
comment.is_editable_by_user?(@user))
if !((comment = find_comment) && comment.is_editable_by_user?(@user))
return render :text => "can't find comment", :status => 400
end
@ -149,7 +144,7 @@ class CommentsController < ApplicationController
end
def unvote
if !(comment = Comment.find_by_short_id(params[:comment_id]))
if !(comment = find_comment)
return render :text => "can't find comment", :status => 400
end
@ -160,7 +155,7 @@ class CommentsController < ApplicationController
end
def upvote
if !(comment = Comment.find_by_short_id(params[:comment_id]))
if !(comment = find_comment)
return render :text => "can't find comment", :status => 400
end
@ -171,7 +166,7 @@ class CommentsController < ApplicationController
end
def downvote
if !(comment = Comment.find_by_short_id(params[:comment_id]))
if !(comment = find_comment)
return render :text => "can't find comment", :status => 400
end
@ -198,13 +193,17 @@ class CommentsController < ApplicationController
@page = params[:page].to_i
end
@comments = Comment.find(
:all,
:conditions => "is_deleted = 0 AND is_moderated = 0",
:order => "created_at DESC",
:offset => ((@page - 1) * COMMENTS_PER_PAGE),
:limit => COMMENTS_PER_PAGE,
:include => [ :user, :story ])
@comments = Comment.where(
:is_deleted => false, :is_moderated => false
).order(
"created_at DESC"
).offset(
(@page - 1) * COMMENTS_PER_PAGE
).limit(
COMMENTS_PER_PAGE
).includes(
:user, :story
)
if @user
@votes = Vote.comment_votes_by_user_for_comment_ids_hash(@user.id,
@ -220,7 +219,7 @@ class CommentsController < ApplicationController
def threads
if params[:user]
@showing_user = User.find_by_username!(params[:user])
@showing_user = User.where(:username => params[:user]).first!
@heading = @title = "Threads for #{@showing_user.username}"
@cur_url = "/threads/#{@showing_user.username}"
elsif !@user
@ -269,4 +268,10 @@ end
@comments = @threads.flatten
end
private
def find_comment
Comment.where(:short_id => params[:comment_id]).first
end
end

View file

@ -23,7 +23,7 @@ class FiltersController < ApplicationController
params.each do |k,v|
if (m = k.match(/^tag_(.+)$/)) && v.to_i == 1 &&
(t = Tag.find_by_tag(m[1])) && t.valid_for?(@user)
(t = Tag.where(:tag => m[1]).first) && t.valid_for?(@user)
new_filters.push m[1]
end
end
@ -40,7 +40,7 @@ class FiltersController < ApplicationController
new_filters.each do |t|
tf = TagFilter.new
tf.user_id = @user.id
tf.tag_id = Tag.find_by_tag(t).id
tf.tag_id = Tag.where(:tag => t).first.id
tf.save!
end
else

View file

@ -55,7 +55,7 @@ class HomeController < ApplicationController
end
def newest_by_user
for_user = User.find_by_username!(params[:user])
for_user = User.where(:username => params[:user]).first!
@stories = find_stories_for_user_and_tag_and_newest_and_by_user(@user,
nil, false, for_user.id)
@ -70,7 +70,7 @@ class HomeController < ApplicationController
end
def tagged
@tag = Tag.find_by_tag!(params[:tag])
@tag = Tag.where(:tag => params[:tag]).first!
@stories = find_stories_for_user_and_tag_and_newest_and_by_user(@user,
@tag, false, nil)
@ -168,13 +168,16 @@ private
conds += filtered_tag_ids
end
stories = Story.find(
:all,
:conditions => conds,
:include => [ :user, { :taggings => :tag } ],
:limit => STORIES_PER_PAGE + 1,
:offset => ((@page - 1) * STORIES_PER_PAGE),
:order => (newest ? "stories.created_at DESC" : "hotness")
stories = Story.where(
*conds
).includes(
:user, :taggings => :tag
).limit(
STORIES_PER_PAGE + 1
).offset(
(@page - 1) * STORIES_PER_PAGE
).order(
newest ? "stories.created_at DESC" : "hotness"
)
show_more = false
@ -201,7 +204,7 @@ private
# eager load comment counts
if stories.any?
comment_counts = {}
Keystore.find(:all, :conditions => stories.map{|s|
Keystore.where(stories.map{|s|
"`key` = 'story:#{s.id}:comment_count'" }.join(" OR ")).each do |ks|
comment_counts[ks.key[/\d+/].to_i] = ks.value
end

View file

@ -10,7 +10,7 @@ class InvitationsController < ApplicationController
end
def confirm_email
if !(ir = InvitationRequest.find_by_code(params[:code].to_s))
if !(ir = InvitationRequest.where(:code => params[:code].to_s).first)
flash[:error] = "Invalid or expired invitation request"
return redirect_to "/invitations/request"
end
@ -67,7 +67,7 @@ class InvitationsController < ApplicationController
end
def send_for_request
if !(ir = InvitationRequest.find_by_code(params[:code].to_s))
if !(ir = InvitationRequest.where(:code => params[:code].to_s).first)
flash[:error] = "Invalid or expired invitation request"
return redirect_to "/invitations"
end

View file

@ -51,7 +51,7 @@ class LoginController < ApplicationController
@title = "Reset Password"
if params[:token].blank? ||
!(@reset_user = User.find_by_password_reset_token(params[:token].to_s))
!(@reset_user = User.where(:password_reset_token => params[:token].to_s).first)
flash[:error] = "Invalid reset token. It may have already been " <<
"used or you may have copied it incorrectly."
return redirect_to forgot_password_url

View file

@ -96,7 +96,7 @@ class MessagesController < ApplicationController
private
def find_message
if @message = Message.find_by_short_id(params[:message_id] || params[:id])
if @message = Message.where(:short_id => params[:message_id] || params[:id]).first
if (@message.author_user_id == @user.id ||
@message.recipient_user_id == @user.id)
return true

View file

@ -14,6 +14,6 @@ class ModerationsController < ApplicationController
end
@moderations = Moderation.order("id desc").limit(50).offset((@page - 1) *
50).all
50)
end
end

View file

@ -16,7 +16,7 @@ class SignupController < ApplicationController
return redirect_to "/"
end
if !(@invitation = Invitation.find_by_code(params[:invitation_code].to_s))
if !(@invitation = Invitation.where(:code => params[:invitation_code].to_s).first)
flash[:error] = "Invalid or expired invitation"
return redirect_to "/signup"
end
@ -30,7 +30,7 @@ class SignupController < ApplicationController
end
def signup
if !(@invitation = Invitation.find_by_code(params[:invitation_code].to_s))
if !(@invitation = Invitation.where(:code => params[:invitation_code].to_s).first)
flash[:error] = "Invalid or expired invitation."
return redirect_to "/signup"
end

View file

@ -5,7 +5,7 @@ class StoriesController < ApplicationController
before_filter :require_logged_in_user, :only => [ :delete, :create, :edit,
:fetch_url_title, :new ]
before_filter :find_story, :only => [ :destroy, :edit, :undelete, :update ]
before_filter :find_user_story, :only => [ :destroy, :edit, :undelete, :update ]
def create
@title = "Submit Story"
@ -116,7 +116,7 @@ class StoriesController < ApplicationController
end
def show
@story = Story.find_by_short_id!(params[:id])
@story = Story.where(:short_id => params[:id]).first!
if @story.can_be_seen_by_user?(@user)
@title = @story.title
@ -144,11 +144,11 @@ class StoriesController < ApplicationController
end
def show_comment
@story = Story.find_by_short_id!(params[:id])
@story = Story.where(:short_id => params[:id]).first!
@title = @story.title
@showing_comment = Comment.find_by_short_id(params[:comment_short_id])
@showing_comment = Comment.where(:short_id => params[:comment_short_id]).first
if !@showing_comment
flash[:error] = "Could not find comment. It may have been deleted."
@ -208,7 +208,7 @@ class StoriesController < ApplicationController
end
def unvote
if !(story = Story.find_by_short_id(params[:story_id]))
if !(story = find_story)
return render :text => "can't find story", :status => 400
end
@ -219,7 +219,7 @@ class StoriesController < ApplicationController
end
def upvote
if !(story = Story.find_by_short_id(params[:story_id]))
if !(story = find_story)
return render :text => "can't find story", :status => 400
end
@ -230,7 +230,7 @@ class StoriesController < ApplicationController
end
def downvote
if !(story = Story.find_by_short_id(params[:story_id]))
if !(story = find_story)
return render :text => "can't find story", :status => 400
end
@ -245,12 +245,17 @@ class StoriesController < ApplicationController
end
private
def find_story
Story.where(:short_id => params[:story_id]).first
end
def find_user_story
if @user.is_moderator?
@story = Story.find_by_short_id(params[:story_id] || params[:id])
@story = Story.where(:short_id => params[:story_id] || params[:id]).first
else
@story = Story.find_by_user_id_and_short_id(@user.id,
(params[:story_id] || params[:id]))
@story = Story.where(:user_id => @user.id, :short_id =>
(params[:story_id] || params[:id])).first
end
if !@story
@ -263,8 +268,8 @@ private
def load_user_votes
if @user
if v = Vote.find_by_user_id_and_story_id_and_comment_id(@user.id,
@story.id, nil)
if v = Vote.where(:user_id => @user.id, :story_id => @story.id,
:comment_id => nil).first
@story.vote = v.vote
end

View file

@ -1,6 +1,6 @@
class UsersController < ApplicationController
def show
@showing_user = User.find_by_username!(params[:id])
@showing_user = User.where(:username => params[:id]).first!
@title = "User #{@showing_user.username}"
end
@ -13,7 +13,7 @@ class UsersController < ApplicationController
(parents[u.invited_by_user_id.to_i] ||= []).push u
end
Keystore.find(:all, :conditions => "`key` like 'user:%:karma'").each do |k|
Keystore.where("key like 'user:%:karma'").each do |k|
karmas[k.key[/\d+/].to_i] = k.value
end

View file

@ -130,7 +130,7 @@ class Comment < ActiveRecord::Base
def deliver_mention_notifications
self.plaintext_comment.scan(/\B\@([\w\-]+)/).flatten.uniq.each do |mention|
if u = User.find_by_username(mention)
if u = User.where(:username => mention).first
if u.id == self.user.id
next
end
@ -309,8 +309,7 @@ class Comment < ActiveRecord::Base
cs = [ "story_id = ?", story_id ]
end
Comment.find(:all, :conditions => cs, :order => "confidence DESC",
:include => :user).each do |c|
Comment.where(*cs).order("confidence DESC").includes(:user).each do |c|
(parents[c.parent_comment_id.to_i] ||= []).push c
end

View file

@ -18,7 +18,8 @@ class Invitation < ActiveRecord::Base
raise "too many hash collisions"
end
if !Invitation.find_by_code(self.code = Utils.random_str(15))
self.code = Utils.random_str(15)
unless Invitation.exists?(:code => self.code)
break
end
end

View file

@ -18,7 +18,8 @@ class InvitationRequest < ActiveRecord::Base
raise "too many hash collisions"
end
if !InvitationRequest.find_by_code(self.code = Utils.random_str(15))
self.code = Utils.random_str(15)
unless InvitationRequest.exists?(:code => self.code)
break
end
end

View file

@ -4,7 +4,7 @@ class Keystore < ActiveRecord::Base
attr_accessible nil
def self.get(key)
Keystore.find_by_key(key)
self.where(:key => key).first
end
def self.put(key, value)

View file

@ -60,7 +60,7 @@ class Message < ActiveRecord::Base
def recipient_username=(username)
self.recipient_user_id = nil
if u = User.find_by_username(username)
if u = User.where(:username => username).first
self.recipient_user_id = u.id
@recipient_username = username
else

View file

@ -96,7 +96,7 @@ class Story < ActiveRecord::Base
end
conds[0] << ")"
if s = Story.find(:first, :conditions => conds)
if s = Story.where(*conds).first
return s
end
@ -302,8 +302,8 @@ class Story < ActiveRecord::Base
end
new_tag_names_a.each do |tag_name|
if tag_name.to_s != "" && !self.tags.map{|t| t.tag }.include?(tag_name)
if t = Tag.find_by_tag(tag_name)
if tag_name.to_s != "" && !self.tags.exists?(:tag => tag_name)
if t = Tag.where(:tag => tag_name).first
# we can't lookup whether the user is allowed to use this tag yet
# because we aren't assured to have a user_id by now; we'll do it in
# the validation with check_tags

View file

@ -9,7 +9,7 @@ class Tag < ActiveRecord::Base
end
def self.all_with_filtered_counts_for(user)
counts = TagFilter.count(:group => "tag_id")
counts = TagFilter.group(:tag_id).count
Tag.order(:tag).accessible_to(user).map{|t|
t.filtered_count = counts[t.id].to_i

View file

@ -36,8 +36,11 @@ class Vote < ActiveRecord::Base
def self.comment_votes_by_user_for_story_hash(user_id, story_id)
votes = {}
Vote.find(:all, :conditions => [ "user_id = ? AND story_id = ? AND " +
"comment_id IS NOT NULL", user_id, story_id ]).each do |v|
Vote.where(
:user_id => user_id, :story_id => story_id
).where(
"comment_id IS NOT NULL"
).each do |v|
votes[v.comment_id] = { :vote => v.vote, :reason => v.reason }
end
@ -58,7 +61,7 @@ class Vote < ActiveRecord::Base
end
cond[0] += ")"
Vote.find(:all, :conditions => cond).each do |v|
Vote.where(*cond).each do |v|
votes[v.story_id] = { :vote => v.vote, :reason => v.reason }
end
@ -79,7 +82,7 @@ class Vote < ActiveRecord::Base
end
cond[0] += ")"
Vote.find(:all, :conditions => cond).each do |v|
Vote.where(*cond).each do |v|
votes[v.comment_id] = { :vote => v.vote, :reason => v.reason }
end
@ -88,8 +91,8 @@ class Vote < ActiveRecord::Base
def self.vote_thusly_on_story_or_comment_for_user_because(vote, story_id,
comment_id, user_id, reason, update_counters = true)
v = Vote.find_or_initialize_by_user_id_and_story_id_and_comment_id(user_id,
story_id, comment_id)
v = Vote.where(:user_id => user_id, :story_id => story_id,
:comment_id => comment_id).first_or_initialize
if !v.new_record? && v.vote == vote
return

View file

@ -3,7 +3,7 @@ class AddTagMediaTypes < ActiveRecord::Migration
add_column :tags, :is_media, :boolean, :default => false
[ "pdf", "video" ].each do |t|
if tag = Tag.find_by_tag(t)
if tag = Tag.where(:tag => t).first
tag.is_media = true
tag.save
end

View file

@ -33,7 +33,7 @@ class Markdowner
if !opts[:disable_profile_links]
# make @username link to that user's profile
html.gsub!(/\B\@([\w\-]+)/) do |u|
if User.find_by_username(u[1 .. -1])
if User.exists?(:username => u[1 .. -1])
"<a href=\"/u/#{u[1 .. -1]}\">#{u}</a>"
else
u

View file

@ -70,9 +70,9 @@ end
irt = email[:in_reply_to].to_s.gsub(/[^A-Za-z0-9@\.]/, "")
if m = irt.match(/^comment\.([^\.]+)\.\d+@/)
parent = Comment.find_by_short_id(m[1])
parent = Comment.where(:short_id => m[1]).first
elsif m = irt.match(/^story\.([^\.]+)\.\d+@/)
parent = Story.find_by_short_id(m[1])
parent = Story.where(:short_id => m[1]).first
end
if !parent