Module: EffectiveMembershipsApplicant

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

Overview

EffectiveMembershipsApplicant

Mark your owner model with effective_memberships_applicant to get all the includes

Defined Under Namespace

Modules: Base, ClassMethods

Instance Method Summary collapse

Instance Method Details

#applicant_course(applicant_course_name: nil) ⇒ Object



639
640
641
642
# File 'app/models/concerns/effective_memberships_applicant.rb', line 639

def applicant_course(applicant_course_name: nil)
  applicant_courses.find { |ac| ac.applicant_course_name_id == applicant_course_name.id } ||
  applicant_courses.build(applicant_course_name: applicant_course_name, applicant_course_area: applicant_course_name.applicant_course_area)
end

#applicant_course_area_sum(applicant_course_area:) ⇒ Object



644
645
646
# File 'app/models/concerns/effective_memberships_applicant.rb', line 644

def applicant_course_area_sum(applicant_course_area:)
  applicant_courses.select { |ac| ac.applicant_course_area_id == applicant_course_area.id }.sum { |ac| ac.amount.to_i }
end

#applicant_course_areas_collectionObject



631
632
633
# File 'app/models/concerns/effective_memberships_applicant.rb', line 631

def applicant_course_areas_collection
  Effective::ApplicantCourseArea.deep.sorted
end

#applicant_course_names_collection(applicant_course_area:) ⇒ Object



635
636
637
# File 'app/models/concerns/effective_memberships_applicant.rb', line 635

def applicant_course_names_collection(applicant_course_area:)
  applicant_course_area.applicant_course_names
end

#applicant_courses_sumObject



648
649
650
# File 'app/models/concerns/effective_memberships_applicant.rb', line 648

def applicant_courses_sum
  applicant_courses.sum { |ac| ac.amount.to_i }
end

#applicant_endorsements_required?Boolean

Returns:

  • (Boolean)


671
672
673
# File 'app/models/concerns/effective_memberships_applicant.rb', line 671

def applicant_endorsements_required?
  required_steps.include?(:endorsements) && min_applicant_endorsements > 0
end

#applicant_references_required?Boolean

Returns:

  • (Boolean)


662
663
664
# File 'app/models/concerns/effective_memberships_applicant.rb', line 662

def applicant_references_required?
  required_steps.include?(:references) && min_applicant_references > 0
end

#applicant_review(reviewer:) ⇒ Object

Find



770
771
772
# File 'app/models/concerns/effective_memberships_applicant.rb', line 770

def applicant_review(reviewer:)
  applicant_reviews.find { |ar| ar.reviewer_id == reviewer.id && ar.reviewer_type == reviewer.class.name }
end

#applicant_reviews_required?Boolean

Returns:

  • (Boolean)


761
762
763
# File 'app/models/concerns/effective_memberships_applicant.rb', line 761

def applicant_reviews_required?
  (min_applicant_reviews > 0 || applicant_reviews.present?)
end

#apply_to_join?Boolean

Returns:

  • (Boolean)


523
524
525
# File 'app/models/concerns/effective_memberships_applicant.rb', line 523

def apply_to_join?
  applicant_type == 'Apply to Join'
end

#approve!Object

Admin approves an applicant. Registers the owner. Sends an email.



819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
# File 'app/models/concerns/effective_memberships_applicant.rb', line 819

def approve!
  raise('already approved') if was_approved?
  raise('applicant must have been submitted to approve!') unless 

  # Complete the wizard step. Just incase this is run out of order.
  wizard_steps[:checkout] ||= Time.zone.now
  wizard_steps[:submitted] ||= Time.zone.now
  assign_attributes(missing_info_reason: nil)

  approved!

  if apply_to_join?
    EffectiveMemberships.Registrar.register!(
      owner,
      to: to_category,
      status: to_status,
      date: approved_membership_date.presence,       # Set by the Admin Process form, or nil
      number: approved_membership_number.presence    # Set by the Admin Process form, or nil
    )
  elsif reclassification?
    EffectiveMemberships.Registrar.reclassify!(owner, to: to_category, status: to_status)
  elsif reinstatement?
    EffectiveMemberships.Registrar.reinstate!(owner, from: from_status)
  else
    raise('unsupported approval applicant_type')
  end

  save!

  after_commit { send_email(:applicant_approved) }
  true
end

#approve_email_templatesObject



805
806
807
808
# File 'app/models/concerns/effective_memberships_applicant.rb', line 805

def approve_email_templates
  raise('expected EffectiveEmailTemplates') unless defined?(EffectiveEmailTemplates)
  Effective::EmailTemplate.where('template_name ILIKE ?', 'applicant_approve%').order(:template_name)
end

#build_applicant_review(reviewer:) ⇒ Object

Find or build



775
776
777
# File 'app/models/concerns/effective_memberships_applicant.rb', line 775

def build_applicant_review(reviewer:)
  applicant_review(reviewer: reviewer) || applicant_reviews.build(reviewer: reviewer)
end

#build_organization(params = {}) ⇒ Object



519
520
521
# File 'app/models/concerns/effective_memberships_applicant.rb', line 519

def build_organization(params = {})
  self.organization = EffectiveMemberships.Organization.new(params)
end

#can_apply_applicant_types_collectionObject

Used by the select step



585
586
587
588
589
590
591
592
593
# File 'app/models/concerns/effective_memberships_applicant.rb', line 585

def can_apply_applicant_types_collection
  if owner.blank? || owner.membership.blank? || owner.membership.categories.blank?
    ['Apply to Join']
  elsif owner.membership.statuses.any?(&:reinstatable?)
    ['Apply for Reinstatement', 'Apply to Join']
  else
    ['Apply to Reclassify']
  end
end

#can_apply_categories_collectionObject

Used by the select step



596
597
598
599
600
601
602
603
604
605
606
607
608
609
# File 'app/models/concerns/effective_memberships_applicant.rb', line 596

def can_apply_categories_collection
  categories = EffectiveMemberships.Category.sorted.can_apply

  if user.blank? || !user.is?(:member)
    return categories.where(can_apply_new: true)
  end

  category_ids = user.membership.category_ids

  categories.select do |cat|
    cat.can_apply_existing? ||
    (cat.can_apply_restricted? && (category_ids & cat.can_apply_restricted_ids).present?)
  end
end

#complete!Object



725
726
727
728
729
730
731
732
733
734
735
736
# File 'app/models/concerns/effective_memberships_applicant.rb', line 725

def complete!
  raise('applicant must have been submitted to complete!') unless 

  # Let an admin ignore these requirements if need be
  # return false unless submitted? && completed_requirements.values.all?

  assign_attributes(missing_info_reason: nil)
  completed!

  after_commit { send_email(:applicant_completed) }
  true
end

#completed_requirementsObject

When an application is submitted, these must be done to go to completed. An Admin can override this and just set them to completed.



706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
# File 'app/models/concerns/effective_memberships_applicant.rb', line 706

def completed_requirements
  requirements = {}
  return requirements unless category.present?

  if applicant_endorsements_required?
    requirements['Applicant Endorsements'] = (applicant_endorsements.count(&:completed?) >= min_applicant_endorsements)
  end

  if applicant_references_required?
    requirements['Applicant References'] = (applicant_references.count(&:completed?) >= min_applicant_references)
  end

  if transcripts_required?
    requirements['Applicant Transcripts'] = transcripts_received?
  end

  requirements
end

#decline!Object

Admin approves an applicant. Registers the owner. Sends an email.



853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
# File 'app/models/concerns/effective_memberships_applicant.rb', line 853

def decline!
  raise('already declined') if was_declined?
  raise('previously approved') if was_approved?
  raise('applicant must have been submitted to decline!') unless 

  # Complete the wizard step. Just incase this is run out of order.
  wizard_steps[:checkout] ||= Time.zone.now
  wizard_steps[:submitted] ||= Time.zone.now
  declined!

  save!

  after_commit { send_email(:applicant_declined) }
  true
end

#done?Boolean

Returns:

  • (Boolean)


547
548
549
# File 'app/models/concerns/effective_memberships_applicant.rb', line 547

def done?
  approved? || declined?
end

#in_progress?Boolean

Returns:

  • (Boolean)


543
544
545
# File 'app/models/concerns/effective_memberships_applicant.rb', line 543

def in_progress?
  !approved? && !declined?
end

#individual?Boolean

Returns:

  • (Boolean)


535
536
537
# File 'app/models/concerns/effective_memberships_applicant.rb', line 535

def individual?
  !(owner.kind_of?(EffectiveMemberships.Organization) && category&.organization?)
end

#min_applicant_coursesObject

Courses Amounts step



627
628
629
# File 'app/models/concerns/effective_memberships_applicant.rb', line 627

def min_applicant_courses
  category&.min_applicant_courses.to_i
end

#min_applicant_educationsObject

Educations Step



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

def min_applicant_educations
  category&.min_applicant_educations.to_i
end

#min_applicant_endorsementsObject

Endorsements Step



667
668
669
# File 'app/models/concerns/effective_memberships_applicant.rb', line 667

def min_applicant_endorsements
  category&.min_applicant_endorsements.to_i
end

#min_applicant_equivalencesObject

Equivalences Step



676
677
678
# File 'app/models/concerns/effective_memberships_applicant.rb', line 676

def min_applicant_equivalences
  category&.min_applicant_equivalences.to_i
end

#min_applicant_experiences_monthsObject

Work Experiences Step



653
654
655
# File 'app/models/concerns/effective_memberships_applicant.rb', line 653

def min_applicant_experiences_months
  category&.min_applicant_experiences_months.to_i
end

#min_applicant_filesObject

Files Step



690
691
692
# File 'app/models/concerns/effective_memberships_applicant.rb', line 690

def min_applicant_files
  category&.min_applicant_files.to_i
end

#min_applicant_referencesObject

References Step



658
659
660
# File 'app/models/concerns/effective_memberships_applicant.rb', line 658

def min_applicant_references
  category&.min_applicant_references.to_i
end

#min_applicant_reviewsObject



765
766
767
# File 'app/models/concerns/effective_memberships_applicant.rb', line 765

def min_applicant_reviews
  category&.min_applicant_reviews.to_i
end

#missing!Object



738
739
740
741
742
743
744
745
# File 'app/models/concerns/effective_memberships_applicant.rb', line 738

def missing!
  raise('applicant must have been submitted to missing!') unless 

  missing_info!

  after_commit { send_email(:applicant_missing_info) }
  true
end

#organization?Boolean

Returns:

  • (Boolean)


539
540
541
# File 'app/models/concerns/effective_memberships_applicant.rb', line 539

def organization?
  owner.kind_of?(EffectiveMemberships.Organization) && category&.organization?
end

#ownerObject



511
512
513
# File 'app/models/concerns/effective_memberships_applicant.rb', line 511

def owner
  organization || user
end

#owner_symbolObject



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

def owner_symbol
  organization? ? :organization : :user
end

#reclassification?Boolean

Returns:

  • (Boolean)


527
528
529
# File 'app/models/concerns/effective_memberships_applicant.rb', line 527

def reclassification?
  applicant_type == 'Apply to Reclassify'
end

#reinstatement?Boolean

Returns:

  • (Boolean)


531
532
533
# File 'app/models/concerns/effective_memberships_applicant.rb', line 531

def reinstatement?
  applicant_type == 'Apply for Reinstatement'
end

#resubmit!Object



747
748
749
750
751
752
753
754
# File 'app/models/concerns/effective_memberships_applicant.rb', line 747

def resubmit!
  raise('applicant must have been submitted and missing info to resubmit!') unless  && was_missing_info?
  raise('already submitted') if 
  raise('expected a purchased order') unless submit_order&.purchased?

  assign_attributes(skip_to_step: :submitted, submitted_at: Time.zone.now)
  
end

#review!Object



798
799
800
801
802
803
# File 'app/models/concerns/effective_memberships_applicant.rb', line 798

def review!
  raise('applicant must have been submitted to review!') unless 

  # Let an admin ignore these requirements if need be
  reviewed!
end

#reviewable?Boolean

Completed -> Reviewed requirements

Returns:

  • (Boolean)


757
758
759
# File 'app/models/concerns/effective_memberships_applicant.rb', line 757

def reviewable?
  completed?
end

#reviewed_requirementsObject

When an application is completed, these must be done to go to reviewed An Admin can override this and just set them to reviewed.



781
782
783
784
785
786
787
788
789
790
# File 'app/models/concerns/effective_memberships_applicant.rb', line 781

def reviewed_requirements
  requirements = {}
  return requirements unless category.present?

  if applicant_reviews_required?
    requirements['Applicant Reviews'] = (applicant_reviews.count(&:done?) >= min_applicant_reviews)
  end

  requirements
end

#select!Object



611
612
613
614
615
616
617
618
619
# File 'app/models/concerns/effective_memberships_applicant.rb', line 611

def select!
  raise('cannot select a submitted applicant') if 
  raise('cannot select a purchased applicant') if orders.any? { |order| order.purchased? }

  # Reset the progress so far. They have to click through screens again.
  assign_attributes(wizard_steps: wizard_steps.slice(:start, :select))

  save!
end

#stampObject

Stamps step



695
696
697
698
699
700
701
702
# File 'app/models/concerns/effective_memberships_applicant.rb', line 695

def stamp
  stamps.first || stamps.build(
    owner: owner,
    name: owner.to_s,
    shipping_address: (owner.try(:shipping_address) || owner.try(:billing_address)),
    price: 0
  )
end

#status_labelObject



551
552
553
# File 'app/models/concerns/effective_memberships_applicant.rb', line 551

def status_label
  (status_was || status).to_s.gsub('_', ' ')
end

#summaryObject



555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
# File 'app/models/concerns/effective_memberships_applicant.rb', line 555

def summary
  case status_was
  when 'draft'
    "Applicant has not yet completed the #{category} wizard steps or paid to submit this application. This application will transition to 'submitted' after payment has been collected."
  when 'submitted'
    summary = "Application has been purchased and submitted."
    tasks = "The following tasks remain before it can be completed:"
    approval = "Waiting on approval."
    items = completed_requirements.map { |item, done| "<li>#{item}: #{done ? 'Complete' : 'Incomplete'}</li>" }.join
    completed_requirements.present? ? "<p>#{summary} #{tasks}</p><ul>#{items}</ul>" : "#{summary} #{approval}"
  when 'completed'
    if applicant_reviews_required?
      "All required materials have been provided. This application will transition to 'reviewed' after all reviewers have voted."
    else
      "This application has been completed and is now ready for an admin to approve or decline it. If approved, prorated fees will be generated."
    end
  when 'missing_info'
    "Missing the following information: <ul><li>#{missing_info_reason}</li></ul>"
  when 'reviewed'
    "This application has been reviewed and is now ready for an admin to approve or decline it. If approved, prorated fees will be generated."
  when 'approved'
    "The application has been approved. All done."
  when 'declined'
    "This application has been declined."
  else
    raise("unexpected status #{status}")
  end.html_safe
end

#to_categoryObject



810
811
812
# File 'app/models/concerns/effective_memberships_applicant.rb', line 810

def to_category
  category
end

#to_sObject

Instance Methods



505
506
507
508
509
# File 'app/models/concerns/effective_memberships_applicant.rb', line 505

def to_s
  return 'New Applicant' if applicant_type.blank? || category.blank? || owner.blank?

  "#{owner} - #{applicant_type} to #{category}"
end

#to_statusObject



814
815
816
# File 'app/models/concerns/effective_memberships_applicant.rb', line 814

def to_status
  nil
end

#transcripts_received?Boolean

Transcripts Step

Returns:

  • (Boolean)


681
682
683
# File 'app/models/concerns/effective_memberships_applicant.rb', line 681

def transcripts_received?
  transcripts_received_on_was.present?
end

#transcripts_required?Boolean

Returns:

  • (Boolean)


685
686
687
# File 'app/models/concerns/effective_memberships_applicant.rb', line 685

def transcripts_required?
  required_steps.include?(:transcripts)
end

#try_reviewed!Object

Called when an applicant_review was submitted



793
794
795
796
# File 'app/models/concerns/effective_memberships_applicant.rb', line 793

def try_reviewed!
  return false unless completed? && reviewed_requirements.values.all?
  reviewed!
end