diff options
author | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-10-17 16:34:22 -0200 |
---|---|---|
committer | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-10-19 14:58:27 -0200 |
commit | 530aae9080942646b130510e970d9d82c009d8e5 (patch) | |
tree | 149a06878ac9f335ead5db39af4328413bdc8ab1 /app | |
parent | 074c964913218e99c42f0d8b5855c4ad2ad93267 (diff) | |
download | gitlab-ce-530aae9080942646b130510e970d9d82c009d8e5.tar.gz |
Abstract LabelPriority away into methods on Label model
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/projects/labels_controller.rb | 14 | ||||
-rw-r--r-- | app/models/label.rb | 16 |
2 files changed, 21 insertions, 9 deletions
diff --git a/app/controllers/projects/labels_controller.rb b/app/controllers/projects/labels_controller.rb index f453b70fb5d..42fd09e9b7e 100644 --- a/app/controllers/projects/labels_controller.rb +++ b/app/controllers/projects/labels_controller.rb @@ -84,7 +84,7 @@ class Projects::LabelsController < Projects::ApplicationController respond_to do |format| label = @available_labels.find(params[:id]) - if label.priorities.where(project_id: project).delete_all + if label.unprioritize!(project) format.json { render json: label } else format.json { head :unprocessable_entity } @@ -94,14 +94,12 @@ class Projects::LabelsController < Projects::ApplicationController def set_priorities Label.transaction do - label_ids = @available_labels.where(id: params[:label_ids]).pluck(:id) + available_labels_ids = @available_labels.where(id: params[:label_ids]).pluck(:id) + label_ids = params[:label_ids].select { |id| available_labels_ids.include?(id.to_i) } - params[:label_ids].each_with_index do |label_id, index| - next unless label_ids.include?(label_id.to_i) - - label_priority = LabelPriority.find_or_initialize_by(project_id: @project.id, label_id: label_id) - label_priority.priority = index - label_priority.save! + label_ids.each_with_index do |label_id, index| + label = @available_labels.find(label_id) + label.prioritize!(project, index) end end diff --git a/app/models/label.rb b/app/models/label.rb index 6fd45d251a8..ae07e8f60e1 100644 --- a/app/models/label.rb +++ b/app/models/label.rb @@ -97,6 +97,20 @@ class Label < ActiveRecord::Base merge_requests_count(user, project_id: project.try(:id) || project_id, state: 'opened') end + def prioritize!(project, value) + label_priority = priorities.find_or_initialize_by(project_id: project.id) + label_priority.priority = value + label_priority.save! + end + + def unprioritize!(project) + priorities.where(project: project).delete_all + end + + def priority(project) + priorities.find_by(project: project).try(:priority) + end + def template? template end @@ -135,7 +149,7 @@ class Label < ActiveRecord::Base def as_json(options = {}) super(options).tap do |json| - json[:priority] = priorities.find_by(project: options[:project]).try(:priority) if options.has_key?(:project) + json[:priority] = priority(options[:project]) if options.has_key?(:project) end end |