<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

  # Program ID is denormalized, referencing the active assignment for this bike
  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
  has_many :assignments
  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
  after_create  :create_assignment

  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
        current_assignment = self.assignments.where("active = ?",true).first
        if current_assignment
            current_assignment.closed_at = DateTime.now()
        end
        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(colors,status,sortBy,search,min,max,all)
    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 (") + ")"
    searchSql = []
    search.split.each do |ss|
        searchSql.push("bikes.number LIKE '%#{ss}%' OR bikes.color LIKE '%#{ss}%' OR brands.name LIKE '%#{ss}%'")
    end
    puts searchSql
    if search.nil? or search.empty?
        searchSqlString = "1=1"
    else
        searchSqlString = "(" + searchSql.join(") AND (") + ")"
    end
    count = Bike.select("bikes.*,programs.*,hooks.*,COALESCE(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")
            .where("color IN (?) AND (#{statusSqlString}) AND (#{searchSqlString})",colors)
    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("color IN (?) AND (#{statusSqlString}) AND (#{searchSqlString})",colors)
            .order(sortBy)
            .limit((all == "true" ? 2000 : 10))
            .offset(min)<commit_msg>Case insensitive like for postgres<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

  # Program ID is denormalized, referencing the active assignment for this bike
  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
  has_many :assignments
  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
  after_create  :create_assignment

  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
        current_assignment = self.assignments.where("active = ?",true).first
        if current_assignment
            current_assignment.closed_at = DateTime.now()
        end
        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(colors,status,sortBy,search,min,max,all)
    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 (") + ")"
    searchSql = []
    search.split.each do |ss|
        searchSql.push("bikes.number ILIKE '%#{ss}%' OR bikes.color ILIKE '%#{ss}%' OR brands.name ILIKE '%#{ss}%'")
    end
    puts searchSql
    if search.nil? or search.empty?
        searchSqlString = "1=1"
    else
        searchSqlString = "(" + searchSql.join(") AND (") + ")"
    end
    count = Bike.select("bikes.*,programs.*,hooks.*,COALESCE(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")
            .where("color IN (?) AND (#{statusSqlString}) AND (#{searchSqlString})",colors)
    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("color IN (?) AND (#{statusSqlString}) AND (#{searchSqlString})",colors)
            .order(sortBy)
            .limit((all == "true" ? 2000 : 10))
            .offset(min)
{'fraction_non_alphanumeric': 0.1309823677581864, 'fraction_numerical': 0.017422334172963894, 'mean_word_length': 2.394727467046669, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '3172363', 'n_tokens_mistral': 3250, 'n_tokens_neox': 3020, 'n_words': 675}