diff --git a/app/controllers/admin/registrations_controller.rb b/app/controllers/admin/registrations_controller.rb index 30236e58..891cd126 100644 --- a/app/controllers/admin/registrations_controller.rb +++ b/app/controllers/admin/registrations_controller.rb @@ -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 - 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 - 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 + @person = Person.new params[:registration][:person] + end + @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] + @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! - redirect_to admin_conference_registrations_path(@conference.short_title) - flash[:notice] = "Successfully created new registration for #{person.email}." + @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) + rescue ActiveRecord::RecordInvalid + render action: "new" + end end def delete diff --git a/app/models/person.rb b/app/models/person.rb index 1762b021..ea61ef2c 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -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 diff --git a/app/models/supporter_registration.rb b/app/models/supporter_registration.rb index a0a638b1..715748ac 100644 --- a/app/models/supporter_registration.rb +++ b/app/models/supporter_registration.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 1639aa44..da74413c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/views/admin/registrations/new.html.haml b/app/views/admin/registrations/new.html.haml index 1d81f757..f4c64d8c 100644 --- a/app/views/admin/registrations/new.html.haml +++ b/app/views/admin/registrations/new.html.haml @@ -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 @@ -71,4 +71,4 @@ onSelect: function(selected) { $("#registration-arrival-datepicker").datepicker("option","maxDate", selected) } - }); \ No newline at end of file + });