2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
class NotificationSettingsController < ApplicationController
|
|
|
|
before_action :authenticate_user!
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
feature_category :users
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
def create
|
2016-08-24 12:49:21 +05:30
|
|
|
return render_404 unless can_read?(resource)
|
2016-06-22 15:30:34 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
@notification_setting = current_user.notification_settings_for(resource)
|
2018-11-20 20:47:30 +05:30
|
|
|
@saved = @notification_setting.update(notification_setting_params_for(resource))
|
2016-06-22 15:30:34 +05:30
|
|
|
|
|
|
|
render_response
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
@notification_setting = current_user.notification_settings.find(params[:id])
|
2018-11-20 20:47:30 +05:30
|
|
|
@saved = @notification_setting.update(notification_setting_params_for(@notification_setting.source))
|
2016-06-22 15:30:34 +05:30
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
render_response
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def resource
|
|
|
|
@resource ||=
|
|
|
|
if params[:project_id].present?
|
|
|
|
Project.find(params[:project_id])
|
|
|
|
elsif params[:namespace_id].present?
|
|
|
|
Group.find(params[:namespace_id])
|
|
|
|
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
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
def render_response
|
|
|
|
btn_class = nil
|
|
|
|
|
|
|
|
if params[:hide_label].present?
|
|
|
|
btn_class = 'btn-xs' if params[:project_id].present?
|
|
|
|
response_template = 'shared/notifications/_new_button'
|
|
|
|
else
|
|
|
|
response_template = 'shared/notifications/_button'
|
|
|
|
end
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
render json: {
|
2019-03-02 22:35:43 +05:30
|
|
|
html: view_to_html_string(response_template, notification_setting: @notification_setting, btn_class: btn_class),
|
2016-06-22 15:30:34 +05:30
|
|
|
saved: @saved
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
def notification_setting_params_for(source)
|
2019-12-21 20:55:43 +05:30
|
|
|
params.require(:notification_setting).permit(NotificationSetting.allowed_fields(source))
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
end
|