Make sorting and attachments work
This commit is contained in:
@@ -76,7 +76,8 @@ gem 'omniauth-instagram'
|
|||||||
#gem 'simple_form'
|
#gem 'simple_form'
|
||||||
gem 'active_decorator' #, path: '/Users/bterkuile/companytools/development/rails/components/active_decorator'
|
gem 'active_decorator' #, path: '/Users/bterkuile/companytools/development/rails/components/active_decorator'
|
||||||
#gem 'cmtool', github: 'bterkuile/cmtool'
|
#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', path: './../cmtool'
|
||||||
gem 'cmtool', github: 'bterkuile/cmtool', branch: :master
|
gem 'cmtool', github: 'bterkuile/cmtool', branch: :master
|
||||||
gem 'kaminari'
|
gem 'kaminari'
|
||||||
|
|||||||
+7
-1
@@ -303,6 +303,12 @@ GEM
|
|||||||
activerecord
|
activerecord
|
||||||
kaminari-core (= 1.2.2)
|
kaminari-core (= 1.2.2)
|
||||||
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)
|
launchy (3.1.1)
|
||||||
addressable (~> 2.8)
|
addressable (~> 2.8)
|
||||||
childprocess (~> 5.0)
|
childprocess (~> 5.0)
|
||||||
@@ -619,6 +625,7 @@ DEPENDENCIES
|
|||||||
js-routes
|
js-routes
|
||||||
jsonapi-serializers!
|
jsonapi-serializers!
|
||||||
kaminari
|
kaminari
|
||||||
|
kt-paperclip
|
||||||
launchy
|
launchy
|
||||||
letter_opener
|
letter_opener
|
||||||
mini_magick
|
mini_magick
|
||||||
@@ -626,7 +633,6 @@ DEPENDENCIES
|
|||||||
naught
|
naught
|
||||||
omniauth-facebook
|
omniauth-facebook
|
||||||
omniauth-instagram
|
omniauth-instagram
|
||||||
paperclip (>= 4.2.4, != 4.3.0)
|
|
||||||
pickadate-rails
|
pickadate-rails
|
||||||
poltergeist
|
poltergeist
|
||||||
pry-doc
|
pry-doc
|
||||||
|
|||||||
@@ -107,7 +107,7 @@ module Suppliers
|
|||||||
BASE64_IMAGE_MATCHER = /^data:image\/(\w+);base64,/ # data:image/png;base64,
|
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
|
# inspired by: https://stackoverflow.com/questions/32984963/upload-base64-encoded-image-with-paperclip-rails
|
||||||
def decode_base64_params(authorized_params, attributes = [])
|
def decode_base64_params(authorized_params, attributes = [])
|
||||||
tempfiles = []
|
@tempfiles = []
|
||||||
attributes = Array.wrap(attributes) # allow single attribute argument without array notation
|
attributes = Array.wrap(attributes) # allow single attribute argument without array notation
|
||||||
attributes.each do |attribute|
|
attributes.each do |attribute|
|
||||||
if value = authorized_params[attribute].presence
|
if value = authorized_params[attribute].presence
|
||||||
@@ -115,6 +115,7 @@ module Suppliers
|
|||||||
image_type = match[1]
|
image_type = match[1]
|
||||||
decoded_attribute = Base64.decode64 value.sub BASE64_IMAGE_MATCHER, ''
|
decoded_attribute = Base64.decode64 value.sub BASE64_IMAGE_MATCHER, ''
|
||||||
file = Tempfile.new(['image', ".#{image_type}"])
|
file = Tempfile.new(['image', ".#{image_type}"])
|
||||||
|
tempfiles << file
|
||||||
file.binmode
|
file.binmode
|
||||||
file.write decoded_attribute
|
file.write decoded_attribute
|
||||||
authorized_params[attribute] = file
|
authorized_params[attribute] = file
|
||||||
@@ -126,7 +127,8 @@ module Suppliers
|
|||||||
end
|
end
|
||||||
authorized_params
|
authorized_params
|
||||||
ensure
|
ensure
|
||||||
tempfiles.each &:unlink
|
@tempfiles.each &:rewind
|
||||||
|
# tempfiles.each &:unlink
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
+20
-1
@@ -45,9 +45,28 @@ class Product
|
|||||||
url: '/system/product/:id/images/:style.:extension',
|
url: '/system/product/:id/images/:style.:extension',
|
||||||
styles: {medium: '512x512>', thumb: '128x128>', large: '896x896>', small: '320x320>'},
|
styles: {medium: '512x512>', thumb: '128x128>', large: '896x896>', small: '320x320>'},
|
||||||
default_url: '/assets/user/blank-pixel.png'
|
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
|
#private
|
||||||
|
|
||||||
#def persist_product_category_ids
|
#def persist_product_category_ids
|
||||||
|
|||||||
Reference in New Issue
Block a user