Sha256: 2df39016c642f125ec5f8fd25e5c0a797d8ce0a60aec976f9736feed1f325afc

Contents?: true

Size: 1.67 KB

Versions: 7

Compression:

Stored size: 1.67 KB

Contents

class Basket < ActiveRecord::Base
  default_scope :order => 'id DESC'
  scope :will_expire, lambda {|date| {:conditions => ['created_at < ?', date]}}
  belongs_to :user, :validate => true
  has_many :checked_items, :dependent => :destroy
  has_many :items, :through => :checked_items
  has_many :checkouts
  has_many :checkins

  validates_associated :user, :on => :create
  # 貸出完了後にかごのユーザidは破棄する
  validates_presence_of :user, :on => :create
  validate :check_suspended

  attr_protected :user_id
  attr_accessor :user_number

  def check_suspended
    if self.user
      errors[:base] << I18n.t('basket.this_account_is_suspended') unless self.user.active_for_authentication?
    else
      errors[:base] << I18n.t('user.not_found')
    end
  end

  def basket_checkout(librarian)
    return nil if self.checked_items.size == 0
    Item.transaction do
      self.checked_items.each do |checked_item|
        checkout = self.user.checkouts.new(:librarian_id => librarian.id, :item_id => checked_item.item.id, :basket_id => self.id, :due_date => checked_item.due_date)
        if checked_item.item.checkout!(self.user)
          checkout.save!
        end
      end
      CheckedItem.destroy_all(:basket_id => self.id)
    end
  end

  def self.expire
    Basket.will_expire(Time.zone.now.beginning_of_day).destroy_all
    logger.info "#{Time.zone.now} baskets expired!"
  end

end

# == Schema Information
#
# Table name: baskets
#
#  id           :integer         not null, primary key
#  user_id      :integer
#  note         :text
#  type         :string(255)
#  lock_version :integer         default(0), not null
#  created_at   :datetime
#  updated_at   :datetime
#

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
enju_circulation-0.0.10 app/models/basket.rb
enju_circulation-0.0.9 app/models/basket.rb
enju_circulation-0.0.8 app/models/basket.rb
enju_circulation-0.0.7 app/models/basket.rb
enju_circulation-0.0.6 app/models/basket.rb
enju_circulation-0.0.4 app/models/basket.rb
enju_circulation-0.0.3 app/models/basket.rb