Add product category importer

This commit is contained in:
2014-09-23 16:00:31 +02:00
parent 29c908a601
commit 1f81f942f9
5 changed files with 104 additions and 0 deletions
@@ -9,6 +9,22 @@ module Suppliers
respond_to do |format|
format.html # index.html.erb
format.json { render json: @product_categories }
format.yaml do
@product_categories.include_relation(:products)
h = {product_categories: []}
@product_categories.each do |product_category|
h[:product_categories] << {
name: product_category.name,
products: product_category.products.map{|product| {
name: product.name,
price: product.price,
code: product.code,
description: product.description
}}
}
end
render text: h.deep_stringify_keys.to_yaml
end
end
end
@@ -39,6 +55,18 @@ module Suppliers
@product_category = ProductCategory.find(params[:id])
end
def import
redirect_to({action: :index}, alert: 'no_file_given') and return unless params[:file].present?
result = ProductCategoriesImporter.new(current_supplier).import(params[:file].read)
if result.errors.any?
redirect_to({action: :index}, alert: result.errors.message)
else
redirect_to action: :index
end
rescue => e
redirect_to({action: :index}, alert: 'something went wrong')
end
# POST /product_categories
# POST /product_categories.json
def create