<commit_before># -*- encoding: utf-8 -*-
class Reserve < ActiveRecord::Base
  scope :hold, where('item_id IS NOT NULL AND state = ?', 'retained')
  scope :not_hold, where(:item_id => nil)
  scope :waiting, where('canceled_at IS NULL AND expired_at > ? AND state != ? AND state != ?', Time.zone.now, 'completed', 'retained')
  scope :completed, where('checked_out_at IS NOT NULL')
  scope :canceled, where('canceled_at IS NOT NULL')
  scope :retained, where(:state => ['retained'], :retained => !true)
  scope :not_retained, where(:state => ['requested','pending'])
  scope :not_waiting, where(:state => ['retained','canceled','in_process', 'completed'])
  scope :will_expire_retained, lambda {|datetime| {:conditions => ['checked_out_at IS NULL AND canceled_at IS NULL AND expired_at <= ? AND state = ?', datetime, 'retained'], :order => 'expired_at'}}
  scope :will_expire_pending, lambda {|datetime| {:conditions => ['checked_out_at IS NULL AND canceled_at IS NULL AND expired_at <= ? AND state = ?', datetime, 'pending'], :order => 'expired_at'}}
  scope :created, lambda {|start_date, end_date| {:conditions => ['created_at >= ? AND created_at < ?', start_date, end_date]}}
  scope :not_sent_expiration_notice_to_patron, where(:state => 'expired', :expiration_notice_to_patron => false)
  scope :not_sent_expiration_notice_to_library, where(:state => 'expired', :expiration_notice_to_library => false)
  scope :sent_expiration_notice_to_patron, where(:state => 'expired', :expiration_notice_to_patron => true)
  scope :sent_expiration_notice_to_library, where(:state => 'expired', :expiration_notice_to_library => true)
  scope :not_sent_cancel_notice_to_patron, where(:state => 'canceled', :expiration_notice_to_patron => false)
  scope :not_sent_cancel_notice_to_library, where(:state => 'canceled', :expiration_notice_to_library => false)
  scope :previous_reserves, where(:state => ['requested', 'retained'])  
  acts_as_list :scope => :manifestation_id

  belongs_to :user, :validate => true
  belongs_to :manifestation, :validate => true
  belongs_to :librarian, :class_name => 'User', :validate => true
  belongs_to :item, :validate => true
  has_one :inter_library_loan
  belongs_to :request_status_type

  validates_associated :user, :librarian, :item, :request_status_type, :manifestation
  validates_presence_of :user, :manifestation, :request_status_type, :expired_at, :created_by
  #validates_uniqueness_of :manifestation_id, :scope => :user_id
  validates_date :expired_at, :allow_blank => true
  validate :manifestation_must_include_item
  validate :available_for_reservation?, :on => :create
  before_validation :set_item_and_manifestation, :on => :create
  before_validation :set_expired_at
  before_validation :set_request_status, :on => :create

  attr_accessor :user_number, :item_identifier

  state_machine :initial => :pending do
    before_transition [:pending, :retained] => :requested, :do => :do_request
    before_transition [:pending, :requested, :retained, :in_process] => :retained, :do => :retain
    before_transition [:requested] => :in_process, :do => :to_process
    before_transition [:pending ,:requested, :retained, :in_process] => :canceled, :do => :cancel
    before_transition [:canceled] => :expired, :do => :expire
    before_transition [:retained, :requested] => :completed, :do => :checkout


    event :sm_request do
      transition [:pending, :retained] => :requested
    end

    event :sm_retain do
      transition [:pending, :requested, :retained, :in_process] => :retained
    end

    event :sm_process do
      transition [:requested] => :in_process, :do => :to_process
    end

    event :sm_cancel do
      transition [:pending, :requested, :retained, :in_process] => :canceled
    end

    event :sm_expire do
      transition [:canceled] => :expired
    end

    event :sm_complete do
      transition [:requested, :retained] => :completed
    end
  end<commit_msg>refs 2170 modified state machine for reserve<commit_after># -*- encoding: utf-8 -*-
class Reserve < ActiveRecord::Base
  scope :hold, where('item_id IS NOT NULL AND state = ?', 'retained')
  scope :not_hold, where(:item_id => nil)
  scope :waiting, where('canceled_at IS NULL AND expired_at > ? AND state != ? AND state != ?', Time.zone.now, 'completed', 'retained')
  scope :completed, where('checked_out_at IS NOT NULL')
  scope :canceled, where('canceled_at IS NOT NULL')
  scope :retained, where(:state => ['retained'], :retained => !true)
  scope :not_retained, where(:state => ['requested','pending'])
  scope :not_waiting, where(:state => ['retained','canceled','in_process', 'completed'])
  scope :will_expire_retained, lambda {|datetime| {:conditions => ['checked_out_at IS NULL AND canceled_at IS NULL AND expired_at <= ? AND state = ?', datetime, 'retained'], :order => 'expired_at'}}
  scope :will_expire_pending, lambda {|datetime| {:conditions => ['checked_out_at IS NULL AND canceled_at IS NULL AND expired_at <= ? AND state = ?', datetime, 'pending'], :order => 'expired_at'}}
  scope :created, lambda {|start_date, end_date| {:conditions => ['created_at >= ? AND created_at < ?', start_date, end_date]}}
  scope :not_sent_expiration_notice_to_patron, where(:state => 'expired', :expiration_notice_to_patron => false)
  scope :not_sent_expiration_notice_to_library, where(:state => 'expired', :expiration_notice_to_library => false)
  scope :sent_expiration_notice_to_patron, where(:state => 'expired', :expiration_notice_to_patron => true)
  scope :sent_expiration_notice_to_library, where(:state => 'expired', :expiration_notice_to_library => true)
  scope :not_sent_cancel_notice_to_patron, where(:state => 'canceled', :expiration_notice_to_patron => false)
  scope :not_sent_cancel_notice_to_library, where(:state => 'canceled', :expiration_notice_to_library => false)
  scope :previous_reserves, where(:state => ['requested', 'retained'])  
  acts_as_list :scope => :manifestation_id

  belongs_to :user, :validate => true
  belongs_to :manifestation, :validate => true
  belongs_to :librarian, :class_name => 'User', :validate => true
  belongs_to :item, :validate => true
  has_one :inter_library_loan
  belongs_to :request_status_type

  validates_associated :user, :librarian, :item, :request_status_type, :manifestation
  validates_presence_of :user, :manifestation, :request_status_type, :expired_at, :created_by
  #validates_uniqueness_of :manifestation_id, :scope => :user_id
  validates_date :expired_at, :allow_blank => true
  validate :manifestation_must_include_item
  validate :available_for_reservation?, :on => :create
  before_validation :set_item_and_manifestation, :on => :create
  before_validation :set_expired_at
  before_validation :set_request_status, :on => :create

  attr_accessor :user_number, :item_identifier

  state_machine :initial => :pending do
    before_transition [:pending, :retained] => :requested, :do => :do_request
    before_transition [:pending, :requested, :retained, :in_process] => :retained, :do => :retain
    before_transition [:requested] => :in_process, :do => :to_process
    before_transition [:pending ,:requested, :retained, :in_process] => :canceled, :do => :cancel
    before_transition [:pending, :requested, :retained, :in_process] => :expired, :do => :expire
    before_transition [:retained, :requested] => :completed, :do => :checkout


    event :sm_request do
      transition [:pending, :retained] => :requested
    end

    event :sm_retain do
      transition [:pending, :requested, :retained, :in_process] => :retained
    end

    event :sm_process do
      transition [:requested] => :in_process, :do => :to_process
    end

    event :sm_cancel do
      transition [:pending, :requested, :retained, :in_process] => :canceled
    end

    event :sm_expire do
      transition [:canceled] => :expired
    end

    event :sm_complete do
      transition [:requested, :retained] => :completed
    end
  end
{'fraction_non_alphanumeric': 0.13402322059565877, 'fraction_numerical': 0.000757193336698637, 'mean_word_length': 5.670875420875421, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 115, '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': '5408989', 'n_tokens_mistral': 2600, 'n_tokens_neox': 2558, 'n_words': 630}