Allow users to subscribe to group labels at project-level

parent b34c063e
......@@ -3,7 +3,7 @@ class Projects::LabelsController < Projects::ApplicationController
before_action :module_enabled
before_action :label, only: [:edit, :update, :destroy]
before_action :find_labels, only: [:index, :set_priorities, :remove_priority]
before_action :find_labels, only: [:index, :set_priorities, :remove_priority, :toggle_subscription]
before_action :authorize_read_label!
before_action :authorize_admin_labels!, only: [:new, :create, :edit, :update,
:generate, :destroy, :remove_priority,
......@@ -123,7 +123,10 @@ class Projects::LabelsController < Projects::ApplicationController
def label
@label ||= @project.labels.find(params[:id])
end
alias_method :subscribable_resource, :label
def subscribable_resource
@available_labels.find(params[:id])
end
def subscribable_project
@project
......
......@@ -68,11 +68,9 @@ module LabelsHelper
end
end
def toggle_subscription_data(label)
return unless label.is_a?(ProjectLabel)
def toggle_subscription_data(label, project)
{
url: toggle_subscription_namespace_project_label_path(label.project.namespace, label.project, label)
url: toggle_subscription_namespace_project_label_path(project.namespace, project, label)
}
end
......@@ -149,17 +147,11 @@ module LabelsHelper
end
def label_subscription_status(label, project)
case label
when GroupLabel then 'Subscribing to group labels is currently not supported.'
when ProjectLabel then label.subscribed?(current_user, project) ? 'subscribed' : 'unsubscribed'
end
label.subscribed?(current_user, project) ? 'subscribed' : 'unsubscribed'
end
def label_subscription_toggle_button_text(label, project)
case label
when GroupLabel then 'Subscribing to group labels is currently not supported.'
when ProjectLabel then label.subscribed?(current_user, project) ? 'Unsubscribe' : 'Subscribe'
end
label.subscribed?(current_user, project) ? 'Unsubscribe' : 'Subscribe'
end
def label_deletion_confirm_text(label)
......
......@@ -18,8 +18,8 @@
%li
= link_to_label(label, subject: subject) do
= pluralize open_issues_count, 'open issue'
- if current_user
%li.label-subscription{ data: toggle_subscription_data(label) }
- if current_user && defined?(@project)
%li.label-subscription{ data: toggle_subscription_data(label, @project) }
%a.js-subscribe-button.label-subscribe-button.subscription-status{ role: "button", href: "#", data: { toggle: "tooltip", status: label_subscription_status(label, @project) } }
%span= label_subscription_toggle_button_text(label, @project)
- if can?(current_user, :admin_label, label)
......@@ -34,11 +34,11 @@
= link_to_label(label, subject: subject, css_class: 'btn btn-transparent btn-action') do
= pluralize open_issues_count, 'open issue'
- if current_user
.label-subscription.inline{ data: toggle_subscription_data(label) }
- if current_user && defined?(@project)
.label-subscription.inline{ data: toggle_subscription_data(label, @project) }
%button.js-subscribe-button.label-subscribe-button.btn.btn-transparent.btn-action.subscription-status{ type: "button", title: label_subscription_toggle_button_text(label, @project), data: { toggle: "tooltip", status: label_subscription_status(label, @project) } }
%span.sr-only= label_subscription_toggle_button_text(label, @project)
= icon('eye', class: 'label-subscribe-button-icon', disabled: label.is_a?(GroupLabel))
= icon('eye', class: 'label-subscribe-button-icon')
= icon('spinner spin', class: 'label-subscribe-button-loading')
- if can?(current_user, :admin_label, label)
......@@ -49,6 +49,6 @@
%span.sr-only Delete
= icon('trash-o')
- if current_user && label.is_a?(ProjectLabel)
- if current_user && defined?(@project)
:javascript
new Subscription('##{dom_id(label)} .label-subscription');
......@@ -72,14 +72,8 @@ describe Projects::LabelsController do
end
describe 'POST #generate' do
let(:admin) { create(:admin) }
before do
sign_in(admin)
end
context 'personal project' do
let(:personal_project) { create(:empty_project) }
let(:personal_project) { create(:empty_project, namespace: user.namespace) }
it 'creates labels' do
post :generate, namespace_id: personal_project.namespace.to_param, project_id: personal_project.to_param
......@@ -96,4 +90,22 @@ describe Projects::LabelsController do
end
end
end
describe 'POST #toggle_subscription' do
it 'allows user to toggle subscription on project labels' do
label = create(:label, project: project)
post :toggle_subscription, namespace_id: project.namespace.to_param, project_id: project.to_param, id: label.id
expect(response).to have_http_status(200)
end
it 'allows user to toggle subscription on group labels' do
group_label = create(:group_label, group: group)
post :toggle_subscription, namespace_id: project.namespace.to_param, project_id: project.to_param, id: group_label.id
expect(response).to have_http_status(200)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment