make csv export more compatible with Microsoft Excel
This commit is contained in:
parent
71a922a034
commit
724ce6ff60
2 changed files with 62 additions and 31 deletions
|
@ -1,16 +1,9 @@
|
||||||
require 'csv'
|
require 'csv'
|
||||||
|
|
||||||
class OrderCsv
|
class OrderCsv < RenderCSV
|
||||||
include ActionView::Helpers::NumberHelper
|
|
||||||
|
|
||||||
def initialize(order, options={})
|
def header
|
||||||
@order = order
|
[
|
||||||
end
|
|
||||||
|
|
||||||
def to_csv
|
|
||||||
CSV.generate do |csv|
|
|
||||||
# header
|
|
||||||
csv << [
|
|
||||||
OrderArticle.human_attribute_name(:units_to_order),
|
OrderArticle.human_attribute_name(:units_to_order),
|
||||||
Article.human_attribute_name(:order_number),
|
Article.human_attribute_name(:order_number),
|
||||||
Article.human_attribute_name(:name),
|
Article.human_attribute_name(:name),
|
||||||
|
@ -18,9 +11,11 @@ class OrderCsv
|
||||||
ArticlePrice.human_attribute_name(:price),
|
ArticlePrice.human_attribute_name(:price),
|
||||||
OrderArticle.human_attribute_name(:total_price)
|
OrderArticle.human_attribute_name(:total_price)
|
||||||
]
|
]
|
||||||
# data
|
end
|
||||||
@order.order_articles.ordered.includes([:article, :article_price]).all.map do |oa|
|
|
||||||
csv << [
|
def data
|
||||||
|
@object.order_articles.ordered.includes([:article, :article_price]).all.map do |oa|
|
||||||
|
yield [
|
||||||
oa.units_to_order,
|
oa.units_to_order,
|
||||||
oa.article.order_number,
|
oa.article.order_number,
|
||||||
oa.article.name,
|
oa.article.name,
|
||||||
|
@ -30,10 +25,4 @@ class OrderCsv
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
# Helper method to test pdf via rails console: OrderCsv.new(order).save_tmp
|
|
||||||
def save_tmp
|
|
||||||
File.open("#{Rails.root}/tmp/#{self.class.to_s.underscore}.csv", 'w') {|f| f.write(to_csv.force_encoding("UTF-8")) }
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
42
lib/render_csv.rb
Normal file
42
lib/render_csv.rb
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
require 'csv'
|
||||||
|
|
||||||
|
class RenderCSV
|
||||||
|
include ActionView::Helpers::NumberHelper
|
||||||
|
|
||||||
|
def initialize(object, options={})
|
||||||
|
@object = object
|
||||||
|
@options = options
|
||||||
|
# defaults to please Microsoft Excel ...
|
||||||
|
@options[:col_sep] ||= FoodsoftConfig[:csv_col_sep] || ';'
|
||||||
|
@options[:row_sep] ||= FoodsoftConfig[:csv_row_sep] if FoodsoftConfig[:csv_row_sep]
|
||||||
|
@options[:encoding] ||= FoodsoftConfig[:csv_encoding] || 'ISO-8859-15'
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_csv
|
||||||
|
CSV.generate @options do |csv|
|
||||||
|
if h = header
|
||||||
|
csv << h
|
||||||
|
end
|
||||||
|
data {|d| csv << d}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def header
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def data
|
||||||
|
yield []
|
||||||
|
end
|
||||||
|
|
||||||
|
# Helper method to test pdf via rails console: OrderCsv.new(order).save_tmp
|
||||||
|
def save_tmp
|
||||||
|
encoding = @options[:encoding] || 'UTF-8'
|
||||||
|
File.open("#{Rails.root}/tmp/#{self.class.to_s.underscore}.csv", 'w') {|f| f.write(to_csv.force_encoding(encoding)) }
|
||||||
|
end
|
||||||
|
|
||||||
|
# XXX avoid encoding confusion when using unicode whitespace
|
||||||
|
def number_to_currency(number, options={})
|
||||||
|
super(number, options).gsub("\u202f", ' ')
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue