Ruby on Rails

Customizing image sizes in Solidus


Reading Time: 2 minutes

Some of the most common tasks when implementing a Solidus store is to modify image sizes or add new attachment definitions. Solidus has recently added some functionality to make this task easy without the need of decorating existing classes.

 
One of the first tasks to do when creating a new store with Solidus is to change the image sizes so they adapt to the design. Solidus added some pretty good changes to allow this to be easy, especially with a new concept they introduced called attachment definitions.

What is an attachment definition?

An attachment definition basically encapsulates the engine used to manipulate images. Click To Tweet

It basically encapsulates the engine used to manipulate images; it can be a paperclip, active storage, etc. No need to use any decorators anymore!

Solidus allows to modify what attachment definition is going to be used in image and taxon model. Both are controlled by the following settings:


Spree.config do |config|
  config.taxon_attachment_module = 'Magma::Taxon::PaperclipAttachment'
  config.image_attachment_module = 'Magma::Image::PaperclipAttachment'
end


Using the code above, we replace how the attachments need to behave without the need to decorate image/attachment models.

For example, if you need to add a new attachment to the taxon model, you can easily do the following:


module Magma
  module Taxon
    module PaperclipAttachment
      extend ActiveSupport::Concern

      included do
        has_attached_file :hero_mobile,
                          styles: {
                              mobile_hero_2x: '840x870>',
                              mobile_hero: '420x435>',
                              preview: '200x200>'
                          }
        validates_attachment :hero_mobile,
                             attachment_presence: false,
                             content_type: { content_type: ['image/jpeg', 'image/png'] }
      end
    end
  end
end

Solidus has a built-in mechanism to read attachment definitions and render them inside the taxon edit form. Click To Tweet

Solidus has a built-in mechanism to read attachment definitions and render them inside the taxon edit form. If you are curious, you can read about the change that introduced that functionality here.

The only downside is that it actually replaces everything, leaving the current icon attachment definition out. You can either duplicate the code here or use a composition pattern to extend functionalities from Solidus itself. For example:


module Magma
  module Taxon
    module PaperclipAttachment
      extend ActiveSupport::Concern

      include ::Spree::Taxon::PaperclipAttachment

      included do
        has_attached_file :hero_mobile,
                          styles: {
                              mobile_hero_2x: '840x870>',
                              mobile_hero: '420x435>',
                              preview: '200x200>'
                          }
        validates_attachment :hero_mobile,
                             attachment_presence: false,
                             content_type: { content_type: ['image/jpeg', 'image/png'] }
      end
    end
  end
end

And you're done! You can now easily add more attachment definitions or even add new styles to an existing one.

Another alternative to add new styles is to use a paperclip initializer:


Spree::Taxon.attachment_definitions[:icon][:styles][:new_size] = '600x600>'

Thank you for reading!

 
@EdwinCruz, CTO at MagmaLabs

Development
Rails + RSpec + Turnip = <3
Development
Customizing Solidus order number generation
Ecommerce
3 plataformas de ecommerce para retailers
Copy link
Powered by Social Snap