# What's the best approach to access category specific settings?

**URL:** https://meta.discourse.org/t/whats-the-best-approach-to-access-category-specific-settings/40728
**Category:** Development
**Created:** [2016 年 3 月 8 日午前 8:51 UTC](https://meta.discourse.org/t/whats-the-best-approach-to-access-category-specific-settings/40728 "2016-03-08T08:51:13Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![fantasticfears](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fantasticfears/32/119608_2.png) [@fantasticfears](https://meta.discourse.org/u/fantasticfears)
#### Post date: [2016 年 3 月 8 日午前 8:51 UTC](https://meta.discourse.org/t/whats-the-best-approach-to-access-category-specific-settings/40728/1 "2016-03-08T08:51:13Z")

</div>

I am developing a paid plugin (don’t know whether it’s ok to public) where I need to set a category specific setting. Therefore, I compare several possible ways and proposal my suggestion.

With existing convenient custom\_fields, it’s intuitive to put category-specific settings in the `CategoryCustomField`. [discourse-solved](https://github.com/discourse/discourse-solved) takes this approach. Solved plugin uses the setting to permit action in server, while I am also interested in knowing the category settings in the client when user opens the category chooser or composer.

## Settings in Category’s `custom_fields`

Simply adding a [template](https://github.com/discourse/discourse-solved/blob/master/assets/javascripts/discourse/connectors/category-custom-settings/solved-settings.hbs) and binding it to associate custom fields:  
[https://github.com/discourse/discourse-solved/blob/master/assets/javascripts/discourse/pre-initializers/extend-category-for-solved.js.es6](https://github.com/discourse/discourse-solved/blob/master/assets/javascripts/discourse/pre-initializers/extend-category-for-solved.js.es6)

Server will store them in CategoryCustomField. It’s [easy to pluck all of them (See Guardian)](https://github.com/discourse/discourse-solved/blob/master/plugin.rb#L173)

Category has 3 serializers. 2 of them:

### `BasicCategorySerializer`

Discourse preloads categories by `BasicCategorySerializer` with initial HTML. But custom\_fields is not included. `custom_fields` may be huge.

### `CategorySerializer`

Right now, discourse serializes `custom_fields` for category by `CategorySerializer`. In client site, we may ask `Category.reloadById` to get more data about category including `custom_fields`. For the record, it’s invoked by `editCategory(category)` in [routes/application.js](https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/routes/application.js.es6#L126) when you click Edit category button.

While you have to try to post a lot of GET requests to get custom fields and it’s asynchronous. The delay and many requests approach failed when you want to change the UI according to user’s interaction. However, if you store anything fairly long, it may not be a bad place.

## Site serializer

When the settings is small (hopefully, most cases), push them to site serializer by: (e.g.)

```
add_to_serializer(:site, :can_create_tag) { scope.can_create_tag? }

```

In client side:

```
Discourse.Site.currentProp('can_tag_topics')

```

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2016 年 3 月 8 日午前 10:34 UTC](https://meta.discourse.org/t/whats-the-best-approach-to-access-category-specific-settings/40728/3 "2016-03-08T10:34:44Z")

</div>

What settings do you have on each category that you need posted down? Is this simply an extra boolean. Why not add something to site serializer that lists the category ids that meet the condition … or something like that?

---

<div class="post-metadata">

### Author: ![fantasticfears](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fantasticfears/32/119608_2.png) [@fantasticfears](https://meta.discourse.org/u/fantasticfears)
#### Post date: [2016 年 3 月 8 日午前 10:45 UTC](https://meta.discourse.org/t/whats-the-best-approach-to-access-category-specific-settings/40728/4 "2016-03-08T10:45:55Z")

</div>

Just want to make sure 🙂

Already updated accordingly in the first post. Hope that helps others.
