Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Admin ticketing #2306

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,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'
Expand Down
35 changes: 32 additions & 3 deletions app/controllers/admin/tickets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,50 @@ def update
end
end

def give
ticket_purchase = @ticket.ticket_purchases.new(gift_ticket_params)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After using this for a conference, the workflow works pretty well! But, this leads to a somewhat weird scenario when a user has an unpaid ticket. Sometimes we want to give a sponsored ticket to someone who started to go through the purchase process and when this happens it requires console access to first delete their ticket. I wonder if it makes sense to have a "replace" option, or if an unpaid ticket exists to just update it to paid with a value of $0.

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

private

def ticket_params
params.require(:ticket).permit(:conference, :title, :url, :description, :conference_id, :price_cents, :price_currency, :price, :registration_ticket)
params.require(:ticket).permit(
:conference, :conference_id,
:title, :url, :description,
:price_cents, :price_currency, :price,
: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
2 changes: 1 addition & 1 deletion app/controllers/conference_registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def create
sign_in(@registration.user)
end

if @conference.tickets.any? && !current_user.supports?(@conference)
if @conference.tickets.visible.any? && !current_user.supports?(@conference)
redirect_to conference_tickets_path(@conference.short_title),
notice: 'You are now registered and will be receiving E-Mail notifications.'
else
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/conferences_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def show
end
end
if splashpage.include_registrations || splashpage.include_tickets
@tickets = @conference.tickets.order('price_cents')
@tickets = @conference.tickets.visible.order('price_cents')
end
if splashpage.include_lodgings
@lodgings = @conference.lodgings.order('name')
Expand Down
7 changes: 6 additions & 1 deletion app/controllers/tickets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
class TicketsController < ApplicationController
before_action :authenticate_user!
load_resource :conference, find_by: :short_title
load_resource :ticket, through: :conference
before_action :load_tickets
authorize_resource :ticket, through: :conference
authorize_resource :conference_registrations, class: Registration
before_action :check_load_resource, only: :index

Expand All @@ -14,4 +15,8 @@ def check_load_resource
redirect_to root_path, notice: "There are no tickets available for #{@conference.title}!"
end
end

def load_tickets
@tickets = @conference.tickets.visible
end
end
4 changes: 3 additions & 1 deletion app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ def signed_in(user)
end

can :index, Organization
can :index, Ticket
can :index, Ticket do |ticket|
ticket.visible
end
can :manage, TicketPurchase, user_id: user.id
can [:new, :create], Payment, user_id: user.id
can [:index, :show], PhysicalTicket, user: user
Expand Down
2 changes: 2 additions & 0 deletions app/models/ticket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Ticket < ApplicationRecord

validates :price_cents, numericality: { greater_than_or_equal_to: 0 }

scope :visible, -> { where(visible: true) }

def bought?(user)
buyers.include?(user)
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/ticket_purchase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def self.purchase(conference, user, purchases)
errors.push('You cannot buy more than one registration tickets.')
else
ActiveRecord::Base.transaction do
conference.tickets.each do |ticket|
conference.tickets.visible.each do |ticket|
quantity = purchases[ticket.id.to_s].to_i
# if the user bought the ticket and is still unpaid, just update the quantity
purchase = if ticket.bought?(user) && ticket.unpaid?(user)
Expand Down
1 change: 1 addition & 0 deletions app/views/admin/tickets/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
= f.input :price
= f.input :price_currency, as: :select, class: 'form-control', collection: ['USD', 'EUR', 'GBP', 'INR', 'CNY', 'CHF'], include_blank: false
= f.input :registration_ticket, hint: 'A registration ticket is with which user register for the conference.'
= f.input :visible, hint: 'Only visible tickets are available to registrants. Non-visible tickets can only be managed by Admins.'
%p.text-right
= f.action :submit, as: :button, button_html: { class: 'btn btn-primary' }
3 changes: 3 additions & 0 deletions app/views/admin/tickets/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
%th Sold
%th Turnover
%th Registration Ticket
%th Visible?
%th Actions
%tbody
- @conference.tickets.each do |ticket|
Expand All @@ -30,6 +31,8 @@
= humanized_money_with_symbol ticket.tickets_turnover_total(ticket.id)
%td
= ticket.registration_ticket? ? 'Yes' : 'No'
%td
= ticket.visible? ? 'Yes' : 'No'
%td
.btn-group
= link_to 'Edit', edit_admin_conference_ticket_path(@conference.short_title, ticket.id),
Expand Down
31 changes: 30 additions & 1 deletion app/views/admin/tickets/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -35,3 +41,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({})
});
2 changes: 1 addition & 1 deletion app/views/conference_registrations/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
= link_to event.title, conference_program_proposal_path(@conference.short_title, event.id)
= '(' + registered_text(event) + ')'

- if @conference.tickets.any?
- if @conference.tickets.visible.any?
.row
.col-md-12
-if @tickets.any?
Expand Down
4 changes: 2 additions & 2 deletions app/views/tickets/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
%h1
Tickets
%p.lead
Please choose your tickets for
Please choose your tickets for
%strong
= @conference.title
here*
Expand All @@ -19,7 +19,7 @@
%th Price
%th Total
%tbody
- @conference.tickets.each do |ticket|
- @conference.tickets.visible.each do |ticket|
= render partial: 'ticket', f: f, locals: {ticket: ticket}
%tr
%td
Expand Down
6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@
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]
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20180409170433_add_visible_to_tickets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class AddVisibleToTickets < ActiveRecord::Migration[5.0]
def up
add_column :tickets, :visible, :boolean, default: true
Ticket.reset_column_information
Ticket.update_all(visible: true) # rubocop:disable Rails/SkipsModelValidations
end

def down
remove_column :tickets, :visible
end
end
2 changes: 1 addition & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,6 @@
t.datetime "updated_at"
t.boolean "include_cfp", default: false
t.boolean "include_booths"
t.boolean "shuffle_highlights", default: false, null: false
end

create_table "sponsors", force: :cascade do |t|
Expand Down Expand Up @@ -530,6 +529,7 @@
t.boolean "registration_ticket", default: false
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "visible", default: true
end

create_table "tracks", force: :cascade do |t|
Expand Down
Loading