<commit_before>module EnjuCirculation
  module Item
    def self.included(base)
      base.extend ClassMethods
    end

    module ClassMethods
      FOR_CHECKOUT_CIRCULATION_STATUS = [
        'Available On Shelf',
        'On Loan',
        'Waiting To Be Reshelved'
      ]
      FOR_CHECKOUT_USE_RESTRICTION = [
        'Available For Supply Without Return',
        'Limited Circulation, Long Loan Period',
        'Limited Circulation, Short Loan Period',
        'No Reproduction',
        'Overnight Only',
        'Renewals Not Permitted',
        'Supervision Required',
        'Term Loan',
        'User Signature Required',
        'Limited Circulation, Normal Loan Period'
      ]

      def enju_circulation_item_model
        include InstanceMethods
        has_many :reserves, :foreign_key => :manifestation_id

        scope :for_checkout, includes(:circulation_status, :use_restriction).where(
            'circulation_statuses.name' => FOR_CHECKOUT_CIRCULATION_STATUS,
            'use_restrictions.name' => FOR_CHECKOUT_USE_RESTRICTION
          ).where('item_identifier IS NOT NULL')
        scope :removed, includes(:circulation_status).where('circulation_statuses.name' => 'Removed')
        has_many :checkouts
        has_many :reserves
        has_many :checked_items, :dependent => :destroy
        has_many :baskets, :through => :checked_items
        belongs_to :circulation_status, :validate => true
        belongs_to :checkout_type
        has_many :lending_policies, :dependent => :destroy
        has_one :item_has_use_restriction, :dependent => :destroy
        has_one :use_restriction, :through => :item_has_use_restriction
        validates_associated :circulation_status, :checkout_type
        validates_presence_of :circulation_status, :checkout_type
        searchable do
          integer :circulation_status_id
        end
        attr_accessible :item_has_use_restriction_attributes
        accepts_nested_attributes_for :item_has_use_restriction
      end
    end

    module InstanceMethods
      def set_circulation_status
        self.circulation_status = CirculationStatus.where(:name => 'In Process').first if self.circulation_status.nil?
      end

      def checkout_status(user)
         user.user_group.user_group_has_checkout_types.where(:checkout_type_id => self.checkout_type.id).first
      end

      def reserved?
        return true if manifestation.next_reservation
        false
      end

      def rent?
        return true if self.checkouts.not_returned.select(:item_id).detect{|checkout| checkout.item_id == self.id}
        false
      end

      def reserved_by_user?(user)
        if manifestation.next_reservation
          return true if manifestation.next_reservation.user == user
        end
        false
      end

      def available_for_checkout?
        if circulation_status.name == 'On Loan'
          false
        else
          manifestation.items.for_checkout.include?(self)
        end
      end

      def checkout!(user)
        self.circulation_status = CirculationStatus.where(:name => 'On Loan').first
        if self.reserved_by_user?(user)
          manifestation.next_reservation.update_attributes(:checked_out_at => Time.zone.now)
          manifestation.next_reservation.sm_complete!
        end
        save!
      end

      def checkin!
        self.circulation_status = CirculationStatus.where(:name => 'Available On Shelf').first
        save(:validate => false)
      end

      def retain(librarian)
        Item.transaction do
          reservation = manifestation.next_reservation
          unless reservation.nil?
            reservation.item = self
            reservation.sm_retain!
            reservation.send_message(librarian)
          end
        end
      end

      def lending_rule(user)
        lending_policies.where(:user_group_id => user.user_group.id).first
      end

      def not_for_loan?
        !manifestation.items.for_checkout.include?(self)
      end
    end
  end
end
<commit_msg>fixed checkout_status<commit_after>module EnjuCirculation
  module Item
    def self.included(base)
      base.extend ClassMethods
    end

    module ClassMethods
      FOR_CHECKOUT_CIRCULATION_STATUS = [
        'Available On Shelf',
        'On Loan',
        'Waiting To Be Reshelved'
      ]
      FOR_CHECKOUT_USE_RESTRICTION = [
        'Available For Supply Without Return',
        'Limited Circulation, Long Loan Period',
        'Limited Circulation, Short Loan Period',
        'No Reproduction',
        'Overnight Only',
        'Renewals Not Permitted',
        'Supervision Required',
        'Term Loan',
        'User Signature Required',
        'Limited Circulation, Normal Loan Period'
      ]

      def enju_circulation_item_model
        include InstanceMethods
        has_many :reserves, :foreign_key => :manifestation_id

        scope :for_checkout, includes(:circulation_status, :use_restriction).where(
            'circulation_statuses.name' => FOR_CHECKOUT_CIRCULATION_STATUS,
            'use_restrictions.name' => FOR_CHECKOUT_USE_RESTRICTION
          ).where('item_identifier IS NOT NULL')
        scope :removed, includes(:circulation_status).where('circulation_statuses.name' => 'Removed')
        has_many :checkouts
        has_many :reserves
        has_many :checked_items, :dependent => :destroy
        has_many :baskets, :through => :checked_items
        belongs_to :circulation_status, :validate => true
        belongs_to :checkout_type
        has_many :lending_policies, :dependent => :destroy
        has_one :item_has_use_restriction, :dependent => :destroy
        has_one :use_restriction, :through => :item_has_use_restriction
        validates_associated :circulation_status, :checkout_type
        validates_presence_of :circulation_status, :checkout_type
        searchable do
          integer :circulation_status_id
        end
        attr_accessible :item_has_use_restriction_attributes
        accepts_nested_attributes_for :item_has_use_restriction
      end
    end

    module InstanceMethods
      def set_circulation_status
        self.circulation_status = CirculationStatus.where(:name => 'In Process').first if self.circulation_status.nil?
      end

      def checkout_status(user)
        return nil unless user
         user.user_group.user_group_has_checkout_types.where(:checkout_type_id => self.checkout_type.id).first
      end

      def reserved?
        return true if manifestation.next_reservation
        false
      end

      def rent?
        return true if self.checkouts.not_returned.select(:item_id).detect{|checkout| checkout.item_id == self.id}
        false
      end

      def reserved_by_user?(user)
        if manifestation.next_reservation
          return true if manifestation.next_reservation.user == user
        end
        false
      end

      def available_for_checkout?
        if circulation_status.name == 'On Loan'
          false
        else
          manifestation.items.for_checkout.include?(self)
        end
      end

      def checkout!(user)
        self.circulation_status = CirculationStatus.where(:name => 'On Loan').first
        if self.reserved_by_user?(user)
          manifestation.next_reservation.update_attributes(:checked_out_at => Time.zone.now)
          manifestation.next_reservation.sm_complete!
        end
        save!
      end

      def checkin!
        self.circulation_status = CirculationStatus.where(:name => 'Available On Shelf').first
        save(:validate => false)
      end

      def retain(librarian)
        Item.transaction do
          reservation = manifestation.next_reservation
          unless reservation.nil?
            reservation.item = self
            reservation.sm_retain!
            reservation.send_message(librarian)
          end
        end
      end

      def lending_rule(user)
        lending_policies.where(:user_group_id => user.user_group.id).first
      end

      def not_for_loan?
        !manifestation.items.for_checkout.include?(self)
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.07264162637907524, 'fraction_numerical': 0.0, 'mean_word_length': 2.5354951796669587, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 37, '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': '3923232', 'n_tokens_mistral': 2373, 'n_tokens_neox': 2253, 'n_words': 475}