diff --git a/app/models/category.rb b/app/models/category.rb index c0cf16eb6ff..ce2eec881e8 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -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") diff --git a/db/migrate/20140815215618_add_name_lower_to_categories.rb b/db/migrate/20140815215618_add_name_lower_to_categories.rb new file mode 100644 index 00000000000..97c1685337d --- /dev/null +++ b/db/migrate/20140815215618_add_name_lower_to_categories.rb @@ -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 diff --git a/lib/topic_creator.rb b/lib/topic_creator.rb index 47f75cf67e2..9209ec85cab 100644 --- a/lib/topic_creator.rb +++ b/lib/topic_creator.rb @@ -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 diff --git a/spec/components/topic_creator_spec.rb b/spec/components/topic_creator_spec.rb index 1969218eae9..4ef0ab6df8c 100644 --- a/spec/components/topic_creator_spec.rb +++ b/spec/components/topic_creator_spec.rb @@ -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 diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb index 4ef85e07898..a71d78f4fee 100644 --- a/spec/models/category_spec.rb +++ b/spec/models/category_spec.rb @@ -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