Refactoring Tickets

- Tickets now independent from registration
- Implemented money gem
#419
This commit is contained in:
Chrisbr 2014-08-28 15:21:05 +02:00
parent 5485fe0e7f
commit 5f8a8ac6d9
63 changed files with 1581 additions and 573 deletions

View file

@ -0,0 +1,9 @@
class RenameSupporterLevelToTicket < ActiveRecord::Migration
def up
rename_table :supporter_levels, :tickets
end
def down
rename_table :tickets, :supporter_levels
end
end

View file

@ -0,0 +1,69 @@
class MigratingSupporterRegistrationsToTicketUsers < ActiveRecord::Migration
class TempSupporterRegistrations < ActiveRecord::Base
self.table_name = 'supporter_registrations'
attr_accessible :conference_id, :supporter_level_id, :registration_id, :user_id
end
class TempUser < ActiveRecord::Base
self.table_name = 'users'
attr_accessible :user_id
end
class TempRegistration < ActiveRecord::Base
self.table_name = 'registrations'
attr_accessible :user_id
end
def change
rename_column :supporter_registrations, :supporter_level_id, :ticket_id
rename_column :supporter_registrations, :code_is_valid, :paid
add_column :supporter_registrations, :quantity, :integer, default: 1
add_column :supporter_registrations, :user_id, :integer
deleted_user = TempUser.find_by(email: 'deleted@localhost.osem')
TempSupporterRegistrations.all.each do |s|
# Change relation from registration to user
registration = TempRegistration.find_by(id: s.registration_id)
if registration
user = TempUser.find_by(id: registration.user_id)
if user
s.user_id = user.id
s.save
end
end
if !s.user_id
s.user_id = deleted_user.id
s.save
end
end
# Sum up if a user has bought more than one ticket
TempSupporterRegistrations.all.each do |s|
sup_reg = TempSupporterRegistrations.where(
ticket_id: s.ticket_id,
user_id: s.user_id,
conference_id: s.conference_id)
quantity = sup_reg.count
if quantity > 1
# Save the amount in the first one
s.quantity = quantity
s.save
# Delete the other
sup_reg = sup_reg.where('id not in (?)', [s.id])
sup_reg.destroy_all
end
end
remove_column :supporter_registrations, :registration_id
remove_column :supporter_registrations, :code
remove_column :supporter_registrations, :name
remove_column :supporter_registrations, :email
remove_column :conferences, :use_supporter_levels
rename_table :supporter_registrations, :ticket_purchases
end
end

View file

@ -0,0 +1,27 @@
class SplitTicketPriceInPriceAndCurrency < ActiveRecord::Migration
class TempTicket < ActiveRecord::Base
self.table_name = 'tickets'
attr_accessible :ticket_price, :price_cents, :price_currency
end
def change
add_money :tickets, :price
TempTicket.all.each do |ticket|
# Replace currency symbol with ISO Code
ticket.ticket_price.gsub!('€', 'EUR')
ticket.ticket_price.gsub!('$', 'USD')
ticket.ticket_price.gsub!('£', 'GBP')
ticket.ticket_price.gsub!('¥', 'CNY')
ticket.ticket_price.gsub!('₹', 'INR')
money = ticket.ticket_price.to_money
ticket.price_cents = money.cents
ticket.price_currency = money.currency_as_string
ticket.save
end
remove_column :tickets, :ticket_price
remove_column :tickets, :url
end
end