From e7a037709f916bedcd8fcfb3fc3bbaeb812337f4 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 21 Oct 2013 17:17:40 +1100 Subject: [PATCH] allow ordering categories in the edit category ui --- .../javascripts/discourse/models/category.js | 3 ++- .../modal/edit_category.js.handlebars | 5 ++++ app/controllers/categories_controller.rb | 27 ++++++++++++------- app/serializers/category_serializer.rb | 2 +- config/locales/client.en.yml | 1 + 5 files changed, 26 insertions(+), 12 deletions(-) diff --git a/app/assets/javascripts/discourse/models/category.js b/app/assets/javascripts/discourse/models/category.js index 965f68434..efb5b7112 100644 --- a/app/assets/javascripts/discourse/models/category.js +++ b/app/assets/javascripts/discourse/models/category.js @@ -57,7 +57,8 @@ Discourse.Category = Discourse.Model.extend({ hotness: this.get('hotness'), secure: this.get('secure'), permissions: this.get('permissionsForUpdate'), - auto_close_days: this.get('auto_close_days') + auto_close_days: this.get('auto_close_days'), + position: this.get('position') }, type: this.get('id') ? 'PUT' : 'POST' }); diff --git a/app/assets/javascripts/discourse/templates/modal/edit_category.js.handlebars b/app/assets/javascripts/discourse/templates/modal/edit_category.js.handlebars index 900cb157a..e8bb4fd15 100644 --- a/app/assets/javascripts/discourse/templates/modal/edit_category.js.handlebars +++ b/app/assets/javascripts/discourse/templates/modal/edit_category.js.handlebars @@ -86,6 +86,11 @@ {{autoCloseForm autoCloseDays=auto_close_days labelKey="category.auto_close_label"}} +
+ + {{textField value=position}} +
+
{{view Discourse.HotnessView hotnessBinding="hotness"}} diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb index 16bd5bd9a..058db07dd 100644 --- a/app/controllers/categories_controller.rb +++ b/app/controllers/categories_controller.rb @@ -52,12 +52,17 @@ class CategoriesController < ApplicationController @category = Category.create(category_params.merge(user: current_user)) return render_json_error(@category) unless @category.save + @category.move_to(category_params[:position].to_i) if category_params[:position] + render_serialized(@category, CategorySerializer) end def update guardian.ensure_can_edit!(@category) - json_result(@category, serializer: CategorySerializer) { |cat| cat.update_attributes(category_params) } + json_result(@category, serializer: CategorySerializer) { |cat| + cat.update_attributes(category_params) + cat.move_to(category_params[:position].to_i) if category_params[:position] + } end def destroy @@ -74,17 +79,19 @@ class CategoriesController < ApplicationController end def category_params - required_param_keys.each do |key| - params.require(key) - end - - if p = params[:permissions] - p.each do |k,v| - p[k] = v.to_i + @category_params ||= begin + required_param_keys.each do |key| + params.require(key) end - end - params.permit(*required_param_keys, :hotness, :auto_close_days, :permissions => [*p.try(:keys)]) + if p = params[:permissions] + p.each do |k,v| + p[k] = v.to_i + end + end + + params.permit(*required_param_keys, :position, :hotness, :auto_close_days, :permissions => [*p.try(:keys)]) + end end def fetch_category diff --git a/app/serializers/category_serializer.rb b/app/serializers/category_serializer.rb index b565f2026..85f872404 100644 --- a/app/serializers/category_serializer.rb +++ b/app/serializers/category_serializer.rb @@ -1,6 +1,6 @@ class CategorySerializer < BasicCategorySerializer - attributes :read_restricted, :available_groups, :auto_close_days, :group_permissions + attributes :read_restricted, :available_groups, :auto_close_days, :group_permissions, :position def group_permissions @group_permissions ||= begin diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index c26c25794..e48234933 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -975,6 +975,7 @@ en: edit_permissions: "Edit Permissions" add_permission: "Add Permission" this_year: "this year" + position: "position" flagging: title: 'Why are you flagging this post?'