mumuki-domain 9.0.3 → 9.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 07771f9e0e712988ad05c336cabef30fea9af24bbfd6da99dc48d7ab19aee1ba
4
- data.tar.gz: 52c848e349daf6f371daf73d5ef675c06af6da0eee19ce25f6329db5ff5a9383
3
+ metadata.gz: 469928d18be5b255deb12fc4bf34681d5be7c4fd8ce1d19943e5d931152f89b6
4
+ data.tar.gz: 36afde0edf8b67cdfb86fd3f6b4b31df496a4a8e33081ca2f8497e34395a64a4
5
5
  SHA512:
6
- metadata.gz: 556d16f80533171f7e67f9652f84c955a5d9f8d7efdd981ec7b141afb845f4bb880c391486c2a5f2adad434410c0a789a1552b0dd1019d22b24ee149d335c1e7
7
- data.tar.gz: 00cbc6852719e47a496f2bb2569cd98db9499df559bc9958ca6e482b3ece6c03498540b65cb38c8e0bee813c70e10eeca7ed2c87fc70c80d1a59bc9b407efee0
6
+ metadata.gz: 4f803662c7ac490c3531cb7adba534d871efb9e4b54bde3eb9f294971da09794834842892fbbc420d9fc4d231bb51372a76b0b06f021ac70f2fd74f844933284
7
+ data.tar.gz: 3001b4fa192a0065ed973d70b066612af5b739d1d144a41b037c89136fa06d9289f5d1e1c922063bf045502cab6aa8c02a8c238987ad34563ef0db92712f517c
@@ -87,6 +87,22 @@ class ApplicationRecord < ActiveRecord::Base
87
87
  end
88
88
  end
89
89
 
90
+ def self.with_temporary_token(field_name, duration = 2.hours)
91
+ class_eval do
92
+ token_attribute = field_name
93
+ token_date_attribute = "#{field_name}_expiration_date"
94
+
95
+ define_method("generate_#{field_name}!") do
96
+ update!(token_attribute => self.class.generate_secure_token, token_date_attribute => duration.from_now)
97
+ end
98
+
99
+ define_method("#{field_name}_matches?") do |token|
100
+ actual_token = attribute(token_attribute)
101
+ actual_token.present? && token == actual_token && attribute(token_date_attribute)&.future?
102
+ end
103
+ end
104
+ end
105
+
90
106
  def self.numbered(*associations)
91
107
  class_eval do
92
108
  associations.each do |it|
@@ -118,6 +134,19 @@ class ApplicationRecord < ActiveRecord::Base
118
134
  end
119
135
  end
120
136
 
137
+ def self.active_between(start_date_field, end_date_field, **options)
138
+ define_singleton_method(:active) do |actually_filter=true|
139
+ if actually_filter
140
+ self.where("(#{start_date_field} IS NULL OR #{start_date_field} < :now) AND (#{end_date_field} IS NULL OR #{end_date_field} > :now)", now: Time.now)
141
+ else
142
+ all
143
+ end
144
+ end
145
+
146
+ aliased_as = options.delete(:aliased_as)
147
+ singleton_class.send(:alias_method, aliased_as, :active) if aliased_as
148
+ end
149
+
121
150
  ## Partially implements resource-hash protocol, by
122
151
  ## defining `to_resource_h` and helper methods `resource_fields` and `slice_resource_h`
123
152
  ## using the given fields
@@ -140,4 +169,8 @@ class ApplicationRecord < ActiveRecord::Base
140
169
  def raise_foreign_key_error!
141
170
  raise ActiveRecord::InvalidForeignKey.new "#{model_name} is still referenced"
142
171
  end
172
+
173
+ def self.generate_secure_token
174
+ SecureRandom.base58(24)
175
+ end
143
176
  end
@@ -2,6 +2,8 @@ class CertificateProgram < ApplicationRecord
2
2
  belongs_to :organization
3
3
  has_many :certificates
4
4
 
5
+ active_between :start_date, :end_date, aliased_as: :ongoing
6
+
5
7
  def friendly
6
8
  title
7
9
  end
@@ -23,8 +25,8 @@ class CertificateProgram < ApplicationRecord
23
25
  }
24
26
  </style>
25
27
  <!-- You can use interpolations like --
26
- <%#= certificate.start_date %>
27
- <%#= certificate.end_date %>
28
+ <%#= certificate.started_at %>
29
+ <%#= certificate.ended_at %>
28
30
  <%#= user.formal_first_name %>
29
31
  <%#= user.formal_last_name %>
30
32
  <%#= user.formal_full_name %>
@@ -2,8 +2,8 @@ class Chapter < ApplicationRecord
2
2
  include WithStats
3
3
  include WithNumber
4
4
 
5
- include SiblingsNavigation
6
5
  include TerminalNavigation
6
+ include SiblingsNavigation
7
7
 
8
8
  include FriendlyName
9
9
 
@@ -29,8 +29,4 @@ class Chapter < ApplicationRecord
29
29
  def structural_parent
30
30
  book
31
31
  end
32
-
33
- def pending_siblings_for(user)
34
- book.pending_chapters(user)
35
- end
36
32
  end
@@ -12,10 +12,9 @@ module SiblingsNavigation
12
12
  structural_parent.structural_children
13
13
  end
14
14
 
15
- #TODO reestablish this after indicators reliably linked to assignments
16
- # def pending_siblings_for(user, organization=Organization.current)
17
- # siblings.reject { |it| it.progress_for(user, organization).completed? }
18
- # end
15
+ def pending_siblings_for(user, organization=Organization.current)
16
+ siblings.reject { |it| it.progress_for(user, organization).completed? }
17
+ end
19
18
 
20
19
  # Names
21
20
 
@@ -0,0 +1,16 @@
1
+ module WithSoftDeletion
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ enum deletion_motive: %i(self_deleted inappropriate_content shares_solution discloses_personal_information)
6
+ belongs_to :deleted_by, class_name: 'User', optional: true
7
+ end
8
+
9
+ def soft_delete!(motive, deleter)
10
+ update! deletion_motive: motive, deleted_by: deleter, deleted_at: Time.now
11
+ end
12
+
13
+ def deleted?
14
+ deleted_at.present?
15
+ end
16
+ end
@@ -38,6 +38,10 @@ class Discussion < ApplicationRecord
38
38
  end
39
39
  end
40
40
 
41
+ def visible_messages
42
+ messages.where.not(deletion_motive: :self_deleted).or(messages.where(deletion_motive: nil))
43
+ end
44
+
41
45
  def try_solve!
42
46
  if opened?
43
47
  update! status: reachable_statuses_for(initiator).first
@@ -73,7 +77,7 @@ class Discussion < ApplicationRecord
73
77
  end
74
78
 
75
79
  def last_message_date
76
- messages.last&.created_at || created_at
80
+ visible_messages.last&.created_at || created_at
77
81
  end
78
82
 
79
83
  def friendly
@@ -125,11 +129,11 @@ class Discussion < ApplicationRecord
125
129
  end
126
130
 
127
131
  def has_messages?
128
- messages.exists? || description.present?
132
+ visible_messages.exists? || description.present?
129
133
  end
130
134
 
131
135
  def responses_count
132
- messages.where.not(sender: initiator.uid).count
136
+ visible_messages.where.not(sender: initiator.uid).count
133
137
  end
134
138
 
135
139
  def has_responses?
@@ -167,7 +171,7 @@ class Discussion < ApplicationRecord
167
171
  end
168
172
 
169
173
  def being_accessed_by_moderator?
170
- last_moderator_access_at.present? && last_moderator_access_at.future? - MODERATOR_REVIEW_AVERAGE_TIME
174
+ last_moderator_access_at.present? && (last_moderator_access_at + MODERATOR_REVIEW_AVERAGE_TIME).future?
171
175
  end
172
176
 
173
177
  def last_moderator_access_visible_for?(user)
@@ -186,6 +190,6 @@ class Discussion < ApplicationRecord
186
190
  private
187
191
 
188
192
  def messages_by_updated_at(direction = :desc)
189
- messages.reorder(updated_at: direction)
193
+ messages.where(deletion_motive: nil).reorder(updated_at: direction)
190
194
  end
191
195
  end
@@ -18,6 +18,17 @@ class ExamAuthorizationRequest < ApplicationRecord
18
18
  exam_registration.description
19
19
  end
20
20
 
21
+ def icon
22
+ case status.to_sym
23
+ when :pending
24
+ { class: 'info-circle', type: 'info' }
25
+ when :approved
26
+ { class: 'check-circle', type: 'success' }
27
+ when :rejected
28
+ { class: 'times-circle', type: 'danger' }
29
+ end
30
+ end
31
+
21
32
  private
22
33
 
23
34
  def notify_user!
@@ -5,6 +5,8 @@ class ExamRegistration < ApplicationRecord
5
5
  belongs_to :organization
6
6
  has_and_belongs_to_many :exams
7
7
  has_many :authorization_requests, class_name: 'ExamAuthorizationRequest'
8
+ has_and_belongs_to_many :registrees, class_name: 'User'
9
+ has_many :notifications, as: :target
8
10
 
9
11
  enum authorization_criterion_type: %i(none passed_exercises), _prefix: :authorization_criterion
10
12
 
@@ -22,8 +24,20 @@ class ExamRegistration < ApplicationRecord
22
24
  authorization_criterion.ensure_valid!
23
25
  end
24
26
 
25
- def start!(users)
26
- users.each &method(:notify_user!)
27
+ def notify_unnotified_registrees!
28
+ unnotified_registrees.each { |registree| notify_registree! registree }
29
+ end
30
+
31
+ def unnotified_registrees?
32
+ unnotified_registrees.exists?
33
+ end
34
+
35
+ def register_users!(users)
36
+ users.each { |user| register! user }
37
+ end
38
+
39
+ def unnotified_registrees
40
+ registrees.where.not(id: Notification.notified_users_ids_for(self, self.organization))
27
41
  end
28
42
 
29
43
  def process_requests!
@@ -38,9 +52,17 @@ class ExamRegistration < ApplicationRecord
38
52
  ExamAuthorizationRequest.new(exam_registration: self, organization: organization)
39
53
  end
40
54
 
55
+ def register!(user)
56
+ registrees << user unless registered?(user)
57
+ end
58
+
59
+ def registered?(user)
60
+ registrees.include? user
61
+ end
62
+
41
63
  private
42
64
 
43
- def notify_user!(user)
44
- Notification.create! organization: organization, user: user, target: self
65
+ def notify_registree!(registree)
66
+ Notification.create! organization: organization, user: registree, target: self
45
67
  end
46
68
  end
@@ -249,18 +249,6 @@ class Exercise < ApplicationRecord
249
249
  private
250
250
 
251
251
  def evaluation_class
252
- if manual_evaluation?
253
- manual_evaluation_class
254
- else
255
- automated_evaluation_class
256
- end
257
- end
258
-
259
- def manual_evaluation_class
260
- Mumuki::Domain::Evaluation::Manual
261
- end
262
-
263
- def automated_evaluation_class
264
252
  Mumuki::Domain::Evaluation::Automated
265
253
  end
266
254
 
@@ -33,13 +33,31 @@ class Problem < QueriableChallenge
33
33
  end
34
34
 
35
35
  def evaluation_criteria?
36
- manual_evaluation? || expectations? || test.present?
36
+ manual_evaluation? || automated_evaluation?
37
+ end
38
+
39
+ def mixed_evaluation?
40
+ manual_evaluation? && automated_evaluation?
41
+ end
42
+
43
+ def automated_evaluation?
44
+ expectations? || test.present?
37
45
  end
38
46
 
39
47
  def expectations?
40
48
  own_expectations.present? || own_custom_expectations.present?
41
49
  end
42
50
 
51
+ def evaluation_class
52
+ if mixed_evaluation?
53
+ Mumuki::Domain::Evaluation::Mixed
54
+ elsif manual_evaluation?
55
+ Mumuki::Domain::Evaluation::Manual
56
+ else
57
+ Mumuki::Domain::Evaluation::Automated
58
+ end
59
+ end
60
+
43
61
  # Sets the layout. This method accepts input_kids as a synonym of input_primary
44
62
  # for historical reasons
45
63
  def layout=(layout)
@@ -1,14 +1,16 @@
1
1
  class Message < ApplicationRecord
2
+ include WithSoftDeletion
2
3
 
3
4
  belongs_to :discussion, optional: true
4
5
  belongs_to :assignment, foreign_key: :submission_id, primary_key: :submission_id, optional: true
6
+ belongs_to :approved_by, class_name: 'User', optional: true
7
+
5
8
  has_one :exercise, through: :assignment
6
9
 
7
10
  validates_presence_of :content, :sender
8
11
  validates_presence_of :submission_id, :unless => :discussion_id?
9
12
 
10
13
  after_save :update_counters_cache!
11
- after_destroy :update_counters_cache!
12
14
 
13
15
  markdown_on :content
14
16
 
@@ -41,7 +43,8 @@ class Message < ApplicationRecord
41
43
  end
42
44
 
43
45
  def to_resource_h
44
- as_json(except: [:id, :type, :discussion_id, :approved, :not_actually_a_question],
46
+ as_json(except: [:id, :type, :discussion_id, :approved, :approved_at, :approved_by_id,
47
+ :not_actually_a_question, :deletion_motive, :deleted_at, :deleted_by_id],
45
48
  include: {exercise: {only: [:bibliotheca_id]}})
46
49
  .merge(organization: Organization.current.name)
47
50
  end
@@ -50,14 +53,22 @@ class Message < ApplicationRecord
50
53
  update! read: true
51
54
  end
52
55
 
53
- def toggle_approved!
54
- toggle! :approved
56
+ def toggle_approved!(user)
57
+ if approved?
58
+ disapprove!
59
+ else
60
+ approve!(user)
61
+ end
55
62
  end
56
63
 
57
64
  def toggle_not_actually_a_question!
58
65
  toggle! :not_actually_a_question
59
66
  end
60
67
 
68
+ def approved?
69
+ approved_at?
70
+ end
71
+
61
72
  def validated?
62
73
  approved? || from_moderator?
63
74
  end
@@ -93,4 +104,14 @@ class Message < ApplicationRecord
93
104
  Assignment.find_by(submission_id: message_data.delete('submission_id'))&.receive_answer! message_data
94
105
  end
95
106
  end
107
+
108
+ private
109
+
110
+ def approve!(user)
111
+ update! approved: true, approved_at: Time.now, approved_by: user
112
+ end
113
+
114
+ def disapprove!
115
+ update! approved: false, approved_at: nil, approved_by: nil
116
+ end
96
117
  end
@@ -3,6 +3,11 @@ class Notification < ApplicationRecord
3
3
  belongs_to :organization
4
4
  belongs_to :target, polymorphic: true
5
5
 
6
+
7
+ scope :notified_users_ids_for, ->(target, organization=Organization.current) do
8
+ where(target: target, organization: organization).pluck(:user_id)
9
+ end
10
+
6
11
  def mark_as_read!
7
12
  update read: true
8
13
  end
@@ -21,6 +21,8 @@ class Organization < ApplicationRecord
21
21
  belongs_to :book
22
22
  has_many :usages
23
23
 
24
+ has_many :certificate_programs
25
+
24
26
  validates_presence_of :contact_email, :locale
25
27
  validates_presence_of :welcome_email_template, if: :greet_new_users?
26
28
  validates :name, uniqueness: true,
@@ -31,7 +33,7 @@ class Organization < ApplicationRecord
31
33
  after_create :reindex_usages!
32
34
  after_update :reindex_usages!, if: lambda { |user| user.saved_change_to_book_id? }
33
35
 
34
- has_many :guides, through: 'usages', source: 'item', source_type: 'Guide'
36
+ has_many :guides, -> { where 'usages.parent_item_type' => 'Lesson' }, through: 'usages', source: 'item', source_type: 'Guide'
35
37
  has_many :exercises, through: :guides
36
38
  has_many :assignments, through: :exercises
37
39
  has_many :exams
@@ -108,6 +110,10 @@ class Organization < ApplicationRecord
108
110
  name
109
111
  end
110
112
 
113
+ def ongoing_certificate_programs?
114
+ certificate_programs.ongoing.exists?
115
+ end
116
+
111
117
  # Tells if the given user can
112
118
  # ask for help in this organization
113
119
  #
data/app/models/user.rb CHANGED
@@ -49,6 +49,7 @@ class User < ApplicationRecord
49
49
  PLACEHOLDER_IMAGE_URL = 'user_shape.png'.freeze
50
50
 
51
51
  resource_fields :uid, :social_id, :email, :permissions, :verified_first_name, :verified_last_name, *profile_fields
52
+ with_temporary_token :delete_account_token
52
53
 
53
54
  def last_lesson
54
55
  last_guide.try(:lesson)
@@ -210,6 +211,10 @@ class User < ApplicationRecord
210
211
  name.split.map(&:first).map(&:capitalize).join(' ')
211
212
  end
212
213
 
214
+ def abbreviated_name
215
+ "#{first_name} #{last_name.first.capitalize + '.' if last_name.present?}".strip
216
+ end
217
+
213
218
  def progress_at(content, organization)
214
219
  Indicator.find_or_initialize_by(user: self, organization: organization, content: content)
215
220
  end
@@ -282,11 +287,11 @@ class User < ApplicationRecord
282
287
  end
283
288
 
284
289
  def formal_first_name
285
- verified_first_name || first_name
290
+ verified_first_name.presence || first_name
286
291
  end
287
292
 
288
293
  def formal_last_name
289
- verified_last_name || last_name
294
+ verified_last_name.presence || last_name
290
295
  end
291
296
 
292
297
  def formal_full_name
@@ -301,6 +306,12 @@ class User < ApplicationRecord
301
306
  certificates.where(certificate_program: certificate_program).exists?
302
307
  end
303
308
 
309
+ def certificate_in(certificate_program, certificate_h)
310
+ return if certificated_in?(certificate_program)
311
+ certificate = certificates.create certificate_h.merge(certificate_program: certificate_program)
312
+ UserMailer.certificate(certificate).deliver_later
313
+ end
314
+
304
315
  private
305
316
 
306
317
  def welcome_to_new_organizations!
@@ -16,10 +16,10 @@ class UserStats < ApplicationRecord
16
16
  exercises: {
17
17
  solved_count: organization_exercises
18
18
  .joins(:assignments)
19
- .where(assignments: { top_submission_status: [:passed, :skipped], submitter: user }.merge(date_filter))
19
+ .where(assignments: { top_submission_status: [:passed, :skipped], submitter: user, organization: organization }.merge(date_filter))
20
20
  .count,
21
- count: organization_exercises.count},
22
-
21
+ count: organization_exercises.count
22
+ },
23
23
  messages: messages_in_discussions_count(date_range)
24
24
  }
25
25
  end
@@ -0,0 +1,6 @@
1
+ class AddApprovedByAndAtToMessage < ActiveRecord::Migration[5.1]
2
+ def change
3
+ add_column :messages, :approved_at, :datetime
4
+ add_reference :messages, :approved_by, index: true
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ class AddDeleteAccountTokenToUser < ActiveRecord::Migration[5.1]
2
+ def change
3
+ add_column :users, :delete_account_token, :string
4
+ add_column :users, :delete_account_token_expiration_date, :datetime
5
+ end
6
+ end
@@ -0,0 +1,7 @@
1
+ class AddDeletionFieldsToMessage < ActiveRecord::Migration[5.1]
2
+ def change
3
+ add_column :messages, :deletion_motive, :integer
4
+ add_column :messages, :deleted_at, :datetime
5
+ add_reference :messages, :deleted_by, index: true
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ class AddDatesToCertificateProgram < ActiveRecord::Migration[5.1]
2
+ def change
3
+ add_column :certificate_programs, :start_date, :datetime
4
+ add_column :certificate_programs, :end_date, :datetime
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ class RenameCertificateDates < ActiveRecord::Migration[5.1]
2
+ def change
3
+ rename_column :certificates, :start_date, :started_at
4
+ rename_column :certificates, :end_date, :ended_at
5
+ end
6
+ end
@@ -0,0 +1,8 @@
1
+ class CreateExamRegistrationUserJoinTable < ActiveRecord::Migration[5.1]
2
+ def change
3
+ create_join_table :exam_registrations, :users do |t|
4
+ t.index :user_id
5
+ t.index :exam_registration_id
6
+ end
7
+ end
8
+ end
@@ -3,3 +3,4 @@ end
3
3
 
4
4
  require_relative './evaluation/manual'
5
5
  require_relative './evaluation/automated'
6
+ require_relative './evaluation/mixed'
@@ -0,0 +1,12 @@
1
+ class Mumuki::Domain::Evaluation::Mixed < Mumuki::Domain::Evaluation::Manual
2
+ def evaluate!(assignment, submission)
3
+ evaluation = submission.evaluate! assignment
4
+ if evaluation[:status].passed?
5
+ super
6
+ elsif evaluation[:status].passed_with_warnings?
7
+ evaluation.merge(status: Mumuki::Domain::Status::Submission::Failed)
8
+ else
9
+ evaluation
10
+ end
11
+ end
12
+ end
@@ -1,7 +1,7 @@
1
1
  FactoryBot.define do
2
2
  factory :certificate do
3
- start_date { 1.month.ago }
4
- end_date { 1.minute.ago }
3
+ started_at { 1.month.ago }
4
+ ended_at { 1.minute.ago }
5
5
  user { build :user, first_name: 'Jane', last_name: 'Doe' }
6
6
  certificate_program { build :certificate_program }
7
7
  end
@@ -1,5 +1,5 @@
1
1
  module Mumuki
2
2
  module Domain
3
- VERSION = '9.0.3'
3
+ VERSION = '9.1.1'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mumuki-domain
3
3
  version: !ruby/object:Gem::Version
4
- version: 9.0.3
4
+ version: 9.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Franco Leonardo Bulgarelli
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-10 00:00:00.000000000 Z
11
+ date: 2021-04-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -301,6 +301,7 @@ files:
301
301
  - app/models/concerns/with_scoped_queries/page.rb
302
302
  - app/models/concerns/with_scoped_queries/sort.rb
303
303
  - app/models/concerns/with_slug.rb
304
+ - app/models/concerns/with_soft_deletion.rb
304
305
  - app/models/concerns/with_target_audience.rb
305
306
  - app/models/concerns/with_terms_acceptance.rb
306
307
  - app/models/concerns/with_timed_enablement.rb
@@ -659,12 +660,19 @@ files:
659
660
  - db/migrate/20210119190204_create_exam_registration_exam_join_table.rb
660
661
  - db/migrate/20210301210530_add_period_start_and_end_to_course.rb
661
662
  - db/migrate/20210302181654_add_faqs_to_organizations.rb
663
+ - db/migrate/20210308145910_add_approved_by_and_at_to_message.rb
664
+ - db/migrate/20210310195602_add_delete_account_token_to_user.rb
665
+ - db/migrate/20210318191512_add_deletion_fields_to_message.rb
666
+ - db/migrate/20210318194559_add_dates_to_certificate_program.rb
667
+ - db/migrate/20210318195238_rename_certificate_dates.rb
668
+ - db/migrate/20210330175706_create_exam_registration_user_join_table.rb
662
669
  - lib/mumuki/domain.rb
663
670
  - lib/mumuki/domain/area.rb
664
671
  - lib/mumuki/domain/engine.rb
665
672
  - lib/mumuki/domain/evaluation.rb
666
673
  - lib/mumuki/domain/evaluation/automated.rb
667
674
  - lib/mumuki/domain/evaluation/manual.rb
675
+ - lib/mumuki/domain/evaluation/mixed.rb
668
676
  - lib/mumuki/domain/exceptions.rb
669
677
  - lib/mumuki/domain/exceptions/blocked_forum_error.rb
670
678
  - lib/mumuki/domain/exceptions/disabled_error.rb