summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDouglas Barbosa Alexandre <dbalexandre@gmail.com>2016-09-20 17:07:56 -0300
committerDouglas Barbosa Alexandre <dbalexandre@gmail.com>2016-10-19 14:58:24 -0200
commitcfedd42badc6b84457d1de35cb31988777462d5a (patch)
treeca0588b33e11d2f28104b320dcec6884ac148ec8
parente2dd75c0a2d863c8e530e54f3a0a015bdec1e84f (diff)
downloadgitlab-ce-cfedd42badc6b84457d1de35cb31988777462d5a.tar.gz
Add ProjectLabel model
-rw-r--r--app/finders/issuable_finder.rb3
-rw-r--r--app/helpers/labels_helper.rb8
-rw-r--r--app/models/label.rb7
-rw-r--r--app/models/project.rb6
-rw-r--r--app/models/project_label.rb5
-rw-r--r--app/policies/project_label_policy.rb (renamed from app/policies/label_policy.rb)4
-rw-r--r--app/views/projects/labels/_form.html.haml2
-rw-r--r--app/views/projects/labels/edit.html.haml2
-rw-r--r--app/views/projects/labels/new.html.haml2
-rw-r--r--db/migrate/20160920191518_set_project_label_type_on_labels.rb17
-rw-r--r--lib/banzai/filter/label_reference_filter.rb2
-rw-r--r--lib/gitlab/google_code_import/importer.rb2
-rw-r--r--spec/factories/labels.rb2
-rw-r--r--spec/models/label_spec.rb52
-rw-r--r--spec/models/project_label_spec.rb11
-rw-r--r--spec/models/project_spec.rb2
16 files changed, 75 insertions, 52 deletions
diff --git a/app/finders/issuable_finder.rb b/app/finders/issuable_finder.rb
index 37151f8d134..6f2adf47c3a 100644
--- a/app/finders/issuable_finder.rb
+++ b/app/finders/issuable_finder.rb
@@ -128,7 +128,8 @@ class IssuableFinder
@labels = Label.where(title: label_names)
if projects
- @labels = @labels.where(project: projects)
+ label_ids = LabelsFinder.new(current_user, project_id: projects).execute.select(:id)
+ @labels = @labels.where(labels: { id: label_ids })
end
else
@labels = Label.none
diff --git a/app/helpers/labels_helper.rb b/app/helpers/labels_helper.rb
index c14caa5e387..844bd3fd183 100644
--- a/app/helpers/labels_helper.rb
+++ b/app/helpers/labels_helper.rb
@@ -57,21 +57,21 @@ module LabelsHelper
def edit_label_path(label)
case label
when GroupLabel then edit_group_label_path(label.group, label)
- else edit_namespace_project_label_path(label.project.namespace, label.project, label)
+ when ProjectLabel then edit_namespace_project_label_path(label.project.namespace, label.project, label)
end
end
def destroy_label_path(label)
case label
when GroupLabel then group_label_path(label.group, label)
- else namespace_project_label_path(label.project.namespace, label.project, label)
+ when ProjectLabel then namespace_project_label_path(label.project.namespace, label.project, label)
end
end
def toggle_subscription_label_path(label)
case label
when GroupLabel then toggle_subscription_group_label_path(label.group, label)
- else toggle_subscription_namespace_project_label_path(label.project.namespace, label.project, label)
+ when ProjectLabel then toggle_subscription_namespace_project_label_path(label.project.namespace, label.project, label)
end
end
@@ -79,7 +79,7 @@ module LabelsHelper
title, icon =
case label
when GroupLabel then ['Group', 'folder-open']
- else ['Project', 'bookmark']
+ when ProjectLabel then ['Project', 'bookmark']
end
options[:class] ||= ''
diff --git a/app/models/label.rb b/app/models/label.rb
index be0c20479d5..0a68be7a30f 100644
--- a/app/models/label.rb
+++ b/app/models/label.rb
@@ -15,15 +15,12 @@ class Label < ActiveRecord::Base
default_value_for :color, DEFAULT_COLOR
- belongs_to :project
-
has_many :lists, dependent: :destroy
has_many :label_links, dependent: :destroy
has_many :issues, through: :label_links, source: :target, source_type: 'Issue'
has_many :merge_requests, through: :label_links, source: :target, source_type: 'MergeRequest'
validates :color, color: true, allow_blank: false
- validates :project, presence: true, if: :project_label?
# Don't allow ',' for label titles
validates :title, presence: true, format: { with: /\A[^,]+\z/ }
@@ -137,10 +134,6 @@ class Label < ActiveRecord::Base
.count
end
- def project_label?
- type.blank? && !template?
- end
-
def label_format_reference(format = :id)
raise StandardError, 'Unknown format' unless [:id, :name].include?(format)
diff --git a/app/models/project.rb b/app/models/project.rb
index db7301219e5..41125223044 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -107,7 +107,7 @@ class Project < ActiveRecord::Base
# Merge requests from source project should be kept when source project was removed
has_many :fork_merge_requests, foreign_key: 'source_project_id', class_name: MergeRequest
has_many :issues, dependent: :destroy
- has_many :labels, dependent: :destroy
+ has_many :labels, dependent: :destroy, class_name: 'ProjectLabel'
has_many :services, dependent: :destroy
has_many :events, dependent: :destroy
has_many :milestones, dependent: :destroy
@@ -730,8 +730,10 @@ class Project < ActiveRecord::Base
def create_labels
Label.templates.each do |label|
label = label.dup
- label.template = nil
+ label.template = false
label.project_id = self.id
+ label.type = 'ProjectLabel'
+
label.save
end
end
diff --git a/app/models/project_label.rb b/app/models/project_label.rb
new file mode 100644
index 00000000000..3e41113e340
--- /dev/null
+++ b/app/models/project_label.rb
@@ -0,0 +1,5 @@
+class ProjectLabel < Label
+ belongs_to :project
+
+ validates :project, presence: true
+end
diff --git a/app/policies/label_policy.rb b/app/policies/project_label_policy.rb
index 1677ad7f1bb..e7bd58372a6 100644
--- a/app/policies/label_policy.rb
+++ b/app/policies/project_label_policy.rb
@@ -1,7 +1,5 @@
-class LabelPolicy < BasePolicy
+class ProjectLabelPolicy < BasePolicy
def rules
- return unless @user
-
can! :admin_label if Ability.allowed?(@user, :admin_label, @subject.project)
end
end
diff --git a/app/views/projects/labels/_form.html.haml b/app/views/projects/labels/_form.html.haml
index 5f7be074f25..28a062c7eb5 100644
--- a/app/views/projects/labels/_form.html.haml
+++ b/app/views/projects/labels/_form.html.haml
@@ -1,4 +1,4 @@
-= form_for [@project.namespace.becomes(Namespace), @project, @label], html: { class: 'form-horizontal label-form js-quick-submit js-requires-input' } do |f|
+= form_for @label, as: :label, url: url, html: { class: 'form-horizontal label-form js-quick-submit js-requires-input' } do |f|
= form_errors(@label)
.form-group
diff --git a/app/views/projects/labels/edit.html.haml b/app/views/projects/labels/edit.html.haml
index c9ec371c3e1..372abcb8773 100644
--- a/app/views/projects/labels/edit.html.haml
+++ b/app/views/projects/labels/edit.html.haml
@@ -7,4 +7,4 @@
= icon('bookmark')
Edit Project Label
%hr
- = render 'form'
+ = render 'form', url: namespace_project_label_path(@project.namespace.becomes(Namespace), @project, @label)
diff --git a/app/views/projects/labels/new.html.haml b/app/views/projects/labels/new.html.haml
index a1e2df6c55d..f170c41bfc4 100644
--- a/app/views/projects/labels/new.html.haml
+++ b/app/views/projects/labels/new.html.haml
@@ -7,4 +7,4 @@
= icon('bookmark')
New Project Label
%hr
- = render 'form'
+ = render 'form', url: namespace_project_labels_path(@project.namespace.becomes(Namespace), @project)
diff --git a/db/migrate/20160920191518_set_project_label_type_on_labels.rb b/db/migrate/20160920191518_set_project_label_type_on_labels.rb
new file mode 100644
index 00000000000..af47d0320e2
--- /dev/null
+++ b/db/migrate/20160920191518_set_project_label_type_on_labels.rb
@@ -0,0 +1,17 @@
+class SetProjectLabelTypeOnLabels < ActiveRecord::Migration
+ include Gitlab::Database::MigrationHelpers
+
+ DOWNTIME = false
+
+ def up
+ update_column_in_batches(:labels, :type, 'ProjectLabel') do |table, query|
+ query.where(table[:project_id].not_eq(nil))
+ end
+ end
+
+ def down
+ update_column_in_batches(:labels, :type, nil) do |table, query|
+ query.where(table[:project_id].not_eq(nil))
+ end
+ end
+end
diff --git a/lib/banzai/filter/label_reference_filter.rb b/lib/banzai/filter/label_reference_filter.rb
index 3a09912f1be..4c4784b0052 100644
--- a/lib/banzai/filter/label_reference_filter.rb
+++ b/lib/banzai/filter/label_reference_filter.rb
@@ -70,7 +70,7 @@ module Banzai
end
def object_link_text(object, matches)
- if object.project.nil? || object.project == context[:project]
+ if object.is_a?(GroupLabel) || object.project == context[:project]
LabelsHelper.render_colored_label(object)
else
LabelsHelper.render_colored_cross_project_label(object)
diff --git a/lib/gitlab/google_code_import/importer.rb b/lib/gitlab/google_code_import/importer.rb
index 62da327931f..ef8c3e35619 100644
--- a/lib/gitlab/google_code_import/importer.rb
+++ b/lib/gitlab/google_code_import/importer.rb
@@ -237,7 +237,7 @@ module Gitlab
def create_label(name)
color = nice_label_color(name)
- Label.create!(project_id: project.id, name: name, color: color)
+ project.labels.create!(name: name, color: color)
end
def format_content(raw_content)
diff --git a/spec/factories/labels.rb b/spec/factories/labels.rb
index eb489099854..ec4c56457ea 100644
--- a/spec/factories/labels.rb
+++ b/spec/factories/labels.rb
@@ -1,5 +1,5 @@
FactoryGirl.define do
- factory :label do
+ factory :label, class: ProjectLabel do
sequence(:title) { |n| "label#{n}" }
color "#990000"
project
diff --git a/spec/models/label_spec.rb b/spec/models/label_spec.rb
index 894021dc8e6..1f1fe45d5a7 100644
--- a/spec/models/label_spec.rb
+++ b/spec/models/label_spec.rb
@@ -1,47 +1,41 @@
require 'spec_helper'
describe Label, models: true do
- let(:label) { create(:label) }
+ describe 'modules' do
+ it { is_expected.to include_module(Referable) }
+ it { is_expected.to include_module(Subscribable) }
+ end
describe 'associations' do
- it { is_expected.to belong_to(:project) }
-
- it { is_expected.to have_many(:label_links).dependent(:destroy) }
it { is_expected.to have_many(:issues).through(:label_links).source(:target) }
+ it { is_expected.to have_many(:label_links).dependent(:destroy) }
it { is_expected.to have_many(:lists).dependent(:destroy) }
end
- describe 'modules' do
- subject { described_class }
-
- it { is_expected.to include_module(Referable) }
- end
-
describe 'validation' do
- it { is_expected.to validate_presence_of(:project) }
it { is_expected.to validate_uniqueness_of(:title) }
it 'validates color code' do
- expect(label).not_to allow_value('G-ITLAB').for(:color)
- expect(label).not_to allow_value('AABBCC').for(:color)
- expect(label).not_to allow_value('#AABBCCEE').for(:color)
- expect(label).not_to allow_value('GGHHII').for(:color)
- expect(label).not_to allow_value('#').for(:color)
- expect(label).not_to allow_value('').for(:color)
-
- expect(label).to allow_value('#AABBCC').for(:color)
- expect(label).to allow_value('#abcdef').for(:color)
+ is_expected.not_to allow_value('G-ITLAB').for(:color)
+ is_expected.not_to allow_value('AABBCC').for(:color)
+ is_expected.not_to allow_value('#AABBCCEE').for(:color)
+ is_expected.not_to allow_value('GGHHII').for(:color)
+ is_expected.not_to allow_value('#').for(:color)
+ is_expected.not_to allow_value('').for(:color)
+
+ is_expected.to allow_value('#AABBCC').for(:color)
+ is_expected.to allow_value('#abcdef').for(:color)
end
it 'validates title' do
- expect(label).not_to allow_value('G,ITLAB').for(:title)
- expect(label).not_to allow_value('').for(:title)
-
- expect(label).to allow_value('GITLAB').for(:title)
- expect(label).to allow_value('gitlab').for(:title)
- expect(label).to allow_value('G?ITLAB').for(:title)
- expect(label).to allow_value('G&ITLAB').for(:title)
- expect(label).to allow_value("customer's request").for(:title)
+ is_expected.not_to allow_value('G,ITLAB').for(:title)
+ is_expected.not_to allow_value('').for(:title)
+
+ is_expected.to allow_value('GITLAB').for(:title)
+ is_expected.to allow_value('gitlab').for(:title)
+ is_expected.to allow_value('G?ITLAB').for(:title)
+ is_expected.to allow_value('G&ITLAB').for(:title)
+ is_expected.to allow_value("customer's request").for(:title)
end
end
@@ -53,6 +47,8 @@ describe Label, models: true do
end
describe '#to_reference' do
+ let(:label) { create(:label) }
+
context 'using id' do
it 'returns a String reference to the object' do
expect(label.to_reference).to eq "~#{label.id}"
diff --git a/spec/models/project_label_spec.rb b/spec/models/project_label_spec.rb
new file mode 100644
index 00000000000..93062b9d402
--- /dev/null
+++ b/spec/models/project_label_spec.rb
@@ -0,0 +1,11 @@
+require 'spec_helper'
+
+describe ProjectLabel, models: true do
+ describe 'relationships' do
+ it { is_expected.to belong_to(:project) }
+ end
+
+ describe 'validations' do
+ it { is_expected.to validate_presence_of(:project) }
+ end
+end
diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb
index 67dbcc362f6..e6d98e25d0b 100644
--- a/spec/models/project_spec.rb
+++ b/spec/models/project_spec.rb
@@ -56,7 +56,7 @@ describe Project, models: true do
it { is_expected.to have_many(:runners) }
it { is_expected.to have_many(:variables) }
it { is_expected.to have_many(:triggers) }
- it { is_expected.to have_many(:labels).dependent(:destroy) }
+ it { is_expected.to have_many(:labels).class_name('ProjectLabel').dependent(:destroy) }
it { is_expected.to have_many(:users_star_projects).dependent(:destroy) }
it { is_expected.to have_many(:environments).dependent(:destroy) }
it { is_expected.to have_many(:deployments).dependent(:destroy) }