Skip to content

Commit

Permalink
Extract picture preprocessing into service class
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdeyen committed Jun 15, 2020
1 parent 1e7c025 commit e251ae5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
5 changes: 2 additions & 3 deletions app/models/alchemy/picture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ class Picture < BaseRecord
# Enables Dragonfly image processing
dragonfly_accessor :image_file, app: :alchemy_pictures do
# Preprocess after uploading the picture
after_assign do |p|
resize = Config.get(:preprocess_image_resize)
p.thumb!(resize) if resize.present?
after_assign do |image|
Preprocessor.new(image).call
end
end

Expand Down
26 changes: 26 additions & 0 deletions app/models/alchemy/picture/preprocessor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module Alchemy
class Picture < BaseRecord
class Preprocessor
def initialize(image_file)
@image_file = image_file
end

# Preprocess images after upload
#
# Define preprocessing options in the Alchemy::Config
#
# preprocess_image_resize [String] - Downsizing example: '1000x1000>'
#
def call
max_image_size = Alchemy::Config.get(:preprocess_image_resize)
image_file.thumb!(max_image_size) if max_image_size.present?
end

private

attr_reader :image_file
end
end
end

0 comments on commit e251ae5

Please sign in to comment.