From 4ed2b53805a42963e2642a2802252f262e6e2068 Mon Sep 17 00:00:00 2001 From: Joran Kikke Date: Tue, 9 Apr 2024 09:14:41 +1200 Subject: [PATCH 1/3] Only require needed params --- lib/discourse_api/api/categories.rb | 4 +++- spec/spec_helper.rb | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/discourse_api/api/categories.rb b/lib/discourse_api/api/categories.rb index 0437f15..c9f9b69 100644 --- a/lib/discourse_api/api/categories.rb +++ b/lib/discourse_api/api/categories.rb @@ -9,8 +9,10 @@ def create_category(args = {}) args = API .params(args) - .required(:name, :color, :text_color) + .required(:name) .optional( + :color, + :text_color, :slug, :permissions, :auto_close_hours, diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 945b626..c4f2aee 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -9,6 +9,7 @@ require "discourse_api" require "rspec" require "webmock/rspec" +require "ostruct" RSpec.configure do |config| config.expect_with :rspec do |c| From 2330125245db8c54da250e15edef2e94983880c7 Mon Sep 17 00:00:00 2001 From: Joran Kikke Date: Tue, 9 Apr 2024 09:21:54 +1200 Subject: [PATCH 2/3] Standardise params across create/update --- lib/discourse_api/api/categories.rb | 95 ++++++++++++----------------- 1 file changed, 39 insertions(+), 56 deletions(-) diff --git a/lib/discourse_api/api/categories.rb b/lib/discourse_api/api/categories.rb index c9f9b69..0ebfd56 100644 --- a/lib/discourse_api/api/categories.rb +++ b/lib/discourse_api/api/categories.rb @@ -6,67 +6,15 @@ module Categories # :permissions is a hash with the group name and permission_type which is # an integer 1 = Full 2 = Create Post 3 = Read Only def create_category(args = {}) - args = - API - .params(args) - .required(:name) - .optional( - :color, - :text_color, - :slug, - :permissions, - :auto_close_hours, - :auto_close_based_on_last_post, - :position, - :email_in, - :email_in_allow_strangers, - :logo_url, - :background_url, - :allow_badges, - :topic_template, - :custom_fields, - :description, - :reviewable_by_group_name, - :show_subcategory_list, - :subcategory_list_style, - :allowed_tags, - :allowed_tag_groups, - :required_tag_group_name, - ) - .default(parent_category_id: nil) - response = post("/categories", args) + params = common_category_params(args) + response = post("/categories", params.to_h) response["category"] end def update_category(args = {}) category_id = args[:id] - args = - API - .params(args) - .required(:id, :name, :color, :text_color) - .optional( - :slug, - :permissions, - :auto_close_hours, - :auto_close_based_on_last_post, - :position, - :email_in, - :email_in_allow_strangers, - :logo_url, - :background_url, - :allow_badges, - :topic_template, - :custom_fields, - :description, - :reviewable_by_group_name, - :show_subcategory_list, - :subcategory_list_style, - :allowed_tags, - :allowed_tag_groups, - :required_tag_group_name, - ) - .default(parent_category_id: nil) - response = put("/categories/#{category_id}", args) + params = common_category_params(args, include_id: true) + response = put("/categories/#{category_id}", params.to_h) response["body"]["category"] if response["body"] end @@ -146,6 +94,41 @@ def category_set_user_notification_level(category_id, params) params = API.params(params).required(:notification_level) post("/category/#{category_id}/notifications", params) end + + private + + def common_category_params(args, include_id: false) + params = API.params(args) + params = params.required(:id) if include_id + params.required(:name) + .optional( + :color, + :text_color, + :slug, + :permissions, + :auto_close_hours, + :auto_close_based_on_last_post, + :position, + :email_in, + :email_in_allow_strangers, + :logo_url, + :background_url, + :allow_badges, + :topic_template, + :custom_fields, + :description, + :reviewable_by_group_name, + :show_subcategory_list, + :subcategory_list_style, + :allowed_tags, + :allowed_tag_groups, + :required_tag_group_name, + :topic_featured_links_allowed, + :search_priority, + :form_template_ids + ) + .default(parent_category_id: nil) + end end end end From cd72f0017163adc69652829b8587db580315ca57 Mon Sep 17 00:00:00 2001 From: Blake Erickson Date: Wed, 8 May 2024 12:52:44 -0600 Subject: [PATCH 3/3] linting --- lib/discourse_api/api/categories.rb | 57 +++++++++++++++-------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/lib/discourse_api/api/categories.rb b/lib/discourse_api/api/categories.rb index 0ebfd56..03bc6e1 100644 --- a/lib/discourse_api/api/categories.rb +++ b/lib/discourse_api/api/categories.rb @@ -100,34 +100,35 @@ def category_set_user_notification_level(category_id, params) def common_category_params(args, include_id: false) params = API.params(args) params = params.required(:id) if include_id - params.required(:name) - .optional( - :color, - :text_color, - :slug, - :permissions, - :auto_close_hours, - :auto_close_based_on_last_post, - :position, - :email_in, - :email_in_allow_strangers, - :logo_url, - :background_url, - :allow_badges, - :topic_template, - :custom_fields, - :description, - :reviewable_by_group_name, - :show_subcategory_list, - :subcategory_list_style, - :allowed_tags, - :allowed_tag_groups, - :required_tag_group_name, - :topic_featured_links_allowed, - :search_priority, - :form_template_ids - ) - .default(parent_category_id: nil) + params + .required(:name) + .optional( + :color, + :text_color, + :slug, + :permissions, + :auto_close_hours, + :auto_close_based_on_last_post, + :position, + :email_in, + :email_in_allow_strangers, + :logo_url, + :background_url, + :allow_badges, + :topic_template, + :custom_fields, + :description, + :reviewable_by_group_name, + :show_subcategory_list, + :subcategory_list_style, + :allowed_tags, + :allowed_tag_groups, + :required_tag_group_name, + :topic_featured_links_allowed, + :search_priority, + :form_template_ids, + ) + .default(parent_category_id: nil) end end end