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

optimizing volunteer update action

parent 6817a8f8
......@@ -40,11 +40,10 @@ class VolunteersController < ApplicationController
end
def update
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.primary_email)
@volunteer.attributes = volunteer_params
return render :edit unless @volunteer.valid?
if handle_volunteer_update
redirect_to volunteers_path, notice: t('invite_sent', email: @volunteer.primary_email)
else
redirect_to @volunteer, notice: t('volunteer_updated')
end
......@@ -63,12 +62,17 @@ class VolunteersController < ApplicationController
private
def handle_volunteer_update
if @volunteer.acceptance_change == ['undecided', 'accepted'] && @volunteer.user_id.blank?
@volunteer.save && invite_volunteer_user
else
@volunteer.save!
false
end
end
def invite_volunteer_user
new_user = User.new(
email: @volunteer.contact.primary_email, password: Devise.friendly_token,
role: 'volunteer', volunteer: @volunteer
)
new_user.save && new_user.invite!
User.new_volunteer(@volunteer) && @volunteer.user.invite!
end
def set_volunteer
......
......@@ -58,10 +58,14 @@ class User < ApplicationRecord
new_user = User.new(
email: email, password: Devise.friendly_token, role: role
)
new_user.save! && new_user.send_reset_password_instructions
end
def self.new_volunteer(volunteer)
User.new(email: volunteer.primary_email, password: Devise.friendly_token, role: 'volunteer',
volunteer: volunteer).save
end
def to_s
email
end
......
Markdown is supported
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