Sha256: 70b5791332f35380c6ad37e451e287f2c0be1f480e2415ff37027b5e3da446a1

Contents?: true

Size: 1.83 KB

Versions: 10

Compression:

Stored size: 1.83 KB

Contents

module EnjuCirculation
  module EnjuManifestation
    extend ActiveSupport::Concern

    included do
      has_many :reserves, :foreign_key => :manifestation_id

      searchable do
        boolean :reservable do
          items.for_checkout.exists?
        end
      end
    end

    def next_reservation
      self.reserves.waiting.order('reserves.created_at ASC').readonly(false).first
    end

    def available_checkout_types(user)
      if user
        user.profile.user_group.user_group_has_checkout_types.available_for_carrier_type(self.carrier_type)
      end
    end

    def is_reservable_by?(user)
      return false if items.for_checkout.empty?
      unless user.has_role?('Librarian')
        unless items.size == (items.size - user.checkouts.overdue(Time.zone.now).collect(&:item).size)
          return false
        end
      end
      true
    end

    def is_reserved_by?(user)
      return nil unless user
      reserve = Reserve.waiting.where(:user_id => user.id, :manifestation_id => id).first
      if reserve
        reserve
      else
        false
      end
    end

    def is_reserved?
      if self.reserves.present?
        true
      else
        false
      end
    end

    def checkouts(start_date, end_date)
      Checkout.completed(start_date, end_date).where(:item_id => self.items.collect(&:id))
    end

    def checkout_period(user)
      if available_checkout_types(user)
        available_checkout_types(user).collect(&:checkout_period).max || 0
      end
    end

    def reservation_expired_period(user)
      if available_checkout_types(user)
        available_checkout_types(user).collect(&:reservation_expired_period).max || 0
      end
    end

    def is_checked_out_by?(user)
      if items.size > items.size - user.checkouts.not_returned.collect(&:item).size
        true
      else
        false
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
enju_circulation-0.2.5 app/models/concerns/enju_circulation/enju_manifestation.rb
enju_circulation-0.2.4 app/models/concerns/enju_circulation/enju_manifestation.rb
enju_circulation-0.2.3 app/models/concerns/enju_circulation/enju_manifestation.rb
enju_circulation-0.2.2 app/models/concerns/enju_circulation/enju_manifestation.rb
enju_circulation-0.2.1 app/models/concerns/enju_circulation/enju_manifestation.rb
enju_circulation-0.2.0 app/models/concerns/enju_circulation/enju_manifestation.rb
enju_circulation-0.2.0.beta.4 app/models/concerns/enju_circulation/enju_manifestation.rb
enju_circulation-0.2.0.beta.3 app/models/concerns/enju_circulation/enju_manifestation.rb
enju_circulation-0.2.0.beta.2 app/models/concerns/enju_circulation/enju_manifestation.rb
enju_circulation-0.2.0.beta.1 app/models/concerns/enju_circulation/enju_manifestation.rb