diff --git a/app/controllers/conferences_controller.rb b/app/controllers/conferences_controller.rb index e7d196d5..f581e367 100644 --- a/app/controllers/conferences_controller.rb +++ b/app/controllers/conferences_controller.rb @@ -27,6 +27,10 @@ class ConferencesController < ApplicationController redirect_to admin_conference_splashpage_path(@conference.short_title) && return end + # User messages at the top of the page. + @unpaid_tickets = current_user_has_unpaid_tickets? + @user_needs_to_register = current_user_needs_to_register? + @image_url = "#{request.protocol}#{request.host}#{@conference.picture}" if splashpage.include_cfp @@ -79,4 +83,17 @@ class ConferencesController < ApplicationController format.html { head :ok } end if request.options? end + + def current_user_tickets + @current_user_tickets ||= current_user.ticket_purchases.by_conference(@conference) + end + + def current_user_needs_to_register? + current_user && !@conference.user_registered?(current_user) && + current_user_tickets.where(ticket: @conference.registration_tickets).paid.any? + end + + def current_user_has_unpaid_tickets? + current_user && current_user_tickets.unpaid.any? + end end diff --git a/app/models/conference.rb b/app/models/conference.rb index c48dcf02..b4b19e2c 100644 --- a/app/models/conference.rb +++ b/app/models/conference.rb @@ -30,6 +30,7 @@ class Conference < ApplicationRecord has_many :payments, dependent: :destroy has_many :supporters, through: :ticket_purchases, source: :user has_many :tickets, dependent: :destroy + has_many :registration_tickets, -> { for_registration }, class_name: 'Ticket' has_many :resources, dependent: :destroy has_many :booths, dependent: :destroy has_many :confirmed_booths, -> { where(state: 'confirmed') }, class_name: 'Booth' @@ -111,6 +112,13 @@ class Conference < ApplicationRecord user.present? && registrations.where(user_id: user.id).count > 0 end + ## + # True when there is at least one ticket marked as "registration" + # A user must get a registration ticket before registering. + def registration_ticket_required? + registration_tickets.any? + end + ## # Delete all EventSchedules that are not in the hours range # After the conference has been successfully updated diff --git a/app/models/registration.rb b/app/models/registration.rb index 759003fd..0e5a9d44 100644 --- a/app/models/registration.rb +++ b/app/models/registration.rb @@ -32,6 +32,8 @@ class Registration < ApplicationRecord if: -> { conference.try(:code_of_conduct).present? } } + validate :user_has_registration_ticket, if: -> { conference.registration_ticket_required? } + after_create :set_week, :subscribe_to_conference, :send_registration_mail ## @@ -65,4 +67,12 @@ class Registration < ApplicationRecord errors.add(:base, 'Registration limit exceeded') end end + + def user_has_registration_ticket + return if TicketPurchase.where(user: user, ticket: conference.registration_tickets).paid.any? + errors.add(:base, 'You must purchase a registration ticket before registering') + if TicketPurchase.where(user: user, ticket: conference.registration_tickets).unpaid.any? + errors.add(:base, 'You currently have a ticket with an unfinished purchase') + end + end end diff --git a/app/views/conferences/show.html.haml b/app/views/conferences/show.html.haml index f067fe2d..f498bf97 100644 --- a/app/views/conferences/show.html.haml +++ b/app/views/conferences/show.html.haml @@ -16,6 +16,28 @@ = content_for :title do = @conference.title += content_for :additional_messages do + - if @unpaid_tickets + .row + .col-md-12 + .alert.alert-dismissable.alert-info.text-center#unpaid-tickets{role: 'alert'} + %button.button.close{"data-dismiss" => "alert", "aria-label"=>"close"} + × + %p + You have unpaid tickets. Please complete your purchase. + = link_to('Purchase Tickets', new_conference_payment_path(@conference), class: 'btn btn-success btn-lg') + + + - if @user_needs_to_register + .row + .col-md-12 + .alert.alert-dismissable.alert-warning.text-center#flash{role: 'alert'} + %button.button.close{"data-dismiss" => "alert", 'aria-label': 'close'} + × + %p + You still need to complete your registration for #{@conference.title}. + = link_to('Complete Registration', new_conference_conference_registration_path(@conference), class: 'btn btn-success btn-lg') + #splash - if @conference.code_of_conduct.present? diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 86eafac9..df1b9da4 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -30,6 +30,7 @@ #messages .container = render 'layouts/messages' + = yield :additional_messages #content = yield