Refactor update_all statements in order to prevent deprecation warnings in Rails 4

This commit is contained in:
Stephan Kaag 2013-07-01 20:45:52 +02:00
parent 5770879472
commit 89b621d31a
18 changed files with 45 additions and 50 deletions

View file

@ -8,7 +8,7 @@ class EmailLog < ActiveRecord::Base
after_create do
# Update last_emailed_at if the user_id is present
User.update_all("last_emailed_at = CURRENT_TIMESTAMP", id: user_id) if user_id.present?
User.where(id: user_id).update_all("last_emailed_at = CURRENT_TIMESTAMP") if user_id.present?
end
def self.count_per_day(sinceDaysAgo = 30)

View file

@ -11,7 +11,7 @@ class EmailToken < ActiveRecord::Base
after_create do
# Expire the previous tokens
EmailToken.update_all 'expired = true', ['user_id = ? and id != ?', self.user_id, self.id]
EmailToken.where(['user_id = ? and id != ?', self.user_id, self.id]).update_all 'expired = true'
end
def self.token_length
@ -43,7 +43,7 @@ class EmailToken < ActiveRecord::Base
user = email_token.user
User.transaction do
row_count = EmailToken.update_all 'confirmed = true', id: email_token.id, expired: false
row_count = EmailToken.where(id: email_token.id, expired: false).update_all 'confirmed = true'
if row_count == 1
# If we are activating the user, send the welcome message
user.send_welcome_message = !user.active?

View file

@ -27,9 +27,8 @@ InviteRedeemer = Struct.new(:invite) do
end
def mark_invite_redeemed
Invite.update_all('redeemed_at = CURRENT_TIMESTAMP',
['id = ? AND redeemed_at IS NULL AND created_at >= ?',
invite.id, SiteSetting.invite_expiry_days.days.ago])
Invite.where(['id = ? AND redeemed_at IS NULL AND created_at >= ?',
invite.id, SiteSetting.invite_expiry_days.days.ago]).update_all('redeemed_at = CURRENT_TIMESTAMP')
end
def get_invited_user
@ -62,7 +61,7 @@ InviteRedeemer = Struct.new(:invite) do
end
def send_welcome_message
if Invite.update_all(['user_id = ?', invited_user.id], ['email = ?', invite.email]) == 1
if Invite.where(['email = ?', invite.email]).update_all(['user_id = ?', invited_user.id]) == 1
invited_user.send_welcome_message = true
end
end

View file

@ -32,7 +32,7 @@ class Notification < ActiveRecord::Base
end
def self.mark_posts_read(user, topic_id, post_numbers)
Notification.update_all "read = 't'", user_id: user.id, topic_id: topic_id, post_number: post_numbers, read: false
Notification.where(user_id: user.id, topic_id: topic_id, post_number: post_numbers, read: false).update_all "read = 't'"
end
def self.interesting_after(min_date)

View file

@ -366,7 +366,7 @@ class Post < ActiveRecord::Base
return if post.nil?
post_reply = post.post_replies.new(reply_id: id)
if post_reply.save
Post.update_all ['reply_count = reply_count + 1'], id: post.id
Post.where(id: post.id).update_all ['reply_count = reply_count + 1']
end
end
end

View file

@ -63,9 +63,9 @@ class PostAction < ActiveRecord::Base
moderator_id == -1 ? PostActionType.auto_action_flag_types.values : PostActionType.flag_types.values
end
PostAction.update_all({ deleted_at: Time.zone.now, deleted_by: moderator_id }, { post_id: post.id, post_action_type_id: actions })
PostAction.where({ post_id: post.id, post_action_type_id: actions }).update_all({ deleted_at: Time.zone.now, deleted_by: moderator_id })
f = actions.map{|t| ["#{PostActionType.types[t]}_count", 0]}
Post.with_deleted.update_all(Hash[*f.flatten], id: post.id)
Post.where(id: post.id).with_deleted.update_all(Hash[*f.flatten])
update_flagged_posts_count
end
@ -224,19 +224,19 @@ class PostAction < ActiveRecord::Base
case post_action_type
when :vote
# Voting also changes the sort_order
Post.update_all ["vote_count = vote_count + :delta, sort_order = :max - (vote_count + :delta)",
Post.where(id: post_id).update_all ["vote_count = vote_count + :delta, sort_order = :max - (vote_count + :delta)",
delta: delta,
max: Topic.max_sort_order], id: post_id
max: Topic.max_sort_order]
when :like
# `like_score` is weighted higher for staff accounts
Post.update_all ["like_count = like_count + :delta, like_score = like_score + :score_delta",
Post.where(id: post_id).update_all ["like_count = like_count + :delta, like_score = like_score + :score_delta",
delta: delta,
score_delta: user.staff? ? delta * SiteSetting.staff_like_weight : delta], id: post_id
score_delta: user.staff? ? delta * SiteSetting.staff_like_weight : delta]
else
Post.update_all ["#{column} = #{column} + ?", delta], id: post_id
Post.where(id: post_id).update_all ["#{column} = #{column} + ?", delta]
end
Topic.update_all ["#{column} = #{column} + ?", delta], id: post.topic_id
Topic.where(id: post.topic_id).update_all ["#{column} = #{column} + ?", delta]
if PostActionType.notify_flag_type_ids.include?(post_action_type_id)
@ -271,10 +271,9 @@ class PostAction < ActiveRecord::Base
reason = guess_hide_reason(old_flags)
end
Post.update_all(["hidden = true, hidden_reason_id = COALESCE(hidden_reason_id, ?)", reason], id: post.id)
Topic.update_all({ visible: false },
["id = :topic_id AND NOT EXISTS(SELECT 1 FROM POSTS WHERE topic_id = :topic_id AND NOT hidden)",
topic_id: post.topic_id])
Post.where(id: post.id).update_all(["hidden = true, hidden_reason_id = COALESCE(hidden_reason_id, ?)", reason])
Topic.where(["id = :topic_id AND NOT EXISTS(SELECT 1 FROM POSTS WHERE topic_id = :topic_id AND NOT hidden)",
topic_id: post.topic_id]).update_all({ visible: false })
# inform user
if post.user

View file

@ -59,7 +59,7 @@ class PostMover
def move(post, post_number)
@first_post_number_moved ||= post.post_number
Post.update_all(
Post.where(id: post.id, topic_id: original_topic.id).update_all(
[
['post_number = :post_number',
'topic_id = :topic_id',
@ -67,9 +67,7 @@ class PostMover
].join(', '),
post_number: post_number,
topic_id: destination_topic.id
],
id: post.id,
topic_id: original_topic.id
]
)
end

View file

@ -16,7 +16,7 @@ class PostTiming < ActiveRecord::Base
args)
if rows == 0
Post.update_all 'reads = reads + 1', ['topic_id = :topic_id and post_number = :post_number', args]
Post.where(['topic_id = :topic_id and post_number = :post_number', args]).update_all 'reads = reads + 1'
exec_sql("INSERT INTO post_timings (topic_id, user_id, post_number, msecs)
SELECT :topic_id, :user_id, :post_number, :msecs
WHERE NOT EXISTS(SELECT 1 FROM post_timings

View file

@ -311,14 +311,14 @@ class Topic < ActiveRecord::Base
old_category = category
if category_id.present? && category_id != cat.id
Category.update_all 'topic_count = topic_count - 1', ['id = ?', category_id]
Category.where(['id = ?', category_id]).update_all 'topic_count = topic_count - 1'
end
self.category_id = cat.id
save
CategoryFeaturedTopic.feature_topics_for(old_category)
Category.update_all 'topic_count = topic_count + 1', id: cat.id
Category.where(id: cat.id).update_all 'topic_count = topic_count + 1'
CategoryFeaturedTopic.feature_topics_for(cat) unless old_category.try(:id) == cat.try(:id)
end
end
@ -354,7 +354,7 @@ class Topic < ActiveRecord::Base
if name.blank?
if category_id.present?
CategoryFeaturedTopic.feature_topics_for(category)
Category.update_all 'topic_count = topic_count - 1', id: category_id
Category.where(id: category_id).update_all 'topic_count = topic_count - 1'
end
self.category_id = nil
save

View file

@ -87,7 +87,7 @@ class TopicUser < ActiveRecord::Base
attrs_sql = attrs_array.map { |t| "#{t[0]} = ?" }.join(", ")
vals = attrs_array.map { |t| t[1] }
rows = TopicUser.update_all([attrs_sql, *vals], topic_id: topic_id, user_id: user_id)
rows = TopicUser.where(topic_id: topic_id, user_id: user_id).update_all([attrs_sql, *vals])
if rows == 0
now = DateTime.now
@ -109,7 +109,7 @@ class TopicUser < ActiveRecord::Base
def track_visit!(topic,user)
now = DateTime.now
rows = TopicUser.update_all({last_visited_at: now}, {topic_id: topic.id, user_id: user.id})
rows = TopicUser.where({topic_id: topic.id, user_id: user.id}).update_all({last_visited_at: now})
if rows == 0
TopicUser.create(topic_id: topic.id, user_id: user.id, last_visited_at: now, first_visited_at: now)
else

View file

@ -230,8 +230,7 @@ class User < ActiveRecord::Base
end
def saw_notification_id(notification_id)
User.update_all ["seen_notification_id = ?", notification_id],
["seen_notification_id < ?", notification_id]
User.where(["seen_notification_id < ?", notification_id]).update_all ["seen_notification_id = ?", notification_id]
end
def publish_notifications_state
@ -461,7 +460,7 @@ class User < ActiveRecord::Base
if last_seen.present?
diff = (Time.now.to_f - last_seen.to_f).round
if diff > 0 && diff < MAX_TIME_READ_DIFF
User.update_all ["time_read = time_read + ?", diff], id: id, time_read: time_read
User.where(id: id, time_read: time_read).update_all ["time_read = time_read + ?", diff]
end
end
$redis.set(last_seen_key, Time.now.to_f)
@ -531,10 +530,10 @@ class User < ActiveRecord::Base
where_conditions = {notifications_reason_id: nil, user_id: id}
if auto_track_topics_after_msecs < 0
TopicUser.update_all({notification_level: TopicUser.notification_levels[:regular]}, where_conditions)
TopicUser.where(where_conditions).update_all({notification_level: TopicUser.notification_levels[:regular]})
else
TopicUser.update_all(["notification_level = CASE WHEN total_msecs_viewed < ? THEN ? ELSE ? END",
auto_track_topics_after_msecs, TopicUser.notification_levels[:regular], TopicUser.notification_levels[:tracking]], where_conditions)
TopicUser.where(where_conditions).update_all(["notification_level = CASE WHEN total_msecs_viewed < ? THEN ? ELSE ? END",
auto_track_topics_after_msecs, TopicUser.notification_levels[:regular], TopicUser.notification_levels[:tracking]])
end
end

View file

@ -209,9 +209,9 @@ ORDER BY p.created_at desc
def self.update_like_count(user_id, action_type, delta)
if action_type == LIKE
User.update_all("likes_given = likes_given + #{delta.to_i}", id: user_id)
User.where(id: user_id).update_all("likes_given = likes_given + #{delta.to_i}")
elsif action_type == WAS_LIKED
User.update_all("likes_received = likes_received + #{delta.to_i}", id: user_id)
User.where(id: user_id).update_all("likes_received = likes_received + #{delta.to_i}")
end
end

View file

@ -24,7 +24,7 @@ class View < ActiveRecord::Base
# Update the views count in the parent, if it exists.
if parent.respond_to?(:views)
parent.class.update_all 'views = views + 1', id: parent.id
parent.class.where(id: parent.id).update_all 'views = views + 1'
end
end
end

View file

@ -52,9 +52,9 @@ class SpamRulesEnforcer
def punish_user
Post.transaction do
Post.update_all(["hidden = true, hidden_reason_id = COALESCE(hidden_reason_id, ?)", Post.hidden_reasons[:new_user_spam_threshold_reached]], user_id: @user.id)
Post.where(user_id: @user.id).update_all(["hidden = true, hidden_reason_id = COALESCE(hidden_reason_id, ?)", Post.hidden_reasons[:new_user_spam_threshold_reached]])
topic_ids = Post.where('user_id = ? and post_number = ?', @user.id, 1).pluck(:topic_id)
Topic.update_all({ visible: false }, id: topic_ids) unless topic_ids.empty?
Topic.where(id: topic_ids).update_all({ visible: false }) unless topic_ids.empty?
SystemMessage.create(@user, :too_many_spam_flags)
GroupMessage.create(Group[:moderators].name, :user_automatically_blocked, {user: @user, limit_once_per: false})
@user.blocked = true

View file

@ -1,9 +1,9 @@
class ChangeSupressToSuppress < ActiveRecord::Migration
def up
SiteSetting.update_all({name: "supress_reply_directly_below"}, name: "suppress_reply_directly_below")
SiteSetting.where(name: "suppress_reply_directly_below").update_all({name: "supress_reply_directly_below"})
end
def down
SiteSetting.update_all({name: "suppress_reply_directly_below"}, name: "supress_reply_directly_below")
SiteSetting.where(name: "supress_reply_directly_below").update_all({name: "suppress_reply_directly_below"})
end
end

View file

@ -30,7 +30,7 @@ class PostDestroyer
# If the poster doesn't have any other posts in the topic, clear their posted flag
unless Post.exists?(["topic_id = ? and user_id = ? and id <> ?", @post.topic_id, @post.user_id, @post.id])
TopicUser.update_all 'posted = false', topic_id: @post.topic_id, user_id: @post.user_id
TopicUser.where(topic_id: @post.topic_id, user_id: @post.user_id).update_all 'posted = false'
end
end
@ -40,7 +40,7 @@ class PostDestroyer
@post.post_actions.map(&:trash!)
f = PostActionType.types.map{|k,v| ["#{k}_count", 0]}
Post.with_deleted.update_all(Hash[*f.flatten], id: @post.id)
Post.with_deleted.where(id: @post.id).update_all(Hash[*f.flatten])
@post.trash!

View file

@ -28,13 +28,13 @@ class SiteSettings::DbProvider
return unless table_exists?
count = @model.update_all({
count = @model.where({
name: name
}).update_all({
name: name,
value: value,
data_type: data_type,
updated_at: Time.now
}, {
name: name
})
if count == 0

View file

@ -39,7 +39,7 @@ module Trashable
#
# Fixed in Rails 4
#
self.class.unscoped.update_all({deleted_at: nil}, id: self.id)
self.class.unscoped.where(id: self.id).update_all({deleted_at: nil})
raw_write_attribute :deleted_at, nil
end