Class: Proclaim::ImagesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/proclaim/images_controller.rb

Instance Method Summary collapse

Instance Method Details

#cacheObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/controllers/proclaim/images_controller.rb', line 29

def cache
  @image = Image.new

  begin
    authorize @image

    @image.image = file_params[:file]

    respond_to do |format|
      format.json { render json: @image.image.url }
    end
  rescue Pundit::NotAuthorizedError
    respond_to do |format|
      format.json { render json: true, status: :unauthorized }
    end
  end
end

#createObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/proclaim/images_controller.rb', line 7

def create
  @image = Image.new(post_id: image_params[:post_id])

  begin
    authorize @image

    @image.image = image_params[:image]

    respond_to do |format|
      if @image.save
        format.json { render json: @image.image.url }
      else
        format.json { render json: @image.errors.full_messages, status: :unprocessable_entity }
      end
    end
  rescue Pundit::NotAuthorizedError
    respond_to do |format|
      format.json { render json: true, status: :unauthorized }
    end
  end
end

#destroyObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/controllers/proclaim/images_controller.rb', line 79

def destroy
  @image = Image.find(params[:id])

  begin
    authorize @image

    respond_to do |format|
      @image.destroy
      format.json { render json: true, status: :ok }
    end
  rescue Pundit::NotAuthorizedError
    respond_to do |format|
      format.json { render json: true, status: :unauthorized }
    end
  end
end

#discardObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/proclaim/images_controller.rb', line 47

def discard
  url = file_params[:file]
  image_id = nil

  # Is this a cached image?
  if (url.include? Proclaim::ImageUploader.cache_dir)
    # If so, retrieve it from the cache
    @image = Image.new
    @image.image.retrieve_from_cache!(cache_name_from_url(url))
  else
    # If not, retrieve it from the database
    image_id, image_name = image_id_and_name_from_url(url)
    @image = Image.find(image_id)
  end

  begin
    authorize @image

    if @image.new_record?
      @image.image.remove!
    end

    respond_to do |format|
      format.json { render json: {id: image_id}, status: :ok }
    end
  rescue Pundit::NotAuthorizedError
    respond_to do |format|
      format.json { render json: true, status: :unauthorized }
    end
  end
end