Skip to content

Commit 92fac1a

Browse files
Only require needed params (#259)
Standardise params across create/update
1 parent e4b02bf commit 92fac1a

File tree

2 files changed

+41
-54
lines changed

2 files changed

+41
-54
lines changed

lib/discourse_api/api/categories.rb

Lines changed: 40 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,65 +6,15 @@ module Categories
66
# :permissions is a hash with the group name and permission_type which is
77
# an integer 1 = Full 2 = Create Post 3 = Read Only
88
def create_category(args = {})
9-
args =
10-
API
11-
.params(args)
12-
.required(:name, :color, :text_color)
13-
.optional(
14-
:slug,
15-
:permissions,
16-
:auto_close_hours,
17-
:auto_close_based_on_last_post,
18-
:position,
19-
:email_in,
20-
:email_in_allow_strangers,
21-
:logo_url,
22-
:background_url,
23-
:allow_badges,
24-
:topic_template,
25-
:custom_fields,
26-
:description,
27-
:reviewable_by_group_name,
28-
:show_subcategory_list,
29-
:subcategory_list_style,
30-
:allowed_tags,
31-
:allowed_tag_groups,
32-
:required_tag_group_name,
33-
)
34-
.default(parent_category_id: nil)
35-
response = post("/categories", args)
9+
params = common_category_params(args)
10+
response = post("/categories", params.to_h)
3611
response["category"]
3712
end
3813

3914
def update_category(args = {})
4015
category_id = args[:id]
41-
args =
42-
API
43-
.params(args)
44-
.required(:id, :name, :color, :text_color)
45-
.optional(
46-
:slug,
47-
:permissions,
48-
:auto_close_hours,
49-
:auto_close_based_on_last_post,
50-
:position,
51-
:email_in,
52-
:email_in_allow_strangers,
53-
:logo_url,
54-
:background_url,
55-
:allow_badges,
56-
:topic_template,
57-
:custom_fields,
58-
:description,
59-
:reviewable_by_group_name,
60-
:show_subcategory_list,
61-
:subcategory_list_style,
62-
:allowed_tags,
63-
:allowed_tag_groups,
64-
:required_tag_group_name,
65-
)
66-
.default(parent_category_id: nil)
67-
response = put("/categories/#{category_id}", args)
16+
params = common_category_params(args, include_id: true)
17+
response = put("/categories/#{category_id}", params.to_h)
6818
response["body"]["category"] if response["body"]
6919
end
7020

@@ -144,6 +94,42 @@ def category_set_user_notification_level(category_id, params)
14494
params = API.params(params).required(:notification_level)
14595
post("/category/#{category_id}/notifications", params)
14696
end
97+
98+
private
99+
100+
def common_category_params(args, include_id: false)
101+
params = API.params(args)
102+
params = params.required(:id) if include_id
103+
params
104+
.required(:name)
105+
.optional(
106+
:color,
107+
:text_color,
108+
:slug,
109+
:permissions,
110+
:auto_close_hours,
111+
:auto_close_based_on_last_post,
112+
:position,
113+
:email_in,
114+
:email_in_allow_strangers,
115+
:logo_url,
116+
:background_url,
117+
:allow_badges,
118+
:topic_template,
119+
:custom_fields,
120+
:description,
121+
:reviewable_by_group_name,
122+
:show_subcategory_list,
123+
:subcategory_list_style,
124+
:allowed_tags,
125+
:allowed_tag_groups,
126+
:required_tag_group_name,
127+
:topic_featured_links_allowed,
128+
:search_priority,
129+
:form_template_ids,
130+
)
131+
.default(parent_category_id: nil)
132+
end
147133
end
148134
end
149135
end

spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
require "discourse_api"
1010
require "rspec"
1111
require "webmock/rspec"
12+
require "ostruct"
1213

1314
RSpec.configure do |config|
1415
config.expect_with :rspec do |c|

0 commit comments

Comments
 (0)