Merge pull request #3021 from jmay/custom-category-slug

optional custom value for category slug (create and update)
This commit is contained in:
Robin Ward 2014-12-29 10:34:23 -05:00
commit 1055fc0919
4 changed files with 43 additions and 12 deletions

View file

@ -138,6 +138,7 @@ class CategoriesController < ApplicationController
:logo_url,
:background_url,
:allow_badges,
:slug,
:permissions => [*p.try(:keys)])
end
end

View file

@ -32,7 +32,7 @@ class Category < ActiveRecord::Base
length: { in: 1..50 }
validate :parent_category_validator
before_validation :ensure_slug
validate :ensure_slug
before_save :apply_permissions
before_save :downcase_email
before_save :downcase_name
@ -196,18 +196,23 @@ SQL
end
def duplicate_slug?
Category.where(slug: self.slug, parent_category_id: parent_category_id).where.not(id: id).any?
end
def ensure_slug
if name.present?
self.name.strip!
if slug.present?
# custom slug
errors.add(:slug, "is already in use") if duplicate_slug?
else
# auto slug
self.slug = Slug.for(name)
return if self.slug.blank?
# If a category with that slug already exists, set the slug to nil so the category can be found
# another way.
category = Category.where(slug: self.slug, parent_category_id: parent_category_id)
category = category.where("id != ?", id) if id.present?
self.slug = '' if category.exists?
self.slug = '' if duplicate_slug?
end
end
end

View file

@ -43,13 +43,12 @@ describe CategoriesController do
end
end
describe "success" do
it "works" do
readonly = CategoryGroup.permission_types[:readonly]
create_post = CategoryGroup.permission_types[:create_post]
xhr :post, :create, name: "hello", color: "ff0", text_color: "fff",
xhr :post, :create, name: "hello", color: "ff0", text_color: "fff", slug: "hello-cat",
auto_close_hours: 72,
permissions: {
"everyone" => readonly,
@ -62,6 +61,7 @@ describe CategoriesController do
[Group[:everyone].id, readonly],[Group[:staff].id,create_post]
]
category.name.should == "hello"
category.slug.should == "hello-cat"
category.color.should == "ff0"
category.auto_close_hours.should == 72
end
@ -180,7 +180,7 @@ describe CategoriesController do
readonly = CategoryGroup.permission_types[:readonly]
create_post = CategoryGroup.permission_types[:create_post]
xhr :put, :update, id: @category.id, name: "hello", color: "ff0", text_color: "fff",
xhr :put, :update, id: @category.id, name: "hello", color: "ff0", text_color: "fff", slug: "hello-category",
auto_close_hours: 72,
permissions: {
"everyone" => readonly,
@ -193,6 +193,7 @@ describe CategoriesController do
[Group[:everyone].id, readonly],[Group[:staff].id,create_post]
]
@category.name.should == "hello"
@category.slug.should == "hello-category"
@category.color.should == "ff0"
@category.auto_close_hours.should == 72
end

View file

@ -192,6 +192,20 @@ describe Category do
end
end
describe 'custom slug can be provided' do
it 'has the custom value' do
c = Fabricate(:category, name: "Cats", slug: "cats-category")
c.slug.should eq("cats-category")
end
it 'fails if custom slug is duplicate with existing' do
c1 = Fabricate(:category, name: "Cats", slug: "cats")
c2 = Fabricate.build(:category, name: "More Cats", slug: "cats")
c2.should_not be_valid
c2.errors[:slug].should be_present
end
end
describe 'description_text' do
it 'correctly generates text description as needed' do
c = Category.new
@ -284,6 +298,16 @@ describe Category do
end
end
describe "update" do
it "should enforce uniqueness of slug" do
Fabricate(:category, slug: "the-slug")
c2 = Fabricate(:category, slug: "different-slug")
c2.slug = "the-slug"
c2.should_not be_valid
c2.errors[:slug].should be_present
end
end
describe 'destroy' do
before do
@category = Fabricate(:category)