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 def create
@conference = Conference.find_all_by_short_title(params[:conference_id]).first @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? if @person
reg = @conference.registrations.where("person_id = ?", person.id).first if @person.registrations.where(conference_id: @conference).empty?
if reg.nil? @person.attributes = params[:registration][:person] # Should we really modify person information?
registration = person.registrations.new
else else
redirect_to admin_conference_registrations_path(@conference.short_title) redirect_to admin_conference_registrations_path(@conference.short_title)
flash[:notice] = "#{person.email} is already registred!" flash[:notice] = "#{@person.email} is already registred!"
return return
end end
else else
if params[:registration][:person][:first_name].blank? || params[:registration][:person][:last_name].blank? @person = Person.new params[:registration][:person]
redirect_to(:back, :alert => "Please fill in your first and last name before registering.") end
return @person.email = email
end
user = User.new @user = @person.user
user.email = params[:registration][:user][:email] if @user.nil?
user.password = rand(36**6).to_s(36) @user = @person.build_user
begin user.save! @user.password = rand(36**6).to_s(36)
user.skip_confirmation! @user.skip_confirmation!
person = Person.where("user_id = ?", user.id).first end
person.update_attributes(params[:registration][:person]) @user.email = @person.email
begin
registration = person.registrations.new @registration = @person.registrations.build
if params[:registration][:supporter_registration] 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) @supporter_registration = @registration.build_supporter_registration
end @supporter_registration.attributes = params[:registration][:supporter_registration]
rescue Exception => e @supporter_registration.conference_id = @conference.id
user.destroy else
person.destroy @supporter_registration = @conference.supporter_registrations.new
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
end end
params[:registration].delete :person params[:registration].delete :person
params[:registration].delete :user params[:registration].delete :user
params[:registration].delete :supporter_registration params[:registration].delete :supporter_registration
registration.update_attributes(params[:registration]) @registration.attributes = params[:registration]
registration.update_attributes(:conference_id => @conference.id, :attended => true) @registration.conference_id = @conference.id
registration.save! @registration.attended = true
redirect_to admin_conference_registrations_path(@conference.short_title) begin
flash[:notice] = "Successfully created new registration for #{person.email}." 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)
rescue ActiveRecord::RecordInvalid
render action: "new"
end
end end
def delete 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 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 :event_people, :dependent => :destroy
has_many :events, :through => :event_people, :uniq => true has_many :events, :through => :event_people, :uniq => true
has_many :registrations, :dependent => :destroy has_many :registrations, :dependent => :destroy

View file

@ -1,7 +1,13 @@
class SupporterRegistration < ActiveRecord::Base class SupporterRegistration < ActiveRecord::Base
belongs_to :supporter_level belongs_to :supporter_level
belongs_to :registration 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 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 end

View file

@ -8,7 +8,7 @@ class User < ActiveRecord::Base
:confirmable :confirmable
has_and_belongs_to_many :roles 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 attr_accessible :email, :password, :password_confirmation, :remember_me, :role_id, :role_ids, :person_attributes
accepts_nested_attributes_for :person accepts_nested_attributes_for :person
@ -50,7 +50,7 @@ class User < ActiveRecord::Base
private private
def create_person def create_person
# TODO Search people for existing email address, add to their account # 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 true
end end
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| = semantic_form_for(@registration, :url => admin_conference_registrations_new_path(@conference.short_title), :html => { :method => :put }) do |f|
= f.inputs "Your details" do = 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| = 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 :first_name, :as => :string
= p.input :last_name, :as => :string = p.input :last_name, :as => :string
= p.input :public_name, :as => :string = p.input :public_name, :as => :string
@ -71,4 +71,4 @@
onSelect: function(selected) { onSelect: function(selected) {
$("#registration-arrival-datepicker").datepicker("option","maxDate", selected) $("#registration-arrival-datepicker").datepicker("option","maxDate", selected)
} }
}); });