Class: ExamRegistration

Inherits:
ApplicationRecord show all
Includes:
TerminalNavigation, WithTimedEnablement
Defined in:
app/models/exam_registration.rb

Defined Under Namespace

Classes: AuthorizationCriterion

Instance Method Summary collapse

Methods included from TerminalNavigation

#friendly, #navigable_name, #navigation_end?, #next_for, #siblings, #structural_parent

Methods included from WithTimedEnablement

#enabled?, #enabled_range

Methods inherited from ApplicationRecord

active_between, aggregate_of, all_except, defaults, #delete, #destroy!, enum_prefixed_translations_for, numbered, organic_on, resource_fields, #save, #save_and_notify!, #save_and_notify_changes!, serialize_symbolized_hash_array, teaser_on, #update_and_notify!, update_or_create!, whitelist_attributes, with_pg_retry, with_temporary_token

Methods included from WithPgLock

#with_pg_lock

Instance Method Details

#authorization_criterionObject



21
22
23
# File 'app/models/exam_registration.rb', line 21

def authorization_criterion
  @authorization_criterion ||= ExamRegistration::AuthorizationCriterion.parse(authorization_criterion_type, authorization_criterion_value)
end

#authorization_request_for(user) ⇒ Object



62
63
64
65
# File 'app/models/exam_registration.rb', line 62

def authorization_request_for(user)
  authorization_requests.find_by(user: user) ||
    ExamAuthorizationRequest.new(exam_registration: self, organization: organization)
end

#authorization_request_ids_countsObject



113
114
115
# File 'app/models/exam_registration.rb', line 113

def authorization_request_ids_counts
  authorization_requests.group(:exam_id).count
end

#available_examsObject



79
80
81
82
83
84
85
# File 'app/models/exam_registration.rb', line 79

def available_exams
  return exams unless limited_authorization_requests?
  counts = authorization_request_ids_counts
  exams.select do |it|
    counts[it.id].to_i < authorization_requests_limit
  end
end

#ended?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'app/models/exam_registration.rb', line 95

def ended?
  end_time.past?
end

#ensure_valid_authorization_criterion!Object



25
26
27
# File 'app/models/exam_registration.rb', line 25

def ensure_valid_authorization_criterion!
  authorization_criterion.ensure_valid!
end

#exam_available?(exam) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
# File 'app/models/exam_registration.rb', line 117

def exam_available?(exam)
  available_exams.include? exam
end

#limited_authorization_requests?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'app/models/exam_registration.rb', line 87

def limited_authorization_requests?
  authorization_requests_limit.present?
end

#meets_criterion?(user) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'app/models/exam_registration.rb', line 75

def meets_criterion?(user)
  authorization_criterion.meets_criterion?(user, organization)
end

#multiple_options?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'app/models/exam_registration.rb', line 91

def multiple_options?
  exams.count > 1
end

#notify_unnotified_registrees!Object



29
30
31
# File 'app/models/exam_registration.rb', line 29

def notify_unnotified_registrees!
  unnotified_registrees.each { |registree| notify_registree! registree }
end

#process_authorization_requests!Object



54
55
56
57
58
59
60
# File 'app/models/exam_registration.rb', line 54

def process_authorization_requests!
  authorization_requests.each do |it|
    process_request! it
    it.try_authorize!
  end
  update! processed: true
end

#process_requests!Object

Try to process authorization request, by acquiring a database lock for update the appropriate record.

This method is aimed to be sent across multiple servers or processed concurrently and still not send duplicate mails



50
51
52
# File 'app/models/exam_registration.rb', line 50

def process_requests!
  with_pg_lock proc { process_authorization_requests! }, proc { !processed? }
end

#register!(user) ⇒ Object



67
68
69
# File 'app/models/exam_registration.rb', line 67

def register!(user)
  registrees << user unless registered?(user)
end

#register_users!(users) ⇒ Object



37
38
39
# File 'app/models/exam_registration.rb', line 37

def register_users!(users)
  users.each { |user| register! user }
end

#registered?(user) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'app/models/exam_registration.rb', line 71

def registered?(user)
  registrees.include? user
end

#request_authorization!(user, exam) ⇒ Object



99
100
101
102
103
104
105
# File 'app/models/exam_registration.rb', line 99

def request_authorization!(user, exam)
  with_available_exam exam do
    authorization_requests.find_or_create_by! user: user do |it|
      it.assign_attributes organization: organization, exam: exam
    end
  end
end

#unnotified_registreesObject



41
42
43
# File 'app/models/exam_registration.rb', line 41

def unnotified_registrees
  registrees.where.not(id: Notification.notified_users_ids_for(self, self.organization))
end

#unnotified_registrees?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'app/models/exam_registration.rb', line 33

def unnotified_registrees?
  unnotified_registrees.exists?
end

#update_authorization_request_by_id!(request_id, exam) ⇒ Object



107
108
109
110
111
# File 'app/models/exam_registration.rb', line 107

def update_authorization_request_by_id!(request_id, exam)
  with_available_exam exam do
    authorization_requests.update request_id, exam: exam
  end
end

#with_available_exam(exam, &block) ⇒ Object



121
122
123
124
125
126
# File 'app/models/exam_registration.rb', line 121

def with_available_exam(exam, &block)
  transaction do
    raise Mumuki::Domain::GoneError unless exam_available?(exam)
    block.call
  end
end