clarion/app/models/volunteer.rb

47 lines
1.5 KiB
Ruby
Raw Normal View History

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]
attachment :picture, type: :image
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/}
validates :volunteer_teams, presence: true
2016-07-06 11:24:20 +03:00
validate :volunteer_teams_belong_to_conference
2015-10-21 23:13:39 +03:00
phony_normalize :phone, default_country_code: 'BG'
belongs_to :conference
has_and_belongs_to_many :volunteer_teams
before_create :assign_unique_id
2016-09-26 17:50:27 +03:00
after_create :send_notification_to_organizers
2015-10-21 23:13:39 +03:00
after_create :send_notification_to_volunteer
private
def assign_unique_id
self.unique_id = Digest::SHA256.hexdigest(SecureRandom.uuid)
end
def send_notification_to_organizers
VolunteerMailer.team_notification(self).deliver_later
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
unless volunteer_teams.all? { |team| conference_volunteer_teams.include? team }
errors.add :volunteer_teams, :invalid_volunteer_team
end
end
2015-10-21 23:13:39 +03:00
end