DEV: Replace #pluck_first freedom patch with AR #pick in core (#19893)

The #pluck_first freedom patch, first introduced by @danielwaterworth has served us well, and is used widely throughout both core and plugins. It seems to have been a common enough use case that Rails 6 introduced it's own method #pick with the exact same implementation. This allows us to retire the freedom patch and switch over to the built-in ActiveRecord method.

There is no replacement for #pluck_first!, but a quick search shows we are using this in a very limited capacity, and in some cases incorrectly (by assuming a nil return rather than an exception), which can quite easily be replaced with #pick plus some extra handling.
This commit is contained in:
Ted Johansson
2023-02-13 12:39:45 +08:00
committed by GitHub
parent a90ad52dff
commit 25a226279a
56 changed files with 112 additions and 123 deletions

View File

@@ -49,13 +49,13 @@ RSpec.describe PostAction do
expect(topic_user_ids).to include(codinghorror.id)
expect(topic_user_ids).to include(mod.id)
expect(topic.topic_users.where(user_id: mod.id).pluck_first(:notification_level)).to eq(
expect(topic.topic_users.where(user_id: mod.id).pick(:notification_level)).to eq(
TopicUser.notification_levels[:tracking],
)
expect(
topic.topic_users.where(user_id: codinghorror.id).pluck_first(:notification_level),
).to eq(TopicUser.notification_levels[:watching])
expect(topic.topic_users.where(user_id: codinghorror.id).pick(:notification_level)).to eq(
TopicUser.notification_levels[:watching],
)
# reply to PM should not clear flag
PostCreator.new(

View File

@@ -62,11 +62,11 @@ RSpec.describe TopTopic do
TopTopic.refresh!
top_topics = TopTopic.all
expect(top_topics.where(topic_id: topic_1.id).pluck_first(:yearly_score)).to eq(27)
expect(top_topics.where(topic_id: topic_2.id).pluck_first(:yearly_score)).to be_within(
expect(top_topics.where(topic_id: topic_1.id).pick(:yearly_score)).to eq(27)
expect(top_topics.where(topic_id: topic_2.id).pick(:yearly_score)).to be_within(
0.0000000001,
).of(18.301029995664)
expect(top_topics.where(topic_id: topic_3.id).pluck_first(:yearly_score)).to be_within(
expect(top_topics.where(topic_id: topic_3.id).pick(:yearly_score)).to be_within(
0.0000000001,
).of(10.602059991328)
@@ -84,11 +84,11 @@ RSpec.describe TopTopic do
TopTopic.refresh!
top_topics = TopTopic.all
expect(top_topics.where(topic_id: topic_1.id).pluck_first(:yearly_score)).to eq(27)
expect(top_topics.where(topic_id: topic_2.id).pluck_first(:yearly_score)).to be_within(
expect(top_topics.where(topic_id: topic_1.id).pick(:yearly_score)).to eq(27)
expect(top_topics.where(topic_id: topic_2.id).pick(:yearly_score)).to be_within(
0.0000000001,
).of(18.301029995664)
expect(top_topics.where(topic_id: topic_3.id).pluck_first(:yearly_score)).to be_within(
expect(top_topics.where(topic_id: topic_3.id).pick(:yearly_score)).to be_within(
0.0000000001,
).of(11.2041199826559)
@@ -106,11 +106,11 @@ RSpec.describe TopTopic do
TopTopic.refresh!
top_topics = TopTopic.all
expect(top_topics.where(topic_id: topic_1.id).pluck_first(:yearly_score)).to eq(69)
expect(top_topics.where(topic_id: topic_2.id).pluck_first(:yearly_score)).to be_within(
expect(top_topics.where(topic_id: topic_1.id).pick(:yearly_score)).to eq(69)
expect(top_topics.where(topic_id: topic_2.id).pick(:yearly_score)).to be_within(
0.0000000001,
).of(33.301029995664)
expect(top_topics.where(topic_id: topic_3.id).pluck_first(:yearly_score)).to be_within(
expect(top_topics.where(topic_id: topic_3.id).pick(:yearly_score)).to be_within(
0.0000000001,
).of(10.602059991328)
@@ -128,11 +128,11 @@ RSpec.describe TopTopic do
TopTopic.refresh!
top_topics = TopTopic.all
expect(top_topics.where(topic_id: topic_1.id).pluck_first(:yearly_score)).to eq(30)
expect(top_topics.where(topic_id: topic_2.id).pluck_first(:yearly_score)).to be_within(
expect(top_topics.where(topic_id: topic_1.id).pick(:yearly_score)).to eq(30)
expect(top_topics.where(topic_id: topic_2.id).pick(:yearly_score)).to be_within(
0.0000000001,
).of(21.301029995664)
expect(top_topics.where(topic_id: topic_3.id).pluck_first(:yearly_score)).to be_within(
expect(top_topics.where(topic_id: topic_3.id).pick(:yearly_score)).to be_within(
0.0000000001,
).of(10.602059991328)
@@ -144,11 +144,11 @@ RSpec.describe TopTopic do
TopTopic.refresh!
top_topics = TopTopic.all
expect(top_topics.where(topic_id: topic_1.id).pluck_first(:yearly_score)).to eq(27)
expect(top_topics.where(topic_id: topic_2.id).pluck_first(:yearly_score)).to be_within(
expect(top_topics.where(topic_id: topic_1.id).pick(:yearly_score)).to eq(27)
expect(top_topics.where(topic_id: topic_2.id).pick(:yearly_score)).to be_within(
0.0000000001,
).of(18.301029995664)
expect(top_topics.where(topic_id: topic_3.id).pluck_first(:yearly_score)).to be_within(
expect(top_topics.where(topic_id: topic_3.id).pick(:yearly_score)).to be_within(
0.0000000001,
).of(10.602059991328)
end

View File

@@ -53,8 +53,8 @@ RSpec.describe TopicLink do
TopicLink.extract_from(post)
# we have a special rule for images title where we pull them out of the filename
expect(topic.topic_links.where(url: png).pluck_first(:title)).to eq(png_title)
expect(topic.topic_links.where(url: non_png).pluck_first(:title)).to eq("amazing")
expect(topic.topic_links.where(url: png).pick(:title)).to eq(png_title)
expect(topic.topic_links.where(url: non_png).pick(:title)).to eq("amazing")
expect(topic.topic_links.pluck(:url)).to contain_exactly(
png,