Class: RailsAdmin::CSVConverter
- Inherits:
-
Object
- Object
- RailsAdmin::CSVConverter
- Defined in:
- lib/rails_admin/support/csv_converter.rb
Instance Method Summary collapse
-
#initialize(objects = [], schema = {}) ⇒ CSVConverter
constructor
A new instance of CSVConverter.
- #to_csv(options = {}) ⇒ Object
Constructor Details
#initialize(objects = [], schema = {}) ⇒ CSVConverter
Returns a new instance of CSVConverter.
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/rails_admin/support/csv_converter.rb', line 10 def initialize(objects = [], schema = {}) return self if (@objects = objects).blank? @model = objects.dup.first.class @abstract_model = RailsAdmin::AbstractModel.new(@model) @model_config = @abstract_model.config @methods = [(schema[:only] || []) + (schema[:methods] || [])].flatten.compact @fields = @methods.collect { |m| export_fields_for(m).first } @empty = ::I18n.t('admin.export.empty_value_for_associated_objects') schema_include = schema.delete(:include) || {} @associations = schema_include.inject({}) do |hash, (key, values)| association = association_for(key) model_config = association.associated_model_config abstract_model = model_config.abstract_model methods = [(values[:only] || []) + (values[:methods] || [])].flatten.compact hash[key] = { association: association, model: abstract_model.model, abstract_model: abstract_model, model_config: model_config, fields: methods.collect { |m| export_fields_for(m, model_config).first } } hash end end |
Instance Method Details
#to_csv(options = {}) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/rails_admin/support/csv_converter.rb', line 38 def to_csv( = {}) # encoding shenanigans first @encoding_from = UTF8_ENCODINGS.include?(@abstract_model.encoding) ? 'UTF-8' : @abstract_model.encoding @encoding_to = [:encoding_to].presence || @encoding_from csv_string = generate_csv_string() # Add a BOM for utf8 encodings, helps with utf8 auto-detect for some versions of Excel. # Don't add if utf8 but user don't want to touch input encoding: # If user chooses utf8, they will open it in utf8 and BOM will disappear at reading. # But that way "English" users who don't bother and chooses to let utf8 by default won't get BOM added # and will not see it if Excel opens the file with a different encoding. if [:encoding_to] == 'UTF-8' csv_string = csv_string.force_encoding('UTF-8') if csv_string.respond_to?(:force_encoding) csv_string = "\xEF\xBB\xBF#{csv_string}" end # global conversion for non ASCII encodings if @encoding_to =~ NON_ASCII_ENCODINGS && @encoding_to != @encoding_from require 'iconv' begin if @iconv = Iconv.new("#{@encoding_to}//TRANSLIT//IGNORE", @encoding_from) csv_string = @iconv.iconv(csv_string) rescue csv_string end rescue Rails.logger.error("Iconv cannot convert to #{@encoding_to}: #{$ERROR_INFO}\nNo conversion will take place") end end [![:skip_header], @encoding_to, csv_string] end |