mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-27 09:36:19 -05:00
Merge pull request #3068 from fantasticfears/category_slug
support setting category slug
This commit is contained in:
commit
9fcaf090ec
11 changed files with 92 additions and 15 deletions
|
@ -146,9 +146,9 @@ export default ObjectController.extend(ModalFunctionality, {
|
|||
|
||||
}).catch(function(error) {
|
||||
if (error && error.responseText) {
|
||||
self.flash($.parseJSON(error.responseText).errors[0]);
|
||||
self.flash($.parseJSON(error.responseText).errors[0], 'error');
|
||||
} else {
|
||||
self.flash(I18n.t('generic_error'));
|
||||
self.flash(I18n.t('generic_error'), 'error');
|
||||
}
|
||||
self.set('saving', false);
|
||||
});
|
||||
|
|
|
@ -58,6 +58,7 @@ Discourse.Category = Discourse.Model.extend({
|
|||
return Discourse.ajax(url, {
|
||||
data: {
|
||||
name: this.get('name'),
|
||||
slug: this.get('slug'),
|
||||
color: this.get('color'),
|
||||
text_color: this.get('text_color'),
|
||||
secure: this.get('secure'),
|
||||
|
|
|
@ -1,7 +1,13 @@
|
|||
<form>
|
||||
<section class='field'>
|
||||
<label>{{i18n 'category.name'}}</label>
|
||||
{{text-field value=name placeholderKey="category.name_placeholder" maxlength="50"}}
|
||||
<section class="field-item">
|
||||
<label>{{i18n 'category.name'}}</label>
|
||||
{{text-field value=name placeholderKey="category.name_placeholder" maxlength="50"}}
|
||||
</section>
|
||||
<section class="field-item">
|
||||
<label>{{i18n 'category.slug'}}</label>
|
||||
{{text-field value=slug placeholderKey="category.slug_placeholder" maxlength="255"}}
|
||||
</section>
|
||||
</section>
|
||||
|
||||
{{#if canSelectParentCategory}}
|
||||
|
|
|
@ -130,6 +130,10 @@
|
|||
section.field {
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
section.field .field-item {
|
||||
display: inline-block;
|
||||
margin-right: 10px;
|
||||
}
|
||||
}
|
||||
|
||||
.reply-where-modal {
|
||||
|
|
|
@ -95,6 +95,19 @@ class CategoriesController < ApplicationController
|
|||
end
|
||||
end
|
||||
|
||||
def update_slug
|
||||
@category = Category.find(params[:category_id].to_i)
|
||||
guardian.ensure_can_edit!(@category)
|
||||
|
||||
custom_slug = params[:slug].to_s
|
||||
|
||||
if custom_slug.present? && @category.update_attributes(slug: custom_slug)
|
||||
render json: success_json
|
||||
else
|
||||
render_json_error(@category)
|
||||
end
|
||||
end
|
||||
|
||||
def set_notifications
|
||||
category_id = params[:category_id].to_i
|
||||
notification_level = params[:notification_level].to_i
|
||||
|
|
|
@ -201,18 +201,19 @@ SQL
|
|||
end
|
||||
|
||||
def ensure_slug
|
||||
if name.present?
|
||||
self.name.strip!
|
||||
return unless name.present?
|
||||
|
||||
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?
|
||||
self.slug = '' if duplicate_slug?
|
||||
end
|
||||
self.name.strip!
|
||||
|
||||
if slug.present?
|
||||
# santized custom slug
|
||||
self.slug = Slug.for(slug)
|
||||
errors.add(:slug, 'is already in use') if duplicate_slug?
|
||||
else
|
||||
# auto slug
|
||||
self.slug = Slug.for(name)
|
||||
return if self.slug.blank?
|
||||
self.slug = '' if duplicate_slug?
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -1281,6 +1281,8 @@ en:
|
|||
delete: 'Delete Category'
|
||||
create: 'New Category'
|
||||
save: 'Save Category'
|
||||
slug: 'Category Slug'
|
||||
slug_placeholder: '(Optional) dashed-words for url'
|
||||
creation_error: There has been an error during the creation of the category.
|
||||
save_error: There was an error saving the category.
|
||||
name: "Category Name"
|
||||
|
|
|
@ -325,6 +325,7 @@ Discourse::Application.routes.draw do
|
|||
post "category/uploads" => "categories#upload"
|
||||
post "category/:category_id/move" => "categories#move"
|
||||
post "category/:category_id/notifications" => "categories#set_notifications"
|
||||
put "category/:category_id/slug" => "categories#update_slug"
|
||||
|
||||
get "c/:id/show" => "categories#show"
|
||||
get "c/:category.rss" => "list#category_feed", format: :rss
|
||||
|
|
|
@ -203,4 +203,42 @@ describe CategoriesController do
|
|||
|
||||
end
|
||||
|
||||
describe 'update_slug' do
|
||||
it 'requires the user to be logged in' do
|
||||
lambda { xhr :put, :update_slug, category_id: 'category'}.should raise_error(Discourse::NotLoggedIn)
|
||||
end
|
||||
|
||||
describe 'logged in' do
|
||||
let(:valid_attrs) { {id: @category.id, slug: 'fff'} }
|
||||
|
||||
before do
|
||||
@user = log_in(:admin)
|
||||
@category = Fabricate(:happy_category, user: @user)
|
||||
end
|
||||
|
||||
it 'rejects blank' do
|
||||
xhr :put, :update_slug, category_id: @category.id, slug: nil
|
||||
response.status.should == 422
|
||||
end
|
||||
|
||||
it 'accepts valid custom slug' do
|
||||
xhr :put, :update_slug, category_id: @category.id, slug: 'valid-slug'
|
||||
response.should be_success
|
||||
category = Category.find(@category.id)
|
||||
category.slug.should == 'valid-slug'
|
||||
end
|
||||
|
||||
it 'accepts not well formed custom slug' do
|
||||
xhr :put, :update_slug, category_id: @category.id, slug: ' valid slug'
|
||||
response.should be_success
|
||||
category = Category.find(@category.id)
|
||||
category.slug.should == 'valid-slug'
|
||||
end
|
||||
|
||||
it 'rejects invalid custom slug' do
|
||||
xhr :put, :update_slug, category_id: @category.id, slug: ' '
|
||||
response.status.should == 422
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -7,3 +7,9 @@ Fabricator(:diff_category, from: :category) do
|
|||
name "Different Category"
|
||||
user
|
||||
end
|
||||
|
||||
Fabricator(:happy_category, from: :category) do
|
||||
name 'Happy Category'
|
||||
slug 'happy'
|
||||
user
|
||||
end
|
||||
|
|
|
@ -198,6 +198,11 @@ describe Category do
|
|||
c.slug.should eq("cats-category")
|
||||
end
|
||||
|
||||
it 'and be sanitized' do
|
||||
c = Fabricate(:category, name: 'Cats', slug: ' invalid slug')
|
||||
c.slug.should == 'invalid-slug'
|
||||
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")
|
||||
|
|
Loading…
Reference in a new issue