mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
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:
@@ -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(
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user