FIX: Consistently handle category param

FIX: Consistently handle category param

See API POST to /posts.json inconsistent between users - bug - Discourse Meta for more info.

This commit removes a 5 year old temporary fix that is no longer needed.

Refactors TopicCreator · discourse/discourse@bc1824a · GitHub

The web interface uses category_id when creating a topic so I think we should unify on category_id when using the api.

diff --git a/lib/seed_data/topics.rb b/lib/seed_data/topics.rb
index 4f54c57..f019ec1 100644
--- a/lib/seed_data/topics.rb
+++ b/lib/seed_data/topics.rb
@@ -132,7 +132,7 @@ module SeedData
         title: title,
         raw: raw,
         skip_validations: true,
-        category: category&.name
+        category: category&.id
       )
 
       if static_first_reply
diff --git a/lib/topic_creator.rb b/lib/topic_creator.rb
index e46f2f6..5726d74 100644
--- a/lib/topic_creator.rb
+++ b/lib/topic_creator.rb
@@ -153,13 +153,8 @@ class TopicCreator
         return Category.find(SiteSetting.shared_drafts_category)
       end
 
-      # Temporary fix to allow older clients to create topics.
-      # When all clients are updated the category variable should
-      # be set directly to the contents of the if statement.
       if (@opts[:category].is_a? Integer) || (@opts[:category] =~ /^\d+$/)
         Category.find_by(id: @opts[:category])
-      else
-        Category.find_by(name_lower: @opts[:category].try(:downcase))
       end
     end
   end
diff --git a/spec/components/topic_creator_spec.rb b/spec/components/topic_creator_spec.rb
index cda1cb6..347719a 100644
--- a/spec/components/topic_creator_spec.rb
+++ b/spec/components/topic_creator_spec.rb
@@ -53,9 +53,9 @@ describe TopicCreator do
           expect(topic.public_topic_timer).to eq(nil)
         end
 
-        it "category name is case insensitive" do
+        it "can create a topic in a category" do
           category = Fabricate(:category, name: "Neil's Blog")
-          topic = TopicCreator.create(user, Guardian.new(user), valid_attrs.merge(category: "neil's blog"))
+          topic = TopicCreator.create(user, Guardian.new(user), valid_attrs.merge(category: category.id))
           expect(topic).to be_valid
           expect(topic.category).to eq(category)
         end
@@ -103,18 +103,18 @@ describe TopicCreator do
 
         it "fails for regular user if minimum_required_tags is not satisfied" do
           expect do
-            TopicCreator.create(user, Guardian.new(user), valid_attrs.merge(category: "beta"))
+            TopicCreator.create(user, Guardian.new(user), valid_attrs.merge(category: category.id))
           end.to raise_error(ActiveRecord::Rollback)
         end
 
         it "lets admin create a topic regardless of minimum_required_tags" do
-          topic = TopicCreator.create(admin, Guardian.new(admin), valid_attrs.merge(tags: [tag1.name], category: "beta"))
+          topic = TopicCreator.create(admin, Guardian.new(admin), valid_attrs.merge(tags: [tag1.name], category: category.id))
           expect(topic).to be_valid
           expect(topic.tags.length).to eq(1)
         end
 
         it "works for regular user if minimum_required_tags is satisfied" do
-          topic = TopicCreator.create(user, Guardian.new(user), valid_attrs.merge(tags: [tag1.name, tag2.name], category: "beta"))
+          topic = TopicCreator.create(user, Guardian.new(user), valid_attrs.merge(tags: [tag1.name, tag2.name], category: category.id))
           expect(topic).to be_valid
           expect(topic.tags.length).to eq(2)
         end
@@ -122,7 +122,7 @@ describe TopicCreator do
         it "lets new user create a topic if they don't have sufficient trust level to tag topics" do
           SiteSetting.min_trust_level_to_tag_topics = 1
           new_user = Fabricate(:newuser)
-          topic = TopicCreator.create(new_user, Guardian.new(new_user), valid_attrs.merge(category: "beta"))
+          topic = TopicCreator.create(new_user, Guardian.new(new_user), valid_attrs.merge(category: category.id))
           expect(topic).to be_valid
         end
       end
diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb
index 3d60cc3..39bce54 100644
--- a/spec/models/category_spec.rb
+++ b/spec/models/category_spec.rb
@@ -580,7 +580,7 @@ describe Category do
     context 'for uncategorized category' do
       before do
         @uncategorized = Category.find(SiteSetting.uncategorized_category_id)
-        create_post(user: Fabricate(:user), category: @uncategorized.name)
+        create_post(user: Fabricate(:user), category: @uncategorized.id)
         Category.update_stats
         @uncategorized.reload
       end

GitHub sha: 373b8a21

2 Likes