Sha256: 7e9929efd1065c051aec15f99ce5d71a7ef95b0e78b4ed8bfb55770981ebec04

Contents?: true

Size: 1.75 KB

Versions: 30

Compression:

Stored size: 1.75 KB

Contents

class Basket < ActiveRecord::Base
  attr_accessible :note, :user_number
  default_scope :order => 'baskets.id DESC'
  scope :will_expire, lambda {|date| {:conditions => ['created_at < ?', date]}}
  belongs_to :user, :validate => true
  has_many :accepts

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

  attr_accessor :user_number

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

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

  if defined?(EnjuCirculation)
    has_many :checked_items, :dependent => :destroy
    has_many :items, :through => :checked_items
    has_many :checkouts
    has_many :checkins

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

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

Version data entries

30 entries across 30 versions & 2 rubygems

Version Path
enju_library-0.1.0.pre33 app/models/basket.rb
enju_library-0.1.0.pre32 app/models/basket.rb
enju_library-0.1.0.pre31 app/models/basket.rb
enju_library-0.1.0.pre30 app/models/basket.rb
enju_library-0.1.0.pre29 app/models/basket.rb
enju_library-0.1.0.pre28 app/models/basket.rb
enju_library-0.1.0.pre27 app/models/basket.rb
enju_library-0.1.0.pre26 app/models/basket.rb
enju_library-0.1.0.pre25 app/models/basket.rb
enju_library-0.1.0.pre24 app/models/basket.rb
enju_library-0.1.0.pre23 app/models/basket.rb
enju_library-0.1.0.pre22 app/models/basket.rb
enju_library-0.1.0.pre21 app/models/basket.rb
enju_library-0.1.0.pre20 app/models/basket.rb
enju_library-0.1.0.pre19 app/models/basket.rb
enju_library-0.1.0.pre18 app/models/basket.rb
enju_library-0.1.0.pre17 app/models/basket.rb
enju_library-0.1.0.pre16 app/models/basket.rb
enju_library-0.1.0.pre15 app/models/basket.rb
enju_library-0.1.0.pre14 app/models/basket.rb