summaryrefslogtreecommitdiff
path: root/app/controllers/source_changes_controller.rb
blob: fc4bdd601263f2ff92e8ea83e6eba55edb850274 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# frozen_string_literal: true

class SourceChangesController < ApplicationController
  def destroy
    authorize! :delete, SourceChange
    @source_change = SourceChange.find(params[:id])
    @post = @source_change.image
    bad_val = @source_change.new_value
    SourceChange.where(new_value: bad_val, post_id: @post.id).delete_all
    if @post.source_url == bad_val
      latest_source = SourceChange.where(post_id: @post.id).order(created_at: :desc).first
      @post.source_url = if latest_source
        latest_source.new_value
      else
        ''
      end
      @post.save
    end

    respond_to do |format|
      format.html { redirect_back notice: 'Removed source change successfully.' }
      format.json { head :ok }
    end
  end
end