From 8e9a91238f767a3a83985703ffcad167daf85335 Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Thu, 28 Dec 2023 02:21:02 +0530 Subject: [PATCH 1/3] FIX: use safe navigation operator for params Bug report: https://meta.discourse.org/t/-/289850 Solution ref: https://stackoverflow.com/questions/34794697 --- lib/discourse_topic_voting/categories_controller_extension.rb | 2 +- 1 file changed, 1 insertion(+), 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 From d66eee7f6c00b94cc348e9e235c0976bbc9d50bf Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Thu, 28 Dec 2023 12:11:44 +0530 Subject: [PATCH 2/3] added spec --- spec/requests/categories_controller_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spec/requests/categories_controller_spec.rb b/spec/requests/categories_controller_spec.rb index f00e387..d2b6774 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 From b101dd564c8a13f59696fb522c0224cbee22fb9f Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Thu, 28 Dec 2023 12:23:45 +0530 Subject: [PATCH 3/3] formatting fix --- spec/requests/categories_controller_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/requests/categories_controller_spec.rb b/spec/requests/categories_controller_spec.rb index d2b6774..d8ad13f 100644 --- a/spec/requests/categories_controller_spec.rb +++ b/spec/requests/categories_controller_spec.rb @@ -46,7 +46,7 @@ end it "works fine when `custom_fields` isn't passed " do - put "/categories/#{category.id}.json", params: {hello: "world"} + put "/categories/#{category.id}.json", params: { hello: "world" } expect(response.status).to eq(200) end end