2018-09-14 05:42:05 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-14 18:36:36 +00:00
|
|
|
class NotificationSettingsController < ApplicationController
|
|
|
|
before_action :authenticate_user!
|
|
|
|
|
|
|
|
def create
|
2016-06-21 19:50:13 +00:00
|
|
|
return render_404 unless can_read?(resource)
|
|
|
|
|
|
|
|
@notification_setting = current_user.notification_settings_for(resource)
|
2018-07-23 02:42:19 +00:00
|
|
|
@saved = @notification_setting.update(notification_setting_params_for(resource))
|
2016-06-14 18:36:36 +00:00
|
|
|
|
|
|
|
render_response
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
@notification_setting = current_user.notification_settings.find(params[:id])
|
2018-07-23 02:42:19 +00:00
|
|
|
@saved = @notification_setting.update(notification_setting_params_for(@notification_setting.source))
|
2016-06-14 18:36:36 +00:00
|
|
|
|
2018-12-07 14:11:42 +00:00
|
|
|
if params[:hide_label].present?
|
|
|
|
render_response("projects/buttons/_notifications")
|
|
|
|
else
|
|
|
|
render_response
|
|
|
|
end
|
2016-06-14 18:36:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-06-22 13:50:19 +00:00
|
|
|
def resource
|
|
|
|
@resource ||=
|
|
|
|
if params[:project_id].present?
|
|
|
|
Project.find(params[:project_id])
|
|
|
|
elsif params[:namespace_id].present?
|
|
|
|
Group.find(params[:namespace_id])
|
2016-06-21 19:50:13 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_read?(resource)
|
|
|
|
ability_name = resource.class.name.downcase
|
|
|
|
ability_name = "read_#{ability_name}".to_sym
|
|
|
|
|
|
|
|
can?(current_user, ability_name, resource)
|
|
|
|
end
|
|
|
|
|
2018-12-07 14:11:42 +00:00
|
|
|
def render_response(response_template = "shared/notifications/_button")
|
2016-06-14 18:36:36 +00:00
|
|
|
render json: {
|
2018-12-07 14:11:42 +00:00
|
|
|
html: view_to_html_string(response_template, notification_setting: @notification_setting),
|
2016-06-14 18:36:36 +00:00
|
|
|
saved: @saved
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-07-23 02:42:19 +00:00
|
|
|
def notification_setting_params_for(source)
|
|
|
|
allowed_fields = NotificationSetting.email_events(source).dup
|
2016-06-14 18:36:36 +00:00
|
|
|
allowed_fields << :level
|
|
|
|
params.require(:notification_setting).permit(allowed_fields)
|
|
|
|
end
|
|
|
|
end
|