<commit_before>class Promotion < ActiveRecord::Base
  has_many :orders
  has_many :order_items
  has_many :conditions, :class_name => 'PromotionCondition', :order => 'type ASC'
  has_many :effects,    :class_name => 'PromotionEffect',    :order => 'type ASC'

  attr_accessible :name, :start_at, :end_at, :conditions_attributes, :effects_attributes, :active, :description

  accepts_nested_attributes_for :conditions, :reject_if => :condition_or_effect_inactive?
  accepts_nested_attributes_for :effects,    :reject_if => :condition_or_effect_inactive?

  before_save :clean_conditions_and_effects

  validations_from_schema
  validates_associated :conditions, :effects

  def active?
    now = Time.now
    start_at <= now and (end_at.nil? || end_at >= now)
  end

  # Derives a description of the promotion by looking at the conditions and the
  # effect.
  def summary

  end

  def qualifies?(order)
    conditions.map {|c| c.qualifies?(order)}.all?
  end

  def apply!(order)<commit_msg>Fix the condition and effect update/removal to run before validation.<commit_after>class Promotion < ActiveRecord::Base
  has_many :orders
  has_many :order_items
  has_many :conditions, :class_name => 'PromotionCondition', :order => 'type ASC'
  has_many :effects,    :class_name => 'PromotionEffect',    :order => 'type ASC'

  attr_accessible :name, :start_at, :end_at, :conditions_attributes, :effects_attributes, :active, :description

  accepts_nested_attributes_for :conditions
  accepts_nested_attributes_for :effects

  before_validation :clean_conditions_and_effects

  validations_from_schema
  validates_associated :conditions, :effects

  def active?
    now = Time.now
    start_at <= now and (end_at.nil? || end_at >= now)
  end

  # Derives a description of the promotion by looking at the conditions and the
  # effect.
  def summary

  end

  def qualifies?(order)
    conditions.map {|c| c.qualifies?(order)}.all?
  end

  def apply!(order)
{'fraction_non_alphanumeric': 0.09533572526909277, 'fraction_numerical': 0.0, 'mean_word_length': 4.861861861861862, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, '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': '1322460', 'n_tokens_mistral': 630, 'n_tokens_neox': 600, 'n_words': 162}