diff --git a/lib/discourse_api/api/categories.rb b/lib/discourse_api/api/categories.rb index 0437f15..03bc6e1 100644 --- a/lib/discourse_api/api/categories.rb +++ b/lib/discourse_api/api/categories.rb @@ -6,65 +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, :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 = 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 @@ -144,6 +94,42 @@ 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 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|