## fq
class BidingProject < ActiveRecord::Base
  attr_accessible :bid_id, :project_id, :user_id, :description
  
  belongs_to :bid
  belongs_to :project
  belongs_to :user
  
  DESCRIPTION_LENGTH_LIMIT = 500
  
  validates_length_of :description, :maximum => DESCRIPTION_LENGTH_LIMIT
  validates_presence_of :user_id, :bid_id, :project_id
  validate :validate_user
  validate :validate_bid
  validate :validate_project
  validates_uniqueness_of :bid_id, :scope => :project_id
  
  def self.cerate_bidding(bid_id, project_id, description = nil)
    self.create(:user_id => User.current.id, :bid_id => bid_id,
                    :project_id => project_id, :description => description)
  end
  
  def cancel_bidding
    unless self.nil?
      if User.current.id == self.user_id
        self.destroy  
      end   
    end   
  end
  
  private
  
  def validate_user
    errors.add :user_id, :invalid if user.nil? || !user.active?
  end
  
  def validate_bid
    errors.add :bid_id, :invalid if bid.nil?
  end
  
  def validate_project
    errors.add :project_id, :invalid if project.nil?
  end
    
end
