Commit e2b34e3c authored by Kaspar Vollenweider's avatar Kaspar Vollenweider 👻 Committed by Kaspar
Browse files

validate presence of first and lastname

parent 2bcbb80f
......@@ -21,8 +21,6 @@ class VolunteersController < ApplicationController
def create
@volunteer = Volunteer.new(volunteer_params)
@volunteer.schedules << Schedule.build
@volunteer.build_contact
@volunteer.registrar = current_user
authorize @volunteer
if @volunteer.save
......@@ -36,7 +34,8 @@ class VolunteersController < ApplicationController
state_was_registered = @volunteer.registered?
return render :edit unless @volunteer.update(volunteer_params)
if state_was_registered && @volunteer.accepted? && invite_volunteer_user
redirect_to volunteers_path, notice: t('invite_sent', email: @volunteer.contact.contact_emails.first.body)
redirect_to volunteers_path,
notice: t('invite_sent', email: @volunteer.contact.contact_emails.first.body)
else
redirect_to @volunteer, notice: t('volunteer_updated')
end
......
class Contact < ApplicationRecord
belongs_to :contactable, polymorphic: true, optional: true
validates :last_name, presence: true, if: :department?
has_many :contact_emails
accepts_nested_attributes_for :contact_emails, allow_destroy: true
has_many :contact_phones
accepts_nested_attributes_for :contact_phones, allow_destroy: true
validates :last_name, presence: true
validates :first_name, presence: true, unless: :department?
def to_s
last_name
end
def full_name
"#{try(:first_name)} #{last_name}"
"#{try(:first_name)} #{try(:last_name)}"
end
def department?
contactable_type == 'Department'
end
def volunteer?
contactable_type == 'Volunteer'
end
end
......@@ -2,14 +2,20 @@ class Volunteer < ApplicationRecord
include AssociatableFields
include GenderCollection
include FullName
acts_as_paranoid
before_save :default_state
has_one :contact, as: :contactable
accepts_nested_attributes_for :contact
has_one :first_language
acts_as_paranoid
belongs_to :user, optional: true
before_save :default_state
belongs_to :registrar, optional: true,
class_name: 'User', foreign_key: 'registrar_id'
has_attached_file :avatar, styles: { thumb: '100x100#' }
REGISTERED = 'registered'.freeze
ACCEPTED = 'accepted'.freeze
......@@ -19,12 +25,7 @@ class Volunteer < ApplicationRecord
STATES_FOR_REVIEWED = [ACCEPTED, REJECTED, INACTIVE, RESIGNED].freeze
STATES = [REGISTERED] + STATES_FOR_REVIEWED
belongs_to :user, optional: true
belongs_to :registrar, optional: true,
class_name: 'User', foreign_key: 'registrar_id'
has_attached_file :avatar, styles: { thumb: '100x100#' }
validates :contact, presence: true
validates :state, inclusion: { in: STATES }
validates_attachment :avatar, content_type: {
content_type: /\Aimage\/.*\z/
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment