Sha256: 9ceabd3279a2b8e9f541849c02a92f587b34b760a1a51c646475baad54161b25

Contents?: true

Size: 979 Bytes

Versions: 55

Compression:

Stored size: 979 Bytes

Contents

# A period of time during which a Participant was awake and may have
# participated in Activities.
class AwakePeriod < ActiveRecord::Base
  belongs_to :participant

  validates :participant, :start_time, :end_time, presence: true
  validate :end_time_after_start_time
  validate :awake_periods_cannot_overlap

  scope :periods_for, lambda { |start_time, end_time|
    where(
      arel_table[:start_time]
      .lteq(end_time)
      .and(
        arel_table[:end_time]
        .gteq(start_time)
      )
    )
  }

  private

  def awake_periods_cannot_overlap
    overlapping_period_exists =
      participant
      .awake_periods
      .periods_for(start_time, end_time)
      .limit(1)
      .count == 1
    if overlapping_period_exists
      errors.add(:base, "This awake period already exists")
    end
  end

  def end_time_after_start_time
    if start_time && end_time && end_time <= start_time
      errors.add :end_time, "Must be after the start time"
    end
  end
end

Version data entries

55 entries across 55 versions & 1 rubygems

Version Path
think_feel_do_engine-3.12.7 app/models/awake_period.rb
think_feel_do_engine-3.12.6 app/models/awake_period.rb
think_feel_do_engine-3.12.5 app/models/awake_period.rb
think_feel_do_engine-3.12.4 app/models/awake_period.rb
think_feel_do_engine-3.12.3 app/models/awake_period.rb
think_feel_do_engine-3.12.2 app/models/awake_period.rb
think_feel_do_engine-3.12.1 app/models/awake_period.rb
think_feel_do_engine-3.12.0 app/models/awake_period.rb
think_feel_do_engine-3.11.3 app/models/awake_period.rb
think_feel_do_engine-3.11.0 app/models/awake_period.rb
think_feel_do_engine-3.10.10 app/models/awake_period.rb
think_feel_do_engine-3.10.9 app/models/awake_period.rb
think_feel_do_engine-3.10.8 app/models/awake_period.rb
think_feel_do_engine-3.10.7 app/models/awake_period.rb
think_feel_do_engine-3.10.6 app/models/awake_period.rb