Module: EffectiveEventsEventRegistration

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/effective_events_event_registration.rb

Overview

EffectiveEventsEventRegistration

Mark your owner model with effective_events_event_registration to get all the includes

Defined Under Namespace

Modules: Base, ClassMethods

Instance Method Summary collapse

Instance Method Details

#addons!Object



470
471
472
473
474
475
476
# File 'app/models/concerns/effective_events_event_registration.rb', line 470

def addons!
  after_commit do
    update_submit_fees_and_order! if submit_order.present?
  end

  save!
end

#build_event_addonsObject

This builds the default event addons used by the wizard form



528
529
530
531
532
533
534
535
536
537
538
539
540
# File 'app/models/concerns/effective_events_event_registration.rb', line 528

def build_event_addons
  if event_addons.blank?
    raise('expected owner and event to be present') unless owner && event

    event_addons.build(
      first_name: owner.try(:first_name),
      last_name: owner.try(:last_name),
      email: owner.try(:email)
    )
  end

  event_addons
end

#build_event_registrant(event_ticket:) ⇒ Object



504
505
506
# File 'app/models/concerns/effective_events_event_registration.rb', line 504

def build_event_registrant(event_ticket:)
  event_registrants.build(event: event, event_ticket: event_ticket, owner: owner)
end

#checkout!Object



478
479
480
481
482
483
484
# File 'app/models/concerns/effective_events_event_registration.rb', line 478

def checkout!
  after_commit do
    send_order_emails! if submit_order&.deferred?
  end

  save!
end

#complete!Object



492
493
494
495
496
497
498
499
500
501
502
# File 'app/models/concerns/effective_events_event_registration.rb', line 492

def complete!
  raise('event registration must be submitted to complete!') unless 
  raise('expected a purchased order') unless submit_order&.purchased?

  wizard_steps[:checkout] ||= Time.zone.now
  wizard_steps[:submitted] ||= Time.zone.now
  wizard_steps[:complete] = Time.zone.now

  completed!
  true
end

#details!Object



462
463
464
465
466
467
468
# File 'app/models/concerns/effective_events_event_registration.rb', line 462

def details!
  after_commit do
    update_submit_fees_and_order! if submit_order.present?
  end

  save!
end

#display_countdown?Boolean

Returns:

  • (Boolean)


349
350
351
352
353
354
355
356
# File 'app/models/concerns/effective_events_event_registration.rb', line 349

def display_countdown?
  return false if done?
  return false unless selected_at.present?
  return false unless current_step.present?
  return false if draft? && orders.any? { |order| order.declined? && order.delayed? }

  [:start, :tickets, :submitted, :complete].exclude?(current_step)
end

#done?Boolean

Returns:

  • (Boolean)


345
346
347
# File 'app/models/concerns/effective_events_event_registration.rb', line 345

def done?
  completed?
end

#emailObject



337
338
339
# File 'app/models/concerns/effective_events_event_registration.rb', line 337

def email
  owner&.email
end

#event_addon(event_product:, first_name:, last_name:, email:) ⇒ Object

Find or build. But it’s not gonna work with more than 1. This is for testing only really.



515
516
517
518
# File 'app/models/concerns/effective_events_event_registration.rb', line 515

def event_addon(event_product:, first_name:, last_name:, email:)
  addon = event_addons.find { |er| er.event_product == event_product && er.first_name == first_name && er.last_name == last_name && er.email == email }
  addon || event_addons.build(event: event, event_product: event_product, owner: owner, first_name: first_name, last_name: last_name, email: email)
end

#event_ticket_selection(event_ticket:, quantity: 0) ⇒ Object

Find or build



521
522
523
524
525
# File 'app/models/concerns/effective_events_event_registration.rb', line 521

def event_ticket_selection(event_ticket:, quantity: 0)
  selection = event_ticket_selections.find { |ets| ets.event_ticket == event_ticket } || event_ticket_selections.build(event_ticket: event_ticket)
  selection.assign_attributes(quantity: quantity)
  selection
end

#event_ticketsObject



542
543
544
# File 'app/models/concerns/effective_events_event_registration.rb', line 542

def event_tickets
  present_event_registrants.map(&:event_ticket).uniq
end

#in_progress?Boolean

Returns:

  • (Boolean)


341
342
343
# File 'app/models/concerns/effective_events_event_registration.rb', line 341

def in_progress?
  draft? || 
end

#just_let_them_edit_tickets_and_register_anyway?Boolean

Returns:

  • (Boolean)


610
611
612
# File 'app/models/concerns/effective_events_event_registration.rb', line 610

def just_let_them_edit_tickets_and_register_anyway?
  false
end

#select_event_registrantsObject

Assigns the selected at time to start the reservation window



415
416
417
418
# File 'app/models/concerns/effective_events_event_registration.rb', line 415

def select_event_registrants
  now = Time.zone.now
  present_event_registrants.each { |er| er.assign_attributes(selected_at: now) }
end

#selected_atObject

When we make a ticket selection, we assign the selected_at to all tickets So the max or the min should be the same here.



360
361
362
# File 'app/models/concerns/effective_events_event_registration.rb', line 360

def selected_at
  event_registrants.map(&:selected_at).compact.max
end

#selected_expired?Boolean

Returns:

  • (Boolean)


368
369
370
371
# File 'app/models/concerns/effective_events_event_registration.rb', line 368

def selected_expired?
  return false if selected_at.blank?
  Time.zone.now >= selected_expires_at
end

#selected_expires_atObject



364
365
366
# File 'app/models/concerns/effective_events_event_registration.rb', line 364

def selected_expires_at
  selected_at + EffectiveEvents.EventRegistration.selection_window
end

#selection_not_expired?Boolean

Returns:

  • (Boolean)


373
374
375
376
# File 'app/models/concerns/effective_events_event_registration.rb', line 373

def selection_not_expired?
  return true if selected_at.blank?
  Time.zone.now < selected_expires_at
end

#send_event_capacity_released_email!Object



606
607
608
# File 'app/models/concerns/effective_events_event_registration.rb', line 606

def send_event_capacity_released_email!
  EffectiveEvents.send_email(:event_capacity_released, self)
end

#send_event_registrants_cancelled_email!Object



602
603
604
# File 'app/models/concerns/effective_events_event_registration.rb', line 602

def send_event_registrants_cancelled_email!
  submit_order.send_event_registrants_cancelled_email!
end

#send_order_emails!Object



598
599
600
# File 'app/models/concerns/effective_events_event_registration.rb', line 598

def send_order_emails!
  submit_order.send_order_emails!
end

#ticket_selection_expired!Object

Called by a before_action on the event registration show action



379
380
381
382
383
384
385
386
387
388
389
# File 'app/models/concerns/effective_events_event_registration.rb', line 379

def ticket_selection_expired!
  raise("unexpected submitted registration") if 
  raise("unexpected purchased order") if submit_order&.purchased?
  raise("unexpected deferred order") if submit_order&.deferred?

  event_registrants.each { |er| er.assign_attributes(selected_at: nil) }
  event_ticket_selections.each { |ets| ets.assign_attributes(quantity: 0) }
  assign_attributes(current_step: nil, wizard_steps: {}) # Reset all steps

  save!
end

#tickets!Object



439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# File 'app/models/concerns/effective_events_event_registration.rb', line 439

def tickets!
  assign_attributes(current_step: :tickets) # Ensure the unavailable tickets validations are run

  reset_all_wizard_steps_after(:tickets) unless 

  update_event_registrants
  select_event_registrants

  if event_registrants.any? { |er| er.marked_for_destruction? && !er.waitlisted_not_promoted? && er.event_ticket.capacity_available == 0 }
    assign_attributes(send_event_capacity_released_email: true)
  end

  waitlist_event_registrants

  after_commit do
    update_submit_fees_and_order! if submit_order.present?
    update_deferred_event_registration! if submit_order&.deferred?
    send_event_capacity_released_email! if send_event_capacity_released_email
  end

  save!
end

#to_sObject

Instance Methods



333
334
335
# File 'app/models/concerns/effective_events_event_registration.rb', line 333

def to_s
  event.present? ? "Event registration - #{event}" : model_name.human
end

#try_completed!Object



486
487
488
489
490
# File 'app/models/concerns/effective_events_event_registration.rb', line 486

def try_completed!
  return false unless 
  return false unless submit_order&.purchased?
  complete!
end

#unavailable_event_productsObject



556
557
558
559
560
561
562
563
564
# File 'app/models/concerns/effective_events_event_registration.rb', line 556

def unavailable_event_products
  unavailable = []

  present_event_addons.map(&:event_product).group_by { |p| p }.each do |event_product, event_products|
    unavailable << event_product unless event.event_product_available?(event_product, quantity: event_products.length)
  end

  unavailable
end

#unavailable_event_ticketsObject



546
547
548
549
550
551
552
553
554
# File 'app/models/concerns/effective_events_event_registration.rb', line 546

def unavailable_event_tickets
  unavailable = []

  present_event_registrants.map(&:event_ticket).group_by { |t| t }.each do |event_ticket, event_tickets|
    unavailable << event_ticket unless event.event_ticket_available?(event_ticket, except: self, quantity: event_tickets.length)
  end

  unavailable
end

#update_blank_registrants!Object



566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
# File 'app/models/concerns/effective_events_event_registration.rb', line 566

def update_blank_registrants!
  # This method is called by the user on a submitted or completed event registration.
  # Allow them to update blank registrants
  # Don't let them hack the form and change any information.
  if changes.present? || previous_changes.present?
    raise('unable to make changes to event while updating blank registrants')
  end

  if event_registrants.any? { |er| (er.changes.keys - Effective::EventRegistrant::PERMITTED_BLANK_REGISTRANT_CHANGES).present? }
    raise('unable to make changes to event registrants while updating blank registrants')
  end

  if event_registrants.any? { |er| er.blank_registrant_was == false && er.changes.present? }
    raise('unable to make changes to non-blank registrant while updating blank registrants')
  end

  if event_addons.any? { |ea| ea.changes.present? }
    raise('unable to make changes to event addons while updating blank registrants')
  end

  assign_attributes(current_step: :details) if current_step.blank? # Enables validations
  save!

  update_submit_fees_and_order! if submit_order.present? && !submit_order.purchased?

  after_commit do
    send_order_emails!
  end

  true
end

#update_event_registrantsObject

This considers the event_ticket_selection and builds the appropriate event_registrants



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'app/models/concerns/effective_events_event_registration.rb', line 392

def update_event_registrants
  event_ticket_selections.each do |event_ticket_selection|
    event_ticket = event_ticket_selection.event_ticket
    quantity = event_ticket_selection.quantity.to_i

    # All the registrants for this event ticket
    registrants = event_registrants.select { |er| er.event_ticket == event_ticket }

    # Delete over quantity
    if (diff = registrants.length - quantity) > 0
      registrants.last(diff).each { |er| er.mark_for_destruction }
    end

    # Create upto quantity
    if (diff = quantity - registrants.length) > 0
      diff.times { build_event_registrant(event_ticket: event_ticket) }
    end
  end

  event_registrants
end

#validate_one_ticket_per_event?Boolean

We always validate that the user cannot register for the same event ticket more than once This is an additional validation where they cannot register for the same event more than once Can be overridden in the model if you need event specific validation.

Returns:

  • (Boolean)


617
618
619
# File 'app/models/concerns/effective_events_event_registration.rb', line 617

def validate_one_ticket_per_event?
  EffectiveEvents.validate_one_ticket_per_event?
end

#waitlist_event_registrantsObject

Looks at any unselected event registrants and assigns a waitlist value If a single non-promoted waitlisted registrant exists, then all tickets purchased must also be waitlisted tickets.



422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'app/models/concerns/effective_events_event_registration.rb', line 422

def waitlist_event_registrants
  present_event_registrants.group_by { |er| er.event_ticket }.each do |event_ticket, event_registrants|
    if event_ticket.waitlist?
      existing = event_registrants.find { |er| er.persisted? && er.waitlisted_not_promoted? }.present?
      capacity = event.capacity_available(event_ticket: event_ticket, event_registration: self)

      # Count already-registered non-waitlisted registrants in this registration
      registered = event_registrants.count { |er| er.registered? && !er.waitlisted? }
      available = [capacity - registered, 0].max

      event_registrants.reject(&:registered?).each_with_index { |er, index| er.assign_attributes(waitlisted: (existing || index >= available)) }
    else
      event_registrants.each { |er| er.assign_attributes(waitlisted: false) }
    end
  end
end

#wizard_step_title(step) ⇒ Object



621
622
623
624
# File 'app/models/concerns/effective_events_event_registration.rb', line 621

def wizard_step_title(step)
  return EffectiveEvents.code_of_conduct_page_title if step == :code_of_conduct
  default_wizard_step_title(step)
end