class TransactionItem < ActiveRecord::Base
  belongs_to :donator_transaction
  
  belongs_to :transaction_content, :polymorphic => true
  accepts_nested_attributes_for :transaction_content
  
  
  validates :amount, :presence => true, :numericality => {:greater_than => 1.0}
  
  before_validation do
    puts "TTEEEEEEEEEEEETTEEEEEEEEEEEETTEEEEEEEEEEEETTEEEEEEEEEEEETTEEEEEEEEEEEETTEEEEEEEEEEEETTEEEEEEEEEEEETTEEEEEEEEEEEETTEEEEEEEEEEEETTEEEEEEEEEEEETTEEEEEEEEEEEETTEEEEEEEEEEEETTEEEEEEEEEEEETTEEEEEEEEEEEETTEEEEEEEEEEEETTEEEEEEEEEEEE"
  end
  
  def attributes=(attributes = {})
      self.transaction_content_type = attributes[:transaction_content_type]
      super
    end

    def transaction_content_attributes=(attributes)
      if self.transaction_content_id.to_i > 0
        some_transaction_content = self.transaction_content_type.constantize.find(self.transaction_content_id)
      
      else
        some_transaction_content = self.transaction_content_type.constantize.new()
      end
      
      
      some_transaction_content.attributes = attributes
      self.transaction_content = some_transaction_content
    end
end
