Skip to content

Commit

Permalink
FIX: use safe navigation operator for params (#177)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Faizaan Gagan committed Dec 28, 2023
1 parent be71ec4 commit 829424c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions spec/requests/categories_controller_spec.rb
Expand Up @@ -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

0 comments on commit 829424c

Please sign in to comment.