Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 1 addition & 32 deletions lib/comma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,11 @@ module Comma
}.freeze
end

require 'active_support'
require 'active_support/lazy_load_hooks'
ActiveSupport.on_load(:active_record) do
require 'comma/relation' if defined?(ActiveRecord::Relation)
end

ActiveSupport.on_load(:mongoid) do
require 'comma/mongoid'
end

require 'comma/data_mapper_collection' if defined? DataMapper

require 'comma/options'
require 'comma/generator'
require 'comma/array'
require 'comma/object'

# Load into Rails controllers
ActiveSupport.on_load(:action_controller) do
if defined?(ActionController::Renderers) && ActionController::Renderers.respond_to?(:add)
ActionController::Renderers.add :csv do |obj, options|
filename = options[:filename] || 'data'
extension = options[:extension] || 'csv'
mime_type = if Rails.version >= '5.0.0'
options[:mime_type] || Mime[:csv]
else
options[:mime_type] || Mime::CSV
end
with_bom = options.delete(:with_bom) || false

# Capture any CSV optional settings passed to comma or comma specific options
csv_options = options.slice(*CSV_HANDLER::DEFAULT_OPTIONS.merge(Comma::DEFAULT_OPTIONS).keys)
data = obj.to_comma(csv_options)
data = "\xEF\xBB\xBF#{data}" if with_bom
disposition = "attachment; filename=\"#{filename}.#{extension}\""
send_data data, type: mime_type, disposition: disposition
end
end
end
require 'comma/rails'
16 changes: 16 additions & 0 deletions lib/comma/rails.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

require 'active_support'
require 'active_support/lazy_load_hooks'

ActiveSupport.on_load(:active_record) do
require 'comma/relation' if defined?(ActiveRecord::Relation)
end

ActiveSupport.on_load(:mongoid) do
require 'comma/mongoid'
end

ActiveSupport.on_load(:action_controller) do
require 'comma/rails/renderer'
end
21 changes: 21 additions & 0 deletions lib/comma/rails/renderer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

if defined?(ActionController::Renderers) && ActionController::Renderers.respond_to?(:add)
ActionController::Renderers.add :csv do |obj, options|
filename = options[:filename] || 'data'
extension = options[:extension] || 'csv'
mime_type = if Rails.gem_version >= Gem::Version.new('5.0.0')
options[:mime_type] || Mime[:csv]
else
options[:mime_type] || Mime::CSV
end
with_bom = options.delete(:with_bom) || false

# Capture any CSV optional settings passed to comma or comma specific options
csv_options = options.slice(*CSV_HANDLER::DEFAULT_OPTIONS.merge(Comma::DEFAULT_OPTIONS).keys)
data = obj.to_comma(csv_options)
data = "\xEF\xBB\xBF#{data}" if with_bom
disposition = "attachment; filename=\"#{filename}.#{extension}\""
send_data data, type: mime_type, disposition: disposition
end
end
Loading