diff --git a/app/controllers/admin/tickets_controller.rb b/app/controllers/admin/tickets_controller.rb index 6635ed29..e9f84fdb 100644 --- a/app/controllers/admin/tickets_controller.rb +++ b/app/controllers/admin/tickets_controller.rb @@ -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 diff --git a/app/controllers/conference_registrations_controller.rb b/app/controllers/conference_registrations_controller.rb index 5c17d8dc..88ff816b 100644 --- a/app/controllers/conference_registrations_controller.rb +++ b/app/controllers/conference_registrations_controller.rb @@ -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 diff --git a/app/controllers/conferences_controller.rb b/app/controllers/conferences_controller.rb index 05f98d6d..28309f46 100644 --- a/app/controllers/conferences_controller.rb +++ b/app/controllers/conferences_controller.rb @@ -52,7 +52,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') diff --git a/app/controllers/tickets_controller.rb b/app/controllers/tickets_controller.rb index c62afc06..7766d006 100644 --- a/app/controllers/tickets_controller.rb +++ b/app/controllers/tickets_controller.rb @@ -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 diff --git a/app/models/ability.rb b/app/models/ability.rb index 23eba7a7..9a0fa2fd 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -80,7 +80,9 @@ class Ability 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 diff --git a/app/models/ticket.rb b/app/models/ticket.rb index 8553ebca..d69739ab 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -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 diff --git a/app/models/ticket_purchase.rb b/app/models/ticket_purchase.rb index 129d7916..a3dfd1cd 100644 --- a/app/models/ticket_purchase.rb +++ b/app/models/ticket_purchase.rb @@ -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) diff --git a/app/views/admin/tickets/_form.html.haml b/app/views/admin/tickets/_form.html.haml index f66cea51..73c93229 100644 --- a/app/views/admin/tickets/_form.html.haml +++ b/app/views/admin/tickets/_form.html.haml @@ -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' } diff --git a/app/views/admin/tickets/index.html.haml b/app/views/admin/tickets/index.html.haml index 020cc1c7..ac7f97b5 100644 --- a/app/views/admin/tickets/index.html.haml +++ b/app/views/admin/tickets/index.html.haml @@ -15,6 +15,7 @@ %th Sold %th Turnover %th Registration Ticket + %th Visible? %th Actions %tbody - @conference.tickets.each do |ticket| @@ -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), diff --git a/app/views/conference_registrations/show.html.haml b/app/views/conference_registrations/show.html.haml index 79e4b4fd..8ed24d0d 100644 --- a/app/views/conference_registrations/show.html.haml +++ b/app/views/conference_registrations/show.html.haml @@ -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? diff --git a/app/views/tickets/index.html.haml b/app/views/tickets/index.html.haml index cba13ea5..2f3569a2 100644 --- a/app/views/tickets/index.html.haml +++ b/app/views/tickets/index.html.haml @@ -5,7 +5,7 @@ %h1 Tickets %p.lead - Please choose your tickets for + Please choose your tickets for %strong = @conference.title here* @@ -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 diff --git a/db/migrate/20180409170433_add_visible_to_tickets.rb b/db/migrate/20180409170433_add_visible_to_tickets.rb new file mode 100644 index 00000000..199f7a48 --- /dev/null +++ b/db/migrate/20180409170433_add_visible_to_tickets.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 9e3aae67..5fde0696 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -435,7 +435,6 @@ ActiveRecord::Schema.define(version: 20181113195810) do 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| @@ -530,6 +529,7 @@ ActiveRecord::Schema.define(version: 20181113195810) 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| diff --git a/spec/factories/tickets.rb b/spec/factories/tickets.rb index e71b898f..42d877d0 100644 --- a/spec/factories/tickets.rb +++ b/spec/factories/tickets.rb @@ -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 diff --git a/spec/features/tickets_spec.rb b/spec/features/tickets_spec.rb index a4dfed92..25cc1fab 100644 --- a/spec/features/tickets_spec.rb +++ b/spec/features/tickets_spec.rb @@ -43,6 +43,22 @@ feature Ticket do 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) }