diff --git a/app/models/category.rb b/app/models/category.rb index 3adaf1fec84..0d95aed02bc 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -32,6 +32,7 @@ class Category < ActiveRecord::Base after_create :create_category_definition after_create :publish_categories_list after_destroy :publish_categories_list + after_update :rename_category_definition, if: :name_changed? has_one :category_search_data belongs_to :parent_category, class_name: 'Category' @@ -331,6 +332,15 @@ SQL url << "/#{parent_category.slug}" if parent_category_id url << "/#{slug}" end + + # If the name changes, try and update the category definition topic too if it's + # an exact match + def rename_category_definition + old_name = changed_attributes["name"] + if topic.title == I18n.t("category.topic_prefix", category: old_name) + topic.update_column(:title, I18n.t("category.topic_prefix", category: name)) + end + end end # == Schema Information diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb index 04f88ee1beb..ef7b5e7d623 100644 --- a/spec/models/category_spec.rb +++ b/spec/models/category_spec.rb @@ -220,6 +220,12 @@ describe Category do @category.topics_year.should == 0 end + it "renames the definition when renamed" do + @category.update_attributes(name: 'Troutfishing') + @topic.reload + @topic.title.should =~ /Troutfishing/ + end + it "should not set its description topic to auto-close" do category = Fabricate(:category, name: 'Closing Topics', auto_close_hours: 1) category.topic.auto_close_at.should be_nil