<commit_before># == Schema Information
# Schema version: 20090216032013
#
# Table name: bids
#
#  id              :integer(4)      not null, primary key
#  req_id          :integer(4)      
#  person_id       :integer(4)      
#  status_id       :integer(4)      
#  estimated_hours :decimal(8, 2)   default(0.0)
#  actual_hours    :decimal(8, 2)   default(0.0)
#  expiration_date :datetime        
#  created_at      :datetime        
#  updated_at      :datetime        
#  accepted_at     :datetime        
#  committed_at    :datetime        
#  completed_at    :datetime        
#  approved_at     :datetime        
#  rejected_at     :datetime        
#

class Bid < ActiveRecord::Base
  before_validation_on_create :setup_estimated_hours
  after_validation_on_create :trigger_offered

  include ActionController::UrlWriter
  include AASM

  belongs_to :req
  belongs_to :person
  validates_presence_of :estimated_hours, :person_id
  attr_readonly :estimated_hours

  attr_protected :person_id, :created_at, :updated_at
  attr_protected :status_id, :state

  aasm_column :state

  aasm_initial_state :offered

  aasm_state :offered
  aasm_state :accepted, :enter => :trigger_accepted
  aasm_state :committed, :enter => :trigger_committed
  aasm_state :completed, :enter => :trigger_completed
  aasm_state :approved, :enter => :trigger_approved

  aasm_event :accept do
    transitions :to => :accepted, :from => :offered
  end

  aasm_event :commit do
    transitions :to => :committed, :from => :accepted
  end

  aasm_event :complete do
    transitions :to => :completed, :from => :committed
  end

  aasm_event :pay do
    transitions :to => :approved, :from => [:accepted,:committed,:completed]
  end

  def requestor_event_for_current_state
    requestor_events = requestor_events_for_current_state
    requestor_events[0]
  end

  def bidder_event_for_current_state
    bidder_events = bidder_events_for_current_state
    bidder_events[0]
  end

  def requestor_events_for_current_state
    r_events = aasm_events_for_current_state.map {|event| event.to_s}
    r_events.find_all {|event| ['accept','pay'].include? event}
  end

  def bidder_events_for_current_state
    b_events = aasm_events_for_current_state.map {|event| event.to_s}
    b_events.find_all {|event| ['commit','complete'].include? event}
  end

  private

  def setup_estimated_hours
    if self.expiration_date.blank?
      self.expiration_date = 7.days.from_now
    else
      self.expiration_date += 1.day - 1.second # make expiration date at end of day
    end
  end

  def trigger_offered
    bid_note = Message.new()
    subject = "BID: " + self.estimated_hours.to_s + " hours - " + self.req.name 
    bid_note.subject = subject.length > 75 ? subject.slice(0,75).concat("...") : subject
    bid_note.content = ""
    bid_note.content << self.private_message_to_requestor + "\n--\n\n" if self.private_message_to_requestor.length > 0
    bid_note.content << "See your <a href=\"" + req_path(self.req) + "\">request</a> to consider bid"
    bid_note.sender = self.person
    bid_note.recipient = self.req.person
    bid_note.save!
  end

  def trigger_accepted
    self.accepted_at = Time.now
    save<commit_msg>prevent a negative bid. closes gh-24<commit_after># == Schema Information
# Schema version: 20090216032013
#
# Table name: bids
#
#  id              :integer(4)      not null, primary key
#  req_id          :integer(4)      
#  person_id       :integer(4)      
#  status_id       :integer(4)      
#  estimated_hours :decimal(8, 2)   default(0.0)
#  actual_hours    :decimal(8, 2)   default(0.0)
#  expiration_date :datetime        
#  created_at      :datetime        
#  updated_at      :datetime        
#  accepted_at     :datetime        
#  committed_at    :datetime        
#  completed_at    :datetime        
#  approved_at     :datetime        
#  rejected_at     :datetime        
#

class Bid < ActiveRecord::Base
  before_validation_on_create :setup_estimated_hours
  after_validation_on_create :trigger_offered

  include ActionController::UrlWriter
  include AASM

  belongs_to :req
  belongs_to :person
  validates_presence_of :estimated_hours, :person_id
  attr_readonly :estimated_hours

  attr_protected :person_id, :created_at, :updated_at
  attr_protected :status_id, :state

  aasm_column :state

  aasm_initial_state :offered

  aasm_state :offered
  aasm_state :accepted, :enter => :trigger_accepted
  aasm_state :committed, :enter => :trigger_committed
  aasm_state :completed, :enter => :trigger_completed
  aasm_state :approved, :enter => :trigger_approved

  aasm_event :accept do
    transitions :to => :accepted, :from => :offered
  end

  aasm_event :commit do
    transitions :to => :committed, :from => :accepted
  end

  aasm_event :complete do
    transitions :to => :completed, :from => :committed
  end

  aasm_event :pay do
    transitions :to => :approved, :from => [:accepted,:committed,:completed]
  end

  def requestor_event_for_current_state
    requestor_events = requestor_events_for_current_state
    requestor_events[0]
  end

  def bidder_event_for_current_state
    bidder_events = bidder_events_for_current_state
    bidder_events[0]
  end

  def requestor_events_for_current_state
    r_events = aasm_events_for_current_state.map {|event| event.to_s}
    r_events.find_all {|event| ['accept','pay'].include? event}
  end

  def bidder_events_for_current_state
    b_events = aasm_events_for_current_state.map {|event| event.to_s}
    b_events.find_all {|event| ['commit','complete'].include? event}
  end

  private

  def validate
    unless estimated_hours > 0
      errors.add(:estimated_hours, "must be greater than zero")
    end
  end

  def setup_estimated_hours
    if self.expiration_date.blank?
      self.expiration_date = 7.days.from_now
    else
      self.expiration_date += 1.day - 1.second # make expiration date at end of day
    end
  end

  def trigger_offered
    bid_note = Message.new()
    subject = "BID: " + self.estimated_hours.to_s + " hours - " + self.req.name 
    bid_note.subject = subject.length > 75 ? subject.slice(0,75).concat("...") : subject
    bid_note.content = ""
    bid_note.content << self.private_message_to_requestor + "\n--\n\n" if self.private_message_to_requestor.length > 0
    bid_note.content << "See your <a href=\"" + req_path(self.req) + "\">request</a> to consider bid"
    bid_note.sender = self.person
    bid_note.recipient = self.req.person
    bid_note.save!
  end

  def trigger_accepted
    self.accepted_at = Time.now
    save
{'fraction_non_alphanumeric': 0.09544203120220252, 'fraction_numerical': 0.011777301927194861, 'mean_word_length': 3.3593333333333333, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 36, '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': '5170217', 'n_tokens_mistral': 2294, 'n_tokens_neox': 2192, 'n_words': 469}