Module: Utility
- Defined in:
- lib/generators/leolay/templates/lib/utility.rb
Overview
Utility for general purpose
Class Method Summary collapse
-
.export(params = {}) ⇒ Object
Exports data from a collection using fastercsv gem.
Class Method Details
.export(params = {}) ⇒ Object
Exports data from a collection using fastercsv gem
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/generators/leolay/templates/lib/utility.rb', line 4 def self.export(params={}) require 'fastercsv' return "" unless params[:collection] && params[:collection].size>0 collection = add_collection params[:collection] head = params[:head] head = collection.first.keys if !head && collection && collection.first head = [] unless head p18n = params[:i18n] p18n = "activerecord.attributes.#{params[:collection].first.class.to_s.downcase.to_sym}" if p18n == true #only if boolean and equal true p18n_generic = "activerecord.attributes.generic" str = FasterCSV.generate(:col_sep => params[:col_sep] || ";") do |csv| generic_fields = %w(id created_at updated_at) head.map!{|e| generic_fields.include?(e) ? I18n.t("#{p18n_generic}.#{e}") : I18n.t("#{p18n}.#{e}")} if p18n csv << head collection.each do |record| #each record is an hash csv << record.values.map do |v| case v.class.to_s when 'Time', 'Date' v.strftime "%d/%m/%Y" else v end end end end str end |