Skip to content

Commit 10be694

Browse files
authored
DEV: Use preferences-save-attributes transformer for custom_fields (#164)
discourse/discourse#36658 introduced a `preferences-save-attributes` value transformer, changing `saveAttrNames` from a mutable property to a getter. This broke the previous approach of pushing to the array directly in the save action. Update to use the new transformer API with backward compatibility for older Discourse versions that don't have this transformer.
1 parent 0d6c4f0 commit 10be694

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

assets/javascripts/discourse/initializers/follow-initializer.js

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { action } from "@ember/object";
22
import { withPluginApi } from "discourse/lib/plugin-api";
3+
import { VALUE_TRANSFORMERS } from "discourse/lib/transformer/registry";
34
import { userPath } from "discourse/lib/url";
45
import { i18n } from "discourse-i18n";
56

@@ -53,21 +54,33 @@ export default {
5354
);
5455
}
5556

56-
// workaround to make core save custom fields when changing
57-
// preferences
58-
api.modifyClass(
59-
"controller:preferences/notifications",
60-
(Superclass) =>
61-
class extends Superclass {
62-
@action
63-
save() {
64-
if (!this.saveAttrNames.includes("custom_fields")) {
65-
this.saveAttrNames.push("custom_fields");
66-
}
67-
super.save();
57+
// Add custom_fields to the notifications page save attributes
58+
if (VALUE_TRANSFORMERS.includes("preferences-save-attributes")) {
59+
api.registerValueTransformer(
60+
"preferences-save-attributes",
61+
({ value: attrs, context }) => {
62+
if (context.page === "notifications") {
63+
attrs.push("custom_fields");
6864
}
65+
return attrs;
6966
}
70-
);
67+
);
68+
} else {
69+
// Backward compatibility for older Discourse versions
70+
api.modifyClass(
71+
"controller:preferences/notifications",
72+
(Superclass) =>
73+
class extends Superclass {
74+
@action
75+
save() {
76+
if (!this.saveAttrNames.includes("custom_fields")) {
77+
this.saveAttrNames.push("custom_fields");
78+
}
79+
super.save();
80+
}
81+
}
82+
);
83+
}
7184
});
7285
},
7386
};

0 commit comments

Comments
 (0)