blocks_controller.rb 927 Bytes
Newer Older
1 2
# frozen_string_literal: true

Dan Hansen's avatar
Dan Hansen committed
3
class BlocksController < ApplicationController
4
  before_action :authenticate_user!
Dan Hansen's avatar
Dan Hansen committed
5 6

  def create
7
    block = current_user.blocks.new(block_params)
8

9
    disconnect_if_contact(block.person) if block.save
10

11
    respond_to do |format|
12
      format.json { head :no_content }
13
    end
Dan Hansen's avatar
Dan Hansen committed
14
  end
15 16

  def destroy
17
    notice = if current_user.blocks.find_by(id: params[:id])&.delete
18 19 20 21
               {notice: t("blocks.destroy.success")}
             else
               {error: t("blocks.destroy.failure")}
             end
22

23
    respond_to do |format|
24
      format.json { head :no_content }
25
      format.any { redirect_back fallback_location: privacy_settings_path, flash: notice }
26
    end
27
  end
28

29
  private
30 31

  def disconnect_if_contact(person)
32
    current_user.contact_for(person).try {|contact| current_user.disconnect(contact) }
33
  end
34 35 36 37

  def block_params
    params.require(:block).permit(:person_id)
  end
38
end