From 829424cbbb7a480c319c4d70b912180b5f59542b Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Thu, 28 Dec 2023 12:39:35 +0530 Subject: [PATCH] FIX: use safe navigation operator for params (#177) * FIX: use safe navigation operator for params Bug report: https://meta.discourse.org/t/-/289850 Solution ref: https://stackoverflow.com/questions/34794697 * added spec * formatting fix --- .../categories_controller_extension.rb | 2 +- spec/requests/categories_controller_spec.rb | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/discourse_topic_voting/categories_controller_extension.rb b/lib/discourse_topic_voting/categories_controller_extension.rb index 34e1bb1..f208dbf 100644 --- a/lib/discourse_topic_voting/categories_controller_extension.rb +++ b/lib/discourse_topic_voting/categories_controller_extension.rb @@ -4,7 +4,7 @@ module DiscourseTopicVoting module CategoriesControllerExtension def category_params @vote_enabled ||= - !!ActiveRecord::Type::Boolean.new.cast(params[:custom_fields][:enable_topic_voting]) + !!ActiveRecord::Type::Boolean.new.cast(params&.[](:custom_fields)&.[](:enable_topic_voting)) category_params = super diff --git a/spec/requests/categories_controller_spec.rb b/spec/requests/categories_controller_spec.rb index f00e387..d8ad13f 100644 --- a/spec/requests/categories_controller_spec.rb +++ b/spec/requests/categories_controller_spec.rb @@ -44,4 +44,9 @@ } expect(Category.can_vote?(category.id)).to eq(false) end + + it "works fine when `custom_fields` isn't passed " do + put "/categories/#{category.id}.json", params: { hello: "world" } + expect(response.status).to eq(200) + end end