最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ruby on rails - I have been attempting an interesting problem in a parent child object relationship regarding validators… - Stac

programmeradmin4浏览0评论

Both the parent and child have different validators via validate… or validates…

I want to store the result of each validator across all of them

the valid? command is run on the parent (edited)

when I run valid? on the parent I can verify with break points all 4 validators 2 on the parent and 2 on child are executed correctly

I am looking to monkey patch the process that loops through each of them

This is an example from AI

  def run_validations!(context = nil)
    @validator_results = {}

    # Gather all validators from this class and its ancestors.
    # Note: self.class.validators normally already merges them,
    # but using ancestors ensures we capture everything.
    all_validators = self.class.ancestors
                         .select { |ancestor| ancestor.respond_to?(:_validators) }
                         .map { |ancestor| ancestor.send(:_validators) }
                         .uniq


    

    all_validators.each do |validator|
      # Capture errors before running the validator.
      errors_before = errors.dup

      # Run the validator on self.
      validator.validate(self)

      # Determine if the validator added any new errors.
      new_errors = errors.to_hash.deep_dup.reject { |attr, messages| errors_before[attr] == messages }
      passed = new_errors.empty?

      @validator_results[validator.class.name] = {
        passed: passed,
        new_errors: new_errors
      }
    end
    debugger
    errors.empty?
  end

The method should return all 4 validators but does not

all_validators = self.class.ancestors
                         .select { |ancestor| ancestor.respond_to?(:_validators) }
                         .map { |ancestor| ancestor.send(:_validators) }
                         .uniq

thoughts?

I know this must be possible, I just can’t see where to patch

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论