Extract method to get a few random records
In Ruby 5.2, we get a warning when using the "RANDOM()" function: DEPRECATION WARNING: Dangerous query method (method whose arguments are used as raw SQL) called with non-attribute argument(s): "RANDOM()". Non-attribute arguments will be disallowed in Rails 6.0. This method should not be called with user-provided values, such as request parameters or model attributes. Known-safe values can be passed by wrapping them in Arel.sql(). This warning doesn't make much sense, though, since RANDOM() is a common function which is not dangerous at all. However, since the warning is annoying, we'll probably have to find a way to deal with it. So I'm extracting all our RANDOM() usages into a method. This way we'll only have to change one method to avoid this warning. I've chosen `sample` because it's similar to Ruby's Array#sample, and because `order_by_random` would be confusing if we consider we already have a method called `sort_by_random`.
This commit is contained in:
@@ -1,3 +1,11 @@
|
||||
class ApplicationRecord < ActiveRecord::Base
|
||||
self.abstract_class = true
|
||||
|
||||
def self.sample(count = 1)
|
||||
if count == 1
|
||||
reorder("RANDOM()").first
|
||||
else
|
||||
reorder("RANDOM()").limit(count)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -147,7 +147,7 @@ end
|
||||
|
||||
section "Marking investments as visible to valuators" do
|
||||
(1..50).to_a.sample.times do
|
||||
Budget::Investment.reorder("RANDOM()").first.update(visible_to_valuators: true)
|
||||
Budget::Investment.sample.update(visible_to_valuators: true)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ section "Flagging Debates & Comments" do
|
||||
end
|
||||
|
||||
section "Ignoring flags in Debates, comments & proposals" do
|
||||
Debate.flagged.reorder("RANDOM()").limit(10).each(&:ignore_flag)
|
||||
Comment.flagged.reorder("RANDOM()").limit(30).each(&:ignore_flag)
|
||||
Proposal.flagged.reorder("RANDOM()").limit(10).each(&:ignore_flag)
|
||||
Debate.flagged.sample(10).each(&:ignore_flag)
|
||||
Comment.flagged.sample(30).each(&:ignore_flag)
|
||||
Proposal.flagged.sample(10).each(&:ignore_flag)
|
||||
end
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
section "Hiding debates, comments & proposals" do
|
||||
Comment.with_hidden.flagged.reorder("RANDOM()").limit(30).each(&:hide)
|
||||
Debate.with_hidden.flagged.reorder("RANDOM()").limit(5).each(&:hide)
|
||||
Proposal.with_hidden.flagged.reorder("RANDOM()").limit(10).each(&:hide)
|
||||
Comment.with_hidden.flagged.sample(30).each(&:hide)
|
||||
Debate.with_hidden.flagged.sample(5).each(&:hide)
|
||||
Proposal.with_hidden.flagged.sample(10).each(&:hide)
|
||||
end
|
||||
|
||||
section "Confirming hiding in debates, comments & proposals" do
|
||||
Comment.only_hidden.flagged.reorder("RANDOM()").limit(10).each(&:confirm_hide)
|
||||
Debate.only_hidden.flagged.reorder("RANDOM()").limit(5).each(&:confirm_hide)
|
||||
Proposal.only_hidden.flagged.reorder("RANDOM()").limit(5).each(&:confirm_hide)
|
||||
Comment.only_hidden.flagged.sample(10).each(&:confirm_hide)
|
||||
Debate.only_hidden.flagged.sample(5).each(&:confirm_hide)
|
||||
Proposal.only_hidden.flagged.sample(5).each(&:confirm_hide)
|
||||
end
|
||||
|
||||
@@ -4,10 +4,10 @@ section "Creating comment notifications" do
|
||||
title: Faker::Lorem.sentence(3).truncate(60),
|
||||
description: "<p>#{Faker::Lorem.paragraphs.join("</p><p>")}</p>",
|
||||
tag_list: Tag.all.sample(3).join(","),
|
||||
geozone: Geozone.reorder("RANDOM()").first,
|
||||
geozone: Geozone.sample,
|
||||
terms_of_service: "1")
|
||||
|
||||
comment = Comment.create!(user: User.reorder("RANDOM()").first,
|
||||
comment = Comment.create!(user: User.sample,
|
||||
body: Faker::Lorem.sentence,
|
||||
commentable: debate)
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ section "Creating polls" do
|
||||
starts_at: 5.days.ago,
|
||||
ends_at: 5.days.from_now,
|
||||
geozone_restricted: true,
|
||||
geozones: Geozone.reorder("RANDOM()").limit(3))
|
||||
geozones: Geozone.sample(3))
|
||||
|
||||
Poll.create!(name: I18n.t("seeds.polls.recounting_poll"),
|
||||
slug: I18n.t("seeds.polls.recounting_poll").parameterize,
|
||||
|
||||
Reference in New Issue
Block a user