<commit_before>class EquipmentObject < ActiveRecord::Base
  include Searchable
  include Routing

  belongs_to :equipment_model
  has_one :category, through: :equipment_model
  has_many :reservations

  validates :name,
            :equipment_model, presence: true

  nilify_blanks only: [:deleted_at]

  # table_name is needed to resolve ambiguity for certain queries with
  # 'includes'
  scope :active, ->() { where("#{table_name}.deleted_at is null") }

  searchable_on(:name, :serial)

  def status
    if self.deleted? && deactivation_reason
      "Deactivated (#{deactivation_reason})"
    elsif self.deleted?
      'Deactivated'
    elsif r == current_reservation
      "checked out by #{r.reserver.name} through "\
        "#{r.due_date.strftime('%b %d')}"
    else
      'available'
    end
  end

  def current_reservation
    reservations.checked_out.first
  end

  def available?
    status == 'available'
  end

  def self.for_eq_model(model_id, source_objects)
    # count the number of equipment objects for a given
    # model out of an array of source objects
    # 0 queries

    count = 0
    source_objects.each do |o|
      count += 1 if o.equipment_model_id == model_id
    end
    count
  end

  def make_reservation_notes(procedure_verb, reservation, handler, new_notes,
                             time)
    new_str = "#### [#{procedure_verb.capitalize}]("\
      "#{reservation_path(reservation.id)}) by #{handler.md_link} for "\
      "#{reservation.reserver.md_link} on #{time.to_s(:long)}\n"
    if new_notes.empty?
      new_str += "\n"
    else
      new_str += "##### Notes:\n#{new_notes}\n\n"
    end
    new_str += notes
    update_attributes(notes: new_str)
  end

  def make_switch_notes(old_res, new_res, handler)
    # set text depending on whether or not reservations are passed in
    old_res_msg = old_res ? old_res.md_link : 'available'
    new_res_msg = new_res ? new_res.md_link : 'available'
    update_attributes(notes: "#### Switched by #{handler.md_link} from "\
      "#{old_res_msg} to #{new_res_msg} on "\
      "#{Time.current.to_s(:long)}\n\n" + notes)
  end

  def update(current_user, new_params) # rubocop:disable all
    assign_attributes(new_params)
    changes = self.changes
    if changes.empty?
      self
    else
      new_notes = "#### Edited at #{Time.current.to_s(:long)} by "\
        "#{current_user.md_link}\n\n"
      new_notes += "\n\n#### Changes:"
      changes.each do |param, diff|
        case param
        when 'name'
          name = 'Name'
          old_val = diff[0].to_s
          new_val = diff[1].to_s
        when 'serial'
          name = 'Serial'
          old_val = diff[0].to_s
          new_val = diff[1].to_s
        when 'equipment_model_id'
          name = 'Equipment Model'
          old_val = diff[0] ? EquipmentModel.find(diff[0]).name : 'nil'
          new_val = diff[1] ? EquipmentModel.find(diff[1]).name : 'nil'
        end
        new_notes += "\n#{name} changed from " + old_val + ' to ' + new_val\
          + '.' if old_val && new_val
      end
    end
    new_notes += "\n\n" + notes
    self.notes = new_notes.strip
    self<commit_msg>fixed broken return in EquipmentObject model<commit_after>class EquipmentObject < ActiveRecord::Base
  include Searchable
  include Routing

  belongs_to :equipment_model
  has_one :category, through: :equipment_model
  has_many :reservations

  validates :name,
            :equipment_model, presence: true

  nilify_blanks only: [:deleted_at]

  # table_name is needed to resolve ambiguity for certain queries with
  # 'includes'
  scope :active, ->() { where("#{table_name}.deleted_at is null") }

  searchable_on(:name, :serial)

  def status
    if self.deleted? && deactivation_reason
      "Deactivated (#{deactivation_reason})"
    elsif self.deleted?
      'Deactivated'
    elsif r == current_reservation
      "checked out by #{r.reserver.name} through "\
        "#{r.due_date.strftime('%b %d')}"
    else
      'available'
    end
  end

  def current_reservation
    reservations.checked_out.first
  end

  def available?
    status == 'available'
  end

  def self.for_eq_model(model_id, source_objects)
    # count the number of equipment objects for a given
    # model out of an array of source objects
    # 0 queries

    count = 0
    source_objects.each do |o|
      count += 1 if o.equipment_model_id == model_id
    end
    count
  end

  def make_reservation_notes(procedure_verb, reservation, handler, new_notes,
                             time)
    new_str = "#### [#{procedure_verb.capitalize}]("\
      "#{reservation_path(reservation.id)}) by #{handler.md_link} for "\
      "#{reservation.reserver.md_link} on #{time.to_s(:long)}\n"
    if new_notes.empty?
      new_str += "\n"
    else
      new_str += "##### Notes:\n#{new_notes}\n\n"
    end
    new_str += notes
    update_attributes(notes: new_str)
  end

  def make_switch_notes(old_res, new_res, handler)
    # set text depending on whether or not reservations are passed in
    old_res_msg = old_res ? old_res.md_link : 'available'
    new_res_msg = new_res ? new_res.md_link : 'available'
    update_attributes(notes: "#### Switched by #{handler.md_link} from "\
      "#{old_res_msg} to #{new_res_msg} on "\
      "#{Time.current.to_s(:long)}\n\n" + notes)
  end

  def update(current_user, new_params) # rubocop:disable all
    assign_attributes(new_params)
    changes = self.changes
    if changes.empty?
      return self
    else
      new_notes = "#### Edited at #{Time.current.to_s(:long)} by "\
        "#{current_user.md_link}\n\n"
      new_notes += "\n\n#### Changes:"
      changes.each do |param, diff|
        case param
        when 'name'
          name = 'Name'
          old_val = diff[0].to_s
          new_val = diff[1].to_s
        when 'serial'
          name = 'Serial'
          old_val = diff[0].to_s
          new_val = diff[1].to_s
        when 'equipment_model_id'
          name = 'Equipment Model'
          old_val = diff[0] ? EquipmentModel.find(diff[0]).name : 'nil'
          new_val = diff[1] ? EquipmentModel.find(diff[1]).name : 'nil'
        end
        new_notes += "\n#{name} changed from " + old_val + ' to ' + new_val\
          + '.' if old_val && new_val
      end
    end
    new_notes += "\n\n" + notes
    self.notes = new_notes.strip
    self
{'fraction_non_alphanumeric': 0.11911532385466035, 'fraction_numerical': 0.0034755134281200632, 'mean_word_length': 2.644789867587795, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3804643', 'n_tokens_mistral': 2130, 'n_tokens_neox': 2085, 'n_words': 499}