module Unavailability

Constants

VERSION

Public Class Methods

included(base) click to toggle source
# File lib/unavailability.rb, line 10
def self.included(base)
  base.class_eval do
    has_many :unavailable_dates, as: :dateable, dependent: :destroy, class_name: 'Unavailability::UnavailableDate'

    scope :available_for_date, ->(date) do
      user_table  = arel_table
      u_table     = Unavailability::UnavailableDate.arel_table
      u_condition = u_table[:dateable_id].eq(user_table[:id]).and(u_table[:from].lteq(date)).and(u_table[:to].gteq(date))

      where(Unavailability::UnavailableDate.where(u_condition).arel.exists.not)
    end
  end
end

Public Instance Methods

available_for_date?(date) click to toggle source
# File lib/unavailability.rb, line 24
def available_for_date?(date)
  unavailable_dates.each do |unavailable_date|
    return false if (unavailable_date.from..unavailable_date.to).cover?(date)
  end

  true
end
make_available(from:, to:) click to toggle source
# File lib/unavailability.rb, line 36
def make_available(from:, to:)
  Unavailability::UnavailableDates::Remove.new(self, from, to).call
end
make_unavailable(from:, to:) click to toggle source
# File lib/unavailability.rb, line 32
def make_unavailable(from:, to:)
  Unavailability::UnavailableDates::Add.new(self, from, to).call
end