diff --git a/Gemfile b/Gemfile index d7805aea..db355118 100644 --- a/Gemfile +++ b/Gemfile @@ -76,7 +76,8 @@ gem 'omniauth-instagram' #gem 'simple_form' gem 'active_decorator' #, path: '/Users/bterkuile/companytools/development/rails/components/active_decorator' #gem 'cmtool', github: 'bterkuile/cmtool' -gem 'paperclip', ['>= 4.2.4', '!= 4.3.0'] #, '3.5.2' # cmtool +#gem 'paperclip', ['>= 4.2.4', '!= 4.3.0'] #, '3.5.2' # cmtool +gem 'kt-paperclip' #gem 'cmtool', path: './../cmtool' gem 'cmtool', github: 'bterkuile/cmtool', branch: :master gem 'kaminari' diff --git a/Gemfile.lock b/Gemfile.lock index 3656904c..3802db06 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -303,6 +303,12 @@ GEM activerecord kaminari-core (= 1.2.2) kaminari-core (1.2.2) + kt-paperclip (7.2.2) + activemodel (>= 4.2.0) + activesupport (>= 4.2.0) + marcel (~> 1.0.1) + mime-types + terrapin (>= 0.6.0, < 2.0) launchy (3.1.1) addressable (~> 2.8) childprocess (~> 5.0) @@ -619,6 +625,7 @@ DEPENDENCIES js-routes jsonapi-serializers! kaminari + kt-paperclip launchy letter_opener mini_magick @@ -626,7 +633,6 @@ DEPENDENCIES naught omniauth-facebook omniauth-instagram - paperclip (>= 4.2.4, != 4.3.0) pickadate-rails poltergeist pry-doc diff --git a/app/controllers/suppliers/products_controller.rb b/app/controllers/suppliers/products_controller.rb index e91b387c..6635ac90 100644 --- a/app/controllers/suppliers/products_controller.rb +++ b/app/controllers/suppliers/products_controller.rb @@ -107,7 +107,7 @@ module Suppliers BASE64_IMAGE_MATCHER = /^data:image\/(\w+);base64,/ # data:image/png;base64, # inspired by: https://stackoverflow.com/questions/32984963/upload-base64-encoded-image-with-paperclip-rails def decode_base64_params(authorized_params, attributes = []) - tempfiles = [] + @tempfiles = [] attributes = Array.wrap(attributes) # allow single attribute argument without array notation attributes.each do |attribute| if value = authorized_params[attribute].presence @@ -115,6 +115,7 @@ module Suppliers image_type = match[1] decoded_attribute = Base64.decode64 value.sub BASE64_IMAGE_MATCHER, '' file = Tempfile.new(['image', ".#{image_type}"]) + tempfiles << file file.binmode file.write decoded_attribute authorized_params[attribute] = file @@ -126,7 +127,8 @@ module Suppliers end authorized_params ensure - tempfiles.each &:unlink + @tempfiles.each &:rewind + # tempfiles.each &:unlink end end end diff --git a/app/models/product.rb b/app/models/product.rb index c3f45c64..b3806bf1 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -45,9 +45,28 @@ class Product url: '/system/product/:id/images/:style.:extension', styles: {medium: '512x512>', thumb: '128x128>', large: '896x896>', small: '320x320>'}, default_url: '/assets/user/blank-pixel.png' - validates_attachment :image, content_type: {content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"]} + #validates_attachment :image, content_type: {content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"]} + #validates_attachment :image, allow_nil: true + validates_attachment_content_type :image, content_type: %w(image/jpeg image/jpg image/png) + def self.ensure_correct_position_values! + # structured woud be: {supplier1: {category1: [p1, p2, p3], category2: [p1, p2]}, supplier2: {category1: [p1], ...}} + structured = Product.all.inject({}) do |aggregate, p| + aggregate[p.supplier_id] ||= {} + aggregate[p.supplier_id][p.product_category_id] ||= [] + aggregate[p.supplier_id][p.product_category_id] << p + aggregate + end + + structured.each do |(supplier_id, product_categories)| + product_categories.each do |(product_category_id, products)| + products.sort_by{_1.position.to_i}.each.with_index { |product, position| product.update position: position } + end + end + structured + end + #private #def persist_product_category_ids