From 744cd93f284a35ab94d55987119b64e7f4ece10f Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Thu, 31 Oct 2013 16:10:54 -0400 Subject: [PATCH] FIX: Non-English category name regression --- .../controllers/edit_category_controller.js | 6 +++--- .../javascripts/discourse/models/category.js | 15 ++++++++++---- lib/topic_query.rb | 12 ++++++++++- spec/components/topic_query_spec.rb | 20 ++++++++++++++++++- 4 files changed, 44 insertions(+), 9 deletions(-) diff --git a/app/assets/javascripts/discourse/controllers/edit_category_controller.js b/app/assets/javascripts/discourse/controllers/edit_category_controller.js index 6be805503a9..c2a05af4931 100644 --- a/app/assets/javascripts/discourse/controllers/edit_category_controller.js +++ b/app/assets/javascripts/discourse/controllers/edit_category_controller.js @@ -134,13 +134,13 @@ Discourse.EditCategoryController = Discourse.ObjectController.extend(Discourse.M this.set('saving', true); model.set('parentCategory', parentCategory); - var newSlug = Discourse.Category.slugFor(this.get('model')); this.get('model').save().then(function(result) { // success self.send('closeModal'); - Discourse.URL.redirectTo("/category/" + newSlug); - }, function(error) { + model.set('slug', result.category.slug); + Discourse.URL.redirectTo("/category/" + Discourse.Category.slugFor(model)); + }).fail(function(error) { if (error && error.responseText) { self.flash($.parseJSON(error.responseText).errors[0]); diff --git a/app/assets/javascripts/discourse/models/category.js b/app/assets/javascripts/discourse/models/category.js index afd286cf8ec..5fad84eb4d2 100644 --- a/app/assets/javascripts/discourse/models/category.js +++ b/app/assets/javascripts/discourse/models/category.js @@ -11,6 +11,7 @@ Discourse.Category = Discourse.Model.extend({ init: function() { this._super(); this.set("availableGroups", Em.A(this.get("available_groups"))); + this.set("permissions", Em.A(_.map(this.group_permissions, function(elem){ return { group_name: elem.group_name, @@ -146,23 +147,29 @@ Discourse.Category.reopenClass({ return Discourse.Site.currentProp('categories'); }, + findSingleBySlug: function(slug) { + return Discourse.Category.list().find(function(c) { + return Discourse.Category.slugFor(c) === slug; + }); + }, + findBySlug: function(slug, parentSlug) { var categories = Discourse.Category.list(), category; if (parentSlug) { - var parentCategory = categories.findBy('slug', parentSlug); + var parentCategory = Discourse.Category.findSingleBySlug(parentSlug); if (parentCategory) { category = categories.find(function(item) { - return item && item.get('parentCategory') === parentCategory && item.get('slug') === slug; + return item && item.get('parentCategory') === parentCategory && Discourse.Category.slugFor(item) === (parentSlug + "/" + slug); }); } } else { - category = categories.findBy('slug', slug); + category = Discourse.Category.findSingleBySlug(slug); // If we have a parent category, we need to enforce it - if (category.get('parentCategory')) return; + if (category && category.get('parentCategory')) return; } // In case the slug didn't work, try to find it by id instead. diff --git a/lib/topic_query.rb b/lib/topic_query.rb index e3930dc82a6..1cdbdf8a945 100644 --- a/lib/topic_query.rb +++ b/lib/topic_query.rb @@ -233,7 +233,17 @@ class TopicQuery result = result.listable_topics.includes(category: :topic_only_relative_url) result = result.where('categories.name is null or categories.name <> ?', options[:exclude_category]).references(:categories) if options[:exclude_category] - result = result.where('categories.slug = ?', options[:category]).references(:categories) if options[:category].present? + + if options[:category].present? + category_id = options[:category].to_i + if category_id == 0 + result = result.where('categories.slug = ?', options[:category]) + else + result = result.where('categories.id = ?', category_id) + end + result = result.references(:categories) + end + result = result.limit(options[:per_page]) unless options[:limit] == false result = result.visible if options[:visible] || @user.nil? || @user.regular? result = result.where('topics.id <> ?', options[:except_topic_id]).references(:topics) if options[:except_topic_id] diff --git a/spec/components/topic_query_spec.rb b/spec/components/topic_query_spec.rb index 3cd82bef2dd..44fbcc66a7b 100644 --- a/spec/components/topic_query_spec.rb +++ b/spec/components/topic_query_spec.rb @@ -39,6 +39,25 @@ describe TopicQuery do end + context 'category filter' do + let(:category) { Fabricate(:category) } + let(:diff_category) { Fabricate(:category) } + + it "returns topics in the category when we filter to it" do + TopicQuery.new(moderator).list_latest.topics.size.should == 0 + + # Filter by slug + TopicQuery.new(moderator, category: category.slug).list_latest.topics.size.should == 1 + TopicQuery.new(moderator, category: "#{category.id}-category").list_latest.topics.size.should == 1 + TopicQuery.new(moderator, category: diff_category.slug).list_latest.topics.size.should == 1 + TopicQuery.new(moderator, category: 'made up slug').list_latest.topics.size.should == 0 + end + + + + end + + context 'a bunch of topics' do let!(:regular_topic) { Fabricate(:topic, title: 'this is a regular topic', user: creator, bumped_at: 15.minutes.ago) } let!(:pinned_topic) { Fabricate(:topic, title: 'this is a pinned topic', user: creator, pinned_at: 10.minutes.ago, bumped_at: 10.minutes.ago) } @@ -69,7 +88,6 @@ describe TopicQuery do it "no longer shows the pinned topic at the top" do topics.should == [closed_topic, archived_topic, pinned_topic, regular_topic] end - end end