mumuki-domain 9.0.4 → 9.1.2

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: a503cf97148dbefc08c07e442bb086ac2eb37ad13e00abb39497f980e5b2e5ec
4
- data.tar.gz: 41056f07210ae148026a0333518a5948739766ad70333038413f393acd7a3ad1
3
+ metadata.gz: cee0ac6003bfa5540350ced96322774553ba6999f9c37d90aa48cb08d7af787d
4
+ data.tar.gz: 69f2c3b8f4025cd5eee0b1b3dcfa155a2f278719bd220c34910bdf04ad1bf019
5
5
  SHA512:
6
- metadata.gz: 643e7098cd029bd2185c99ea476b3d82e04b3e161970047b310f78edef0f68b753bcf142ceab2e0b53711f353092a176c40babfef9259f437178fefcb5f483f6
7
- data.tar.gz: 85e12de8b448db4fffcf1227231de46026a7e2142664a31c15310042138ed86cfd5f44aa09afe7c7bae27aaf3aeb4ab6cce9835604c809e1285301d14432beae
6
+ metadata.gz: 5e9421f3b18be2279b97185b622dd3079588e2843e9e217d610749f458433ed37c9cba47d551da49c0623aa5b891a746980c510b893423526ce665eb1f615b50
7
+ data.tar.gz: 2fcca6401cf692bd457b6273ca39aaa3f16864e6cc7da2f62f1c468409e3f11e8b99ad292f58d52e35c02e1428ce2c050cfc302bdde5e5aa1e223aaecd4f8829
@@ -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
 
@@ -18,4 +18,8 @@ module TerminalNavigation
18
18
  def siblings
19
19
  []
20
20
  end
21
+
22
+ def next_for(_user)
23
+ nil
24
+ end
21
25
  end
@@ -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?
@@ -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.4'
3
+ VERSION = '9.1.2'
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.4
4
+ version: 9.1.2
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-11 00:00:00.000000000 Z
11
+ date: 2021-05-03 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