summaryrefslogtreecommitdiff
path: root/app/services/application/upload_photo.rb
blob: 66f916c445378724bceedb03e76a31765140cc7e (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
26
27
28
29
30
class UploadPhoto
  attr_reader :cake, :storage

  def initialize(cake)
    @cake = cake
    @storage = TemporaryStorage.new
  end

  def run(attributes)
    with_transaction do
      create_photo!(attributes[:watermark]) do |photo|
        ProcessPhotoJob.perform_later(photo, storage.store(attributes[:image]))
      end
    end
  end

  private

  def with_transaction
    ActiveRecord::Base.transaction do
      yield
    end
  end

  def create_photo!(watermark)
    cake.photos.create!(image_processing: true, watermark: watermark).tap do |photo|
      yield photo
    end
  end
end