Sha256: 728af9da7db533d59f0502f5e23b9823de24064ec21eab6393c611aa4c2dafc5

Contents?: true

Size: 1.72 KB

Versions: 19

Compression:

Stored size: 1.72 KB

Contents

module EnjuCirculation
  module EnjuUser
    extend ActiveSupport::Concern

    included do
      has_many :checkouts, dependent: :nullify
      has_many :reserves, dependent: :destroy
      has_many :reserved_manifestations, through: :reserves, source: :manifestation
      has_many :checkout_stat_has_users
      has_many :user_checkout_stats, through: :checkout_stat_has_users
      has_many :reserve_stat_has_users
      has_many :user_reserve_stats, through: :reserve_stat_has_users
      has_many :baskets, dependent: :destroy

      before_destroy :check_item_before_destroy
    end

    def check_item_before_destroy
      # TODO: 貸出記録を残す場合
      if checkouts.size > 0
        raise 'This user has items still checked out.'
      end
    end

    def checked_item_count
      checkout_count = {}
      CheckoutType.all.each do |checkout_type|
        # 資料種別ごとの貸出中の冊数を計算
        checkout_count[:"#{checkout_type.name}"] = checkouts.count_by_sql(["
          SELECT count(item_id) FROM checkouts
            WHERE item_id IN (
              SELECT id FROM items
                WHERE checkout_type_id = ?
            )
            AND user_id = ? AND checkin_id IS NULL", checkout_type.id, id]
        )
      end
      checkout_count
    end

    def reached_reservation_limit?(manifestation)
      return true if profile.user_group.user_group_has_checkout_types.available_for_carrier_type(manifestation.carrier_type).where(user_group_id: profile.user_group.id).collect(&:reservation_limit).max.to_i <= reserves.waiting.size
      false
    end

    def has_overdue?(day = 1)
      true if checkouts.where(checkin_id: nil).where(Checkout.arel_table[:due_date].lt day.days.ago).count >= 1
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
enju_circulation-0.3.11 app/models/concerns/enju_circulation/enju_user.rb
enju_circulation-0.3.10 app/models/concerns/enju_circulation/enju_user.rb
enju_circulation-0.3.9 app/models/concerns/enju_circulation/enju_user.rb
enju_circulation-0.4.0.rc.1 app/models/concerns/enju_circulation/enju_user.rb
enju_circulation-0.3.8 app/models/concerns/enju_circulation/enju_user.rb
enju_circulation-0.3.7 app/models/concerns/enju_circulation/enju_user.rb
enju_circulation-0.4.0.beta.4 app/models/concerns/enju_circulation/enju_user.rb
enju_circulation-0.4.0.beta.3 app/models/concerns/enju_circulation/enju_user.rb
enju_circulation-0.3.6 app/models/concerns/enju_circulation/enju_user.rb
enju_circulation-0.4.0.beta.2 app/models/concerns/enju_circulation/enju_user.rb
enju_circulation-0.4.0.beta.1 app/models/concerns/enju_circulation/enju_user.rb
enju_circulation-0.3.5 app/models/concerns/enju_circulation/enju_user.rb
enju_circulation-0.3.4 app/models/concerns/enju_circulation/enju_user.rb
enju_circulation-0.3.3 app/models/concerns/enju_circulation/enju_user.rb
enju_circulation-0.3.2 app/models/concerns/enju_circulation/enju_user.rb
enju_circulation-0.3.1 app/models/concerns/enju_circulation/enju_user.rb
enju_circulation-0.3.0 app/models/concerns/enju_circulation/enju_user.rb
enju_circulation-0.3.0.rc.1 app/models/concerns/enju_circulation/enju_user.rb
enju_circulation-0.3.0.beta.1 app/models/concerns/enju_circulation/enju_user.rb