Reuse some code from Spree::Image in the spree initializer and extract some code to a class method so we can reuse afterwards

This commit is contained in:
Luis Ramos
2020-09-18 17:39:17 +01:00
parent 25ab5f7f24
commit 98c5fdcf26
2 changed files with 13 additions and 28 deletions

View File

@@ -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.

View File

@@ -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