Skip to content

Commit

Permalink
initial commmit
Browse files Browse the repository at this point in the history
  • Loading branch information
pfaffman committed Nov 26, 2024
1 parent 7bd7d0a commit 5cb36a2
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 2 deletions.
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# Default User Custom Field Value

**Theme Summary**
**Make a User Custom Field have a default value (proof of concept) **

For more information, please see: **url to meta topic**
This sets user_custom_field_1 to "I am not important" if it is null.

To be useful, settings would determine which field was given a default value and what the value would be. To be really useful, it would do it for an arbitray number of custom user fields.

Also, it would be better to move the code like

```
// if [userfields[1] is null make it a default strring
if (userFields[1] == null) {
userFields[1] = "I am not important";
}
console.log("got user fields!", userFields);
```

into a function so that it could be called in both places.

It might be better if it called `super()` and/or just updated `this.site.get("user_fields")` rather than overriding anything.

The other solution would be a plugin that set defaults on custom fields when a user was created and/or had a job that created some default value if none existed. An advantage of this solution is that changing the default value is easy since it doesn't actually exist, where a plugin would need to update all user custom field if there was a change.
Empty file added assets/.gitkeep
Empty file.
Empty file added common/common.scss
Empty file.
Empty file added desktop/desktop.scss
Empty file.
Empty file added javascripts/.gitkeep
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { apiInitializer } from "discourse/lib/api";

Check failure on line 1 in javascripts/discourse/api-initializers/default-user-custom-field-value.js

View workflow job for this annotation

GitHub Actions / ci / linting

Run autofix to sort these imports!
import discourseComputed, { on } from "discourse-common/utils/decorators";

Check failure on line 2 in javascripts/discourse/api-initializers/default-user-custom-field-value.js

View workflow job for this annotation

GitHub Actions / ci / linting

'on' is defined but never used
import { set } from "@ember/object"; // <-- Import set function here
import { dasherize } from "@ember/string";
import EmberObject from "@ember/object"; // Import EmberObject here

Check failure on line 5 in javascripts/discourse/api-initializers/default-user-custom-field-value.js

View workflow job for this annotation

GitHub Actions / ci / linting

'@ember/object' import is duplicated
import UserCardContents from "discourse/components/user-card-contents";

Check failure on line 6 in javascripts/discourse/api-initializers/default-user-custom-field-value.js

View workflow job for this annotation

GitHub Actions / ci / linting

'UserCardContents' is defined but never used
import { isEmpty } from "@ember/utils";

export default apiInitializer("1.8.0", ( api ) => {
console.log("hello world from api initializer!");

Check failure on line 10 in javascripts/discourse/api-initializers/default-user-custom-field-value.js

View workflow job for this annotation

GitHub Actions / ci / linting

Unexpected console statement


api.modifyClass("component:user-card-contents", {
pluginId: "discourse-default-user-custom-fields",
@discourseComputed("[email protected]")
publicUserFields() {
// Custom logic here
console.log("hello world from publicUserFields in my theme component!");

Check failure on line 18 in javascripts/discourse/api-initializers/default-user-custom-field-value.js

View workflow job for this annotation

GitHub Actions / ci / linting

Unexpected console statement
const siteUserFields = this.site.get("user_fields");
if (!isEmpty(siteUserFields)) {
console.log("got site user fields!", siteUserFields);

Check failure on line 21 in javascripts/discourse/api-initializers/default-user-custom-field-value.js

View workflow job for this annotation

GitHub Actions / ci / linting

Unexpected console statement
const userFields = this.get("user.user_fields");
// if [userfields[1] is null make it a default strring
if (userFields[1] == null) {
userFields[1] = "I am not important";
}
console.log("got user fields!", userFields);

Check failure on line 27 in javascripts/discourse/api-initializers/default-user-custom-field-value.js

View workflow job for this annotation

GitHub Actions / ci / linting

Unexpected console statement
return siteUserFields
.filterBy("show_on_user_card", true)
.sortBy("position")
.map((field) => {
set(field, "dasherized_name", dasherize(field.get("name")));
const value = userFields ? userFields[field.get("id")] : null;
return isEmpty(value) ? null : EmberObject.create({ value, field });
})
.compact();
}
}
});

Check failure on line 39 in javascripts/discourse/api-initializers/default-user-custom-field-value.js

View workflow job for this annotation

GitHub Actions / ci / linting

Trailing spaces not allowed

// Modify User controller
api.modifyClass("controller:user", {
pluginId: "discourse-default-user-custom-fields",

@discourseComputed("[email protected]")
publicUserFields() {
console.log("Overriding publicUserFields in User controller...");

Check failure on line 47 in javascripts/discourse/api-initializers/default-user-custom-field-value.js

View workflow job for this annotation

GitHub Actions / ci / linting

Unexpected console statement
const siteUserFields = this.site.get("user_fields");

if (!isEmpty(siteUserFields)) {
const userFields = this.get("model.user_fields");
if (userFields[1] == null) {
userFields[1] = "I am not important";
}
console.log("got user fields!", userFields);
return siteUserFields
.filterBy("show_on_profile", true)
.sortBy("position")
.map((field) => {
set(field, "dasherized_name", dasherize(field.get("name")));
const value = userFields
? userFields[field.get("id").toString()]
: null;
return isEmpty(value) ? null : EmberObject.create({ value, field });
})
.compact();
}
},
});
});
Empty file added mobile/mobile.scss
Empty file.
Empty file added scss/.gitkeep
Empty file.
Empty file added spec/system/.gitkeep
Empty file.
Empty file added test/acceptance/.gitkeep
Empty file.

0 comments on commit 5cb36a2

Please sign in to comment.