Skip to content

Commit

Permalink
Merge pull request #4318 from solidusio/waiting-for-dev/active_storag…
Browse files Browse the repository at this point in the history
…e_size

Fixes defining thumbnail sizes through ActiveStorage adapter
  • Loading branch information
waiting-for-dev authored Apr 11, 2022
2 parents dddc570 + b231ddd commit 6c79f34
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 10 deletions.
33 changes: 23 additions & 10 deletions core/app/models/concerns/spree/active_storage_adapter/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

module Spree
module ActiveStorageAdapter
# Decorares AtiveStorage attachment to add methods exptected by Solidus'
# Decorates ActiveStorage attachment to add methods expected by Solidus'
# Paperclip-oriented attachment support.
class Attachment
delegate_missing_to :@attachment

attr_reader :attachment

def initialize(attachment, styles: {})
@attachment = attachment
@styles = normalize_styles(styles)
@transformations = styles_to_transformations(styles)
end

def exists?
Expand All @@ -27,13 +29,13 @@ def url(style = nil)
end

def variant(style = nil)
size = style_to_size(style)
@attachment.variant(
resize_to_limit: size,
transformation = @transformations[style] || default_transformation(width, height)

@attachment.variant({
saver: {
strip: true
}
).processed
}.merge(transformation)).processed
end

def height
Expand All @@ -59,12 +61,23 @@ def metadata
@attachment.metadata
end

def normalize_styles(styles)
styles.transform_values { |v| v.split('x').map(&:to_i) }
def styles_to_transformations(styles)
styles.transform_values(&method(:imagemagick_to_image_processing_definition))
end

def imagemagick_to_image_processing_definition(definition)
width_height = definition.split('x').map(&:to_i)

case definition[-1].to_sym
when :^
{ resize_to_fill: width_height }
else
default_transformation(*width_height)
end
end

def style_to_size(style)
@styles.fetch(style&.to_sym) { [width, height] }
def default_transformation(width, height)
{ resize_to_limit: [width, height] }
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# frozen_string_literal: true

require 'rails_helper'

unless ENV['DISABLE_ACTIVE_STORAGE']
RSpec.describe Spree::ActiveStorageAdapter::Attachment do
describe '#variant' do
it "converts to resize_to_limit when definition doesn't contain any special symbol" do
image = create(:image)

attachment = described_class.new(image.attachment.attachment, styles: { mini: '10x10' })

expect(
attachment.variant(:mini).variation.transformations
).to include(resize_to_limit: [10, 10])
end

it 'converts to resize_to_limit when definition ends with >' do
image = create(:image)

attachment = described_class.new(image.attachment.attachment, styles: { mini: '10x10>' })

expect(
attachment.variant(:mini).variation.transformations
).to include(resize_to_limit: [10, 10])
end

it 'converts to resize_to_fill when definition ends with ^' do
image = create(:image)

attachment = described_class.new(image.attachment.attachment, styles: { mini: '10x10^' })

expect(
attachment.variant(:mini).variation.transformations
).to include(resize_to_fill: [10, 10])
end

it 'strips definitions' do
image = create(:image)

attachment = described_class.new(image.attachment.attachment, styles: { mini: ' 10x10 ' })

expect(
attachment.variant(:mini).variation.transformations
).to include(resize_to_limit: [10, 10])
end


it "defaults to the image's width and height" do
image = create(:image)

attachment = described_class.new(image.attachment.attachment, styles: {})

expect(
attachment.variant(:mini).variation.transformations
).to include(resize_to_limit: [attachment.width, attachment.height])
end
end
end
end

0 comments on commit 6c79f34

Please sign in to comment.