Make sorting and attachments work

This commit is contained in:
2025-09-02 13:33:48 -05:00
parent 73f430eadf
commit ea56d33472
4 changed files with 33 additions and 5 deletions
+2 -1
View File
@@ -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'
+7 -1
View File
@@ -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
@@ -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
+20 -1
View File
@@ -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