Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to change images size #3700

Closed
sechix opened this issue Jul 10, 2020 · 8 comments · Fixed by #4245
Closed

Not able to change images size #3700

sechix opened this issue Jul 10, 2020 · 8 comments · Fixed by #4245
Labels
confirmed Validated report type:bug Error, flaw or fault

Comments

@sechix
Copy link

sechix commented Jul 10, 2020

I am trying to change sizes with this in the initializer:
Spree::Image.attachment_definitions[:attachment][:styles] = {
mini: '48x48>',
small: '100x100>',
product: '240x240>',
large: '600x600>'
}
But 'module Spree::Image::ActiveStorageAttachment' don't take new sizes. How to do it?

Solidus Version:
Last version of master

To Reproduce

@sechix
Copy link
Author

sechix commented Sep 24, 2020

Hi, any idea? @kennyadsl

@hefan
Copy link
Contributor

hefan commented Sep 25, 2020

It doesn't seem to work any longer with Rails 6 / Zeitwerk in an initializer.
Somehow it gets overriden again with the default styles from core/app/models/spree/image/paperclip_attachment.rb.
To get it to work with Rails 6 you have to decorate Spree::Image and do something like

module ImageDecorator

  def self.prepended(base)
    base.attachment_definitions[:attachment][:styles] = {
      mini: '48x48>', small: '100x100>', product: '240x240>', large: '600x600>'
    }
  end

  Spree::Image.prepend self
end

For more details about Decorations: https://guides.solidus.io/developers/customizations/decorators.html

With Rails 5.2 it works fine

@kennyadsl i am not sure how to make it work again in Zeitwerk. Maybe you can suggest something?

@kennyadsl
Copy link
Member

I think it's a filename issue. Where this ImageDecorator lives exactly?

@hefan
Copy link
Contributor

hefan commented Sep 28, 2020

The Image Decorator would be a workaround. Normally

Spree::Image.attachment_definitions[:attachment][:styles] = {
  mini: '48x48>',
  small: '100x100>',
  product: '240x240>',
  large: '600x600>'
}

should work in an initializer like stated here Product images which seems not to work in rails 6

@kennyadsl
Copy link
Member

Oh, you are right. This seems like a bug then, I'll try to take a look asap.

@kennyadsl kennyadsl added type:bug Error, flaw or fault confirmed Validated report Hacktoberfest labels Sep 28, 2020
@EduardoGHdez
Copy link

EduardoGHdez commented Oct 3, 2020

There is another workaround

You are able to set your own image_attachment_module from Spree::Config 😄 , rather than decorating
https://github.com/solidusio/solidus/blob/master/core/lib/spree/app_configuration.rb#L477-L483

@nbelzer
Copy link
Contributor

nbelzer commented Jan 13, 2022

Given that this issue has been open for a while, I decided to verify if the bug still exists. Below I have described my findings:

To update the product_image_styles I was able to use the following initializer snippet:

Spree.config do |config|
  ...
  config.product_image_styles = {
    mini: '48x48>',
    small: '100x100>',
    product: '240x240>',
    large: '600x600>'
  }
  ...
end

This configuration option is used in Spree::Image::PaperclipAttachment andSpree::Image::ActiveStorageAttachment, of which by default the later is included in Spree::Image as image_attachment_module by default. Lets focus on the Spree::Image::ActiveStorageAttachment first:

has_attachment :attachment,
styles: Spree::Config.product_image_styles,
default_style: Spree::Config.product_image_style_default

The method has_attachment stems from Spree::ActiveStorageAdapter, the following snippet shows how calling has_attachment sets the @attachment_definition which is used in the attachment_definitions (note the plural name) method.

def has_attachment(name, definition)
@attachment_name = name.to_sym
@attachment_definition = definition
has_one_attached attachment_name
override_reader
override_writer
define_image_validation
define_presence_reader
end
def attachment_definitions
{ attachment_name => attachment_definition }
end

Therefore by using the configuration snippet above you are able to customize the attachment styles, therefore there is no longer a need to override or patch the attachment_definitions method. Looking at the git blame for when the configuration option was introduced, it seems that the author indeed meant to deprecate the need to override the attachment_definitions.

Both the developer guides and edgeguides still recommend using the following snippets:

# From the developer guides
Spree::Image.attachment_definitions[:attachment][:styles] = {
  mini: '48x48>',
  small: '100x100>',
  product: '240x240>',
  large: '600x600>'
}
# From the EdgeGuides
module AmazingStore
  module Spree
    module Image
      module CustomizeSizes
        def self.prepended(klass)
          klass.attachment_definitions[:attachment][:styles] = {
            mini: '48x48>',
            small: '400x400>',
            product: '680x680>',
            large: '1200x1200>',
            jumbo: '1600x1600>'
          }
        end

        ::Spree::Image.prepend self
      end
    end
  end
end

As this is outdated, I recommend we use this issue to create a PR that updates the documentation accordingly, and adds some tests such that we verify this behaviour (I wasn't able to find any existing tests). Once merged, I believe we can close this issue. I will take a look at doing this tomorrow.

What about the PaperclipAttachment?

When the Spree::Config.image_attachment_module is set to Spree::Image::PaperclipAttachment our Spree::Config.product_image_styles are passed to has_attached_file as seen in the following snippet:

has_attached_file :attachment,
styles: Spree::Config.product_image_styles,
default_style: Spree::Config.product_image_style_default,
default_url: 'noimage/:style.png',
url: '/spree/products/:id/:style/:basename.:extension',
path: ':rails_root/public/spree/products/:id/:style/:basename.:extension',
convert_options: { all: '-strip -auto-orient -colorspace sRGB' }

This is a method defined by kt-paperclip which accepts these styles and default styles as defined in the gems documentation. So the configuration is also correctly applied for the Spree::Image::PaperclipAttachment.

@kennyadsl
Copy link
Member

As this is outdated, I recommend we use this issue to create a PR that updates the documentation accordingly, and adds some tests such that we verify this behaviour (I wasn't able to find any existing tests). Once merged, I believe we can close this issue. I will take a look at doing this tomorrow.

I like the plan, when documenting this, please be aware that not all the Solidus versions support ActiveStorage (I think it has been introduced in 2.11), so we should reflect that in the documentation. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed Validated report type:bug Error, flaw or fault
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants