From b9f7a98c46b6da23cbfa413208169aa7af79b7ef Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Thu, 6 Aug 2020 11:25:21 +0100 Subject: [PATCH 1/7] Bring image, asset and images_helper from spree_core --- app/helpers/spree/admin/images_helper.rb | 18 +++++++++ app/models/spree/asset.rb | 6 +++ app/models/spree/image.rb | 50 ++++++++++++++++++++++++ spec/models/asset_spec.rb | 16 ++++++++ spec/models/spree/asset_spec.rb | 16 ++++++++ 5 files changed, 106 insertions(+) create mode 100644 app/helpers/spree/admin/images_helper.rb create mode 100644 app/models/spree/asset.rb create mode 100644 app/models/spree/image.rb create mode 100644 spec/models/asset_spec.rb create mode 100644 spec/models/spree/asset_spec.rb diff --git a/app/helpers/spree/admin/images_helper.rb b/app/helpers/spree/admin/images_helper.rb new file mode 100644 index 0000000000..816972b08b --- /dev/null +++ b/app/helpers/spree/admin/images_helper.rb @@ -0,0 +1,18 @@ +module Spree + module Admin + module ImagesHelper + def options_text_for(image) + if image.viewable.is_a?(Spree::Variant) + if image.viewable.is_master? + Spree.t(:all) + else + image.viewable.options_text + end + else + Spree.t(:all) + end + end + end + end +end + diff --git a/app/models/spree/asset.rb b/app/models/spree/asset.rb new file mode 100644 index 0000000000..4e0f7d81f0 --- /dev/null +++ b/app/models/spree/asset.rb @@ -0,0 +1,6 @@ +module Spree + class Asset < ActiveRecord::Base + belongs_to :viewable, polymorphic: true, touch: true + acts_as_list scope: :viewable + end +end diff --git a/app/models/spree/image.rb b/app/models/spree/image.rb new file mode 100644 index 0000000000..945b1ef11f --- /dev/null +++ b/app/models/spree/image.rb @@ -0,0 +1,50 @@ +module Spree + class Image < Asset + validates_attachment_presence :attachment + validate :no_attachment_errors + + has_attached_file :attachment, + styles: { mini: '48x48>', small: '100x100>', product: '240x240>', large: '600x600>' }, + default_style: :product, + url: '/spree/products/:id/:style/:basename.:extension', + path: ':rails_root/public/spree/products/:id/:style/:basename.:extension', + convert_options: { all: '-strip -auto-orient -colorspace RGB' } + + # save the w,h of the original image (from which others can be calculated) + # we need to look at the write-queue for images which have not been saved yet + after_post_process :find_dimensions + + 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] + + #used by admin products autocomplete + def mini_url + attachment.url(:mini, false) + end + + def find_dimensions + temporary = attachment.queued_for_write[:original] + filename = temporary.path unless temporary.nil? + filename = attachment.path if filename.blank? + geometry = Paperclip::Geometry.from_file(filename) + self.attachment_width = geometry.width + self.attachment_height = geometry.height + end + + # if there are errors from the plugin, then add a more meaningful message + def no_attachment_errors + unless attachment.errors.empty? + # uncomment this to get rid of the less-than-useful interrim messages + # errors.clear + errors.add :attachment, "Paperclip returned errors for file '#{attachment_file_name}' - check ImageMagick installation or image source file." + false + end + end + end +end diff --git a/spec/models/asset_spec.rb b/spec/models/asset_spec.rb new file mode 100644 index 0000000000..b1415cd0de --- /dev/null +++ b/spec/models/asset_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe Spree::Asset do + describe "#viewable" do + it "touches association" do + product = create(:custom_product) + asset = Spree::Asset.create! { |a| a.viewable = product.master } + + product.update_column(:updated_at, 1.day.ago) + + expect do + asset.touch + end.to change { product.reload.updated_at } + end + end +end diff --git a/spec/models/spree/asset_spec.rb b/spec/models/spree/asset_spec.rb new file mode 100644 index 0000000000..b1415cd0de --- /dev/null +++ b/spec/models/spree/asset_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe Spree::Asset do + describe "#viewable" do + it "touches association" do + product = create(:custom_product) + asset = Spree::Asset.create! { |a| a.viewable = product.master } + + product.update_column(:updated_at, 1.day.ago) + + expect do + asset.touch + end.to change { product.reload.updated_at } + end + end +end From 13f0a46bc2c4f9796879118ced760bbca4986dff Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Thu, 6 Aug 2020 11:26:30 +0100 Subject: [PATCH 2/7] Merge decorator with original file from spree --- app/models/spree/image.rb | 22 ++++++++++++++++++++++ app/models/spree/image_decorator.rb | 23 ----------------------- 2 files changed, 22 insertions(+), 23 deletions(-) delete mode 100644 app/models/spree/image_decorator.rb diff --git a/app/models/spree/image.rb b/app/models/spree/image.rb index 945b1ef11f..ded26d20fd 100644 --- a/app/models/spree/image.rb +++ b/app/models/spree/image.rb @@ -46,5 +46,27 @@ module Spree false end 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. + # See also: ImageSettingsController decorator. + # + # eg. {'mini' => ['48x48>', 'png']} is converted to {mini: ['48x48>', :png]} + def self.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 self.reformat_styles + Spree::Image.attachment_definitions[:attachment][:styles] = + format_styles(Spree::Image.attachment_definitions[:attachment][:styles]) + end + + reformat_styles end end diff --git a/app/models/spree/image_decorator.rb b/app/models/spree/image_decorator.rb deleted file mode 100644 index 7d86aae3a6..0000000000 --- a/app/models/spree/image_decorator.rb +++ /dev/null @@ -1,23 +0,0 @@ -Spree::Image.class_eval do - # 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. - # See also: ImageSettingsController decorator. - # - # eg. {'mini' => ['48x48>', 'png']} is converted to {mini: ['48x48>', :png]} - def self.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 self.reformat_styles - Spree::Image.attachment_definitions[:attachment][:styles] = - format_styles(Spree::Image.attachment_definitions[:attachment][:styles]) - end - - reformat_styles -end From 5958c2f68c782973232ec46277debf18b4792253 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Thu, 6 Aug 2020 11:27:31 +0100 Subject: [PATCH 3/7] Rubocop autocorrect --- app/helpers/spree/admin/images_helper.rb | 3 ++- app/models/spree/asset.rb | 2 ++ app/models/spree/image.rb | 4 +++- spec/models/spree/asset_spec.rb | 4 +++- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/app/helpers/spree/admin/images_helper.rb b/app/helpers/spree/admin/images_helper.rb index 816972b08b..9c12507504 100644 --- a/app/helpers/spree/admin/images_helper.rb +++ b/app/helpers/spree/admin/images_helper.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Spree module Admin module ImagesHelper @@ -15,4 +17,3 @@ module Spree end end end - diff --git a/app/models/spree/asset.rb b/app/models/spree/asset.rb index 4e0f7d81f0..d5eb00d506 100644 --- a/app/models/spree/asset.rb +++ b/app/models/spree/asset.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Spree class Asset < ActiveRecord::Base belongs_to :viewable, polymorphic: true, touch: true diff --git a/app/models/spree/image.rb b/app/models/spree/image.rb index ded26d20fd..51de34e69a 100644 --- a/app/models/spree/image.rb +++ b/app/models/spree/image.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Spree class Image < Asset validates_attachment_presence :attachment @@ -23,7 +25,7 @@ module Spree Spree::Image.attachment_definitions[:attachment][:default_url] = Spree::Config[:attachment_default_url] Spree::Image.attachment_definitions[:attachment][:default_style] = Spree::Config[:attachment_default_style] - #used by admin products autocomplete + # used by admin products autocomplete def mini_url attachment.url(:mini, false) end diff --git a/spec/models/spree/asset_spec.rb b/spec/models/spree/asset_spec.rb index b1415cd0de..d36d505f3a 100644 --- a/spec/models/spree/asset_spec.rb +++ b/spec/models/spree/asset_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe Spree::Asset do @@ -6,7 +8,7 @@ describe Spree::Asset do product = create(:custom_product) asset = Spree::Asset.create! { |a| a.viewable = product.master } - product.update_column(:updated_at, 1.day.ago) + product.update_column(:updated_at, 1.day.ago) expect do asset.touch From b2cf414fb8b32045434782d8d01312fbf2647ac9 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Thu, 6 Aug 2020 11:28:48 +0100 Subject: [PATCH 4/7] Use exiting translation --- app/helpers/spree/admin/images_helper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/helpers/spree/admin/images_helper.rb b/app/helpers/spree/admin/images_helper.rb index 9c12507504..875790438b 100644 --- a/app/helpers/spree/admin/images_helper.rb +++ b/app/helpers/spree/admin/images_helper.rb @@ -6,12 +6,12 @@ module Spree def options_text_for(image) if image.viewable.is_a?(Spree::Variant) if image.viewable.is_master? - Spree.t(:all) + I18n.t(:all) else image.viewable.options_text end else - Spree.t(:all) + I18n.t(:all) end end end From b36d0bc4f39e06666c941b50a462b7617294fecf Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Thu, 6 Aug 2020 11:30:49 +0100 Subject: [PATCH 5/7] Fix easy rubocop isssues --- app/models/spree/image.rb | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/app/models/spree/image.rb b/app/models/spree/image.rb index 51de34e69a..3ece2769aa 100644 --- a/app/models/spree/image.rb +++ b/app/models/spree/image.rb @@ -6,7 +6,8 @@ module Spree validate :no_attachment_errors has_attached_file :attachment, - styles: { mini: '48x48>', small: '100x100>', product: '240x240>', large: '600x600>' }, + styles: { mini: '48x48>', small: '100x100>', + product: '240x240>', large: '600x600>' }, default_style: :product, url: '/spree/products/:id/:style/:basename.:extension', path: ':rails_root/public/spree/products/:id/:style/:basename.:extension', @@ -19,11 +20,14 @@ 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][: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.attachment_definitions[:attachment][:default_url] = + Spree::Config[:attachment_default_url] + Spree::Image.attachment_definitions[:attachment][:default_style] = + Spree::Config[:attachment_default_style] # used by admin products autocomplete def mini_url @@ -41,12 +45,10 @@ module Spree # if there are errors from the plugin, then add a more meaningful message def no_attachment_errors - unless attachment.errors.empty? - # uncomment this to get rid of the less-than-useful interrim messages - # errors.clear - errors.add :attachment, "Paperclip returned errors for file '#{attachment_file_name}' - check ImageMagick installation or image source file." - false - end + return if attachment.errors.empty? + + errors.add :attachment, "Paperclip returned errors for file '#{attachment_file_name}' - check ImageMagick installation or image source file." + false end # Spree stores attachent definitions in JSON. This converts the style name and format to From 653b71dbd42a4f7a0d2638775a904149cdd39d96 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Tue, 1 Sep 2020 17:10:51 +0100 Subject: [PATCH 6/7] Remove duplicated spec --- spec/models/asset_spec.rb | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 spec/models/asset_spec.rb diff --git a/spec/models/asset_spec.rb b/spec/models/asset_spec.rb deleted file mode 100644 index b1415cd0de..0000000000 --- a/spec/models/asset_spec.rb +++ /dev/null @@ -1,16 +0,0 @@ -require 'spec_helper' - -describe Spree::Asset do - describe "#viewable" do - it "touches association" do - product = create(:custom_product) - asset = Spree::Asset.create! { |a| a.viewable = product.master } - - product.update_column(:updated_at, 1.day.ago) - - expect do - asset.touch - end.to change { product.reload.updated_at } - end - end -end From 2cb6124b7a0bb2585fba197dc9de0791f2fd040e Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Tue, 1 Sep 2020 17:13:07 +0100 Subject: [PATCH 7/7] Use existing product factory, the custom product is not needed here --- spec/models/spree/asset_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/models/spree/asset_spec.rb b/spec/models/spree/asset_spec.rb index d36d505f3a..649c1e5691 100644 --- a/spec/models/spree/asset_spec.rb +++ b/spec/models/spree/asset_spec.rb @@ -5,7 +5,7 @@ require 'spec_helper' describe Spree::Asset do describe "#viewable" do it "touches association" do - product = create(:custom_product) + product = create(:product) asset = Spree::Asset.create! { |a| a.viewable = product.master } product.update_column(:updated_at, 1.day.ago)