Class: SlotGroup

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/slot_group.rb

Overview

The concept of a SlotGroup, which can be specialized in FreeSlotGroups or BookingGroups so that batch operations can be performed on them.

Schema Information

Table name: slot_groups

id                 :integer          not null, primary key
monday             :boolean          default("false"), not null
tuesday            :boolean          default("false"), not null
wednesday          :boolean          default("false"), not null
thursday           :boolean          default("false"), not null
friday             :boolean          default("false"), not null
saturday           :boolean          default("false"), not null
sunday             :boolean          default("false"), not null
weekly             :boolean          default("false"), not null
monthly            :boolean          default("false"), not null
capacity           :integer          default("1"), not null
ends_on            :date
created_at         :datetime         not null
updated_at         :datetime         not null
type               :string           not null
starts_at          :datetime         not null
ends_at            :datetime         not null
provider_id        :integer
affiliation_id     :integer
free_slot_group_id :integer

Direct Known Subclasses

BookingGroup, FreeSlotGroup

Instance Method Summary (collapse)

Instance Method Details

- (Object) cant_change_past



86
87
88
89
90
91
92
93
94
# File 'app/models/slot_group.rb', line 86

def cant_change_past
  if starts_at < Time.zone.now
    errors.add :starts_at, "Period can't start in the past."
  end

  if ends_at < Time.zone.now
    errors.add :ends_at, "Period can't end in the past."
  end
end

- (Object) end_by_date_is_future



61
62
63
64
65
# File 'app/models/slot_group.rb', line 61

def end_by_date_is_future
  if ends_on < Time.now
    errors.add(:ends_on, " must be in the future.")
  end
end

- (Object) number_of_slots_is_at_least_2



102
103
104
105
106
107
108
109
110
111
112
# File 'app/models/slot_group.rb', line 102

def number_of_slots_is_at_least_2
  if !weekly && !monthly
    if ([monday, tuesday, wednesday, thursday, friday, saturday, sunday].select { |a| a }.count < 2)
      errors.add(:slots, "At least two days must be selected.")
    end
  end

  if gather_dates(starts_at.to_date, ends_on.to_date, weekly, monthly, new_week_day='').size < 2 || slots.size < 2
    errors.add(:slots, "At least two days must be selected.")
  end
end

- (Object) number_of_slots_is_correct



96
97
98
99
100
# File 'app/models/slot_group.rb', line 96

def number_of_slots_is_correct
  if slots.size != gather_dates(starts_at.to_date, ends_on.to_date, weekly, monthly, new_week_day='').size
    errors.add :slots, "Fewer slots exist than are expected."
  end
end

- (Object) starts_and_ends_in_the_same_day



80
81
82
83
84
# File 'app/models/slot_group.rb', line 80

def starts_and_ends_in_the_same_day
  unless starts_at.beginning_of_day == ends_at.beginning_of_day
    errors.add :ends_at, "Period starts and ends in different days."
  end
end

- (Object) starts_before_ends



74
75
76
77
78
# File 'app/models/slot_group.rb', line 74

def starts_before_ends
  unless starts_at < ends_at
    errors.add :ends_at, "Period starts after it ends."
  end
end

- (Object) weekly_and_monthly_are_not_simultaneously_selected



67
68
69
70
71
72
# File 'app/models/slot_group.rb', line 67

def weekly_and_monthly_are_not_simultaneously_selected
  if weekly && monthly
    errors.add(:weekly, " cannot be selected with Monthly.")
    errors.add(:monthly, " cannot be selected with Weekly.")
  end
end

- (Object) write_attribute(attribute_name, value)



52
53
54
55
56
57
58
59
# File 'app/models/slot_group.rb', line 52

def write_attribute(attribute_name, value)
  if value != read_attribute(attribute_name)
    @changed_attribute = attribute_name
    @new_value = value
    run_callbacks(:attribute_changed)
  end
  super
end