FIX: Non-English category name regression

This commit is contained in:
Robin Ward 2013-10-31 16:10:54 -04:00
parent e1d956f5ee
commit 744cd93f28
4 changed files with 44 additions and 9 deletions

View File

@ -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]);

View File

@ -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.

View File

@ -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]

View File

@ -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