Module: Unavailability

Defined in:
lib/unavailability.rb,
lib/unavailability/version.rb,
lib/unavailability/unavailable_date.rb,
lib/unavailability/unavailable_dates/add.rb,
lib/unavailability/unavailable_dates/remove.rb

Defined Under Namespace

Modules: UnavailableDates Classes: UnavailableDate

Constant Summary collapse

VERSION =
"0.1.3"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# 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

Instance Method Details

#available_for_date?(date) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
# 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:) ⇒ Object



36
37
38
# File 'lib/unavailability.rb', line 36

def make_available(from:, to:)
  Unavailability::UnavailableDates::Remove.new(self, from, to).call
end

#make_unavailable(from:, to:) ⇒ Object



32
33
34
# File 'lib/unavailability.rb', line 32

def make_unavailable(from:, to:)
  Unavailability::UnavailableDates::Add.new(self, from, to).call
end