diff --git a/app/models/registration.rb b/app/models/registration.rb index 99e6766c..33507ec1 100644 --- a/app/models/registration.rb +++ b/app/models/registration.rb @@ -29,7 +29,6 @@ class Registration < ActiveRecord::Base validate :registration_to_events_only_if_present after_create :set_week, :subscribe_to_conference, :send_registration_mail - after_destroy :destroy_purchased_tickets ## # Makes a list of events that includes (in that order): @@ -60,11 +59,6 @@ class Registration < ActiveRecord::Base end end - def destroy_purchased_tickets - ticket_purchased = TicketPurchase.where(conference_id: conference_id, user_id: user.id) - ticket_purchased.destroy_all - end - def subscribe_to_conference Subscription.create(conference_id: conference.id, user_id: user.id) end diff --git a/app/views/conference_registrations/show.html.haml b/app/views/conference_registrations/show.html.haml index 591d0777..5225deb1 100644 --- a/app/views/conference_registrations/show.html.haml +++ b/app/views/conference_registrations/show.html.haml @@ -116,8 +116,12 @@ -if @registration .btn-group-vertical.pull-right = link_to 'Edit your Registration', edit_conference_conference_registration_path(@conference.short_title), class: 'btn btn-success', disabled: @conference.end_date < Date.today - = link_to 'Unregister', conference_conference_registration_path(@conference.short_title), - method: :delete, class: 'btn btn-danger btn-xs', confirm: 'Are you sure you want to unregister?', disabled: @conference.end_date < Date.today + - if @tickets.any? + = link_to 'Unregister', conference_conference_registration_path(@conference.short_title), method: :delete, class: 'btn btn-danger btn-xs', + data: { confirm: "Your ticket purchases won't be refunded. Are you sure you want to unregister?" }, disabled: @conference.end_date < Date.today + - else + = link_to 'Unregister', conference_conference_registration_path(@conference.short_title), method: :delete, class: 'btn btn-danger btn-xs', + data: { confirm: "You haven't purchased any ticket. Are you sure you want to unregister?" }, disabled: @conference.end_date < Date.today - else = link_to 'Register', new_conference_conference_registration_path(@conference.short_title), class: 'btn btn-success btn-lg pull-right' diff --git a/app/views/tickets/index.html.haml b/app/views/tickets/index.html.haml index 894a4178..dc67f529 100644 --- a/app/views/tickets/index.html.haml +++ b/app/views/tickets/index.html.haml @@ -37,7 +37,11 @@ = button_tag(type: 'submit', class: 'btn btn-success btn-lg') do Continue %i.fa.fa-shopping-cart - = link_to 'Cancel registration', conference_conference_registration_path(@conference.short_title), method: :delete, class: 'btn btn-danger btn-sm' + - if current_user.ticket_purchases.by_conference(@conference).any? + = link_to 'Back to registration', conference_conference_registration_path(@conference.short_title), class: 'btn btn-default btn-sm' + - else + = link_to 'Cancel registration', conference_conference_registration_path(@conference.short_title), method: :delete, class: 'btn btn-danger btn-sm', + data: { confirm: 'Are you sure you want to unregister?' } .row .col-md-13 %p.text-muted.text-center diff --git a/spec/features/ticket_purchases_spec.rb b/spec/features/ticket_purchases_spec.rb index d2eadfc9..fc8e5174 100644 --- a/spec/features/ticket_purchases_spec.rb +++ b/spec/features/ticket_purchases_spec.rb @@ -88,5 +88,49 @@ feature Registration do end end end + + context 'who is registered' do + + scenario 'unregisters from conference, but ticket purchases dont delete', feature: true, js: true do + visit root_path + click_link 'Register' + + expect(current_path).to eq(new_conference_conference_registration_path(conference.short_title)) + click_button 'Register' + + fill_in "tickets__#{ticket.id}", with: '2' + expect(current_path).to eq(conference_tickets_path(conference.short_title)) + + click_button 'Continue' + + expect(current_path).to eq(new_conference_payment_path(conference.short_title)) + expect(flash).to eq('Please pay here to get tickets.') + purchase = TicketPurchase.where(user_id: participant.id, ticket_id: ticket.id).first + expect(purchase.quantity).to eq(2) + + if Rails.application.secrets.stripe_publishable_key + find('.stripe-button-el').click + + stripe_iframe = all('iframe[name=stripe_checkout_app]').last + sleep(5) + Capybara.within_frame stripe_iframe do + expect(page).to have_content('book your tickets') + page.execute_script(%{ $('input#card_number').val('4242424242424242'); }) + page.execute_script(%{ $('input#cc-exp').val('08/22'); }) + page.execute_script(%{ $('input#cc-csc').val('123'); }) + page.execute_script(%{ $('#submitButton').click(); }) + sleep(20) + end + + expect(current_path).to eq(conference_conference_registration_path(conference.short_title)) + expect(page.has_content?("2 #{ticket.title} Tickets for $ 10")).to be true + + click_button 'Unregister' + end + + purchase = TicketPurchase.where(user_id: participant.id, ticket_id: ticket.id).first + expect(purchase.quantity).to eq(2) + end + end end end diff --git a/spec/models/registration_spec.rb b/spec/models/registration_spec.rb index 3e71b772..772ccda1 100644 --- a/spec/models/registration_spec.rb +++ b/spec/models/registration_spec.rb @@ -90,23 +90,4 @@ describe 'Registration' do end end end - - describe '#destroy_purchased_tickets' do - it 'destroys purchased tickets if tickets are purchased' do - create(:ticket_purchase, conference: conference, user: user) - expect(user.registrations.size).to be 1 - expect(user.ticket_purchases.size).to be 1 - registration.destroy - expect(user.registrations.size).to be 0 - expect(user.ticket_purchases.size).to be 0 - end - - it 'destroys no tickets if no tickets are purchased' do - expect(user.registrations.size).to be 1 - expect(user.ticket_purchases.size).to be 0 - registration.destroy - expect(user.registrations.size).to be 0 - expect(user.ticket_purchases.size).to be 0 - end - end end