From d35675de64e1f934c6b9c3d4237a3fa88dd65f98 Mon Sep 17 00:00:00 2001 From: drakkensaer Date: Tue, 6 Sep 2016 03:25:11 +0000 Subject: [PATCH 1/5] add attributes to stores (content_type, height, width, etc), return useful paperclip error messages, fix for issue #155 --- app/models/spree/store_decorator.rb | 39 +++++++++++++++---- ...57_add_image_attributes_to_spree_stores.rb | 9 +++++ 2 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 db/migrate/20160906012057_add_image_attributes_to_spree_stores.rb diff --git a/app/models/spree/store_decorator.rb b/app/models/spree/store_decorator.rb index 710d8b6b..4957e87e 100644 --- a/app/models/spree/store_decorator.rb +++ b/app/models/spree/store_decorator.rb @@ -1,5 +1,9 @@ module Spree Store.class_eval do + # 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 + before_save :find_dimensions, if: :logo_updated_at_changed? + has_and_belongs_to_many :products, join_table: 'spree_products_stores' has_many :taxonomies has_many :orders @@ -11,17 +15,36 @@ module Spree has_many :shipping_methods, through: :store_shipping_methods has_and_belongs_to_many :promotion_rules, class_name: 'Spree::Promotion::Rules::Store', join_table: 'spree_promotion_rules_stores', association_foreign_key: 'promotion_rule_id' + + validate :no_logo_errors has_attached_file :logo, - styles: { mini: '48x48>', small: '100x100>', medium: '250x250>' }, - default_style: :medium, - url: 'stores/:id/:style/:basename.:extension', - path: 'stores/:id/:style/:basename.:extension', - convert_options: { all: '-strip -auto-orient' } + styles: { mini: '48x48>', small: '100x100>', logo: '250x250>', large: '600x600>' }, + default_style: :logo, + url: '/spree/logos/:id/:style/:basename.:extension', + path: ':rails_root/public/spree/logos/:id/:style/:basename.:extension', + convert_options: { all: '-strip -auto-orient -colorspace sRGB' } + validates_attachment :logo, + presence: true, + content_type: { content_type: %w(image/jpeg image/jpg image/png image/gif) } - if respond_to? :logo_file_name - validates_attachment_file_name :logo, matches: [/png\Z/i, /jpe?g\Z/i] + def find_dimensions + temporary = logo.queued_for_write[:original] + filename = temporary.path unless temporary.nil? + filename = logo.path if filename.blank? + geometry = Paperclip::Geometry.from_file(filename) + self.logo_width = geometry.width + self.logo_height = geometry.height end + # if there are errors from the plugin, then add a more meaningful message + def no_logo_errors + unless logo.errors.empty? + # uncomment this to get rid of the less-than-useful interim messages + # errors.clear + errors.add :logo, "Paperclip returned errors for file '#{logo_file_name}' - check ImageMagick installation or image source file." + false + end + end end -end +end \ No newline at end of file diff --git a/db/migrate/20160906012057_add_image_attributes_to_spree_stores.rb b/db/migrate/20160906012057_add_image_attributes_to_spree_stores.rb new file mode 100644 index 00000000..51d42b2c --- /dev/null +++ b/db/migrate/20160906012057_add_image_attributes_to_spree_stores.rb @@ -0,0 +1,9 @@ +class AddImageAttributesToSpreeStores < ActiveRecord::Migration + def change + add_column :spree_stores, :logo_content_type, :string + add_column :spree_stores, :logo_file_size, :string + add_column :spree_stores, :logo_width, :string + add_column :spree_stores, :logo_height, :string + add_column :spree_stores, :logo_updated_at, :datetime + end +end From df92abf4c39cacddef67751fefcb4d77dc6b502e Mon Sep 17 00:00:00 2001 From: drakkensaer Date: Wed, 14 Sep 2016 04:38:37 +0000 Subject: [PATCH 2/5] update --- app/models/spree/store_decorator.rb | 41 ++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/app/models/spree/store_decorator.rb b/app/models/spree/store_decorator.rb index 710d8b6b..e60de5aa 100644 --- a/app/models/spree/store_decorator.rb +++ b/app/models/spree/store_decorator.rb @@ -1,5 +1,9 @@ module Spree Store.class_eval do + # 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 + before_save :find_dimensions, if: :logo_updated_at_changed? + has_and_belongs_to_many :products, join_table: 'spree_products_stores' has_many :taxonomies has_many :orders @@ -11,17 +15,36 @@ module Spree has_many :shipping_methods, through: :store_shipping_methods has_and_belongs_to_many :promotion_rules, class_name: 'Spree::Promotion::Rules::Store', join_table: 'spree_promotion_rules_stores', association_foreign_key: 'promotion_rule_id' + has_attached_file :logo, - styles: { mini: '48x48>', small: '100x100>', medium: '250x250>' }, - default_style: :medium, - url: 'stores/:id/:style/:basename.:extension', - path: 'stores/:id/:style/:basename.:extension', - convert_options: { all: '-strip -auto-orient' } - - if respond_to? :logo_file_name - validates_attachment_file_name :logo, matches: [/png\Z/i, /jpe?g\Z/i] + styles: { mini: '48x48>', small: '100x100>', logo: '250x250>', large: '600x600>' }, + default_style: :logo, + url: '/spree/logos/:id/:style/:basename.:extension', + path: ':rails_root/public/spree/logos/:id/:style/:basename.:extension', + convert_options: { all: '-strip -auto-orient -colorspace sRGB' } + + validate :no_logo_errors + validates_attachment :logo, + content_type: { content_type: %w(image/jpeg image/jpg image/png image/gif) } + + def find_dimensions + temporary = logo.queued_for_write[:original] + filename = temporary.path unless temporary.nil? + filename = logo.path if filename.blank? + geometry = Paperclip::Geometry.from_file(filename) + self.logo_width = geometry.width + self.logo_height = geometry.height end + # if there are errors from the plugin, then add a more meaningful message + def no_logo_errors + unless logo.errors.empty? + # uncomment this to get rid of the less-than-useful interim messages + # errors.clear + errors.add :logo, "Paperclip returned errors for file '#{logo_file_name}' - check ImageMagick installation or image source file." + false + end + end end -end +end \ No newline at end of file From bf4c8f337e9766d64d7e15a4ab3cd4a53b271c41 Mon Sep 17 00:00:00 2001 From: drakkensaer Date: Wed, 15 Mar 2017 18:48:07 +0000 Subject: [PATCH 3/5] Removed improper super admin/products_controller_decorator.rb to allow correct invocation of controller --- .../admin/products_controller_decorator.rb | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/app/controllers/spree/admin/products_controller_decorator.rb b/app/controllers/spree/admin/products_controller_decorator.rb index b5903c5d..d7be840e 100644 --- a/app/controllers/spree/admin/products_controller_decorator.rb +++ b/app/controllers/spree/admin/products_controller_decorator.rb @@ -1,17 +1,18 @@ Spree::Admin::ProductsController.class_eval do update.before :set_stores + before_action :find_stores, only: [:update] - def update + private + + def set_stores + @product.store_ids = nil unless params[:product].key? :store_ids + end + + def find_stores store_ids = params[:product][:store_ids] if store_ids.present? params[:product][:store_ids] = store_ids.split(',') end - super end - private - - def set_stores - @product.store_ids = nil unless params[:product].key? :store_ids - end -end +end \ No newline at end of file From f4af4aa8b8925e843c7bea3bf2c0d6e0ef8ab4c8 Mon Sep 17 00:00:00 2001 From: drakkensaer Date: Mon, 10 Apr 2017 18:08:59 +0000 Subject: [PATCH 4/5] update --- app/models/spree/store_decorator.rb | 65 +++++++++++++++-------------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/app/models/spree/store_decorator.rb b/app/models/spree/store_decorator.rb index e60de5aa..37765179 100644 --- a/app/models/spree/store_decorator.rb +++ b/app/models/spree/store_decorator.rb @@ -1,9 +1,5 @@ module Spree Store.class_eval do - # 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 - before_save :find_dimensions, if: :logo_updated_at_changed? - has_and_belongs_to_many :products, join_table: 'spree_products_stores' has_many :taxonomies has_many :orders @@ -16,34 +12,39 @@ module Spree has_and_belongs_to_many :promotion_rules, class_name: 'Spree::Promotion::Rules::Store', join_table: 'spree_promotion_rules_stores', association_foreign_key: 'promotion_rule_id' - - has_attached_file :logo, - styles: { mini: '48x48>', small: '100x100>', logo: '250x250>', large: '600x600>' }, - default_style: :logo, - url: '/spree/logos/:id/:style/:basename.:extension', - path: ':rails_root/public/spree/logos/:id/:style/:basename.:extension', - convert_options: { all: '-strip -auto-orient -colorspace sRGB' } - - validate :no_logo_errors - validates_attachment :logo, - content_type: { content_type: %w(image/jpeg image/jpg image/png image/gif) } - - def find_dimensions - temporary = logo.queued_for_write[:original] - filename = temporary.path unless temporary.nil? - filename = logo.path if filename.blank? - geometry = Paperclip::Geometry.from_file(filename) - self.logo_width = geometry.width - self.logo_height = geometry.height - end - - # if there are errors from the plugin, then add a more meaningful message - def no_logo_errors - unless logo.errors.empty? - # uncomment this to get rid of the less-than-useful interim messages - # errors.clear - errors.add :logo, "Paperclip returned errors for file '#{logo_file_name}' - check ImageMagick installation or image source file." - false + if ActiveRecord::Base.connection.table_exists?(:spree_stores) && ActiveRecord::Base.connection.column_exists?(:spree_stores, :logo_content_type) + # 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 + before_save :find_dimensions, if: :logo_updated_at_changed? + + has_attached_file :logo, + styles: { mini: '48x48>', small: '100x100>', logo: '250x250>', large: '600x600>' }, + default_style: :logo, + url: '/spree/logos/:id/:style/:basename.:extension', + path: ':rails_root/public/spree/logos/:id/:style/:basename.:extension', + convert_options: { all: '-strip -auto-orient -colorspace sRGB' } + + validate :no_logo_errors + validates_attachment :logo, + content_type: { content_type: %w(image/jpeg image/jpg image/png image/gif) } + + def find_dimensions + temporary = logo.queued_for_write[:original] + filename = temporary.path unless temporary.nil? + filename = logo.path if filename.blank? + geometry = Paperclip::Geometry.from_file(filename) + self.logo_width = geometry.width + self.logo_height = geometry.height + end + + # if there are errors from the plugin, then add a more meaningful message + def no_logo_errors + unless logo.errors.empty? + # uncomment this to get rid of the less-than-useful interim messages + # errors.clear + errors.add :logo, "Paperclip returned errors for file '#{logo_file_name}' - check ImageMagick installation or image source file." + false + end end end end From bfc2e78040b028bed2b967b431114a9948b5a8c3 Mon Sep 17 00:00:00 2001 From: drakkensaer Date: Mon, 10 Apr 2017 19:29:41 +0000 Subject: [PATCH 5/5] fixed merged --- app/models/spree/store_decorator.rb | 31 ----------------------------- 1 file changed, 31 deletions(-) diff --git a/app/models/spree/store_decorator.rb b/app/models/spree/store_decorator.rb index fc430f80..37765179 100644 --- a/app/models/spree/store_decorator.rb +++ b/app/models/spree/store_decorator.rb @@ -11,36 +11,6 @@ module Spree has_many :shipping_methods, through: :store_shipping_methods has_and_belongs_to_many :promotion_rules, class_name: 'Spree::Promotion::Rules::Store', join_table: 'spree_promotion_rules_stores', association_foreign_key: 'promotion_rule_id' -<<<<<<< HEAD - - has_attached_file :logo, - styles: { mini: '48x48>', small: '100x100>', logo: '250x250>', large: '600x600>' }, - default_style: :logo, - url: '/spree/logos/:id/:style/:basename.:extension', - path: ':rails_root/public/spree/logos/:id/:style/:basename.:extension', - convert_options: { all: '-strip -auto-orient -colorspace sRGB' } - - validate :no_logo_errors - validates_attachment :logo, - content_type: { content_type: %w(image/jpeg image/jpg image/png image/gif) } - - def find_dimensions - temporary = logo.queued_for_write[:original] - filename = temporary.path unless temporary.nil? - filename = logo.path if filename.blank? - geometry = Paperclip::Geometry.from_file(filename) - self.logo_width = geometry.width - self.logo_height = geometry.height - end - - # if there are errors from the plugin, then add a more meaningful message - def no_logo_errors - unless logo.errors.empty? - # uncomment this to get rid of the less-than-useful interim messages - # errors.clear - errors.add :logo, "Paperclip returned errors for file '#{logo_file_name}' - check ImageMagick installation or image source file." - false -======= if ActiveRecord::Base.connection.table_exists?(:spree_stores) && ActiveRecord::Base.connection.column_exists?(:spree_stores, :logo_content_type) # save the w,h of the original image (from which others can be calculated) @@ -75,7 +45,6 @@ def no_logo_errors errors.add :logo, "Paperclip returned errors for file '#{logo_file_name}' - check ImageMagick installation or image source file." false end ->>>>>>> master end end end