mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-12-17 19:12:37 -05:00
FIX: We should use category_id
instead of category_name
to perform
operations, now that the subcategory names are not unique.
This commit is contained in:
parent
612dcb5805
commit
fb8dda7f42
14 changed files with 51 additions and 48 deletions
|
@ -67,10 +67,10 @@ export default Ember.ArrayController.extend(Discourse.ModalFunctionality, {
|
||||||
},
|
},
|
||||||
|
|
||||||
changeCategory: function() {
|
changeCategory: function() {
|
||||||
var category = Discourse.Category.findById(parseInt(this.get('newCategoryId'), 10)),
|
var categoryId = parseInt(this.get('newCategoryId'), 10) || 0,
|
||||||
categoryName = (category ? category.get('name') : null),
|
category = Discourse.Category.findById(categoryId),
|
||||||
self = this;
|
self = this;
|
||||||
this.perform({type: 'change_category', category_name: categoryName}).then(function(topics) {
|
this.perform({type: 'change_category', category_id: categoryId}).then(function(topics) {
|
||||||
topics.forEach(function(t) {
|
topics.forEach(function(t) {
|
||||||
t.set('category', category);
|
t.set('category', category);
|
||||||
});
|
});
|
||||||
|
|
|
@ -214,7 +214,6 @@ Discourse.TopicController = Discourse.ObjectController.extend(Discourse.Selected
|
||||||
},
|
},
|
||||||
|
|
||||||
finishedEditingTopic: function() {
|
finishedEditingTopic: function() {
|
||||||
var topicController = this;
|
|
||||||
if (this.get('editingTopic')) {
|
if (this.get('editingTopic')) {
|
||||||
|
|
||||||
var topic = this.get('model');
|
var topic = this.get('model');
|
||||||
|
@ -230,6 +229,7 @@ Discourse.TopicController = Discourse.ObjectController.extend(Discourse.Selected
|
||||||
});
|
});
|
||||||
|
|
||||||
// save the modifications
|
// save the modifications
|
||||||
|
var self = this;
|
||||||
topic.save().then(function(result){
|
topic.save().then(function(result){
|
||||||
// update the title if it has been changed (cleaned up) server-side
|
// update the title if it has been changed (cleaned up) server-side
|
||||||
var title = result.basic_topic.title;
|
var title = result.basic_topic.title;
|
||||||
|
@ -238,10 +238,10 @@ Discourse.TopicController = Discourse.ObjectController.extend(Discourse.Selected
|
||||||
title: title,
|
title: title,
|
||||||
fancy_title: fancy_title
|
fancy_title: fancy_title
|
||||||
});
|
});
|
||||||
topicController.set('topicSaving', false);
|
self.set('topicSaving', false);
|
||||||
}, function(error) {
|
}, function(error) {
|
||||||
topicController.set('editingTopic', true);
|
self.set('editingTopic', true);
|
||||||
topicController.set('topicSaving', false);
|
self.set('topicSaving', false);
|
||||||
if (error && error.responseText) {
|
if (error && error.responseText) {
|
||||||
bootbox.alert($.parseJSON(error.responseText).errors[0]);
|
bootbox.alert($.parseJSON(error.responseText).errors[0]);
|
||||||
} else {
|
} else {
|
||||||
|
@ -250,7 +250,7 @@ Discourse.TopicController = Discourse.ObjectController.extend(Discourse.Selected
|
||||||
});
|
});
|
||||||
|
|
||||||
// close editing mode
|
// close editing mode
|
||||||
topicController.set('editingTopic', false);
|
self.set('editingTopic', false);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -193,7 +193,7 @@ Discourse.Topic = Discourse.Model.extend({
|
||||||
|
|
||||||
return Discourse.ajax(this.get('url'), {
|
return Discourse.ajax(this.get('url'), {
|
||||||
type: 'PUT',
|
type: 'PUT',
|
||||||
data: { title: this.get('title'), category: this.get('category.name') }
|
data: { title: this.get('title'), category_id: this.get('category.id') }
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -124,7 +124,7 @@ Discourse.TopicTrackingState = Discourse.Model.extend({
|
||||||
|
|
||||||
row.highest_post_number = topic.highest_post_number;
|
row.highest_post_number = topic.highest_post_number;
|
||||||
if (topic.category) {
|
if (topic.category) {
|
||||||
row.category_name = topic.category.name;
|
row.category_id = topic.category.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
tracker.states["t" + topic.id] = row;
|
tracker.states["t" + topic.id] = row;
|
||||||
|
@ -137,7 +137,7 @@ Discourse.TopicTrackingState = Discourse.Model.extend({
|
||||||
this.set("messageCount", this.get("messageCount") + 1);
|
this.set("messageCount", this.get("messageCount") + 1);
|
||||||
},
|
},
|
||||||
|
|
||||||
countNew: function(category_name){
|
countNew: function(category_id){
|
||||||
return _.chain(this.states)
|
return _.chain(this.states)
|
||||||
.where({last_read_post_number: null})
|
.where({last_read_post_number: null})
|
||||||
.where(function(topic) {
|
.where(function(topic) {
|
||||||
|
@ -145,7 +145,7 @@ Discourse.TopicTrackingState = Discourse.Model.extend({
|
||||||
return (topic.notification_level !== 0 && !topic.notification_level) ||
|
return (topic.notification_level !== 0 && !topic.notification_level) ||
|
||||||
topic.notification_level >= Discourse.Topic.NotificationLevel.TRACKING;
|
topic.notification_level >= Discourse.Topic.NotificationLevel.TRACKING;
|
||||||
})
|
})
|
||||||
.where(function(topic){ return topic.category_name === category_name || !category_name;})
|
.where(function(topic){ return topic.category_id === category_id || !category_id;})
|
||||||
.value()
|
.value()
|
||||||
.length;
|
.length;
|
||||||
},
|
},
|
||||||
|
@ -159,22 +159,22 @@ Discourse.TopicTrackingState = Discourse.Model.extend({
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
countUnread: function(category_name){
|
countUnread: function(category_id){
|
||||||
return _.chain(this.states)
|
return _.chain(this.states)
|
||||||
.where(function(topic){
|
.where(function(topic){
|
||||||
return topic.last_read_post_number !== null &&
|
return topic.last_read_post_number !== null &&
|
||||||
topic.last_read_post_number < topic.highest_post_number;
|
topic.last_read_post_number < topic.highest_post_number;
|
||||||
})
|
})
|
||||||
.where(function(topic) { return topic.notification_level >= Discourse.Topic.NotificationLevel.TRACKING})
|
.where(function(topic) { return topic.notification_level >= Discourse.Topic.NotificationLevel.TRACKING})
|
||||||
.where(function(topic){ return topic.category_name === category_name || !category_name;})
|
.where(function(topic){ return topic.category_id === category_id || !category_id;})
|
||||||
.value()
|
.value()
|
||||||
.length;
|
.length;
|
||||||
},
|
},
|
||||||
|
|
||||||
countCategory: function(category) {
|
countCategory: function(category_id) {
|
||||||
var count = 0;
|
var count = 0;
|
||||||
_.each(this.states, function(topic){
|
_.each(this.states, function(topic){
|
||||||
if (topic.category_name === category) {
|
if (topic.category_id === category_id) {
|
||||||
count += (topic.last_read_post_number === null ||
|
count += (topic.last_read_post_number === null ||
|
||||||
topic.last_read_post_number < topic.highest_post_number) ? 1 : 0;
|
topic.last_read_post_number < topic.highest_post_number) ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -106,11 +106,11 @@ class PostsController < ApplicationController
|
||||||
# to stay consistent with the create api,
|
# to stay consistent with the create api,
|
||||||
# we should allow for title changes and category changes here
|
# we should allow for title changes and category changes here
|
||||||
# we should also move all of this to a post updater.
|
# we should also move all of this to a post updater.
|
||||||
if post.post_number == 1 && (params[:title] || params[:post][:category])
|
if post.post_number == 1 && (params[:title] || params[:post][:category_id])
|
||||||
post.topic.acting_user = current_user
|
post.topic.acting_user = current_user
|
||||||
post.topic.title = params[:title] if params[:title]
|
post.topic.title = params[:title] if params[:title]
|
||||||
Topic.transaction do
|
Topic.transaction do
|
||||||
post.topic.change_category(params[:post][:category])
|
post.topic.change_category_to_id(params[:post][:category_id].to_i)
|
||||||
post.topic.save
|
post.topic.save
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -113,7 +113,7 @@ class TopicsController < ApplicationController
|
||||||
|
|
||||||
success = false
|
success = false
|
||||||
Topic.transaction do
|
Topic.transaction do
|
||||||
success = topic.save && topic.change_category(params[:category])
|
success = topic.save && topic.change_category_to_id(params[:category_id].to_i)
|
||||||
end
|
end
|
||||||
|
|
||||||
# this is used to return the title to the client as it may have been changed by "TextCleaner"
|
# this is used to return the title to the client as it may have been changed by "TextCleaner"
|
||||||
|
|
|
@ -493,13 +493,12 @@ class Topic < ActiveRecord::Base
|
||||||
new_post
|
new_post
|
||||||
end
|
end
|
||||||
|
|
||||||
# Changes the category to a new name
|
def change_category_to_id(category_id)
|
||||||
def change_category(name)
|
|
||||||
# If the category name is blank, reset the attribute
|
# If the category name is blank, reset the attribute
|
||||||
if name.blank?
|
if (category_id.nil? || category_id.to_i == 0)
|
||||||
cat = Category.find_by(id: SiteSetting.uncategorized_category_id)
|
cat = Category.find_by(id: SiteSetting.uncategorized_category_id)
|
||||||
else
|
else
|
||||||
cat = Category.find_by(name: name)
|
cat = Category.where(id: category_id).first
|
||||||
end
|
end
|
||||||
|
|
||||||
return true if cat == category
|
return true if cat == category
|
||||||
|
@ -507,7 +506,6 @@ class Topic < ActiveRecord::Base
|
||||||
changed_to_category(cat)
|
changed_to_category(cat)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def remove_allowed_user(username)
|
def remove_allowed_user(username)
|
||||||
user = User.find_by(username: username)
|
user = User.find_by(username: username)
|
||||||
if user
|
if user
|
||||||
|
|
|
@ -14,7 +14,7 @@ class TopicTrackingState
|
||||||
:highest_post_number,
|
:highest_post_number,
|
||||||
:last_read_post_number,
|
:last_read_post_number,
|
||||||
:created_at,
|
:created_at,
|
||||||
:category_name,
|
:category_id,
|
||||||
:notification_level
|
:notification_level
|
||||||
|
|
||||||
def self.publish_new(topic)
|
def self.publish_new(topic)
|
||||||
|
@ -118,7 +118,7 @@ class TopicTrackingState
|
||||||
topics.created_at,
|
topics.created_at,
|
||||||
highest_post_number,
|
highest_post_number,
|
||||||
last_read_post_number,
|
last_read_post_number,
|
||||||
c.name AS category_name,
|
c.id AS category_id,
|
||||||
tu.notification_level
|
tu.notification_level
|
||||||
FROM users u
|
FROM users u
|
||||||
INNER JOIN user_stats AS us ON us.user_id = u.id
|
INNER JOIN user_stats AS us ON us.user_id = u.id
|
||||||
|
|
|
@ -1,3 +1,8 @@
|
||||||
class TopicTrackingStateSerializer < ApplicationSerializer
|
class TopicTrackingStateSerializer < ApplicationSerializer
|
||||||
attributes :topic_id, :highest_post_number, :last_read_post_number, :created_at, :category_name, :notification_level
|
attributes :topic_id,
|
||||||
|
:highest_post_number,
|
||||||
|
:last_read_post_number,
|
||||||
|
:created_at,
|
||||||
|
:category_id,
|
||||||
|
:notification_level
|
||||||
end
|
end
|
||||||
|
|
|
@ -26,7 +26,7 @@ class TopicsBulkAction
|
||||||
def change_category
|
def change_category
|
||||||
topics.each do |t|
|
topics.each do |t|
|
||||||
if guardian.can_edit?(t)
|
if guardian.can_edit?(t)
|
||||||
@changed_ids << t.id if t.change_category(@operation[:category_name])
|
@changed_ids << t.id if t.change_category_to_id(@operation[:category_id])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -18,7 +18,7 @@ describe TopicsBulkAction do
|
||||||
|
|
||||||
context "when the user can edit the topic" do
|
context "when the user can edit the topic" do
|
||||||
it "changes the category and returns the topic_id" do
|
it "changes the category and returns the topic_id" do
|
||||||
tba = TopicsBulkAction.new(topic.user, [topic.id], type: 'change_category', category_name: category.name)
|
tba = TopicsBulkAction.new(topic.user, [topic.id], type: 'change_category', category_id: category.id)
|
||||||
topic_ids = tba.perform!
|
topic_ids = tba.perform!
|
||||||
topic_ids.should == [topic.id]
|
topic_ids.should == [topic.id]
|
||||||
topic.reload
|
topic.reload
|
||||||
|
@ -29,7 +29,7 @@ describe TopicsBulkAction do
|
||||||
context "when the user can't edit the topic" do
|
context "when the user can't edit the topic" do
|
||||||
it "doesn't change the category" do
|
it "doesn't change the category" do
|
||||||
Guardian.any_instance.expects(:can_edit?).returns(false)
|
Guardian.any_instance.expects(:can_edit?).returns(false)
|
||||||
tba = TopicsBulkAction.new(topic.user, [topic.id], type: 'change_category', category_name: category.name)
|
tba = TopicsBulkAction.new(topic.user, [topic.id], type: 'change_category', category_id: category.id)
|
||||||
topic_ids = tba.perform!
|
topic_ids = tba.perform!
|
||||||
topic_ids.should == []
|
topic_ids.should == []
|
||||||
topic.reload
|
topic.reload
|
||||||
|
|
|
@ -719,8 +719,8 @@ describe TopicsController do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'triggers a change of category' do
|
it 'triggers a change of category' do
|
||||||
Topic.any_instance.expects(:change_category).with('incredible').returns(true)
|
Topic.any_instance.expects(:change_category_to_id).with(123).returns(true)
|
||||||
xhr :put, :update, topic_id: @topic.id, slug: @topic.title, category: 'incredible'
|
xhr :put, :update, topic_id: @topic.id, slug: @topic.title, category_id: 123
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns errors with invalid titles" do
|
it "returns errors with invalid titles" do
|
||||||
|
@ -729,8 +729,8 @@ describe TopicsController do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns errors with invalid categories" do
|
it "returns errors with invalid categories" do
|
||||||
Topic.any_instance.expects(:change_category).returns(false)
|
Topic.any_instance.expects(:change_category_to_id).returns(false)
|
||||||
xhr :put, :update, topic_id: @topic.id, slug: @topic.title, category: ''
|
xhr :put, :update, topic_id: @topic.id, slug: @topic.title
|
||||||
expect(response).not_to be_success
|
expect(response).not_to be_success
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -740,8 +740,8 @@ describe TopicsController do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "can add a category to an uncategorized topic" do
|
it "can add a category to an uncategorized topic" do
|
||||||
Topic.any_instance.expects(:change_category).with('incredible').returns(true)
|
Topic.any_instance.expects(:change_category_to_id).with(456).returns(true)
|
||||||
xhr :put, :update, topic_id: @topic.id, slug: @topic.title, category: 'incredible'
|
xhr :put, :update, topic_id: @topic.id, slug: @topic.title, category_id: 456
|
||||||
response.should be_success
|
response.should be_success
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -236,7 +236,7 @@ describe Category do
|
||||||
describe "trying to change the category topic's category" do
|
describe "trying to change the category topic's category" do
|
||||||
before do
|
before do
|
||||||
@new_cat = Fabricate(:category, name: '2nd Category', user: @category.user)
|
@new_cat = Fabricate(:category, name: '2nd Category', user: @category.user)
|
||||||
@topic.change_category(@new_cat.name)
|
@topic.change_category_to_id(@new_cat.id)
|
||||||
@topic.reload
|
@topic.reload
|
||||||
@category.reload
|
@category.reload
|
||||||
end
|
end
|
||||||
|
|
|
@ -802,7 +802,7 @@ describe Topic do
|
||||||
let(:category) { Fabricate(:category) }
|
let(:category) { Fabricate(:category) }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
topic.change_category(category.name)
|
topic.change_category_to_id(category.id)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "creates a new revision" do
|
it "creates a new revision" do
|
||||||
|
@ -811,7 +811,7 @@ describe Topic do
|
||||||
|
|
||||||
context "removing a category" do
|
context "removing a category" do
|
||||||
before do
|
before do
|
||||||
topic.change_category(nil)
|
topic.change_category_to_id(nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "creates a new revision" do
|
it "creates a new revision" do
|
||||||
|
@ -850,12 +850,12 @@ describe Topic do
|
||||||
describe 'without a previous category' do
|
describe 'without a previous category' do
|
||||||
|
|
||||||
it 'should not change the topic_count when not changed' do
|
it 'should not change the topic_count when not changed' do
|
||||||
lambda { @topic.change_category(@topic.category.name); @category.reload }.should_not change(@category, :topic_count)
|
lambda { @topic.change_category_to_id(@topic.category.id); @category.reload }.should_not change(@category, :topic_count)
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'changed category' do
|
describe 'changed category' do
|
||||||
before do
|
before do
|
||||||
@topic.change_category(@category.name)
|
@topic.change_category_to_id(@category.id)
|
||||||
@category.reload
|
@category.reload
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -867,14 +867,14 @@ describe Topic do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "doesn't change the category when it can't be found" do
|
it "doesn't change the category when it can't be found" do
|
||||||
@topic.change_category('made up')
|
@topic.change_category_to_id(12312312)
|
||||||
@topic.category_id.should == SiteSetting.uncategorized_category_id
|
@topic.category_id.should == SiteSetting.uncategorized_category_id
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'with a previous category' do
|
describe 'with a previous category' do
|
||||||
before do
|
before do
|
||||||
@topic.change_category(@category.name)
|
@topic.change_category_to_id(@category.id)
|
||||||
@topic.reload
|
@topic.reload
|
||||||
@category.reload
|
@category.reload
|
||||||
end
|
end
|
||||||
|
@ -884,18 +884,18 @@ describe Topic do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "doesn't change the topic_count when the value doesn't change" do
|
it "doesn't change the topic_count when the value doesn't change" do
|
||||||
lambda { @topic.change_category(@category.name); @category.reload }.should_not change(@category, :topic_count)
|
lambda { @topic.change_category_to_id(@category.id); @category.reload }.should_not change(@category, :topic_count)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "doesn't reset the category when given a name that doesn't exist" do
|
it "doesn't reset the category when given a name that doesn't exist" do
|
||||||
@topic.change_category('made up')
|
@topic.change_category_to_id(55556)
|
||||||
@topic.category_id.should be_present
|
@topic.category_id.should be_present
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'to a different category' do
|
describe 'to a different category' do
|
||||||
before do
|
before do
|
||||||
@new_category = Fabricate(:category, user: @user, name: '2nd category')
|
@new_category = Fabricate(:category, user: @user, name: '2nd category')
|
||||||
@topic.change_category(@new_category.name)
|
@topic.change_category_to_id(@new_category.id)
|
||||||
@topic.reload
|
@topic.reload
|
||||||
@new_category.reload
|
@new_category.reload
|
||||||
@category.reload
|
@category.reload
|
||||||
|
@ -918,13 +918,13 @@ describe Topic do
|
||||||
let!(:topic) { Fabricate(:topic, category: Fabricate(:category)) }
|
let!(:topic) { Fabricate(:topic, category: Fabricate(:category)) }
|
||||||
|
|
||||||
it 'returns false' do
|
it 'returns false' do
|
||||||
topic.change_category('').should eq(false) # don't use "be_false" here because it would also match nil
|
topic.change_category_to_id(nil).should eq(false) # don't use "be_false" here because it would also match nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'when the category exists' do
|
describe 'when the category exists' do
|
||||||
before do
|
before do
|
||||||
@topic.change_category(nil)
|
@topic.change_category_to_id(nil)
|
||||||
@category.reload
|
@category.reload
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue