From 02832d76ccfef2c6a0ca7c09a0ea47b3eaa3e01b Mon Sep 17 00:00:00 2001 From: Eito Katagiri Date: Sat, 5 Sep 2026 19:55:50 +0900 Subject: [PATCH] Extract Rails integration from lib/comma.rb Move the ActiveSupport.on_load hooks and the render csv: renderer out of the gem entry point into lib/comma/rails.rb and lib/comma/rails/renderer.rb, so lib/comma.rb stays a thin loader and the Rails integration can be read and reviewed in isolation. Closes #164 Co-Authored-By: Claude Sonnet 5 --- lib/comma.rb | 33 +-------------------------------- lib/comma/rails.rb | 16 ++++++++++++++++ lib/comma/rails/renderer.rb | 21 +++++++++++++++++++++ 3 files changed, 38 insertions(+), 32 deletions(-) create mode 100644 lib/comma/rails.rb create mode 100644 lib/comma/rails/renderer.rb diff --git a/lib/comma.rb b/lib/comma.rb index 9f39819..1350d1c 100644 --- a/lib/comma.rb +++ b/lib/comma.rb @@ -11,16 +11,6 @@ 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' @@ -28,25 +18,4 @@ module Comma 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' diff --git a/lib/comma/rails.rb b/lib/comma/rails.rb new file mode 100644 index 0000000..19d0dca --- /dev/null +++ b/lib/comma/rails.rb @@ -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 diff --git a/lib/comma/rails/renderer.rb b/lib/comma/rails/renderer.rb new file mode 100644 index 0000000..ad09c3a --- /dev/null +++ b/lib/comma/rails/renderer.rb @@ -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