FIX: correct return of endorsable categories (#38)

FIX: correct return of endorsable categories (#38)

category_accepting_endorsements setting has string values like “true” or “false”

Comparing <> “” was not enough to ensure that category_accepting_endorsements is checked

In spec I just added new category with “false” to ensure it is not added to endorsable categories.

diff --git a/app/controllers/category_experts_controller.rb b/app/controllers/category_experts_controller.rb
index 72fa3bc..5b42903 100644
--- a/app/controllers/category_experts_controller.rb
+++ b/app/controllers/category_experts_controller.rb
@@ -33,11 +33,11 @@ class CategoryExpertsController < ApplicationController
     categories = Category.where(<<~SQL)
       id IN (
         SELECT cf.category_id
-        FROM category_custom_fields cf, category_custom_fields ocf
+        FROM category_custom_fields cf
+        INNER JOIN category_custom_fields ocf ON ocf.category_id = cf.category_id
+        AND ocf.name = '#{CategoryExperts::CATEGORY_ACCEPTING_ENDORSEMENTS}' AND ocf.value = 'true'
         WHERE cf.name = '#{CategoryExperts::CATEGORY_EXPERT_GROUP_IDS}' AND
-              cf.value <> '' AND
-              ocf.name = '#{CategoryExperts::CATEGORY_ACCEPTING_ENDORSEMENTS}' AND
-              ocf.value <> ''
+              cf.value <> ''
       )
     SQL
 
diff --git a/spec/requests/category_experts_controller_spec.rb b/spec/requests/category_experts_controller_spec.rb
index 6432c4c..da76d9e 100644
--- a/spec/requests/category_experts_controller_spec.rb
+++ b/spec/requests/category_experts_controller_spec.rb
@@ -21,8 +21,8 @@ describe CategoryExpertsController do
   end
 
   def enable_custom_fields_for(category)
-    category.custom_fields[CategoryExperts::CATEGORY_ACCEPTING_QUESTIONS] = true
-    category.custom_fields[CategoryExperts::CATEGORY_ACCEPTING_ENDORSEMENTS] = true
+    category.custom_fields[CategoryExperts::CATEGORY_ACCEPTING_QUESTIONS] = 'true'
+    category.custom_fields[CategoryExperts::CATEGORY_ACCEPTING_ENDORSEMENTS] = 'true'
   end
 
   def set_expert_group_for_category(category, group_ids)
@@ -111,6 +111,7 @@ describe CategoryExpertsController do
     context "logged in" do
       fab!(:private_category) { fabricate_category_with_category_experts }
       fab!(:private_group) { Fabricate(:group) }
+      fab!(:category3) { fabricate_category_with_category_experts }
 
       before do
         sign_in(user)
@@ -125,6 +126,9 @@ describe CategoryExpertsController do
         private_category.set_permissions({ private_group.id => :full })
         private_category.save
 
+        category3.custom_fields[CategoryExperts::CATEGORY_ACCEPTING_ENDORSEMENTS] = 'false'
+        category3.save
+
         # Endorsee and current user cannot see the new category
         get("/category-experts/endorsable-categories/#{endorsee.username}.json")
         expect_categories_in_response(response, [category1, category2])

GitHub sha: 6eabb228

This commit appears in #38 which was approved by eviltrout and markvanlan. It was merged by lis2.