diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 400f5949..19983ff3 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -660,6 +660,7 @@ RSpec/AnyInstance: Exclude: - 'spec/controllers/admin/rooms_controller_spec.rb' - 'spec/controllers/admin/sponsorship_levels_controller_spec.rb' + - 'spec/controllers/admin/tickets_controller_spec.rb' - 'spec/controllers/admin/tracks_controller_spec.rb' - 'spec/controllers/admin/users_controller_spec.rb' - 'spec/controllers/conference_registration_controller_spec.rb' diff --git a/app/controllers/admin/tickets_controller.rb b/app/controllers/admin/tickets_controller.rb index ef309716..eb1d620e 100644 --- a/app/controllers/admin/tickets_controller.rb +++ b/app/controllers/admin/tickets_controller.rb @@ -38,13 +38,30 @@ module Admin end end + def give + ticket_purchase = @ticket.ticket_purchases.new(gift_ticket_params) + recipient = ticket_purchase.user + if ticket_purchase.save + redirect_to( + admin_conference_ticket_path(@conference.short_title, @ticket), + notice: "#{recipient.name} was given a #{@ticket.title} ticket." + ) + else + redirect_back( + fallback_location: admin_conference_ticket_path(@conference.short_title, @ticket), + error: "Unable to give #{recipient.name} a #{@ticket.title} ticket: " + + ticket_purchase.errors.full_messages.to_sentence + ) + end + end + def destroy if @ticket.destroy redirect_to admin_conference_tickets_path(conference_id: @conference.short_title), - notice: 'Ticket successfully destroyed.' + notice: 'Ticket successfully deleted.' else redirect_to admin_conference_tickets_path(conference_id: @conference.short_title), - error: 'Ticket was successfully destroyed.' \ + error: 'Deleting ticket failed! ' \ "#{@ticket.errors.full_messages.join('. ')}." end end @@ -59,5 +76,12 @@ module Admin :registration_ticket, :visible ) end + + def gift_ticket_params + response = params.require(:ticket_purchase).permit( + :user_id + ) + response.merge(paid: true, amount_paid: 0, conference: @conference) + end end end diff --git a/app/views/admin/tickets/show.html.haml b/app/views/admin/tickets/show.html.haml index 6fa74639..6ce20a20 100644 --- a/app/views/admin/tickets/show.html.haml +++ b/app/views/admin/tickets/show.html.haml @@ -7,7 +7,13 @@ Ticket %small = humanized_money_with_symbol @ticket.price - = link_to 'Edit Ticket', edit_admin_conference_ticket_path, class: 'btn btn-primary pull-right' + = link_to 'Edit Ticket', edit_admin_conference_ticket_path, class: 'btn btn-primary pull-right' + - if can? :give, Ticket + .pull-right + = link_to 'Give a Ticket', '#', + data: { toggle: 'modal', target: "#modal-give-ticket-#{@ticket.id}" }, + class: 'button btn btn-default btn-info' + %p.text-muted People who bought this ticket .row @@ -36,3 +42,26 @@ = buyer.affiliation %td = @ticket.tickets_paid(buyer) + +- content_for :modals do + .modal.fade{ id: "modal-give-ticket-#{@ticket.id}" } + .modal-dialog + .modal-content + = semantic_form_for(@ticket.ticket_purchases.new, + url: give_admin_conference_ticket_path(@conference, @ticket)) do |f| + .modal-header + %button.close{ data: { dismiss: 'modal' } } + %i.fa.fa-close + %h3.modal-title + Give a + = @ticket.title + Ticket + .modal-body + = user_selector_input(:user, f, '', false) + .modal-footer + = f.action :submit, as: :button, button_html: { class: 'btn btn-primary' } + +:javascript + $(document).ready(function() { + $('#ticket_purchase_user_id').selectize({}) + }); diff --git a/config/routes.rb b/config/routes.rb index 730d43cf..49246f30 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -112,7 +112,11 @@ Osem::Application.routes.draw do end resources :resources - resources :tickets + resources :tickets do + member do + post :give + end + end resources :sponsors, except: [:show] resources :lodgings, except: [:show] resources :emails, only: [:show, :update, :index] diff --git a/spec/controllers/admin/tickets_controller_spec.rb b/spec/controllers/admin/tickets_controller_spec.rb new file mode 100644 index 00000000..7e668b68 --- /dev/null +++ b/spec/controllers/admin/tickets_controller_spec.rb @@ -0,0 +1,247 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Admin::TicketsController do + let(:admin) { create(:admin) } + let(:conference) { create(:conference) } + let!(:ticket) { create(:ticket, conference: conference) } + let(:new_title) { Faker::Hipster.sentence } + + context 'admin is signed in' do + before { sign_in admin } + + describe 'GET #index' do + before { get :index, conference_id: conference } + + it 'assigns conference and tickets variables via cancancan' do + expect(assigns(:conference)).to eq conference + expect(assigns(:tickets)).to eq conference.tickets + end + + it 'renders index template' do + expect(response).to render_template('index') + end + end + + describe 'GET #edit' do + before { get :edit, conference_id: conference, id: ticket } + + it 'assigns ticket variable via cancancan' do + expect(assigns(:ticket)).to eq ticket + end + + it 'renders edit template' do + expect(response).to render_template('edit') + end + end + + describe 'GET #new' do + before { get :new, conference_id: conference } + + it 'assigns ticket variable' do + expect(assigns(:ticket)).to be_instance_of(Ticket) + end + + it 'renders new template' do + expect(response).to render_template('new') + end + end + + describe 'POST #create' do + context 'saves successfuly' do + before(:each, run: true) do + post :create, conference_id: conference, ticket: attributes_for(:ticket) + end + + let!(:ticket_count) { conference.tickets.count } + + it 'redirects to index path', run: true do + expect(response).to redirect_to( + admin_conference_tickets_path(conference_id: conference) + ) + end + + it 'shows success message in flash notice', run: true do + expect(flash[:notice]).to match('Ticket successfully created.') + end + + it 'creates new ticket' do + expect do + post :create, ticket: attributes_for(:ticket), + conference_id: conference + end.to change{ conference.tickets.count }.from(ticket_count).to(ticket_count + 1) + end + end + + context 'save fails' do + before do + allow_any_instance_of(Ticket).to receive(:save).and_return(false) + post :create, conference_id: conference, ticket: attributes_for(:ticket) + end + + let!(:ticket_count) { conference.tickets.count } + + it 'renders new template' do + expect(response).to render_template('new') + end + + it 'shows error in flash message' do + expect(flash[:error]).to match("Creating Ticket failed: #{ticket.errors.full_messages.join('. ')}.") + end + + it 'does not create new ticket' do + expect(conference.tickets.count).to eq ticket_count + end + end + end + + describe 'PATCH #update' do + context 'updates successfully' do + before do + patch :update, conference_id: conference, id: ticket, + ticket: attributes_for(:ticket, title: new_title) + end + + it 'redirects to index path' do + expect(response).to redirect_to( + admin_conference_tickets_path(conference_id: conference) + ) + end + + it 'shows success message in flash notice' do + expect(flash[:notice]).to match('Ticket successfully updated.') + end + + it 'updates the ticket' do + ticket.reload + expect(ticket.title).to eq(new_title) + end + end + + context 'update fails' do + before do + allow_any_instance_of(Ticket).to receive(:save).and_return(false) + patch :update, conference_id: conference, id: ticket, + ticket: attributes_for(:ticket, title: new_title) + end + + it 'renders edit template' do + expect(response).to render_template('edit') + end + + it 'shows error in flash message' do + expect(flash[:error]).to match("Ticket update failed: #{ticket.errors.full_messages.join('. ')}.") + end + + it 'does not update ticket' do + ticket.reload + expect(ticket.title).not_to eq(new_title) + end + end + end + + describe 'DELETE #destroy' do + context 'deletes successfully' do + before(:each, run: true) do + delete :destroy, conference_id: conference, id: ticket + end + + let!(:ticket_count) { conference.tickets.count } + + it 'redirects to index path', run: true do + expect(response).to redirect_to( + admin_conference_tickets_path(conference_id: conference) + ) + end + + it 'shows success message in flash notice', run: true do + expect(flash[:notice]).to match('Ticket successfully deleted.') + end + + it 'deletes the ticket' do + expect do + delete :destroy, conference_id: conference, id: ticket + end.to change{ conference.tickets.count }.from(ticket_count).to(ticket_count - 1) + end + end + + context 'delete fails' do + let!(:ticket_count) { conference.tickets.count } + + before do + allow_any_instance_of(Ticket).to receive(:destroy).and_return(false) + delete :destroy, conference_id: conference, id: ticket + end + + it 'redirects to index path' do + expect(response).to redirect_to( + admin_conference_tickets_path(conference_id: conference) + ) + end + + it 'shows error in flash message' do + expect(flash[:error]).to match("Deleting ticket failed! #{ticket.errors.full_messages.join('. ')}.") + end + + it 'does not delete ticket' do + expect(conference.tickets.count).to eq(ticket_count) + end + end + end + + describe 'POST #give' do + context 'grants a ticket purchase to a user' do + let!(:purchase_count) { admin.ticket_purchases.count } + + before do + post :give, conference_id: conference, id: ticket, + ticket_purchase: { user_id: admin.id } + end + + it 'redirects to ticket' do + expect(response).to redirect_to( + admin_conference_ticket_path(conference_id: conference, id: ticket) + ) + end + + it 'shows success message in flash notice' do + expect(flash[:notice]).to match( + "#{admin.name} was given a #{ticket.title} ticket." + ) + end + + it 'creates a ticket purchase' do + expect(admin.ticket_purchases.count).to eq(purchase_count + 1) + expect(admin.ticket_purchases.last.ticket).to eq(ticket) + end + end + + context 'giving fails' do + before do + allow_any_instance_of(TicketPurchase).to receive(:save).and_return(false) + post :give, conference_id: conference, id: ticket, + ticket_purchase: { user_id: admin.id } + end + + let(:purchase_count) { admin.ticket_purchases.count } + + it 'redirects to ticket' do + expect(response).to redirect_to( + admin_conference_ticket_path(conference_id: conference, id: ticket) + ) + end + + it 'shows error in flash message' do + expect(flash[:error]).to match( + "Unable to give #{admin.name} a #{ticket.title} ticket: " + ) + end + + it 'does not create a ticket purchase' do + expect(admin.ticket_purchases.count).to eq(purchase_count) + end + end + end + end +end