Make sorting and attachments work
This commit is contained in:
@@ -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
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user