<commit_before>
# == Schema Information
#
# Table name: bikes
#
#  id               :integer         not null, primary key
#  color            :string(255)
#  value            :float
#  seat_tube_height :float
#  top_tube_length  :float
#  created_at       :datetime
#  updated_at       :datetime
#  departed_at      :datetime
#  date_in_shop     :datetime
#  bike_model_id    :integer
#  brand_id         :integer
#  number           :string(255)
#  location_state   :string(255)
#

require "has_one_soft_delete"


class Bike < ActiveRecord::Base
  include HasOneSoftDelete
  extend FriendlyId

  friendly_id :label

  acts_as_commentable

  attr_accessible :color, :value, :wheel_size, :seat_tube_height, :top_tube_length, :bike_model_id, :brand_id, :number, :quality, :condition, :program_id
  
  has_one :hook, :dependent => :nullify, :inverse_of => :bike
  belongs_to :program
  belongs_to :brand
  belongs_to :bike_model

  # Validations
  validates_presence_of :number,:color
  validates :seat_tube_height,:top_tube_length,:value, :numericality => true, :allow_nil => true

  # Callbacks for setting scrap programs as departed
  before_create :depart_scrap
  before_update :depart_scrap
  after_create  :check_hook
  after_update  :check_hook

  WHEEL_SIZES =     [["Unknown",1],
                     ["622 mm",622],
                     ["630 mm",630],
                     ["597 mm",597],
                     ["590 mm",590],
                     ["587 mm",587],
                     ["571 mm",571],
                     ["559 mm",559],
                     ["507 mm",507],
                     ["406 mm",406],
                     ["305 mm",305],
                     ["Other",2]]

  COLORS = ["White","Silver","Gray","Black","Red","Brown","Tan","Maroon",
            "Yellow","Gold","Orange","Olive","DarkGreen","Green","LightGreen",
            "Teal","Blue","LightBlue","Navy","Pink","Purple"]

  STATUSES = ["Available","EAB","Youth","Departed"]

  def depart_scrap
    if self.program_id == Program.where("name = ?","Scrap").first.id
        self.departed_at = DateTime.now()
    end
  end
 
  def check_hook
    if self.departed_at.nil? == false
      if self.hook
        h = Hook.find_by_id(self.hook)
        h.update_attribute(:bike_id,nil)
      end
    end
  end

  def vacate_hook
    if self.nil? == false and self.hook.nil? == false
      h = self.hook
      h.bike = nil
      h.save!
    end
  end

  def reserve_hook_by_id(hook_id)
    if self.nil? == false and self.hook.nil? == true
      h = Hook.find_by_id(hook_id)
      h.bike = self
      h.save!
    end
  end

  def self.filter_bikes(brands,colors,status,sortBy)
    statusSql = []
    if status.nil? or status.empty?
        return []
    end
    if status.include?("-1")
        statusSql.push("departed_at IS NULL AND program_id IS NULL")
        status.delete("-1")
    end
    if status.include?("-2")
        statusSql.push("departed_at NOT NULL")
        status.delete("-2")
    end
    statusSql.push("departed_at IS NULL AND program_id IN (#{status.join(",")})")
    statusSqlString = "(" +  statusSql.join(") OR (") + ")"
    bikes = Bike.select("bikes.*,programs.name,hooks.number as hook_number,COALESCE(brands.name,'n/a') as brand_name")
            .joins("LEFT JOIN hooks ON hooks.bike_id = bikes.id 
                    LEFT JOIN programs ON programs.id = bikes.program_id
                    LEFT JOIN brands ON brands.id = bikes.brand_id")
            .where("(brand_id IN (?) OR brand_id IS NULL) AND color IN (?) AND (#{statusSqlString})",brands,colors)
            .order(sortBy)
    return bikes
  end

  def self.get_bike_details(bike_number)
    bike = Bike.select("bikes.*,programs.name,bike_models.name as model_name,hooks.number as hook_number,brands.name as brand_name")
            .joins("LEFT JOIN hooks ON hooks.bike_id = bikes.id 
                    LEFT JOIN programs ON programs.id = bikes.program_id
                    LEFT JOIN brands ON brands.id = bikes.brand_id
                    LEFT JOIN bike_models ON brands.id = bike_models.brand_id")
            .where("bikes.number = ?",bike_number).first
    return bike
  end

  # Clean up all associations
  # See http://www.mrchucho.net/2008/09/30/the-correct-way-to-override-activerecordbasedestroy
  def destroy_without_callbacks
    unless new_record?<commit_msg>Adding postgres safe db queries<commit_after>
# == Schema Information
#
# Table name: bikes
#
#  id               :integer         not null, primary key
#  color            :string(255)
#  value            :float
#  seat_tube_height :float
#  top_tube_length  :float
#  created_at       :datetime
#  updated_at       :datetime
#  departed_at      :datetime
#  date_in_shop     :datetime
#  bike_model_id    :integer
#  brand_id         :integer
#  number           :string(255)
#  location_state   :string(255)
#

require "has_one_soft_delete"


class Bike < ActiveRecord::Base
  include HasOneSoftDelete
  extend FriendlyId

  friendly_id :label

  acts_as_commentable

  attr_accessible :color, :value, :wheel_size, :seat_tube_height, :top_tube_length, :bike_model_id, :brand_id, :number, :quality, :condition, :program_id
  
  has_one :hook, :dependent => :nullify, :inverse_of => :bike
  belongs_to :program
  belongs_to :brand
  belongs_to :bike_model

  # Validations
  validates_presence_of :number,:color
  validates :seat_tube_height,:top_tube_length,:value, :numericality => true, :allow_nil => true

  # Callbacks for setting scrap programs as departed
  before_create :depart_scrap
  before_update :depart_scrap
  after_create  :check_hook
  after_update  :check_hook

  WHEEL_SIZES =     [["Unknown",1],
                     ["622 mm",622],
                     ["630 mm",630],
                     ["597 mm",597],
                     ["590 mm",590],
                     ["587 mm",587],
                     ["571 mm",571],
                     ["559 mm",559],
                     ["507 mm",507],
                     ["406 mm",406],
                     ["305 mm",305],
                     ["Other",2]]

  COLORS = ["White","Silver","Gray","Black","Red","Brown","Tan","Maroon",
            "Yellow","Gold","Orange","Olive","DarkGreen","Green","LightGreen",
            "Teal","Blue","LightBlue","Navy","Pink","Purple"]

  STATUSES = ["Available","EAB","Youth","Departed"]

  def depart_scrap
    if self.program_id == Program.where("name = ?","Scrap").first.id
        self.departed_at = DateTime.now()
    end
  end
 
  def check_hook
    if self.departed_at.nil? == false
      if self.hook
        h = Hook.find_by_id(self.hook)
        h.update_attribute(:bike_id,nil)
      end
    end
  end

  def vacate_hook
    if self.nil? == false and self.hook.nil? == false
      h = self.hook
      h.bike = nil
      h.save!
    end
  end

  def reserve_hook_by_id(hook_id)
    if self.nil? == false and self.hook.nil? == true
      h = Hook.find_by_id(hook_id)
      h.bike = self
      h.save!
    end
  end

  def self.filter_bikes(brands,colors,status,sortBy)
    statusSql = []
    if status.nil? or status.empty?
        return []
    end
    if status.include?("-1")
        statusSql.push("departed_at IS NULL AND program_id IS NULL")
        status.delete("-1")
    end
    if status.include?("-2")
        statusSql.push("departed_at IS NOT NULL")
        status.delete("-2")
    end
    if status.empty? == false
        statusSql.push("departed_at IS NULL AND program_id IN (#{status.join(",")})")
    end
    statusSqlString = "(" +  statusSql.join(") OR (") + ")"
    bikes = Bike.select("bikes.*,programs.name,hooks.number as hook_number,COALESCE(brands.name,'n/a') as brand_name")
            .joins("LEFT JOIN hooks ON hooks.bike_id = bikes.id 
                    LEFT JOIN programs ON programs.id = bikes.program_id
                    LEFT JOIN brands ON brands.id = bikes.brand_id")
            .where("(brand_id IN (?) OR brand_id IS NULL) AND color IN (?) AND (#{statusSqlString})",brands,colors)
            .order(sortBy)
    return bikes
  end

  def self.get_bike_details(bike_number)
    bike = Bike.select("bikes.*,programs.name,bike_models.name as model_name,hooks.number as hook_number,brands.name as brand_name")
            .joins("LEFT JOIN hooks ON hooks.bike_id = bikes.id 
                    LEFT JOIN programs ON programs.id = bikes.program_id
                    LEFT JOIN brands ON brands.id = bikes.brand_id
                    LEFT JOIN bike_models ON brands.id = bike_models.brand_id")
            .where("bikes.number = ?",bike_number).first
    return bike
  end

  # Clean up all associations
  # See http://www.mrchucho.net/2008/09/30/the-correct-way-to-override-activerecordbasedestroy
  def destroy_without_callbacks
    unless new_record?
{'fraction_non_alphanumeric': 0.12232765519606723, 'fraction_numerical': 0.018977935292100147, 'mean_word_length': 2.4065420560747666, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7013189', 'n_tokens_mistral': 3032, 'n_tokens_neox': 2800, 'n_words': 614}