From 88a51b128fe6a1f079337a22f5befe62d9716ea0 Mon Sep 17 00:00:00 2001 From: Toru Takahashi Date: Tue, 14 Aug 2018 10:15:42 +0900 Subject: [PATCH] [phase-20]Make category index reorder --- app/controllers/glossary_categories_controller.rb | 14 +++++++++++--- app/models/glossary_category.rb | 5 +++++ app/views/glossary_categories/index.html.erb | 10 +++++++++- .../006_add_position_to_glossary_categories.rb | 6 ++++++ 4 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 db/migrate/006_add_position_to_glossary_categories.rb diff --git a/app/controllers/glossary_categories_controller.rb b/app/controllers/glossary_categories_controller.rb index 78e57d9..003b0c0 100644 --- a/app/controllers/glossary_categories_controller.rb +++ b/app/controllers/glossary_categories_controller.rb @@ -4,7 +4,7 @@ class GlossaryCategoriesController < ApplicationController before_action :find_project_by_project_id, :authorize def index - @categories = GlossaryCategory.where(project_id: @project.id) + @categories = GlossaryCategory.where(project_id: @project.id).sorted end def show @@ -28,7 +28,15 @@ class GlossaryCategoriesController < ApplicationController def update @category.attributes = glossary_category_params if @category.save - redirect_to [@project, @category], notice: l(:notice_successful_update) + respond_to do |format| + format.html { + redirect_to [@project, @category], + notice: l(:notice_successful_update) + } + format.js { + head 200 + } + end end rescue ActiveRecord::StaleObjectError flash.now[:error] = l(:notice_locking_conflict) @@ -50,7 +58,7 @@ class GlossaryCategoriesController < ApplicationController def glossary_category_params params.require(:glossary_category).permit( - :name + :name, :position ) end end diff --git a/app/models/glossary_category.rb b/app/models/glossary_category.rb index 43bbade..bbc237f 100644 --- a/app/models/glossary_category.rb +++ b/app/models/glossary_category.rb @@ -1,4 +1,9 @@ class GlossaryCategory < ActiveRecord::Base has_many :terms, class_name: 'GlossaryTerm', foreign_key: 'category_id' belongs_to :project + + acts_as_positioned + + scope :sorted, lambda { order(:position) } + end diff --git a/app/views/glossary_categories/index.html.erb b/app/views/glossary_categories/index.html.erb index 2dc9c5d..54062b7 100644 --- a/app/views/glossary_categories/index.html.erb +++ b/app/views/glossary_categories/index.html.erb @@ -8,11 +8,12 @@ <%= render partial: 'glossary_terms/sidebar' %> - +
+ @@ -20,7 +21,14 @@ + <% end %>
# <%=l :field_name %>
<%= category.id %> <%= link_to category.name, [@project, category] %> + <%= reorder_handle(category, url: project_glossary_category_path(@project, category)) %> +
+ +<%= javascript_tag do %> + $(function() { $("table.table-sortable tbody").positionedItems(); }); +<% end %> diff --git a/db/migrate/006_add_position_to_glossary_categories.rb b/db/migrate/006_add_position_to_glossary_categories.rb new file mode 100644 index 0000000..e006c64 --- /dev/null +++ b/db/migrate/006_add_position_to_glossary_categories.rb @@ -0,0 +1,6 @@ +class AddPositionToGlossaryCategories < ActiveRecord::Migration[5.2] + + def change + add_column :glossary_categories, :position, :integer, default: nil + end +end