Files
mozo-backend/app/controllers/suppliers/products_controller.rb
T

134 lines
4.6 KiB
Ruby

module Suppliers
class ProductsController < Suppliers::ApplicationController
layout 'tablet'
after_authentication only: [:show, :edit, :update, :destroy] do
@product = Product.find_by_supplier_id_and_id!(current_supplier.id, params[:id])
end
# GET /products
# GET /products.json
def index
@products = current_supplier.products
respond_to do |format|
format.html # index.html.erb
format.json { render json: @products }
end
end
# GET /products/1
# GET /products/1.json
def show
respond_to do |format|
format.html # show.html.erb
format.json { render json: @product }
end
end
# GET /products/new
# GET /products/new.json
def new
@product = Product.new supplier: current_supplier
@product.add_product_category ProductCategory.find_by_supplier_id_and_id!(current_supplier.id, params[:product_category_id]) if params[:product_category_id].present?
respond_to do |format|
format.html # new.html.erb
format.json { render json: @product }
end
end
# GET /products/1/edit
def edit
#@product = Product.find(params[:id])
end
# POST /products
# POST /products.json
def create
@product = Product.new(product_params)
@product.supplier = current_supplier
respond_to do |format|
if @product.save
format.html { redirect_to [:suppliers, :products], notice: t('action.create.successfull', model: Product.model_name.human) }
format.json { render json: @product, status: :created }
else
format.html { render action: "new" }
format.json { render json: {errors: @product.errors}, status: :unprocessable_entity }
end
end
end
# PUT /products/1
# PUT /products/1.json
def update
#@product = Product.find_by_supplier_id_and_id!(current_supplier.id, params[:id])
respond_to do |format|
if @product.update_attributes(product_params)
format.html { redirect_to [:suppliers, :products], notice: t('action.update.successfull', model: Product.model_name.human) }
format.json { render json_response @product }
else
format.html { render action: "edit" }
format.json { render json_response @product }
end
end
end
# DELETE /products/1
# DELETE /products/1.json
def destroy
@product = Product.find_by_supplier_id_and_id!(current_supplier.id, params[:id])
@product.destroy
head :no_content
end
def preview_products
#TODO: time zone correction, since the hour and minute are of the supplier time zone, not the system time zone
@time = Time.parse("#{params[:date]}T#{params[:hour]}:#{params[:minute]}:00") rescue Time.now
product_categories = ProductCategory.for_supplier_in_time(current_supplier, @time)
render json: {categories: product_categories.map(&:to_client_format).select(&:present?)}
end
private
def product_params
permitted_attributes = [:name, :code, :price, :description, :image, :visible, :position, :active, :product_category_id]
# do not raise in development and test for json communication
result = if request.format.json?
params.require(:product).slice(*permitted_attributes).permit!
else
params.require(:product).permit permitted_attributes
end
decode_base64_params result, :image
end
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 = []
attributes = Array.wrap(attributes) # allow single attribute argument without array notation
attributes.each do |attribute|
if value = authorized_params[attribute].presence
if value.is_a?(String) and match = value.match(BASE64_IMAGE_MATCHER)
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
else
# Will be a <ActionController::Parameters {"small"=>"/system/product/:id/images/small.?1421223999"} permitted: true>
authorized_params.delete attribute
end
end
end
authorized_params
ensure
@tempfiles.each &:rewind
# tempfiles.each &:unlink
end
end
end