Skip to content

Extract Rails integration from lib/comma.rb #164

Description

@eitoball

Summary

Move Rails-specific integration (lazy hooks and the render csv: renderer) out of lib/comma.rb into dedicated files under lib/comma/rails/.

Problem

lib/comma.rb currently mixes concerns:

  • Gem boot and requires
  • ActiveSupport.on_load hooks for ActiveRecord, Mongoid, ActionController
  • ~30 lines registering the :csv renderer (filename, BOM, MIME type, option slicing, send_data)

This makes the entry point harder to read and means Rails integration cannot be tested or reviewed in isolation without scrolling through unrelated code.

Proposed approach

lib/comma.rb                    # thin loader only
lib/comma/rails.rb              # on_load hooks, require integration files
lib/comma/rails/renderer.rb     # ActionController::Renderers.add :csv

Keep the same lazy-loading behavior via ActiveSupport.on_load(:action_controller).

Optional follow-up (separate issue): require comma/rails only when Rails is present; today the hooks are in the main require path but no-op until frameworks load.

Files likely involved

  • lib/comma.rb (slim down)
  • New: lib/comma/rails.rb, lib/comma/rails/renderer.rb
  • spec/controllers/users_controller_spec.rb (integration regression)
  • spec/spec_helper.rb (load path unchanged for tests)

Acceptance criteria

  • require 'comma' behavior unchanged for non-Rails apps
  • render csv: User.all still works in controller specs
  • All renderer options (filename, extension, BOM, write_headers, etc.) still pass controller specs
  • lib/comma.rb no longer contains renderer implementation details
  • No public API change

Labels (suggested)

refactor

Depends on

Optional: #3 (options normalization) — renderer extraction is simpler if options parsing already lives in Comma::Options.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions