Sha256: 97f57c8ff6522b2b50efae4c1eb16b01361a4aec2a69e1cd89c96419282b7aa3

Contents?: true

Size: 1.8 KB

Versions: 19

Compression:

Stored size: 1.8 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
      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(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 reserves.present?
        true
      else
        false
      end
    end

    def checkouts(start_date, end_date)
      Checkout.completed(start_date, end_date).where(item_id: 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

19 entries across 19 versions & 1 rubygems

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