123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- class Activity < ApplicationRecord
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- belongs_to :group
- has_many :participants,
- dependent: :destroy
- has_many :people, through: :participants
- validates :name, presence: true
- validates :start, presence: true
- validate :deadline_before_start, unless: "self.deadline.blank?"
- validate :end_after_start, unless: "self.end.blank?"
- after_create :create_missing_participants!
-
-
- def organizers
- self.participants.includes(:person).where(is_organizer: true)
- end
-
- def is_participant?(person)
- Participant.exists?(
- activity_id: self.id,
- person_id: person.id
- )
- end
-
- def is_organizer?(person)
- Participant.exists?(
- person_id: person.id,
- activity_id: self.id,
- is_organizer: true
- )
- end
-
- def state_counts
- self.participants.group(:attending).count
- end
-
- def human_state_counts
- c = self.state_counts
- p = c[true]
- a = c[false]
- u = c[nil]
- return "#{p or 0}, #{a or 0}, #{u or 0}"
- end
-
- def may_change?(person)
- person.is_admin ||
- self.is_organizer?(person) ||
- self.group.is_leader?(person)
- end
-
-
-
- def create_missing_participants!
- people = self.group.people
- if not self.participants.empty?
- people = people.where('people.id NOT IN (?)', self.people.ids)
- end
- people.each do |p|
- Participant.create(
- activity: self,
- person: p,
- )
- end
- end
-
- def self.from_csv(content, group)
- reader = CSV.parse(content, {headers: true, skip_blanks: true})
- result = []
- reader.each do |row|
- a = Activity.new
- a.group = group
- a.name = row['name']
- a.description = row['description']
- a.location = row['location']
- sd = Date.strptime(row['start_date'])
- st = Time.strptime(row['start_time'], '%H:%M')
- a.start = Time.zone.local(sd.year, sd.month, sd.day, st.hour, st.min)
- if not row['end_date'].blank?
- ed = Date.strptime(row['end_date'])
- et = Time.strptime(row['end_time'], '%H:%M')
- a.end = Time.zone.local(ed.year, ed.month, ed.day, et.hour, et.min)
- end
- dd = Date.strptime(row['deadline_date'])
- dt = Time.strptime(row['deadline_time'], '%H:%M')
- a.deadline = Time.zone.local(dd.year, dd.month, dd.day, dt.hour, dt.min)
- result << a
- end
- result
- end
-
-
- def send_reminder
-
- return unless !self.reminder_done && self.reminder_at
- return if self.reminder_at > Time.zone.now
- participants = self.participants.where(attending: nil)
- participants.each { |p| p.send_reminder }
- end
- private
-
-
- def deadline_before_start
- if self.deadline > self.start
- errors.add(:deadline, I18n.t('activities.errors.must_be_before_start'))
- end
- end
-
- def end_after_start
- if self.end < self.start
- errors.add(:end, I18n.t('activities.errors.must_be_after_start'))
- end
- end
- end
|