FIX: case-insensitive category lookup when creating topics

This commit is contained in:
Neil Lalonde 2014-08-18 11:07:32 -04:00
parent 6c10cc3788
commit 1445ad61da
5 changed files with 30 additions and 1 deletions

View File

@ -32,6 +32,7 @@ class Category < ActiveRecord::Base
before_validation :ensure_slug
before_save :apply_permissions
before_save :downcase_email
before_save :downcase_name
after_create :create_category_definition
after_create :publish_categories_list
after_destroy :publish_categories_list
@ -254,6 +255,10 @@ SQL
self.email_in = email_in.downcase if self.email_in
end
def downcase_name
self.name_lower = name.downcase if self.name
end
def secure_group_ids
if self.read_restricted?
groups.pluck("groups.id")

View File

@ -0,0 +1,13 @@
class AddNameLowerToCategories < ActiveRecord::Migration
def up
add_column :categories, :name_lower, :string, limit: 50
execute "update categories set name_lower = lower(name)"
change_column :categories, :name_lower, :string, limit: 50, null:false
end
def down
remove_column :categories, :name_lower
end
end

View File

@ -73,7 +73,7 @@ class TopicCreator
if (@opts[:category].is_a? Integer) || (@opts[:category] =~ /^\d+$/)
Category.find_by(id: @opts[:category])
else
Category.find_by(name: @opts[:category])
Category.find_by(name_lower: @opts[:category].try(:downcase))
end
end

View File

@ -40,6 +40,13 @@ describe TopicCreator do
topic.should be_valid
topic.auto_close_at.should be_nil
end
it "category name is case insensitive" do
category = Fabricate(:category, name: "Neil's Blog")
topic = TopicCreator.create(user, Guardian.new(user), valid_attrs.merge(category: "neil's blog"))
topic.should be_valid
topic.category.should == category
end
end
end
end

View File

@ -148,6 +148,10 @@ describe Category do
Fabricate(:category, name: " blanks ").name.should == "blanks"
end
it "sets name_lower" do
Fabricate(:category, name: "Not MySQL").name_lower.should == "not mysql"
end
it "has custom fields" do
category = Fabricate(:category, name: " music")
category.custom_fields["a"].should == nil