Use render instead of redirect if registration/create fails

This commit is contained in:
Ancor Gonzalez Sosa 2013-07-16 16:03:36 +02:00
parent 243f33ff91
commit bae0be338a
5 changed files with 53 additions and 42 deletions

View file

@ -58,53 +58,57 @@ class Admin::RegistrationsController < ApplicationController
def create
@conference = Conference.find_all_by_short_title(params[:conference_id]).first
person = Person.where("email LIKE ?", params[:registration][:user][:email]).first
email = params[:registration][:person].delete(:user)[:email]
@person = Person.find_by_email email
@registration = nil
@user = nil
if !person.nil?
reg = @conference.registrations.where("person_id = ?", person.id).first
if reg.nil?
registration = person.registrations.new
if @person
if @person.registrations.where(conference_id: @conference).empty?
@person.attributes = params[:registration][:person] # Should we really modify person information?
else
redirect_to admin_conference_registrations_path(@conference.short_title)
flash[:notice] = "#{person.email} is already registred!"
flash[:notice] = "#{@person.email} is already registred!"
return
end
else
if params[:registration][:person][:first_name].blank? || params[:registration][:person][:last_name].blank?
redirect_to(:back, :alert => "Please fill in your first and last name before registering.")
return
@person = Person.new params[:registration][:person]
end
user = User.new
user.email = params[:registration][:user][:email]
user.password = rand(36**6).to_s(36)
begin user.save!
user.skip_confirmation!
person = Person.where("user_id = ?", user.id).first
person.update_attributes(params[:registration][:person])
begin
registration = person.registrations.new
@person.email = email
@user = @person.user
if @user.nil?
@user = @person.build_user
@user.password = rand(36**6).to_s(36)
@user.skip_confirmation!
end
@user.email = @person.email
@registration = @person.registrations.build
if params[:registration][:supporter_registration]
registration.supporter_registration = @conference.supporter_registrations.new(:supporter_level_id => params[:registration][:supporter_registration][:supporter_level_id], :code => params[:registration][:supporter_registration][:code], :email => person.email, :name => person.public_name)
end
rescue Exception => e
user.destroy
person.destroy
redirect_to(:back, :alert => "Did not create registration. #{e.message}")
return
end
rescue Exception => e
redirect_to(:back, :alert => "Did not create new user/person. #{e.message}")
return
end
@supporter_registration = @registration.build_supporter_registration
@supporter_registration.attributes = params[:registration][:supporter_registration]
@supporter_registration.conference_id = @conference.id
else
@supporter_registration = @conference.supporter_registrations.new
end
params[:registration].delete :person
params[:registration].delete :user
params[:registration].delete :supporter_registration
registration.update_attributes(params[:registration])
registration.update_attributes(:conference_id => @conference.id, :attended => true)
registration.save!
@registration.attributes = params[:registration]
@registration.conference_id = @conference.id
@registration.attended = true
begin
Registration.transaction do
@person.save!
@user.save!
@registration.save!
end
flash[:notice] = "Successfully created new registration for #{@person.email}."
redirect_to admin_conference_registrations_path(@conference.short_title)
flash[:notice] = "Successfully created new registration for #{person.email}."
rescue ActiveRecord::RecordInvalid
render action: "new"
end
end
def delete

View file

@ -4,6 +4,7 @@ class Person < ActiveRecord::Base
attr_accessible :email, :first_name, :last_name, :public_name, :biography, :company, :avatar, :irc_nickname
belongs_to :user, :inverse_of => :person
has_many :event_people, :dependent => :destroy
has_many :events, :through => :event_people, :uniq => true
has_many :registrations, :dependent => :destroy

View file

@ -1,7 +1,13 @@
class SupporterRegistration < ActiveRecord::Base
belongs_to :supporter_level
belongs_to :registration
before_save :set_attributes_from_person
attr_accessible :registration, :supporter_level_id, :name, :email, :supporter_level, :code, :code_is_valid, :conference_id
def set_attributes_from_person
self.name ||= registration.try(:person).try(:public_name)
self.email ||= registration.try(:person).try(:email)
true
end
end

View file

@ -8,7 +8,7 @@ class User < ActiveRecord::Base
:confirmable
has_and_belongs_to_many :roles
has_one :person
has_one :person, :inverse_of => :user
attr_accessible :email, :password, :password_confirmation, :remember_me, :role_id, :role_ids, :person_attributes
accepts_nested_attributes_for :person
@ -50,7 +50,7 @@ class User < ActiveRecord::Base
private
def create_person
# TODO Search people for existing email address, add to their account
build_person(:email => self.email)
build_person(:email => self.email) if person.nil?
true
end
end

View file

@ -7,9 +7,9 @@
= semantic_form_for(@registration, :url => admin_conference_registrations_new_path(@conference.short_title), :html => { :method => :put }) do |f|
= f.inputs "Your details" do
= f.fields_for :user do |u|
= u.input :email, :as => :string, :hint => "Please enter a valid email address. You will need it to log in to OSEM later."
= f.fields_for @person do |p|
= p.fields_for @user do |u|
= u.input :email, :as => :string, :hint => "Please enter a valid email address. You will need it to log in to OSEM later."
= p.input :first_name, :as => :string
= p.input :last_name, :as => :string
= p.input :public_name, :as => :string