DavyJonesLocker / DavyJonesLocker/client_side_validations

Feature request: Optionally require CSV's modules

Open
#808 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
2.7k
Forks
395
Avg merge
3m
Merged PRs (30d)
1

Description

Hello! As mentioned in my comment here: https://github.com/DavyJonesLocker/client_side_validations/pull/795#issuecomment-691235835

I'm building some "Form Objects" serialized as json for use with client side schema libraries. As such, I do not need the entire CSV gem, so I've added it via gem 'client_side_validations', require: false

However, I noticed here that CSV is included in all instances of ActiveRecord, which I don't really need for my purposes. So as a workaround, I've created a CSVModelProxy class that includes them manually:

# Only include required modules from CSV for building JSON schema
require 'client_side_validations/config'
require 'client_side_validations/active_model'
class CSVModelProxy
  include ClientSideValidations::ActiveModel::Validations
  def initialize(original_model)
    @original_model = original_model
  end
  # Pass along all methods called by CSV (or anyone) to its original instance
  def method_missing(method_name, *args)
    @original_model.send(method_name, *args) || super
  end
end

This seems to work well (performance implications still TBD). But then, I noticed CSV also includes all its modules on all instances of ActiveModel::Validations and ActiveModel::Validator. Again this is overkill for my purposes, and I'd rather not introduce any complications into our ActiveModel::Model instances when I'm really just using CSV as a sort of serializer tool.

So it would be nice to have the option to only import some kind of wrapper class, e.g.

require 'client_side_validations/active_model_wrapper'
class FormObject
  def initialize(resource)
    @resource = ClientSideValidations::ActiveModelWrapper.new(resource)
    @validation_hash = @resource.client_side_validation_hash
  end
end

Would you be interested in a PR to achieve this?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading lib/client_side_validations/active_record.rb and lib/client_side_validations/active_model.rb, especially the module inclusions described in the issue. Trace how the existing ActiveModel and ActiveRecord integrations are loaded, then define the smallest wrapper entry point that avoids those global inclusions. Done means callers can require the wrapper and obtain the validation hash without importing the full CSV integration.

Written by the indexing model from the issue text.

Assessment

Tech stack
rails, ruby
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.