<commit_before>class LineItem < ActiveRecord::Base
  belongs_to :order

  # freezes product attributes in case product name, price, etc. changes
  attr_accessible :quantity, :color, :size, :price, :total, :flatrate_shipping_cost,
    :product_id, :product_name
  validates_presence_of :quantity, :price, :total, :product_id, :product_name

  validate :line_item_quantity_must_be_less_than_or_equal_to_product_quantity
  validate :line_item_order_product_status_must_be_active

  def line_item_quantity_must_be_less_than_or_equal_to_product_quantity
    unless self.product.quantity.nil?
      if self.quantity > self.product.quantity && self.product.unlimited_quantity == false
        errors.add(:quantity, 'must be less than or equal to product quantity')
      end
    else
      # this should *never* happen, but just in case
      if self.product.unlimited_quantity == false
        errors.add(:quantity, 'must be less than or equal to product quantity')
      end
    end
  end

  def line_item_order_product_status_must_be_active
    unless self.product.status == 'Active'
      errors.add(:quantity, 'You cannot purchase a product whose status is not active.')
    end
  end
end<commit_msg>adding product_attributes validation to LineItem<commit_after>class LineItem < ActiveRecord::Base
  belongs_to :order

  # freezes product attributes in case product name, price, etc. changes
  attr_accessible :quantity, :color, :size, :price, :total, :flatrate_shipping_cost,
    :product_id, :product_name
  validates_presence_of :quantity, :price, :total, :product_id, :product_name

  validate :line_item_quantity_must_be_less_than_or_equal_to_product_quantity
  validate :line_item_order_product_status_must_be_active
  validate :product_attributes

  def product_attributes
    product = Product.find(self.product_id)

    unless self.flatrate_shipping_cost.nil?
      if product.flatrate_shipping_cost != self.flatrate_shipping_cost
        errors.add(:flatrate_shipping_cost, 'must be valid.')
      end
    end
    if product.name != self.product_name
      errors.add(:product_name, 'must be valid.')
    end
    if product.price != self.price
      errors.add(:price, 'must be valid.')
    end
    if product.id != self.product_id
      errors.add(:product_id, 'must be valid.')
    end
  end

  def line_item_quantity_must_be_less_than_or_equal_to_product_quantity
    unless self.product.quantity.nil?
      if self.quantity > self.product.quantity && self.product.unlimited_quantity == false
        errors.add(:quantity, 'must be less than or equal to product quantity')
      end
    else
      # this should *never* happen, but just in case
      if self.product.unlimited_quantity == false
        errors.add(:quantity, 'must be less than or equal to product quantity')
      end
    end
  end

  def line_item_order_product_status_must_be_active
    unless self.product.status == 'Active'
      errors.add(:quantity, 'You cannot purchase a product whose status is not active.')
    end
  end
end
{'fraction_non_alphanumeric': 0.07669322709163347, 'fraction_numerical': 0.0, 'mean_word_length': 4.038461538461538, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2229221', 'n_tokens_mistral': 934, 'n_tokens_neox': 895, 'n_words': 243}