11class Meetup < ApplicationRecord
2+ DEFAULT_START_TIME = "18:00" . freeze
3+
24 has_many :talks , dependent : :destroy
35 has_many :attendances , dependent : :destroy
46
57 attribute :event_type , :string
8+ attribute :start_time , :string , default : DEFAULT_START_TIME
69 enum :event_type , { formal : "formal" , bar : "bar" } , default : "formal"
710
811 validates :number , presence : true , uniqueness : true , if : :formal?
912 validates :date , presence : true
13+ validates :start_time , presence : true , format : { with : /\A ([01]\d |2[0-3]):[0-5]\d \z / }
1014
1115 validate :cannot_change_to_bar_if_talks_exist , if : -> { bar? && event_type_changed? }
1216
1317 before_validation :clear_number_for_bar_events , if : -> { bar? && number . present? }
18+ before_validation :apply_default_start_time
1419
1520 scope :ordered , -> { order ( Arel . sql ( "CASE WHEN event_type = 'bar' THEN 1 ELSE 0 END, date DESC" ) ) }
1621 scope :upcoming , -> { where ( "date >= ?" , Date . today ) . order ( date : :asc ) }
@@ -28,4 +33,8 @@ def cannot_change_to_bar_if_talks_exist
2833 def clear_number_for_bar_events
2934 self . number = nil
3035 end
36+
37+ def apply_default_start_time
38+ self . start_time = DEFAULT_START_TIME if start_time . blank?
39+ end
3140end
0 commit comments