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