A platform for community discussion. Free, open, simple.
FIX: Consistently handle category param
Blake Erickson373b8a213996
diff --git a/lib/seed_data/topics.rb b/lib/seed_data/topics.rbindex 4f54c5716bd..f019ec1be63 100644--- a/lib/seed_data/topics.rb+++ b/lib/seed_data/topics.rb@@ -132,7 +132,7 @@ module SeedDatatitle: title,raw: raw,skip_validations: true,- category: category&.name+ category: category&.id)if static_first_replydiff --git a/lib/topic_creator.rb b/lib/topic_creator.rbindex e46f2f6e3ac..5726d744442 100644--- a/lib/topic_creator.rb+++ b/lib/topic_creator.rb@@ -153,13 +153,8 @@ class TopicCreatorreturn 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))endendenddiff --git a/spec/components/topic_creator_spec.rb b/spec/components/topic_creator_spec.rbindex cda1cb67192..347719adf8d 100644--- a/spec/components/topic_creator_spec.rb+++ b/spec/components/topic_creator_spec.rb@@ -53,9 +53,9 @@ describe TopicCreator doexpect(topic.public_topic_timer).to eq(nil)end- it "category name is case insensitive" do+ it "can create a topic in a category" docategory = 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_validexpect(topic.category).to eq(category)end@@ -103,18 +103,18 @@ describe TopicCreator doit "fails for regular user if minimum_required_tags is not satisfied" doexpect 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)endit "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_validexpect(topic.tags.length).to eq(1)endit "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_validexpect(topic.tags.length).to eq(2)end@@ -122,7 +122,7 @@ describe TopicCreator doit "lets new user create a topic if they don't have sufficient trust level to tag topics" doSiteSetting.min_trust_level_to_tag_topics = 1new_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_validendenddiff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rbindex 3d60cc347ff..39bce54d188 100644--- a/spec/models/category_spec.rb+++ b/spec/models/category_spec.rb@@ -580,7 +580,7 @@ describe Category docontext 'for uncategorized category' dobefore 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.reloadend