notification_settings_controller.rb 1017 Bytes
Newer Older
1 2 3 4
class NotificationSettingsController < ApplicationController
  before_action :authenticate_user!

  def create
5
    project = Project.find(params[:project][:id])
6

7 8 9 10 11 12
    return render_404 unless can?(current_user, :read_project, project)

    @notification_setting = current_user.notification_settings_for(project)
    @saved = @notification_setting.update_attributes(notification_setting_params)

    render_response
13 14 15 16 17 18 19 20 21 22 23 24 25
  end

  def update
    @notification_setting = current_user.notification_settings.find(params[:id])
    @saved = @notification_setting.update_attributes(notification_setting_params)

    render_response
  end

  private

  def render_response
    render json: {
26
      html: view_to_html_string("shared/notifications/_button", notification_setting: @notification_setting),
27 28 29 30 31 32 33 34 35 36
      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