Skip to content

Commit

Permalink
add CSV (output) encoding configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Burkhard Vogel-Kreykenbohm committed Nov 26, 2015
1 parent 762b296 commit 0587897
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 4 deletions.
3 changes: 3 additions & 0 deletions lib/generators/wice_grid/templates/wice_grid_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
# Default CSV field separator
Wice::Defaults::CSV_FIELD_SEPARATOR = ','

# Default CSV encoding (p.e. 'CP1252:UTF-8' to make Microsoft Excel(tm) happy)
Wice::Defaults::CSV_ENCODING = nil

# The strategy when to show the filter.
# * <tt>:when_filtered</tt> - when the table is the result of filtering
# * <tt>:always</tt> - show the filter always
Expand Down
2 changes: 1 addition & 1 deletion lib/wice/helpers/wice_grid_view_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ def grid_filter(grid, filter_key)
end

def grid_csv(grid, rendering) #:nodoc:
spreadsheet = ::Wice::Spreadsheet.new(grid.name, grid.csv_field_separator)
spreadsheet = ::Wice::Spreadsheet.new(grid.name, grid.csv_field_separator, grid.csv_encoding)

# columns
spreadsheet << rendering.column_labels(:in_csv)
Expand Down
10 changes: 9 additions & 1 deletion lib/wice/wice_grid_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def export_grid_if_requested(opts = {})
temp_filename = temp_filename.strip
filename = (grid.csv_file_name || grid.name) + '.csv'
grid.csv_tempfile.close
send_file_rails2 temp_filename, filename: filename, type: 'text/csv; charset=utf-8'
send_file_rails2 temp_filename, filename: filename, type: "text/csv; charset=#{get_output_encoding grid.csv_encoding}"
grid.csv_tempfile = nil
true
else
Expand Down Expand Up @@ -162,6 +162,14 @@ def wice_grid_custom_filter_params(opts = {})

private

def get_output_encoding(csv_encoding)
if csv_encoding.blank?
'utf-8'
else
csv_encoding.split(':').first
end
end

def send_file_rails2(path, options = {}) #:nodoc:
fail "Cannot read file #{path}" unless File.file?(path) && File.readable?(path)

Expand Down
3 changes: 2 additions & 1 deletion lib/wice/wice_grid_spreadsheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ class Spreadsheet #:nodoc:
#:nodoc:
attr_reader :tempfile

def initialize(name, field_separator) #:nodoc:
def initialize(name, field_separator, encoding = nil) #:nodoc:
@tempfile = Tempfile.new(name)
@tempfile.set_encoding(encoding) unless encoding.blank?
@csv = CSV.new(@tempfile, col_sep: field_separator)
end

Expand Down
4 changes: 3 additions & 1 deletion lib/wice_grid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class WiceGridEngine < ::Rails::Engine #:nodoc:
# Main class responsible for keeping the state of the grid, building an ActiveRelation, and running queries
class WiceGrid
attr_reader :klass, :name, :resultset, :custom_order, :query_store_model #:nodoc:
attr_reader :ar_options, :status, :export_to_csv_enabled, :csv_file_name, :csv_field_separator, :saved_query #:nodoc:
attr_reader :ar_options, :status, :export_to_csv_enabled, :csv_file_name, :csv_field_separator, :csv_encoding, :saved_query #:nodoc:
attr_writer :renderer #:nodoc:
attr_accessor :output_buffer, :view_helper_finished, :csv_tempfile #:nodoc:

Expand Down Expand Up @@ -110,6 +110,7 @@ def initialize(klass_or_relation, controller, opts = {}) #:nodoc:
conditions: nil,
csv_file_name: nil,
csv_field_separator: ConfigurationProvider.value_for(:CSV_FIELD_SEPARATOR),
csv_encoding: ConfigurationProvider.value_for(:CSV_ENCODING),
custom_order: {},
enable_export_to_csv: ConfigurationProvider.value_for(:ENABLE_EXPORT_TO_CSV),
group: nil,
Expand Down Expand Up @@ -138,6 +139,7 @@ def initialize(klass_or_relation, controller, opts = {}) #:nodoc:
@export_to_csv_enabled = @options[:enable_export_to_csv]
@csv_file_name = @options[:csv_file_name]
@csv_field_separator = @options[:csv_field_separator]
@csv_encoding = @options[:csv_encoding]

case @name = @options[:name]
when String
Expand Down
3 changes: 3 additions & 0 deletions spec/support/wice_grid_test_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
# Default CSV field separator
Wice::Defaults::CSV_FIELD_SEPARATOR = ','

# Default CSV encoding
Wice::Defaults::CSV_ENCODING = nil

# The strategy when to show the filter.
# * <tt>:when_filtered</tt> - when the table is the result of filtering
# * <tt>:always</tt> - show the filter always
Expand Down

0 comments on commit 0587897

Please sign in to comment.