2015-10-21 23:13:39 +03:00
|
|
|
class Volunteer < ActiveRecord::Base
|
|
|
|
TSHIRT_SIZES = [:s, :m, :l, :xl, :xxl, :xxxl]
|
|
|
|
TSHIRT_CUTS = [:unisex, :female]
|
|
|
|
FOOD_PREFERENCES = [:none, :vegetarian, :vegan]
|
|
|
|
|
2024-02-20 02:27:07 +02:00
|
|
|
has_one_attached :picture
|
2015-10-21 23:13:39 +03:00
|
|
|
|
|
|
|
validates :name, :language, :tshirt_size, :tshirt_cut, :food_preferences, presence: true
|
|
|
|
validates :tshirt_size, inclusion: {in: TSHIRT_SIZES.map(&:to_s)}
|
|
|
|
validates :tshirt_cut, inclusion: {in: TSHIRT_CUTS.map(&:to_s)}
|
|
|
|
validates :food_preferences, inclusion: {in: FOOD_PREFERENCES.map(&:to_s)}
|
|
|
|
validates :email, format: {with: /\A[^@]+@[^@]+\z/}, presence: true
|
|
|
|
validates :phone, presence: true, format: {with: /\A[+\- \(\)0-9]+\z/}
|
2024-04-18 18:57:26 +03:00
|
|
|
validates :volunteer_team, presence: true
|
2016-07-06 11:24:20 +03:00
|
|
|
validate :volunteer_teams_belong_to_conference
|
2015-10-21 23:13:39 +03:00
|
|
|
|
2019-04-28 21:10:54 +03:00
|
|
|
phony_normalize :phone, default_country_code: "BG"
|
2015-10-21 23:13:39 +03:00
|
|
|
|
|
|
|
belongs_to :conference
|
2024-04-18 18:57:26 +03:00
|
|
|
belongs_to :volunteer_team
|
|
|
|
has_and_belongs_to_many :additional_volunteer_teams, class_name: "VolunteerTeam"
|
2015-10-21 23:13:39 +03:00
|
|
|
|
2024-04-18 18:57:26 +03:00
|
|
|
before_create :ensure_main_volunteer_team_is_part_of_additional_volunteer_teams
|
2015-10-21 23:13:39 +03:00
|
|
|
before_create :assign_unique_id
|
|
|
|
after_create :send_notification_to_volunteer
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2024-04-18 18:57:26 +03:00
|
|
|
def ensure_main_volunteer_team_is_part_of_additional_volunteer_teams
|
|
|
|
self.additional_volunteer_teams |= [volunteer_team] if volunteer_team
|
2015-10-21 23:13:39 +03:00
|
|
|
end
|
|
|
|
|
2024-04-18 18:57:26 +03:00
|
|
|
def assign_unique_id
|
|
|
|
self.unique_id = Digest::SHA256.hexdigest(SecureRandom.uuid)
|
2015-10-21 23:13:39 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def send_notification_to_volunteer
|
|
|
|
VolunteerMailer.volunteer_notification(self).deliver_later
|
|
|
|
end
|
2016-07-06 11:24:20 +03:00
|
|
|
|
|
|
|
def volunteer_teams_belong_to_conference
|
|
|
|
conference_volunteer_teams = conference.volunteer_teams
|
2024-04-18 18:57:26 +03:00
|
|
|
unless additional_volunteer_teams.all? { |team| conference_volunteer_teams.include? team }
|
|
|
|
errors.add :additional_volunteer_teams, :invalid_volunteer_team
|
|
|
|
end
|
|
|
|
unless conference_volunteer_teams.include?(volunteer_team)
|
|
|
|
errors.add :volunteer_team, :invalid_volunteer_team
|
2016-07-06 11:24:20 +03:00
|
|
|
end
|
|
|
|
end
|
2015-10-21 23:13:39 +03:00
|
|
|
end
|