## fq
class BidingProject < ActiveRecord::Base
  attr_accessible :bid_id, :project_id, :user_id, :description,:reward
  
  belongs_to :bid
  belongs_to :project
  belongs_to :user
  
  DESCRIPTION_LENGTH_LIMIT = 500
  validates :description, length: {maximum: DESCRIPTION_LENGTH_LIMIT}
  validates :user_id, presence: true
  validates :bid_id, presence: true, :uniqueness => { :scope => :project_id}
  validates :project_id, presence: true
  validate :validate_user
  validate :validate_bid
  validate :validate_project
  
  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

  # used to update the reward ,the value varies from 0,1,2,3,4,5
  # added by william
  def update_reward(which)
    self.update_attribute(:reward,which)
  end
  
  # return the reward,the value is a type of string
  # added by william
  def get_reward
    self.reward
  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