From cfedd42badc6b84457d1de35cb31988777462d5a Mon Sep 17 00:00:00 2001 From: Douglas Barbosa Alexandre Date: Tue, 20 Sep 2016 17:07:56 -0300 Subject: Add ProjectLabel model --- app/finders/issuable_finder.rb | 3 ++- app/helpers/labels_helper.rb | 8 ++++---- app/models/label.rb | 7 ------- app/models/project.rb | 6 ++++-- app/models/project_label.rb | 5 +++++ app/policies/label_policy.rb | 7 ------- app/policies/project_label_policy.rb | 5 +++++ app/views/projects/labels/_form.html.haml | 2 +- app/views/projects/labels/edit.html.haml | 2 +- app/views/projects/labels/new.html.haml | 2 +- 10 files changed, 23 insertions(+), 24 deletions(-) create mode 100644 app/models/project_label.rb delete mode 100644 app/policies/label_policy.rb create mode 100644 app/policies/project_label_policy.rb (limited to 'app') 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/label_policy.rb deleted file mode 100644 index 1677ad7f1bb..00000000000 --- a/app/policies/label_policy.rb +++ /dev/null @@ -1,7 +0,0 @@ -class LabelPolicy < BasePolicy - def rules - return unless @user - - can! :admin_label if Ability.allowed?(@user, :admin_label, @subject.project) - end -end diff --git a/app/policies/project_label_policy.rb b/app/policies/project_label_policy.rb new file mode 100644 index 00000000000..e7bd58372a6 --- /dev/null +++ b/app/policies/project_label_policy.rb @@ -0,0 +1,5 @@ +class ProjectLabelPolicy < BasePolicy + def rules + 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) -- cgit v1.2.1