From 98c5fdcf26669f066e1c08d56a9f147e54975703 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Fri, 18 Sep 2020 17:39:17 +0100 Subject: [PATCH] Reuse some code from Spree::Image in the spree initializer and extract some code to a class method so we can reuse afterwards --- app/models/spree/image.rb | 20 ++++++++++++-------- config/initializers/spree.rb | 21 +-------------------- 2 files changed, 13 insertions(+), 28 deletions(-) diff --git a/app/models/spree/image.rb b/app/models/spree/image.rb index 3ece2769aa..be92cd83fb 100644 --- a/app/models/spree/image.rb +++ b/app/models/spree/image.rb @@ -20,14 +20,7 @@ module Spree include Spree::Core::S3Support supports_s3 :attachment - Spree::Image.attachment_definitions[:attachment][:styles] = - ActiveSupport::JSON.decode(Spree::Config[:attachment_styles]).symbolize_keys! - Spree::Image.attachment_definitions[:attachment][:path] = Spree::Config[:attachment_path] - Spree::Image.attachment_definitions[:attachment][:url] = Spree::Config[:attachment_url] - Spree::Image.attachment_definitions[:attachment][:default_url] = - Spree::Config[:attachment_default_url] - Spree::Image.attachment_definitions[:attachment][:default_style] = - Spree::Config[:attachment_default_style] + Spree::Image.set_attachment_definitions # used by admin products autocomplete def mini_url @@ -51,6 +44,17 @@ module Spree false end + def self.set_attachment_definitions(config) + Spree::Image.attachment_definitions[:attachment][:styles] = + ActiveSupport::JSON.decode(Spree::Config[:attachment_styles]).symbolize_keys! + Spree::Image.attachment_definitions[:attachment][:path] = Spree::Config[:attachment_path] + Spree::Image.attachment_definitions[:attachment][:url] = Spree::Config[:attachment_url] + Spree::Image.attachment_definitions[:attachment][:default_url] = + Spree::Config[:attachment_default_url] + Spree::Image.attachment_definitions[:attachment][:default_style] = + Spree::Config[:attachment_default_style] + 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. diff --git a/config/initializers/spree.rb b/config/initializers/spree.rb index 38dca0be22..c2cf7f4d16 100644 --- a/config/initializers/spree.rb +++ b/config/initializers/spree.rb @@ -18,25 +18,6 @@ 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 @@ -84,7 +65,7 @@ Spree.config do |config| Spree::Image.attachment_definitions[:attachment][:default_style] = config.attachment_default_style - reformat_styles + Spree::Image.reformat_styles end # Spree 2.0 recommends explicitly setting this here when using spree_auth_devise