A platform for community discussion. Free, open, simple.
FIX: do not use return in block (#26260)
Joffrey JAFFEUXa884842fa552
diff --git a/app/models/concerns/has_custom_fields.rb b/app/models/concerns/has_custom_fields.rbindex 02ab064cfa9..eb0e4a0c7ab 100644--- a/app/models/concerns/has_custom_fields.rb+++ b/app/models/concerns/has_custom_fields.rb@@ -270,7 +270,7 @@ module HasCustomFields# set of fields.def upsert_custom_fields(fields)fields.each do |k, v|- row_count = _custom_fields.where(name: k).update_all(value: v)+ row_count = _custom_fields.where(name: k).update_all(value: v, updated_at: Time.now)_custom_fields.create!(name: k, value: v) if row_count == 0custom_fields[k.to_s] = v # We normalize custom_fields as stringsdiff --git a/plugins/chat/app/services/chat/list_channel_messages.rb b/plugins/chat/app/services/chat/list_channel_messages.rbindex 36daf51e67c..62b3770ccad 100644--- a/plugins/chat/app/services/chat/list_channel_messages.rb+++ b/plugins/chat/app/services/chat/list_channel_messages.rb@@ -174,7 +174,7 @@ module Chatdef update_user_last_channel(guardian:, channel:)Scheduler::Defer.later "Chat::ListChannelMessages - defer update_user_last_channel" do- return if guardian.user.custom_fields[::Chat::LAST_CHAT_CHANNEL_ID] == channel.id+ next if guardian.user.custom_fields[::Chat::LAST_CHAT_CHANNEL_ID] == channel.idguardian.user.upsert_custom_fields(::Chat::LAST_CHAT_CHANNEL_ID => channel.id)endenddiff --git a/plugins/chat/spec/services/chat/list_channel_messages_spec.rb b/plugins/chat/spec/services/chat/list_channel_messages_spec.rbindex 02fe46f8489..ce210e7c3aa 100644--- a/plugins/chat/spec/services/chat/list_channel_messages_spec.rb+++ b/plugins/chat/spec/services/chat/list_channel_messages_spec.rb@@ -218,5 +218,12 @@ RSpec.describe Chat::ListChannelMessages dochannel.id,)end++ it "doesn’t update the custom field when it was already set to this value" do+ user.upsert_custom_fields(::Chat::LAST_CHAT_CHANNEL_ID => channel.id)+ field = UserCustomField.find_by(name: Chat::LAST_CHAT_CHANNEL_ID, user_id: user.id)++ expect { result }.to_not change { field.reload.updated_at }+ endendenddiff --git a/spec/lib/concern/has_custom_fields_spec.rb b/spec/lib/concern/has_custom_fields_spec.rbindex 26ebe91a337..d054b9fa113 100644--- a/spec/lib/concern/has_custom_fields_spec.rb+++ b/spec/lib/concern/has_custom_fields_spec.rb@@ -459,6 +459,17 @@ RSpec.describe HasCustomFields doexpect(test_item.custom_fields["abc"]).to eq("ghi")end+ it "updates the updated_at timestamp" do+ test_item = CustomFieldsTestItem.create+ test_item.upsert_custom_fields(foo: "bar")+ field = CustomFieldsTestItemCustomField.find_by(name: "foo")+ prev_updated_at = field.updated_at++ test_item.upsert_custom_fields(foo: "baz")++ expect(prev_updated_at).not_to eq(field.reload.updated_at)+ end+it "allows upsert to use keywords" dotest_item = CustomFieldsTestItem.createtest_item.upsert_custom_fields(hello: "world", abc: "def")