<commit_before># This prevents relationships from having differing parents
# For example, on an order line item,
# the order's host must be the same as the line item's host
class RestraintPresentValidator < ActiveModel::EachValidator

  def validate_each(record, attribute, value)
    order = record.order
    line_item = record.line_item

    # restraint.restrictable == line_item
    # restraint.dependable == what is constraining
    restraints = line_item.try(:restraints)

    if restraints.present?
      constrained_object_present = false

      restraints.each do |constraint|
        if order.line_item_matching(constraint.restrictable)
          constrained_object_present = true
        end
      end

      unless constrained_object_present
        record.errors.add(attribute, "(#{line_item.name}) #{attribute}'s constraint does not exist in the order")
      end
    end
  end

end<commit_msg>Fixes #181, #189, #45<commit_after>class RestraintPresentValidator < ActiveModel::EachValidator

  def validate_each(record, attribute, value)
    order = record.order
    line_item = record.line_item

    # restraint.restrictable == line_item
    # restraint.dependable == what is constraining
    restraints = line_item.try(:restraints)
    restraints = restraints.select { |r| r.dependable == line_item } if restraints

    if restraints.present?
      constrained_object_present = false

      restraints.each do |constraint|
        if order.line_item_matching(constraint.restrictable)
          constrained_object_present = true
        end
      end

      unless constrained_object_present
        record.errors.add(attribute, "(#{line_item.name}) #{attribute}'s constraint does not exist in the order")
      end
    end
  end

end
{'fraction_non_alphanumeric': 0.07498568975386377, 'fraction_numerical': 0.0045792787635947334, 'mean_word_length': 3.4591836734693877, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1450724', 'n_tokens_mistral': 497, 'n_tokens_neox': 462, 'n_words': 132}