<commit_before>class Order < ActiveRecord::Base  
  before_create :generate_order_number
  before_save :update_line_items
  
  has_many :line_items, :dependent => :destroy, :attributes => true do
    def in_order(variant)
      find :first, :conditions => ['variant_id = ?', variant.id]
    end
  end
  has_many :products, :through => :line_items
  has_many :inventory_units
  has_many :state_events
  has_many :payments
  has_many :creditcard_payments
  has_many :creditcards
  belongs_to :user

  validates_associated :line_items, :message => "are not valid"
  validates_numericality_of :tax_amount
  validates_numericality_of :ship_amount
  validates_numericality_of :item_total
  validates_numericality_of :total

  named_scope :by_number, lambda {|number| {:conditions => ["orders.number = ?", number]}}
  named_scope :between, lambda {|*dates| {:conditions => ["orders.created_at between :start and :stop", {:start => dates.first.to_date, :stop => dates.last.to_date}]}}
  named_scope :by_customer, lambda {|customer| {:include => :user, :conditions => ["users.email = ?", customer]}}
  named_scope :by_state, lambda {|state| {:conditions => ["state = ?", state]}}
  named_scope :checkout_completed, lambda {|state| {:conditions => ["checkout_complete = ?", state]}}
  
  
  # attr_accessible is a nightmare with attachment_fu, so use attr_protected instead.
  attr_protected :ship_amount, :tax_amount, :item_total, :total, :user, :number, :ip_address, :checkout_complete, :state
  
  # order state machine (see http://github.com/pluginaweek/state_machine/tree/master for details)
  state_machine :initial => 'in_progress' do    
    after_transition :to => 'in_progress', :do => lambda {|order| order.update_attribute(:checkout_complete, false)}
    after_transition :to => 'charged', :do => :complete_order
    after_transition :to => 'canceled', :do => :cancel_order
    after_transition :to => 'returned', :do => :restock_inventory
    after_transition :to => 'resumed', :do => :restore_state 

    before_transition :to => 'paid', :do => :record_payment_event
    
    event :next do
      transition :to => 'creditcard', :from => 'in_progress'
      transition :to => 'charged', :from => 'creditcard'
    end
    event :edit do
      transition :to => 'in_progress', :from => %w{creditcard in_progress}
    end
    event :cancel do
      transition :to => 'canceled', :if => :allow_cancel?
    end
    event :return do
      transition :to => 'returned', :from => 'shipped'
    end
    event :resume do 
      transition :to => 'resumed', :from => 'canceled', :if => :allow_resume?
    end    
    event :pay do
      transition :to => 'paid', :if => :allow_pay?
    end
  end
<commit_msg>Fixed state machine so that orders are considered 'new' after credit card authorization.<commit_after>class Order < ActiveRecord::Base  
  before_create :generate_order_number
  before_save :update_line_items
  
  has_many :line_items, :dependent => :destroy, :attributes => true do
    def in_order(variant)
      find :first, :conditions => ['variant_id = ?', variant.id]
    end
  end
  has_many :products, :through => :line_items
  has_many :inventory_units
  has_many :state_events
  has_many :payments
  has_many :creditcard_payments
  has_many :creditcards
  belongs_to :user

  validates_associated :line_items, :message => "are not valid"
  validates_numericality_of :tax_amount
  validates_numericality_of :ship_amount
  validates_numericality_of :item_total
  validates_numericality_of :total

  named_scope :by_number, lambda {|number| {:conditions => ["orders.number = ?", number]}}
  named_scope :between, lambda {|*dates| {:conditions => ["orders.created_at between :start and :stop", {:start => dates.first.to_date, :stop => dates.last.to_date}]}}
  named_scope :by_customer, lambda {|customer| {:include => :user, :conditions => ["users.email = ?", customer]}}
  named_scope :by_state, lambda {|state| {:conditions => ["state = ?", state]}}
  named_scope :checkout_completed, lambda {|state| {:conditions => ["checkout_complete = ?", state]}}
  
  
  # attr_accessible is a nightmare with attachment_fu, so use attr_protected instead.
  attr_protected :ship_amount, :tax_amount, :item_total, :total, :user, :number, :ip_address, :checkout_complete, :state
  
  # order state machine (see http://github.com/pluginaweek/state_machine/tree/master for details)
  state_machine :initial => 'in_progress' do    
    after_transition :to => 'in_progress', :do => lambda {|order| order.update_attribute(:checkout_complete, false)}
    after_transition :to => 'new', :do => :complete_order
    after_transition :to => 'canceled', :do => :cancel_order
    after_transition :to => 'returned', :do => :restock_inventory
    after_transition :to => 'resumed', :do => :restore_state 

    before_transition :to => 'paid', :do => :record_payment_event
    
    event :next do
      transition :to => 'creditcard', :from => 'in_progress'
      transition :to => 'new', :from => 'creditcard'
    end
    event :edit do
      transition :to => 'in_progress', :from => %w{creditcard in_progress}
    end
    event :cancel do
      transition :to => 'canceled', :if => :allow_cancel?
    end
    event :return do
      transition :to => 'returned', :from => 'shipped'
    end
    event :resume do 
      transition :to => 'resumed', :from => 'canceled', :if => :allow_resume?
    end    
    event :pay do
      transition :to => 'paid', :if => :allow_pay?
    end
  end

{'fraction_non_alphanumeric': 0.13293106600841104, 'fraction_numerical': 0.0, 'mean_word_length': 4.321011673151751, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 85, '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': '6230788', 'n_tokens_mistral': 1748, 'n_tokens_neox': 1718, 'n_words': 460}