Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Optionally require CSV's modules #808

Open
mattboldt opened this issue Sep 17, 2020 · 0 comments
Open

Feature request: Optionally require CSV's modules #808

mattboldt opened this issue Sep 17, 2020 · 0 comments

Comments

@mattboldt
Copy link

Hello! As mentioned in my comment here: #795 (comment)

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant