From 25ab5f7f248e49dc323c1dad6a4d274e6e7f5d52 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Mon, 6 Jul 2020 13:22:47 +0100 Subject: [PATCH] Move reformat_styles to initializer, it's the only place where it is used --- config/initializers/spree.rb | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/config/initializers/spree.rb b/config/initializers/spree.rb index 65f0dbac68..38dca0be22 100644 --- a/config/initializers/spree.rb +++ b/config/initializers/spree.rb @@ -18,6 +18,25 @@ Spree::Gateway.class_eval do acts_as_taggable end +# Spree stores attachent definitions in JSON. This converts the style name and format to +# strings. However, when paperclip encounters these, it doesn't recognise the format. +# Here we solve that problem by converting format and style name to symbols. +# +# eg. {'mini' => ['48x48>', 'png']} is converted to {mini: ['48x48>', :png]} +def format_styles(styles) + styles_a = styles.map do |name, style| + style[1] = style[1].to_sym if style.is_a? Array + [name.to_sym, style] + end + + Hash[styles_a] +end + +def reformat_styles + Spree::Image.attachment_definitions[:attachment][:styles] = + format_styles(Spree::Image.attachment_definitions[:attachment][:styles]) +end + Spree.config do |config| config.shipping_instructions = true config.address_requires_state = true @@ -43,7 +62,7 @@ Spree.config do |config| config.attachment_styles = ENV['ATTACHMENT_STYLES'] if ENV['ATTACHMENT_STYLES'] config.attachment_default_style = ENV['ATTACHMENT_DEFAULT_STYLE'] if ENV['ATTACHMENT_DEFAULT_STYLE'] - # update_paperclip_settings + # Update paperclip settings if config.use_s3 s3_creds = { access_key_id: config.s3_access_key, secret_access_key: config.s3_secret, @@ -65,10 +84,7 @@ Spree.config do |config| Spree::Image.attachment_definitions[:attachment][:default_style] = config.attachment_default_style - # Spree stores attachent definitions in JSON. This converts the style name and format to - # strings. However, when paperclip encounters these, it doesn't recognise the format. - # Here we solve that problem by converting format and style name to symbols. - Spree::Image.reformat_styles + reformat_styles end # Spree 2.0 recommends explicitly setting this here when using spree_auth_devise