Skip to content

Commit 96d7e6b

Browse files
committed
FIX: AI search discoveries user preference not saving
The "Enable AI search discoveries" checkbox in user preferences wasn't persisting changes. The UI showed "saved" but the value never reached the database. The Interface controller's `saveAttrNames` is a computed property that returns a new array on each access. While the plugin called `addSaveableUserOptionField()`, this only registers the field with the User model. The controller filters which fields to save via `saveAttrNames`, and since `ai_search_discoveries` wasn't in that list, it was excluded from the save request. Fix by using `modifyClass` to extend the controller's `save()` action, pushing `ai_search_discoveries` to `saveAttrNames` at the moment of saving before calling the parent method. Ref - meta/t/391135
1 parent c0e9e4b commit 96d7e6b

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-search-discoveries.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import { action } from "@ember/object";
12
import { apiInitializer } from "discourse/lib/api";
23

4+
const FIELD_NAME = "ai_search_discoveries";
5+
36
export default apiInitializer((api) => {
47
const currentUser = api.getCurrentUser();
58
const settings = api.container.lookup("service:site-settings");
@@ -11,7 +14,19 @@ export default apiInitializer((api) => {
1114
return;
1215
}
1316

14-
api.addSaveableUserOptionField("ai_search_discoveries");
17+
api.addSaveableUserOptionField(FIELD_NAME);
18+
19+
api.modifyClass(
20+
"controller:preferences/interface",
21+
(Superclass) =>
22+
class extends Superclass {
23+
@action
24+
save() {
25+
this.saveAttrNames.push(FIELD_NAME);
26+
return super.save(...arguments);
27+
}
28+
}
29+
);
1530

1631
const discobotDiscoveries = api.container.lookup(
1732
"service:discobot-discoveries"

plugins/discourse-ai/spec/system/ai_user_preferences_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@
2525
expect(user_preferences_ai_page).to have_ai_preference("pref-ai-search-discoveries")
2626
end
2727

28+
it "saves the setting when toggled" do
29+
user.user_option.update!(ai_search_discoveries: true)
30+
user_preferences_ai_page.visit(user)
31+
32+
expect(user_preferences_ai_page).to have_ai_preference_checked("pref-ai-search-discoveries")
33+
34+
user_preferences_ai_page.toggle_setting("pref-ai-search-discoveries")
35+
user_preferences_ai_page.save_changes
36+
37+
expect(page).to have_content(I18n.t("js.saved"))
38+
expect(user.user_option.reload.ai_search_discoveries).to eq(false)
39+
end
40+
2841
context "when the user can't use personas" do
2942
it "doesn't render the option in the preferences page" do
3043
Group.find_by(id: Group::AUTO_GROUPS[:admins]).remove(user)

0 commit comments

Comments
 (0)