Allow users to subscribe to group labels at project-level

parent b34c063e
...@@ -3,7 +3,7 @@ class Projects::LabelsController < Projects::ApplicationController ...@@ -3,7 +3,7 @@ class Projects::LabelsController < Projects::ApplicationController
before_action :module_enabled before_action :module_enabled
before_action :label, only: [:edit, :update, :destroy] 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_read_label!
before_action :authorize_admin_labels!, only: [:new, :create, :edit, :update, before_action :authorize_admin_labels!, only: [:new, :create, :edit, :update,
:generate, :destroy, :remove_priority, :generate, :destroy, :remove_priority,
...@@ -123,7 +123,10 @@ class Projects::LabelsController < Projects::ApplicationController ...@@ -123,7 +123,10 @@ class Projects::LabelsController < Projects::ApplicationController
def label def label
@label ||= @project.labels.find(params[:id]) @label ||= @project.labels.find(params[:id])
end end
alias_method :subscribable_resource, :label
def subscribable_resource
@available_labels.find(params[:id])
end
def subscribable_project def subscribable_project
@project @project
......
...@@ -68,11 +68,9 @@ module LabelsHelper ...@@ -68,11 +68,9 @@ module LabelsHelper
end end
end end
def toggle_subscription_data(label) def toggle_subscription_data(label, project)
return unless label.is_a?(ProjectLabel)
{ {
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 end
...@@ -149,17 +147,11 @@ module LabelsHelper ...@@ -149,17 +147,11 @@ module LabelsHelper
end end
def label_subscription_status(label, project) def label_subscription_status(label, project)
case label label.subscribed?(current_user, project) ? 'subscribed' : 'unsubscribed'
when GroupLabel then 'Subscribing to group labels is currently not supported.'
when ProjectLabel then label.subscribed?(current_user, project) ? 'subscribed' : 'unsubscribed'
end
end end
def label_subscription_toggle_button_text(label, project) def label_subscription_toggle_button_text(label, project)
case label label.subscribed?(current_user, project) ? 'Unsubscribe' : 'Subscribe'
when GroupLabel then 'Subscribing to group labels is currently not supported.'
when ProjectLabel then label.subscribed?(current_user, project) ? 'Unsubscribe' : 'Subscribe'
end
end end
def label_deletion_confirm_text(label) def label_deletion_confirm_text(label)
......
...@@ -18,8 +18,8 @@ ...@@ -18,8 +18,8 @@
%li %li
= link_to_label(label, subject: subject) do = link_to_label(label, subject: subject) do
= pluralize open_issues_count, 'open issue' = pluralize open_issues_count, 'open issue'
- if current_user - if current_user && defined?(@project)
%li.label-subscription{ data: toggle_subscription_data(label) } %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) } } %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) %span= label_subscription_toggle_button_text(label, @project)
- if can?(current_user, :admin_label, label) - if can?(current_user, :admin_label, label)
...@@ -34,11 +34,11 @@ ...@@ -34,11 +34,11 @@
= link_to_label(label, subject: subject, css_class: 'btn btn-transparent btn-action') do = link_to_label(label, subject: subject, css_class: 'btn btn-transparent btn-action') do
= pluralize open_issues_count, 'open issue' = pluralize open_issues_count, 'open issue'
- if current_user - if current_user && defined?(@project)
.label-subscription.inline{ data: toggle_subscription_data(label) } .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) } } %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) %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') = icon('spinner spin', class: 'label-subscribe-button-loading')
- if can?(current_user, :admin_label, label) - if can?(current_user, :admin_label, label)
...@@ -49,6 +49,6 @@ ...@@ -49,6 +49,6 @@
%span.sr-only Delete %span.sr-only Delete
= icon('trash-o') = icon('trash-o')
- if current_user && label.is_a?(ProjectLabel) - if current_user && defined?(@project)
:javascript :javascript
new Subscription('##{dom_id(label)} .label-subscription'); new Subscription('##{dom_id(label)} .label-subscription');
...@@ -72,14 +72,8 @@ describe Projects::LabelsController do ...@@ -72,14 +72,8 @@ describe Projects::LabelsController do
end end
describe 'POST #generate' do describe 'POST #generate' do
let(:admin) { create(:admin) }
before do
sign_in(admin)
end
context 'personal project' do context 'personal project' do
let(:personal_project) { create(:empty_project) } let(:personal_project) { create(:empty_project, namespace: user.namespace) }
it 'creates labels' do it 'creates labels' do
post :generate, namespace_id: personal_project.namespace.to_param, project_id: personal_project.to_param post :generate, namespace_id: personal_project.namespace.to_param, project_id: personal_project.to_param
...@@ -96,4 +90,22 @@ describe Projects::LabelsController do ...@@ -96,4 +90,22 @@ describe Projects::LabelsController do
end end
end 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 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