Class: RenderCSV
- Inherits:
-
Object
- Object
- RenderCSV
- Includes:
- ActionView::Helpers::NumberHelper
- Defined in:
- lib/render_csv.rb
Direct Known Subclasses
ArticlesCsv, FinancialTransactionsCsv, OrderCsv, OrdergroupsCsv
Instance Method Summary collapse
- #data {|[]| ... } ⇒ Object
- #header ⇒ Object
-
#initialize(object, options = {}) ⇒ RenderCSV
constructor
A new instance of RenderCSV.
-
#number_to_currency(number, options = {}) ⇒ Object
XXX disable unit to avoid encoding problems, both in unit and whitespace.
-
#save_tmp ⇒ Object
Helper method to test pdf via rails console: OrderCsv.new(order).save_tmp.
- #to_csv ⇒ Object
Constructor Details
#initialize(object, options = {}) ⇒ RenderCSV
Returns a new instance of RenderCSV.
6 7 8 9 10 11 12 13 |
# File 'lib/render_csv.rb', line 6 def initialize(object, ={}) @object = object @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 |
Instance Method Details
#data {|[]| ... } ⇒ Object
30 31 32 |
# File 'lib/render_csv.rb', line 30 def data yield [] end |
#header ⇒ Object
26 27 28 |
# File 'lib/render_csv.rb', line 26 def header nil end |
#number_to_currency(number, options = {}) ⇒ Object
XXX disable unit to avoid encoding problems, both in unit and whitespace. Also allows computations in spreadsheet.
41 42 43 |
# File 'lib/render_csv.rb', line 41 def number_to_currency(number, ={}) super(number, .merge({unit: ''})) end |
#save_tmp ⇒ Object
Helper method to test pdf via rails console: OrderCsv.new(order).save_tmp
35 36 37 38 |
# File 'lib/render_csv.rb', line 35 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 |
#to_csv ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/render_csv.rb', line 15 def to_csv = @options.select {|k| %w(col_sep row_sep).include? k.to_s} ret = CSV.generate do |csv| if h = header csv << h end data {|d| csv << d} end ret.encode(@options[:encoding], invalid: :replace, undef: :replace) end |