From 67314e95ae836365fa1989439a6379aac781a0b4 Mon Sep 17 00:00:00 2001 From: Douglas Barbosa Alexandre Date: Fri, 14 Oct 2016 18:32:44 -0300 Subject: Add support to group labels prioritization on project level --- app/controllers/projects/labels_controller.rb | 13 +++++++------ app/models/label.rb | 10 ++++++---- spec/features/projects/labels/update_prioritization_spec.rb | 8 ++++---- spec/models/label_priority_spec.rb | 1 - 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/app/controllers/projects/labels_controller.rb b/app/controllers/projects/labels_controller.rb index f4ad503c9a6..f453b70fb5d 100644 --- a/app/controllers/projects/labels_controller.rb +++ b/app/controllers/projects/labels_controller.rb @@ -12,8 +12,8 @@ class Projects::LabelsController < Projects::ApplicationController respond_to :js, :html def index - @prioritized_labels = @available_labels.prioritized - @labels = @available_labels.unprioritized.page(params[:page]) + @prioritized_labels = @available_labels.prioritized(@project) + @labels = @available_labels.unprioritized(@project).page(params[:page]) respond_to do |format| format.html @@ -84,11 +84,10 @@ class Projects::LabelsController < Projects::ApplicationController respond_to do |format| label = @available_labels.find(params[:id]) - if label.update_attribute(:priority, nil) + if label.priorities.where(project_id: project).delete_all format.json { render json: label } else - message = label.errors.full_messages.uniq.join('. ') - format.json { render json: { message: message }, status: :unprocessable_entity } + format.json { head :unprocessable_entity } end end end @@ -100,7 +99,9 @@ class Projects::LabelsController < Projects::ApplicationController params[:label_ids].each_with_index do |label_id, index| next unless label_ids.include?(label_id.to_i) - Label.where(id: label_id).update_all(priority: index) + label_priority = LabelPriority.find_or_initialize_by(project_id: @project.id, label_id: label_id) + label_priority.priority = index + label_priority.save! end end diff --git a/app/models/label.rb b/app/models/label.rb index 3ce4e253035..ea11d9d7864 100644 --- a/app/models/label.rb +++ b/app/models/label.rb @@ -32,12 +32,14 @@ class Label < ActiveRecord::Base scope :templates, -> { where(template: true) } scope :with_title, ->(title) { where(title: title) } - def self.prioritized - where.not(priority: nil).reorder(:priority, :title) + def self.prioritized(project) + joins(:priorities) + .where(label_priorities: { project_id: project }) + .reorder('label_priorities.priority ASC, labels.title ASC') end - def self.unprioritized - where(priority: nil) + def self.unprioritized(project) + where.not(id: prioritized(project).select(:id)) end alias_attribute :name, :title diff --git a/spec/features/projects/labels/update_prioritization_spec.rb b/spec/features/projects/labels/update_prioritization_spec.rb index 84a12a38c26..c9fa8315e79 100644 --- a/spec/features/projects/labels/update_prioritization_spec.rb +++ b/spec/features/projects/labels/update_prioritization_spec.rb @@ -35,7 +35,7 @@ feature 'Prioritize labels', feature: true do end scenario 'user can unprioritize a group label', js: true do - feature.update(priority: 1) + create(:label_priority, project: project, label: feature, priority: 1) visit namespace_project_labels_path(project.namespace, project) @@ -70,7 +70,7 @@ feature 'Prioritize labels', feature: true do end scenario 'user can unprioritize a project label', js: true do - bug.update(priority: 1) + create(:label_priority, project: project, label: bug, priority: 1) visit namespace_project_labels_path(project.namespace, project) @@ -89,8 +89,8 @@ feature 'Prioritize labels', feature: true do end scenario 'user can sort prioritized labels and persist across reloads', js: true do - bug.update(priority: 1) - feature.update(priority: 2) + create(:label_priority, project: project, label: bug, priority: 1) + create(:label_priority, project: project, label: feature, priority: 2) visit namespace_project_labels_path(project.namespace, project) diff --git a/spec/models/label_priority_spec.rb b/spec/models/label_priority_spec.rb index 5f7fa3aa047..d18c2f7949a 100644 --- a/spec/models/label_priority_spec.rb +++ b/spec/models/label_priority_spec.rb @@ -9,7 +9,6 @@ describe LabelPriority, models: true do describe 'validations' do it { is_expected.to validate_presence_of(:project) } it { is_expected.to validate_presence_of(:label) } - it { is_expected.to validate_presence_of(:priority) } it { is_expected.to validate_numericality_of(:priority).only_integer.is_greater_than_or_equal_to(0) } it 'validates uniqueness of label_id scoped to project_id' do -- cgit v1.2.1