From f8129c4e43fde0519a013777af91d4b42420c5e0 Mon Sep 17 00:00:00 2001 From: Viktor Palmkvist Date: Thu, 18 Jul 2013 00:58:25 +0200 Subject: [PATCH] Make the composer and TopicCreator use category id instead of category name Also fixes #1171 --- .../javascripts/discourse/models/composer.js | 17 +++++------------ .../discourse/templates/composer.js.handlebars | 2 +- lib/topic_creator.rb | 2 +- spec/components/post_creator_spec.rb | 4 ++-- spec/models/category_featured_topic_spec.rb | 4 ++-- 5 files changed, 11 insertions(+), 18 deletions(-) diff --git a/app/assets/javascripts/discourse/models/composer.js b/app/assets/javascripts/discourse/models/composer.js index dbe43748d..d504ac1f6 100644 --- a/app/assets/javascripts/discourse/models/composer.js +++ b/app/assets/javascripts/discourse/models/composer.js @@ -328,7 +328,7 @@ Discourse.Composer = Discourse.Model.extend({ } this.setProperties({ - categoryName: opts.categoryName || this.get('topic.category.name'), + categoryId: opts.categoryId || this.get('topic.category.id'), archetypeId: opts.archetypeId || Discourse.Site.currentProp('default_archetype'), metaData: opts.metaData ? Em.Object.create(opts.metaData) : null, reply: opts.reply || this.get("reply") || "" @@ -394,16 +394,9 @@ Discourse.Composer = Discourse.Model.extend({ var topic = this.get('topic'); topic.setProperties({ title: this.get('title'), - fancy_title: this.get('title') + fancy_title: this.get('title'), + category_id: parseInt(this.get('categoryId'), 10) }); - - var category = Discourse.Category.list().findProperty('name', this.get('categoryName')); - if (category) { - topic.setProperties({ - categoryName: category.get('name'), - category_id: category.get('id') - }); - } topic.save(); } @@ -442,7 +435,7 @@ Discourse.Composer = Discourse.Model.extend({ var createdPost = Discourse.Post.create({ raw: this.get('reply'), title: this.get('title'), - category: this.get('categoryName'), + category: this.get('categoryId'), topic_id: this.get('topic.id'), reply_to_post_number: post ? post.get('post_number') : null, imageSizes: opts.imageSizes, @@ -529,7 +522,7 @@ Discourse.Composer = Discourse.Model.extend({ reply: this.get('reply'), action: this.get('action'), title: this.get('title'), - categoryName: this.get('categoryName'), + categoryId: this.get('categoryId'), postId: this.get('post.id'), archetypeId: this.get('archetypeId'), metaData: this.get('metaData'), diff --git a/app/assets/javascripts/discourse/templates/composer.js.handlebars b/app/assets/javascripts/discourse/templates/composer.js.handlebars index 3064aeebf..4cd705ff4 100644 --- a/app/assets/javascripts/discourse/templates/composer.js.handlebars +++ b/app/assets/javascripts/discourse/templates/composer.js.handlebars @@ -46,7 +46,7 @@ {{#unless model.creatingPrivateMessage}}
- {{categoryChooser valueAttribute="name" value=model.categoryName}} + {{categoryChooser valueAttribute="id" value=model.categoryId}} {{popupInputTip validation=view.categoryValidation shownAt=view.showCategoryTip}}
{{#if model.archetype.hasOptions}} diff --git a/lib/topic_creator.rb b/lib/topic_creator.rb index f11e4a341..2b932cd95 100644 --- a/lib/topic_creator.rb +++ b/lib/topic_creator.rb @@ -39,7 +39,7 @@ class TopicCreator topic_params[:archetype] = @opts[:archetype] if @opts[:archetype].present? topic_params[:subtype] = @opts[:subtype] if @opts[:subtype].present? - category = Category.where(name: @opts[:category]).first + category = Category.where(id: @opts[:category]).first @guardian.ensure_can_create!(Topic,category) topic_params[:category_id] = category.id if category.present? diff --git a/spec/components/post_creator_spec.rb b/spec/components/post_creator_spec.rb index 8aaa2fa25..cadbef0cc 100644 --- a/spec/components/post_creator_spec.rb +++ b/spec/components/post_creator_spec.rb @@ -17,7 +17,7 @@ describe PostCreator do let(:image_sizes) { {'http://an.image.host/image.jpg' => {"width" => 111, "height" => 222}} } let(:creator) { PostCreator.new(user, basic_topic_params) } - let(:creator_with_category) { PostCreator.new(user, basic_topic_params.merge(category: category.name )) } + let(:creator_with_category) { PostCreator.new(user, basic_topic_params.merge(category: category.id )) } let(:creator_with_meta_data) { PostCreator.new(user, basic_topic_params.merge(meta_data: {hello: "world"} )) } let(:creator_with_image_sizes) { PostCreator.new(user, basic_topic_params.merge(image_sizes: image_sizes)) } @@ -75,7 +75,7 @@ describe PostCreator do reply = nil messages = MessageBus.track_publish do - created_post = PostCreator.new(admin, basic_topic_params.merge(category: cat.name)).create + created_post = PostCreator.new(admin, basic_topic_params.merge(category: cat.id)).create reply = PostCreator.new(admin, raw: "this is my test reply 123 testing", topic_id: created_post.topic_id).create end diff --git a/spec/models/category_featured_topic_spec.rb b/spec/models/category_featured_topic_spec.rb index bde98f0ee..28119a786 100644 --- a/spec/models/category_featured_topic_spec.rb +++ b/spec/models/category_featured_topic_spec.rb @@ -8,7 +8,7 @@ describe CategoryFeaturedTopic do context 'feature_topics_for' do let(:user) { Fabricate(:user) } let(:category) { Fabricate(:category) } - let!(:category_post) { PostCreator.create(user, raw: "I put this post in the category", title: "categorize THIS", category: category.name) } + let!(:category_post) { PostCreator.create(user, raw: "I put this post in the category", title: "categorize THIS", category: category.id) } it "should feature topics for a secure category" do @@ -26,7 +26,7 @@ describe CategoryFeaturedTopic do end it 'should not include invisible topics' do - invisible_post = PostCreator.create(user, raw: "Don't look at this post because it's awful.", title: "not visible to anyone", category: category.name) + invisible_post = PostCreator.create(user, raw: "Don't look at this post because it's awful.", title: "not visible to anyone", category: category.id) invisible_post.topic.update_status('visible', false, Fabricate(:admin)) CategoryFeaturedTopic.feature_topics_for(category) CategoryFeaturedTopic.count.should == 1