Perform the where(...).first to find_by(...) refactoring.

This refactoring was automated using the command: bundle exec "ruby refactorings/where_dot_first_to_find_by/app.rb"
This commit is contained in:
Louis Rose
2014-05-06 14:41:59 +01:00
parent f1369e4503
commit 1574485443
104 changed files with 204 additions and 213 deletions

View File

@@ -5,7 +5,7 @@ class Admin::ApiController < Admin::AdminController
end
def regenerate_key
api_key = ApiKey.where(id: params[:id]).first
api_key = ApiKey.find_by(id: params[:id])
raise Discourse::NotFound.new if api_key.blank?
api_key.regenerate!(current_user)
@@ -13,7 +13,7 @@ class Admin::ApiController < Admin::AdminController
end
def revoke_key
api_key = ApiKey.where(id: params[:id]).first
api_key = ApiKey.find_by(id: params[:id])
raise Discourse::NotFound.new if api_key.blank?
api_key.destroy

View File

@@ -27,13 +27,13 @@ class Admin::UsersController < Admin::AdminController
end
def show
@user = User.where(username_lower: params[:id]).first
@user = User.find_by(username_lower: params[:id])
raise Discourse::NotFound.new unless @user
render_serialized(@user, AdminDetailedUserSerializer, root: false)
end
def delete_all_posts
@user = User.where(id: params[:user_id]).first
@user = User.find_by(id: params[:user_id])
@user.delete_all_posts!(guardian)
render nothing: true
end
@@ -157,7 +157,7 @@ class Admin::UsersController < Admin::AdminController
end
def destroy
user = User.where(id: params[:id]).first
user = User.find_by(id: params[:id])
guardian.ensure_can_delete_user!(user)
begin
if UserDestroyer.new(current_user).destroy(user, params.slice(:delete_posts, :block_email, :block_urls, :block_ip, :context))
@@ -180,7 +180,7 @@ class Admin::UsersController < Admin::AdminController
private
def fetch_user
@user = User.where(id: params[:user_id]).first
@user = User.find_by(id: params[:user_id])
end
def refresh_browser(user)

View File

@@ -203,7 +203,7 @@ class ApplicationController < ActionController::Base
username_lower = params[:username].downcase
username_lower.gsub!(/\.json$/, '')
user = User.where(username_lower: username_lower).first
user = User.find_by(username_lower: username_lower)
raise Discourse::NotFound.new if user.blank?
guardian.ensure_can_see!(user)

View File

@@ -102,6 +102,6 @@ class CategoriesController < ApplicationController
end
def fetch_category
@category = Category.where(slug: params[:id]).first || Category.where(id: params[:id].to_i).first
@category = Category.find_by(slug: params[:id]) || Category.find_by(id: params[:id].to_i)
end
end

View File

@@ -25,7 +25,7 @@ class GroupsController < ApplicationController
def find_group(param_name)
name = params.require(param_name)
group = Group.where("lower(name) = ?", name.downcase).first
group = Group.find_by("lower(name) = ?", name.downcase)
guardian.ensure_can_see!(group)
group
end

View File

@@ -6,7 +6,7 @@ class InvitesController < ApplicationController
before_filter :ensure_logged_in, only: [:destroy, :create]
def show
invite = Invite.where(invite_key: params[:id]).first
invite = Invite.find_by(invite_key: params[:id])
if invite.present?
user = invite.redeem
@@ -42,7 +42,7 @@ class InvitesController < ApplicationController
def destroy
params.require(:email)
invite = Invite.where(invited_by_id: current_user.id, email: params[:email]).first
invite = Invite.find_by(invited_by_id: current_user.id, email: params[:email])
raise Discourse::InvalidParameters.new(:email) if invite.blank?
invite.trash!(current_user)

View File

@@ -35,7 +35,7 @@ class PostActionsController < ApplicationController
end
def destroy
post_action = current_user.post_actions.where(post_id: params[:id].to_i, post_action_type_id: @post_action_type_id, deleted_at: nil).first
post_action = current_user.post_actions.find_by(post_id: params[:id].to_i, post_action_type_id: @post_action_type_id, deleted_at: nil)
raise Discourse::NotFound if post_action.blank?

View File

@@ -11,7 +11,7 @@ class PostsController < ApplicationController
skip_before_filter :check_xhr, only: [:markdown,:short_link]
def markdown
post = Post.where(topic_id: params[:topic_id].to_i, post_number: (params[:post_number] || 1).to_i).first
post = Post.find_by(topic_id: params[:topic_id].to_i, post_number: (params[:post_number] || 1).to_i)
if post && guardian.can_see?(post)
render text: post.raw, content_type: 'text/plain'
else
@@ -208,7 +208,7 @@ class PostsController < ApplicationController
revision = params[:revision].to_i
raise Discourse::InvalidParameters.new(:revision) if revision < 2
post_revision = PostRevision.where(post_id: post_id, number: revision).first
post_revision = PostRevision.find_by(post_id: post_id, number: revision)
post_revision.post = find_post_from_params
guardian.ensure_can_see!(post_revision)

View File

@@ -25,9 +25,9 @@ class SearchController < ApplicationController
# A user is found by username
context_obj = nil
if search_context[:type] == 'user'
context_obj = klass.where(username_lower: params[:search_context][:id].downcase).first
context_obj = klass.find_by(username_lower: params[:search_context][:id].downcase)
else
context_obj = klass.where(id: params[:search_context][:id]).first
context_obj = klass.find_by(id: params[:search_context][:id])
end
guardian.ensure_can_see!(context_obj)

View File

@@ -43,7 +43,7 @@ class TopicsController < ApplicationController
begin
@topic_view = TopicView.new(params[:id] || params[:topic_id], current_user, opts)
rescue Discourse::NotFound
topic = Topic.where(slug: params[:id]).first if params[:id]
topic = Topic.find_by(slug: params[:id]) if params[:id]
raise Discourse::NotFound unless topic
return redirect_to(topic.relative_url)
end
@@ -96,7 +96,7 @@ class TopicsController < ApplicationController
end
def update
topic = Topic.where(id: params[:topic_id]).first
topic = Topic.find_by(id: params[:topic_id])
title, archetype = params[:title], params[:archetype]
guardian.ensure_can_edit!(topic)
@@ -134,14 +134,14 @@ class TopicsController < ApplicationController
enabled = (params[:enabled] == 'true')
check_for_status_presence(:status, status)
@topic = Topic.where(id: topic_id).first
@topic = Topic.find_by(id: topic_id)
guardian.ensure_can_moderate!(@topic)
@topic.update_status(status, enabled, current_user)
render nothing: true
end
def star
@topic = Topic.where(id: params[:topic_id].to_i).first
@topic = Topic.find_by(id: params[:topic_id].to_i)
guardian.ensure_can_see!(@topic)
@topic.toggle_star(current_user, params[:starred] == 'true')
@@ -158,7 +158,7 @@ class TopicsController < ApplicationController
def autoclose
raise Discourse::InvalidParameters.new(:auto_close_time) unless params.has_key?(:auto_close_time)
topic = Topic.where(id: params[:topic_id].to_i).first
topic = Topic.find_by(id: params[:topic_id].to_i)
guardian.ensure_can_moderate!(topic)
topic.set_auto_close(params[:auto_close_time], current_user)
if topic.save
@@ -169,7 +169,7 @@ class TopicsController < ApplicationController
end
def destroy
topic = Topic.where(id: params[:id]).first
topic = Topic.find_by(id: params[:id])
guardian.ensure_can_delete!(topic)
topic.trash!(current_user)
render nothing: true
@@ -188,7 +188,7 @@ class TopicsController < ApplicationController
def remove_allowed_user
params.require(:username)
topic = Topic.where(id: params[:topic_id]).first
topic = Topic.find_by(id: params[:topic_id])
guardian.ensure_can_remove_allowed_users!(topic)
if topic.remove_allowed_user(params[:username])
@@ -201,7 +201,7 @@ class TopicsController < ApplicationController
def invite
username_or_email = params[:user] ? fetch_username : fetch_email
topic = Topic.where(id: params[:topic_id]).first
topic = Topic.find_by(id: params[:topic_id])
guardian.ensure_can_invite_to!(topic)
if topic.invite(current_user, username_or_email)
@@ -225,7 +225,7 @@ class TopicsController < ApplicationController
def merge_topic
params.require(:destination_topic_id)
topic = Topic.where(id: params[:topic_id]).first
topic = Topic.find_by(id: params[:topic_id])
guardian.ensure_can_move_posts!(topic)
dest_topic = topic.move_posts(current_user, topic.posts.pluck(:id), destination_topic_id: params[:destination_topic_id].to_i)
@@ -237,7 +237,7 @@ class TopicsController < ApplicationController
params.require(:topic_id)
params.permit(:category_id)
topic = Topic.where(id: params[:topic_id]).first
topic = Topic.find_by(id: params[:topic_id])
guardian.ensure_can_move_posts!(topic)
dest_topic = move_posts_to_destination(topic)
@@ -276,14 +276,14 @@ class TopicsController < ApplicationController
end
def clear_pin
topic = Topic.where(id: params[:topic_id].to_i).first
topic = Topic.find_by(id: params[:topic_id].to_i)
guardian.ensure_can_see!(topic)
topic.clear_pin_for(current_user)
render nothing: true
end
def re_pin
topic = Topic.where(id: params[:topic_id].to_i).first
topic = Topic.find_by(id: params[:topic_id].to_i)
guardian.ensure_can_see!(topic)
topic.re_pin_for(current_user)
render nothing: true
@@ -330,7 +330,7 @@ class TopicsController < ApplicationController
private
def toggle_mute
@topic = Topic.where(id: params[:topic_id].to_i).first
@topic = Topic.find_by(id: params[:topic_id].to_i)
guardian.ensure_can_see!(@topic)
@topic.toggle_mute(current_user)

View File

@@ -23,7 +23,7 @@ class UploadsController < ApplicationController
url = request.fullpath
# the "url" parameter is here to prevent people from scanning the uploads using the id
if upload = Upload.where(id: id, url: url).first
if upload = Upload.find_by(id: id, url: url)
send_file(Discourse.store.path_for(upload), filename: upload.original_filename)
else
render nothing: true, status: 404

View File

@@ -56,9 +56,9 @@ class UserBadgesController < ApplicationController
params.permit(:badge_name)
if params[:badge_name].nil?
params.require(:badge_id)
badge = Badge.where(id: params[:badge_id]).first
badge = Badge.find_by(id: params[:badge_id])
else
badge = Badge.where(name: params[:badge_name]).first
badge = Badge.find_by(name: params[:badge_name])
end
raise Discourse::NotFound.new if badge.blank?

View File

@@ -312,7 +312,7 @@ class UsersController < ApplicationController
# [LEGACY] avatars in quotes/oneboxes might still be pointing to this route
# fixing it requires a rebake of all the posts
def avatar
user = User.where(username_lower: params[:username].downcase).first
user = User.find_by(username_lower: params[:username].downcase)
if user.present?
size = determine_avatar_size(params[:size])
url = user.avatar_template.gsub("{size}", size.to_s)

View File

@@ -2,8 +2,8 @@ module Jobs
class CloseTopic < Jobs::Base
def execute(args)
if topic = Topic.where(id: args[:topic_id]).first
closer = User.where(id: args[:user_id]).first
if topic = Topic.find_by(id: args[:topic_id])
closer = User.find_by(id: args[:user_id])
topic.auto_close(closer)
end
end

View File

@@ -83,7 +83,7 @@ module Jobs
def execute(args)
raise Discourse::InvalidParameters.new(:topic_link_id) unless args[:topic_link_id].present?
topic_link = TopicLink.where(id: args[:topic_link_id], internal: false, crawled_at: nil).first
topic_link = TopicLink.find_by(id: args[:topic_link_id], internal: false, crawled_at: nil)
return if topic_link.blank?
# Look for a topic embed for the URL. If it exists, use its title and don't crawl

View File

@@ -6,7 +6,7 @@ module Jobs
topic_id = args[:topic_id]
raise Discourse::InvalidParameters.new(:topic_id) unless topic_id.present?
topic = Topic.where(id: topic_id).first
topic = Topic.find_by(id: topic_id)
# there are 3 cases here
# 1. topic was atomically nuked, this should be skipped

View File

@@ -11,8 +11,8 @@ module Jobs
raise Discourse::InvalidParameters.new(:upload_id) if upload_id.blank?
raise Discourse::InvalidParameters.new(:user_id) if user_id.blank?
upload = Upload.where(id: upload_id).first
user = User.where(id: user_id).first
upload = Upload.find_by(id: upload_id)
user = User.find_by(id: user_id)
return if upload.nil? || user.nil?
external_copy = Discourse.store.download(upload) if Discourse.store.external?

View File

@@ -8,7 +8,7 @@ module Jobs
def execute(args)
raise Discourse::InvalidParameters.new(:invite_id) unless args[:invite_id].present?
invite = Invite.where(id: args[:invite_id]).first
invite = Invite.find_by(id: args[:invite_id])
message = InviteMailer.send_invite(invite)
Email::Sender.new(message, :invite).send
end

View File

@@ -5,7 +5,7 @@ module Jobs
def execute(args)
post_id = args[:post_id]
if post_id
post = Post.with_deleted.where(id: post_id).first
post = Post.with_deleted.find_by(id: post_id)
return if post && post.trashed?
end

View File

@@ -10,7 +10,7 @@ module Jobs
users_notified = Set.new
posts = Post.where(id: args[:post_ids]).where('user_id <> ?', args[:moved_by_id]).includes(:user, :topic)
if posts.present?
moved_by = User.where(id: args[:moved_by_id]).first
moved_by = User.find_by(id: args[:moved_by_id])
posts.each do |p|
unless users_notified.include?(p.user_id)

View File

@@ -6,7 +6,7 @@ module Jobs
class ProcessPost < Jobs::Base
def execute(args)
post = Post.where(id: args[:post_id]).first
post = Post.find_by(id: args[:post_id])
# two levels of deletion
return unless post.present? && post.topic.present?

View File

@@ -17,7 +17,7 @@ module Jobs
post_id = args[:post_id]
raise Discourse::InvalidParameters.new(:post_id) unless post_id.present?
post = Post.where(id: post_id).first
post = Post.find_by(id: post_id)
return unless post.present?
raw = post.raw.dup

View File

@@ -11,7 +11,7 @@ module Jobs
user = nil
if args[:user_id]
user = User.where(id: args[:user_id]).first
user = User.find_by(id: args[:user_id])
end
TopicRetriever.new(args[:embed_url], no_throttle: user.try(:staff?)).retrieve

View File

@@ -9,7 +9,7 @@ module Jobs
raise Discourse::InvalidParameters.new(:user_id) unless args[:user_id].present?
raise Discourse::InvalidParameters.new(:message_type) unless args[:message_type].present?
user = User.where(id: args[:user_id]).first
user = User.find_by(id: args[:user_id])
return if user.blank?
system_message = SystemMessage.new(user)

View File

@@ -3,7 +3,7 @@ module Jobs
class UpdateTopRedirection < Jobs::Base
def execute(args)
user = User.where(id: args[:user_id]).first
user = User.find_by(id: args[:user_id])
user.update_column(:last_redirected_to_top_at, args[:redirected_at])
end
end

View File

@@ -14,7 +14,7 @@ module Jobs
raise Discourse::InvalidParameters.new(:type) unless args[:type].present?
# Find the user
@user = User.where(id: args[:user_id]).first
@user = User.find_by(id: args[:user_id])
return skip(I18n.t("email_log.no_user", user_id: args[:user_id])) unless @user
return skip(I18n.t("email_log.suspended_not_pm")) if @user.suspended? && args[:type] != :user_private_message
@@ -27,7 +27,7 @@ module Jobs
# Don't email a user about a post when we've seen them recently.
return skip(I18n.t('email_log.seen_recently')) if seen_recently
post = Post.where(id: args[:post_id]).first
post = Post.find_by(id: args[:post_id])
return skip(I18n.t('email_log.post_not_found', post_id: args[:post_id])) unless post.present?
email_args[:post] = post
@@ -36,13 +36,13 @@ module Jobs
email_args[:email_token] = args[:email_token] if args[:email_token].present?
notification = nil
notification = Notification.where(id: args[:notification_id]).first if args[:notification_id].present?
notification = Notification.find_by(id: args[:notification_id]) if args[:notification_id].present?
if notification.present?
# Don't email a user about a post when we've seen them recently.
return skip(I18n.t('email_log.seen_recently')) if seen_recently && !@user.suspended?
# Load the post if present
email_args[:post] ||= Post.where(id: notification.data_hash[:original_post_id].to_i).first
email_args[:post] ||= Post.find_by(id: notification.data_hash[:original_post_id].to_i)
email_args[:post] ||= notification.post
email_args[:notification] = notification

View File

@@ -23,7 +23,7 @@ module Jobs
end
def poll_feed
user = User.where(username_lower: SiteSetting.embed_by_username.downcase).first
user = User.find_by(username_lower: SiteSetting.embed_by_username.downcase)
return if user.blank?
require 'simple-rss'

View File

@@ -12,7 +12,7 @@ class ApiKey < ActiveRecord::Base
end
def self.create_master_key
api_key = ApiKey.where(user_id: nil).first
api_key = ApiKey.find_by(user_id: nil)
if api_key.blank?
api_key = ApiKey.create(key: SecureRandom.hex(32), created_by: Discourse.system_user)
end

View File

@@ -306,7 +306,7 @@ SQL
end
def self.find_by_email(email)
self.where(email_in: Email.downcase(email)).first
self.find_by(email_in: Email.downcase(email))
end
def has_children?

View File

@@ -9,7 +9,7 @@ class ColorScheme < ActiveRecord::Base
after_destroy :destroy_versions
def self.enabled
current_version.where(enabled: true).first || find(1)
current_version.find_by(enabled: true) || find(1)
end
def can_edit?

View File

@@ -41,7 +41,7 @@ class DiscourseSingleSignOn < SingleSignOn
end
def lookup_or_create_user
sso_record = SingleSignOnRecord.where(external_id: external_id).first
sso_record = SingleSignOnRecord.find_by(external_id: external_id)
if sso_record && user = sso_record.user
sso_record.last_payload = unsigned_payload
@@ -75,7 +75,7 @@ class DiscourseSingleSignOn < SingleSignOn
private
def match_email_or_create_user
user = User.where(email: Email.downcase(email)).first
user = User.find_by(email: Email.downcase(email))
user_params = {
email: email,

View File

@@ -3,7 +3,7 @@ class DraftSequence < ActiveRecord::Base
user_id = user
user_id = user.id unless user.class == Fixnum
h = { user_id: user_id, draft_key: key }
c = DraftSequence.where(h).first
c = DraftSequence.find_by(h)
c ||= DraftSequence.new(h)
c.sequence ||= 0
c.sequence += 1

View File

@@ -19,7 +19,7 @@ class EmailLog < ActiveRecord::Base
end
def self.for(reply_key)
EmailLog.where(reply_key: reply_key).first
EmailLog.find_by(reply_key: reply_key)
end
def self.last_sent_email_address

View File

@@ -155,9 +155,9 @@ class Group < ActiveRecord::Base
def self.lookup_group(name)
if id = AUTO_GROUPS[name]
Group.where(id: id).first
Group.find_by(id: id)
else
unless group = Group.where(name: name).first
unless group = Group.find_by(name: name)
raise ArgumentError, "unknown group"
end
group

View File

@@ -13,7 +13,7 @@ class IncomingLink < ActiveRecord::Base
user_id, host, referer = nil
if request['u']
u = User.select(:id).where(username_lower: request['u'].downcase).first
u = User.select(:id).find_by(username_lower: request["u"].downcase)
user_id = u.id if u
end

View File

@@ -24,7 +24,7 @@ class Invite < ActiveRecord::Base
def user_doesnt_already_exist
@email_already_exists = false
return if email.blank?
u = User.where("email = ?", Email.downcase(email)).first
u = User.find_by("email = ?", Email.downcase(email))
if u && u.id != self.user_id
@email_already_exists = true
errors.add(:email)
@@ -106,7 +106,7 @@ class Invite < ActiveRecord::Base
end
def self.invalidate_for_email(email)
i = Invite.where(email: Email.downcase(email)).first
i = Invite.find_by(email: Email.downcase(email))
if i
i.invalidated_at = Time.zone.now
i.save

View File

@@ -57,7 +57,7 @@ InviteRedeemer = Struct.new(:invite) do
end
def get_existing_user
User.where(email: invite.email).first
User.find_by(email: invite.email)
end

View File

@@ -87,7 +87,7 @@ class Notification < ActiveRecord::Base
def post
return if topic_id.blank? || post_number.blank?
Post.where(topic_id: topic_id, post_number: post_number).first
Post.find_by(topic_id: topic_id, post_number: post_number)
end
def self.recent_report(user, count = nil)

View File

@@ -7,7 +7,7 @@ class OptimizedImage < ActiveRecord::Base
return unless width > 0 && height > 0
# do we already have that thumbnail?
thumbnail = where(upload_id: upload.id, width: width, height: height).first
thumbnail = find_by(upload_id: upload.id, width: width, height: height)
# make sure the previous thumbnail has not failed
if thumbnail && thumbnail.url.blank?

View File

@@ -1,14 +1,14 @@
# API to wrap up plugin store rows
class PluginStore
def self.get(plugin_name, key)
if row = PluginStoreRow.where(plugin_name: plugin_name, key: key).first
if row = PluginStoreRow.find_by(plugin_name: plugin_name, key: key)
cast_value(row.type_name, row.value)
end
end
def self.set(plugin_name, key, value)
hash = {plugin_name: plugin_name, key: key}
row = PluginStoreRow.where(hash).first || row = PluginStoreRow.new(hash)
row = PluginStoreRow.find_by(hash) || row = PluginStoreRow.new(hash)
row.type_name = determine_type(value)
# nil are stored as nil

View File

@@ -67,7 +67,7 @@ class Post < ActiveRecord::Base
end
def self.find_by_detail(key, value)
includes(:post_details).where(post_details: { key: key, value: value }).first
includes(:post_details).find_by(post_details: { key: key, value: value })
end
def add_detail(key, value, extra = nil)
@@ -250,17 +250,12 @@ class Post < ActiveRecord::Base
def reply_to_post
return if reply_to_post_number.blank?
@reply_to_post ||= Post.where("topic_id = :topic_id AND post_number = :post_number",
topic_id: topic_id,
post_number: reply_to_post_number).first
@reply_to_post ||= Post.find_by("topic_id = :topic_id AND post_number = :post_number", topic_id: topic_id, post_number: reply_to_post_number)
end
def reply_notification_target
return if reply_to_post_number.blank?
Post.where("topic_id = :topic_id AND post_number = :post_number AND user_id <> :user_id",
topic_id: topic_id,
post_number: reply_to_post_number,
user_id: user_id).first.try(:user)
Post.find_by("topic_id = :topic_id AND post_number = :post_number AND user_id <> :user_id", topic_id: topic_id, post_number: reply_to_post_number, user_id: user_id).try(:user)
end
def self.excerpt(cooked, maxlength = nil, options = {})
@@ -401,7 +396,7 @@ class Post < ActiveRecord::Base
# Create a reply relationship between quoted posts and this new post
self.quoted_post_numbers.each do |p|
post = Post.where(topic_id: topic_id, post_number: p).first
post = Post.find_by(topic_id: topic_id, post_number: p)
create_reply_relationship_with(post)
end
end
@@ -442,7 +437,7 @@ class Post < ActiveRecord::Base
def revert_to(number)
return if number >= version
post_revision = PostRevision.where(post_id: id, number: number + 1).first
post_revision = PostRevision.find_by(post_id: id, number: (number + 1))
post_revision.modifications.each do |attribute, change|
attribute = "version" if attribute == "cached_version"
write_attribute(attribute, change[0])
@@ -495,7 +490,7 @@ class Post < ActiveRecord::Base
end
def update_revision
revision = PostRevision.where(post_id: id, number: version).first
revision = PostRevision.find_by(post_id: id, number: version)
return unless revision
revision.user_id = last_editor_id
modifications = changes.extract!(:raw, :cooked, :edit_reason)

View File

@@ -143,9 +143,7 @@ class PostAction < ActiveRecord::Base
end
def self.remove_act(user, post, post_action_type_id)
if action = where(post_id: post.id,
user_id: user.id,
post_action_type_id: post_action_type_id).first
if action = find_by(post_id: post.id, user_id: user.id, post_action_type_id: post_action_type_id)
action.remove_act!(user)
end
end
@@ -319,7 +317,7 @@ class PostAction < ActiveRecord::Base
end
def self.post_action_type_for_post(post_id)
post_action = PostAction.where(defer: nil, post_id: post_id, post_action_type_id: PostActionType.flag_types.values, deleted_at: nil).first
post_action = PostAction.find_by(defer: nil, post_id: post_id, post_action_type_id: PostActionType.flag_types.values, deleted_at: nil)
PostActionType.types[post_action.post_action_type_id]
end

View File

@@ -47,8 +47,8 @@ class PostRevision < ActiveRecord::Base
return if prev == cur
{
previous_user: User.where(id: prev).first,
current_user: User.where(id: cur).first
previous_user: User.find_by(id: prev),
current_user: User.find_by(id: cur)
}
end

View File

@@ -17,7 +17,7 @@ class ScreenedEmail < ActiveRecord::Base
end
def self.should_block?(email)
screened_email = ScreenedEmail.where(email: email).first
screened_email = ScreenedEmail.find_by(email: email)
screened_email.record_match! if screened_email
screened_email && screened_email.action_type == actions[:block]
end

View File

@@ -65,7 +65,7 @@ class ScreenedIpAddress < ActiveRecord::Base
#
# http://www.postgresql.org/docs/9.1/static/datatype-net-types.html
# http://www.postgresql.org/docs/9.1/static/functions-net.html
where("'#{ip_address.to_s}' <<= ip_address").first
find_by("'#{ip_address.to_s}' <<= ip_address")
end
def self.should_block?(ip_address)

View File

@@ -50,7 +50,7 @@ class SearchObserver < ActiveRecord::Observer
if obj.class == Topic && obj.title_changed?
if obj.posts
post = obj.posts.where(post_number: 1).first
post = obj.posts.find_by(post_number: 1)
if post
category_name = obj.category.name if obj.category
SearchObserver.update_posts_index(post.id, post.cooked, obj.title, category_name)

View File

@@ -99,7 +99,7 @@ class SiteCustomization < ActiveRecord::Base
return preview_style if preview_style
@lock.synchronize do
style = where(enabled: true).first
style = find_by(enabled: true)
if style
@cache[enabled_key] = style.key
else
@@ -141,7 +141,7 @@ class SiteCustomization < ActiveRecord::Base
return style if style
@lock.synchronize do
style = where(key: key).first
style = find_by(key: key)
style.ensure_stylesheets_on_disk! if style
@cache[key] = style
end

View File

@@ -447,7 +447,7 @@ class Topic < ActiveRecord::Base
end
def changed_to_category(cat)
return true if cat.blank? || Category.where(topic_id: id).first.present?
return true if cat.blank? || Category.find_by(topic_id: id).present?
Topic.transaction do
old_category = category
@@ -502,9 +502,9 @@ class Topic < ActiveRecord::Base
def change_category(name)
# If the category name is blank, reset the attribute
if name.blank?
cat = Category.where(id: SiteSetting.uncategorized_category_id).first
cat = Category.find_by(id: SiteSetting.uncategorized_category_id)
else
cat = Category.where(name: name).first
cat = Category.find_by(name: name)
end
return true if cat == category
@@ -514,9 +514,9 @@ class Topic < ActiveRecord::Base
def remove_allowed_user(username)
user = User.where(username: username).first
user = User.find_by(username: username)
if user
topic_user = topic_allowed_users.where(user_id: user.id).first
topic_user = topic_allowed_users.find_by(user_id: user.id)
if topic_user
topic_user.destroy
else
@@ -559,7 +559,7 @@ class Topic < ActiveRecord::Base
end
def grant_permission_to_user(lower_email)
user = User.where(email: lower_email).first
user = User.find_by(email: lower_email)
topic_allowed_users.create!(user_id: user.id)
end

View File

@@ -24,7 +24,7 @@ class TopicEmbed < ActiveRecord::Base
url = normalize_url(url)
embed = TopicEmbed.where("lower(embed_url) = ?", url).first
embed = TopicEmbed.find_by("lower(embed_url) = ?", url)
content_sha1 = Digest::SHA1.hexdigest(contents)
post = nil

View File

@@ -123,7 +123,7 @@ class TopicLink < ActiveRecord::Base
post_number = route[:post_number] || 1
# Store the canonical URL
topic = Topic.where(id: topic_id).first
topic = Topic.find_by(id: topic_id)
if topic.present?
url = "#{Discourse.base_url}#{topic.relative_url}"
@@ -137,7 +137,7 @@ class TopicLink < ActiveRecord::Base
reflected_post = nil
if post_number && topic_id
reflected_post = Post.where(topic_id: topic_id, post_number: post_number.to_i).first
reflected_post = Post.find_by(topic_id: topic_id, post_number: post_number.to_i)
end
added_urls << url
@@ -152,7 +152,7 @@ class TopicLink < ActiveRecord::Base
# Create the reflection if we can
if topic_id.present?
topic = Topic.where(id: topic_id).first
topic = Topic.find_by(id: topic_id)
if topic && post.topic && post.topic.archetype != 'private_message' && topic.archetype != 'private_message'

View File

@@ -33,7 +33,7 @@ class TopicLinkClick < ActiveRecord::Base
# If we have it somewhere else on the site, just allow the redirect. This is
# likely due to a onebox of another topic.
link = TopicLink.where(url: args[:url]).first
link = TopicLink.find_by(url: args[:url])
return link.present? ? link.url : nil
end

View File

@@ -24,7 +24,7 @@ class TopicNotifier
end
def muted?(user_id)
tu = @topic.topic_users.where(user_id: user_id).first
tu = @topic.topic_users.find_by(user_id: user_id)
tu && tu.notification_level == levels[:muted]
end

View File

@@ -99,7 +99,7 @@ class TopicUser < ActiveRecord::Base
if rows == 0
now = DateTime.now
auto_track_after = User.select(:auto_track_topics_after_msecs).where(id: user_id).first.auto_track_topics_after_msecs
auto_track_after = User.select(:auto_track_topics_after_msecs).find_by(id: user_id).auto_track_topics_after_msecs
auto_track_after ||= SiteSetting.auto_track_topics_after
if auto_track_after >= 0 && auto_track_after <= (attrs[:total_msecs_viewed] || 0)

View File

@@ -17,7 +17,7 @@ class Upload < ActiveRecord::Base
validates_with ::Validators::UploadValidator
def thumbnail(width = self.width, height = self.height)
optimized_images.where(width: width, height: height).first
optimized_images.find_by(width: width, height: height)
end
def has_thumbnail?(width, height)
@@ -53,7 +53,7 @@ class Upload < ActiveRecord::Base
# compute the sha
sha1 = Digest::SHA1.file(file).hexdigest
# check if the file has already been uploaded
upload = Upload.where(sha1: sha1).first
upload = Upload.find_by(sha1: sha1)
# delete the previously uploaded file if there's been an error
if upload && upload.url.blank?
upload.destroy
@@ -112,7 +112,7 @@ class Upload < ActiveRecord::Base
def self.get_from_url(url)
# we store relative urls, so we need to remove any host/cdn
url = url.gsub(/^#{Discourse.asset_host}/i, "") if Discourse.asset_host.present?
Upload.where(url: url).first if Discourse.store.has_been_uploaded?(url)
Upload.find_by(url: url) if Discourse.store.has_been_uploaded?(url)
end
end

View File

@@ -138,7 +138,7 @@ class User < ActiveRecord::Base
def self.find_by_temporary_key(key)
user_id = $redis.get("temporary_key:#{key}")
if user_id.present?
where(id: user_id.to_i).first
find_by(id: user_id.to_i)
end
end
@@ -151,11 +151,11 @@ class User < ActiveRecord::Base
end
def self.find_by_email(email)
where(email: Email.downcase(email)).first
find_by(email: Email.downcase(email))
end
def self.find_by_username(username)
where(username_lower: username.downcase).first
find_by(username_lower: username.downcase)
end
@@ -296,7 +296,7 @@ class User < ActiveRecord::Base
end
def visit_record_for(date)
user_visits.where(visited_at: date).first
user_visits.find_by(visited_at: date)
end
def update_visit_record!(date)
@@ -676,7 +676,7 @@ class User < ActiveRecord::Base
def username_validator
username_format_validator || begin
lower = username.downcase
existing = User.where(username_lower: lower).first
existing = User.find_by(username_lower: lower)
if username_changed? && existing && existing.id != self.id
errors.add(:username, I18n.t(:'user.username.unique'))
end

View File

@@ -155,7 +155,9 @@ LEFT JOIN categories c on c.id = t.category_id
# TODO there are conditions when this is called and user_id was already rolled back and is invalid.
# protect against dupes, for some reason this is failing in some cases
action = self.where(hash.select{|k,v| required_parameters.include?(k)}).first
action = self.find_by(hash.select do |k, v|
required_parameters.include?(k)
end)
return action if action
action = self.new(hash)
@@ -168,7 +170,7 @@ LEFT JOIN categories c on c.id = t.category_id
user_id = hash[:user_id]
update_like_count(user_id, hash[:action_type], 1)
topic = Topic.includes(:category).where(id: hash[:target_topic_id]).first
topic = Topic.includes(:category).find_by(id: hash[:target_topic_id])
# move into Topic perhaps
group_ids = nil
@@ -194,7 +196,7 @@ LEFT JOIN categories c on c.id = t.category_id
def self.remove_action!(hash)
require_parameters(hash, :action_type, :user_id, :acting_user_id, :target_topic_id, :target_post_id)
if action = UserAction.where(hash.except(:created_at)).first
if action = UserAction.find_by(hash.except(:created_at))
action.destroy
MessageBus.publish("/user/#{hash[:user_id]}", {user_action_id: action.id, remove: true})
end

View File

@@ -137,7 +137,7 @@ class PostAlerter
# Returns a list of users who were quoted in the post
def extract_quoted_users(post)
post.raw.scan(/\[quote=\"([^,]+),.+\"\]/).uniq.map do |m|
User.where("username_lower = :username and id != :id", username: m.first.strip.downcase, id: post.user_id).first
User.find_by("username_lower = :username and id != :id", username: m.first.strip.downcase, id: post.user_id)
end.compact
end

View File

@@ -53,7 +53,7 @@ class UserDestroyer
categories.each do |c|
c.user_id = Discourse.system_user.id
c.save!
if topic = Topic.with_deleted.where(id: c.topic_id).first
if topic = Topic.with_deleted.find_by(id: c.topic_id)
topic.try(:recover!)
topic.user_id = Discourse.system_user.id
topic.save!