2016-06-14 14:36:36 -04:00
|
|
|
class NotificationSettingsController < ApplicationController
|
|
|
|
before_action :authenticate_user!
|
|
|
|
|
|
|
|
def create
|
2016-06-21 15:50:13 -04:00
|
|
|
return render_404 unless can_read?(resource)
|
|
|
|
|
|
|
|
@notification_setting = current_user.notification_settings_for(resource)
|
2018-07-02 06:43:06 -04:00
|
|
|
@saved = @notification_setting.update(notification_setting_params)
|
2016-06-14 14:36:36 -04:00
|
|
|
|
|
|
|
render_response
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
@notification_setting = current_user.notification_settings.find(params[:id])
|
2018-07-02 06:43:06 -04:00
|
|
|
@saved = @notification_setting.update(notification_setting_params)
|
2016-06-14 14:36:36 -04:00
|
|
|
|
|
|
|
render_response
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-06-22 09:50:19 -04: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 15:50:13 -04: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
|
|
|
|
|
2016-06-14 14:36:36 -04:00
|
|
|
def render_response
|
|
|
|
render json: {
|
2016-06-17 11:38:49 -04:00
|
|
|
html: view_to_html_string("shared/notifications/_button", notification_setting: @notification_setting),
|
2016-06-14 14:36:36 -04:00
|
|
|
saved: @saved
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def notification_setting_params
|
|
|
|
allowed_fields = NotificationSetting::EMAIL_EVENTS.dup
|
|
|
|
allowed_fields << :level
|
|
|
|
params.require(:notification_setting).permit(allowed_fields)
|
|
|
|
end
|
|
|
|
end
|