Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only require needed params #259

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 39 additions & 54 deletions lib/discourse_api/api/categories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -144,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
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require "discourse_api"
require "rspec"
require "webmock/rspec"
require "ostruct"

RSpec.configure do |config|
config.expect_with :rspec do |c|
Expand Down
Loading