mirror of
https://github.com/discourse/discourse.git
synced 2024-11-26 12:03:45 +08:00
FIX: case-insensitive category lookup when creating topics
This commit is contained in:
parent
6c10cc3788
commit
1445ad61da
|
@ -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")
|
||||
|
|
13
db/migrate/20140815215618_add_name_lower_to_categories.rb
Normal file
13
db/migrate/20140815215618_add_name_lower_to_categories.rb
Normal 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
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user