Add (admin-only) tickets via 'visibile' attribute [migration]

(cherry picked from commit ce0db3d7a736e7bcde480721986dbfb4a82aa083)
This commit is contained in:
James Mason 2018-04-09 11:53:31 -07:00 committed by Henne Vogelsang
parent 6e276b2787
commit 199bc00c08
No known key found for this signature in database
GPG key ID: 97DDB66BDAF8D4D6
15 changed files with 73 additions and 9 deletions

View file

@ -52,7 +52,12 @@ module Admin
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
end
end

View file

@ -57,7 +57,7 @@ class ConferenceRegistrationsController < ApplicationController
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

View file

@ -54,7 +54,7 @@ class ConferencesController < ApplicationController
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)

View file

@ -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
@ -14,4 +15,8 @@ class TicketsController < ApplicationController
redirect_to root_path, notice: "There are no tickets available for #{@conference.title}!"
end
end
def load_tickets
@tickets = @conference.tickets.visible
end
end

View file

@ -85,7 +85,10 @@ class Ability
end
end
can :index, Ticket
can :index, Organization
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

View file

@ -20,6 +20,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

View file

@ -32,7 +32,7 @@ class TicketPurchase < ApplicationRecord
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)

View file

@ -1,3 +1,4 @@
<<<<<<< HEAD
= form_for(@ticket, url: (@ticket.new_record? ? admin_conference_tickets_path : admin_conference_ticket_path(@conference.short_title, @ticket))) do |f|
.form-group
= f.label :title
@ -21,5 +22,8 @@
%label
= f.check_box :registration_ticket
A registration ticket is with which user register for the conference.
%label
= f.check_box :visible
Only visible tickets are available to registrants. Non-visible tickets can only be managed by Admins.
%p.text-right
= f.submit nil, class: 'btn btn-primary'

View file

@ -16,6 +16,7 @@
%th Sold
%th Turnover
%th Registration Ticket
%th Visible?
%th Actions
%tbody
- @conference.tickets.each do |ticket|
@ -31,6 +32,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),

View file

@ -74,7 +74,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
%h4

View file

@ -19,8 +19,8 @@
%th Price
%th Total
%tbody
- @conference.tickets.each do |ticket|
= render 'ticket', f: f, ticket: ticket
- @conference.tickets.visible.each do |ticket|
= render partial: 'ticket', f: f, locals: {ticket: ticket}
%tr
%td
- if @conference.tickets.for_registration.any?

View file

@ -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

View file

@ -522,6 +522,7 @@ ActiveRecord::Schema[7.0].define(version: 2024_11_21_114727) do
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|

View file

@ -5,6 +5,7 @@ FactoryBot.define do
title { "#{Faker::Hipster.word} Ticket" }
price_cents { 1000 }
price_currency { 'USD' }
visible { true }
factory :registration_ticket do
registration_ticket { true }
end

View file

@ -29,6 +29,35 @@ feature Ticket do
expect(Ticket.count).to eq(2)
end
scenario 'add a invalid ticket', feature: true, js: true do
visit admin_conference_tickets_path(conference.short_title)
click_link 'Add Ticket'
fill_in 'ticket_title', with: ''
fill_in 'ticket_price', with: '-1'
click_button 'Create Ticket'
page.find('#flash')
expect(flash).to eq("Creating Ticket failed: Title can't be blank. Price cents must be greater than or equal to 0.")
expect(Ticket.count).to eq(1)
end
scenario 'add a hidden ticket', feature: true do
visit admin_conference_tickets_path(conference.short_title)
click_link 'Add Ticket'
fill_in 'ticket_title', with: 'Hidden Ticket'
fill_in 'ticket_description', with: 'The hidden ticket'
fill_in 'ticket_price', with: '100'
uncheck 'ticket_visible'
click_button 'Create Ticket'
page.find('#flash')
expect(flash).to eq('Ticket successfully created.')
expect(Ticket.count).to eq(2)
expect(Ticket.visible.count).to eq(1)
end
context 'Ticket already created' do
let!(:ticket) { create(:ticket, title: 'Business Ticket', price: 100, conference_id: conference.id) }