mumuki-domain 9.18.0 → 9.22.0

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: 58b906585eb037f9120720bf77a34555343051e1aa2c87636a86208604247c2d
4
- data.tar.gz: ac72f7c3a8e36c388fe4cbf0c7e1e3a51620c9a2a6a333222a033dfa56ab3176
3
+ metadata.gz: 3822b6222097cb19717c7d6a76c97988f3993ef342b7e238a38ffbba6dbd7bb7
4
+ data.tar.gz: 2d6a733e2f9daf5d7c4fe6694f59ce1f5f7887067121d2af0a772be73e785b3a
5
5
  SHA512:
6
- metadata.gz: 254fa485adb9816805590f4fc60877b8e1626f152d7e335e419ac5402079ec9bd4f30a5fd85cab712e694c10f8aeb0a9829d762bd1ec01323ccec9525981d930
7
- data.tar.gz: 5ef6dd8ed07d02d17c9c6049371bcd50a1235e7958d9a68ea41940121ff72c2f81c17beb9b9a69b2b5d70fc4c811df0bd1181cd18ebc28efbd9676d922d8d1a1
6
+ metadata.gz: d0c4b744c65d518051715f839920c3adc7991bd747d26c6d75507335e8b26ce17842226cb3b2e5bac3acec3970f299b2f7b01b18271254f94aff286f62de8640
7
+ data.tar.gz: 26e47f01673258854896ca35468e7b5932514748c066f53c076cd06cfe06a2e92dc87ddd523eef97644e4e2e9d3e1e2fd2bde616c62dc0b06b3f63c884489427
@@ -68,6 +68,10 @@ class Assignment < Progress
68
68
  end
69
69
  end
70
70
 
71
+ def randomized_values
72
+ exercise.randomizer.randomized_values(submitter.id)
73
+ end
74
+
71
75
  def save_submission!(submission)
72
76
  transaction do
73
77
  update! submission_id: submission.id
@@ -182,8 +186,8 @@ class Assignment < Progress
182
186
  language: {only: [:name]}},
183
187
  },
184
188
  exercise: {only: [:name, :number]},
185
- submitter: {only: [:email, :social_id, :uid], methods: [:name, :profile_picture]}}).
186
- deep_merge(
189
+ submitter: {only: [:email, :social_id, :uid], methods: [:name, :profile_picture]}})
190
+ .deep_merge(
187
191
  'organization' => Organization.current.name,
188
192
  'sid' => submission_id,
189
193
  'created_at' => submitted_at || updated_at,
@@ -198,6 +202,7 @@ class Assignment < Progress
198
202
  'position' => navigable_parent.try(:number),
199
203
  'chapter' => guide.chapter.as_json(only: [:id], methods: [:name])
200
204
  }})
205
+ .merge({'randomized_values' => randomized_values.presence}.compact)
201
206
  end
202
207
 
203
208
  def tips
@@ -10,6 +10,6 @@ module Gamified
10
10
  end
11
11
 
12
12
  def net_experience
13
- submission_status.exp_given - top_submission_status.exp_given
13
+ submitter.currently_in_exam? ? 0 : (submission_status.exp_given - top_submission_status.exp_given)
14
14
  end
15
15
  end
@@ -9,7 +9,7 @@ class Organization::Status::InPreparation < Organization::Status::Base
9
9
  end
10
10
 
11
11
  def ex_student_access_mode(user)
12
- OrganizationAccessMode::Forbidden.new user, organization
12
+ OrganizationAccessMode::ReadOnly.new user, organization, :faqs, :profile
13
13
  end
14
14
 
15
15
  def outsider_access_mode(user)
@@ -4,7 +4,6 @@ module WithDiscussionStatus
4
4
  included do
5
5
  serialize :status, Mumuki::Domain::Status::Discussion
6
6
  validates_presence_of :status
7
- scope :by_status, -> (status) { where(status: status) }
8
7
  end
9
8
 
10
9
  delegate :closed?, :opened?, :solved?, :pending_review?, :reachable_statuses, to: :status
@@ -4,7 +4,7 @@ module WithMessages
4
4
  end
5
5
 
6
6
  def send_question!(question)
7
- message = build_message question.merge(sender: submitter.uid, read: true)
7
+ message = build_message question.merge(sender: submitter, read: true)
8
8
  message.save_and_notify!
9
9
  end
10
10
 
@@ -0,0 +1,14 @@
1
+ module WithScopedQueries::Limit
2
+ def self.query_by(params, current_scope, _)
3
+ if params[:limit].present?
4
+ max_limit = [params[:limit].to_i, 25].min
5
+ current_scope.limit(max_limit)
6
+ else
7
+ current_scope
8
+ end
9
+ end
10
+
11
+ def self.add_queriable_attributes_to(klass, _)
12
+ klass.queriable_attributes.merge!(limit: :limit)
13
+ end
14
+ end
@@ -1,10 +1,14 @@
1
1
  module WithScopedQueries::Page
2
2
  def self.query_by(params, current_scope, _)
3
- page_param = params[:page] || 1
4
- current_scope.page(page_param).per(10)
3
+ if params[:limit].present?
4
+ current_scope
5
+ else
6
+ page_param = params[:page] || 1
7
+ current_scope.page(page_param).per(10)
8
+ end
5
9
  end
6
10
 
7
11
  def self.add_queriable_attributes_to(klass, _)
8
- klass.queriable_attributes.merge!(page: :page)
12
+ klass.queriable_attributes.merge!(page: [:page, :limit])
9
13
  end
10
14
  end
@@ -1,7 +1,7 @@
1
1
  module WithScopedQueries
2
2
  extend ActiveSupport::Concern
3
3
 
4
- SCOPING_METHODS = [Filter, Sort, Page]
4
+ SCOPING_METHODS = [Filter, Sort, Page, Limit]
5
5
 
6
6
  included do
7
7
  class_attribute :queriable_attributes, instance_writer: false
@@ -22,14 +22,16 @@ class Discussion < ApplicationRecord
22
22
  .or(where(responsible_moderator_at: nil)) }
23
23
  scope :pending_review, -> { where(status: :pending_review) }
24
24
  scope :unread_first, -> { includes(:subscriptions).reorder('subscriptions.read', created_at: :desc) }
25
+ scope :by_recent, -> (_) { where('created_at > ?', Time.now - 6.months)}
25
26
 
26
27
  after_create :subscribe_initiator!
27
28
 
28
29
  markdown_on :description
29
30
 
30
31
  sortable :responses_count, :upvotes_count, :created_at, default: :created_at_desc
31
- filterable :status, :language, :requires_attention
32
+ filterable :status, :language, :requires_attention, :recent
32
33
  pageable
34
+ limitable
33
35
 
34
36
  delegate :language, to: :item
35
37
  delegate :to_discussion_status, to: :status
@@ -101,7 +103,7 @@ class Discussion < ApplicationRecord
101
103
  end
102
104
 
103
105
  def submit_message!(message, user)
104
- message.merge!(sender: user.uid)
106
+ message.merge!(sender: user)
105
107
  messages.create(message)
106
108
  user.subscribe_to! self
107
109
  mark_subscriptions_as_unread!(user)
@@ -140,7 +142,7 @@ class Discussion < ApplicationRecord
140
142
  end
141
143
 
142
144
  def responses_count
143
- visible_messages.where.not(sender: initiator.uid).count
145
+ visible_messages.where('sender <> ? OR sender_id <> ?', initiator.uid, initiator.id).count
144
146
  end
145
147
 
146
148
  def has_responses?
data/app/models/exam.rb CHANGED
@@ -96,6 +96,10 @@ class Exam < ApplicationRecord
96
96
  end
97
97
  end
98
98
 
99
+ def time_left(user)
100
+ (real_end_time(user) - Time.current).round
101
+ end
102
+
99
103
  def started_at(user)
100
104
  authorization_for(user).started_at
101
105
  end
@@ -4,18 +4,20 @@ class Message < ApplicationRecord
4
4
  belongs_to :discussion, optional: true
5
5
  belongs_to :assignment, optional: true
6
6
  belongs_to :approved_by, class_name: 'User', optional: true
7
+ belongs_to :sender, class_name: 'User'
7
8
 
8
9
  has_one :exercise, through: :assignment
9
10
 
10
- validates_presence_of :content, :sender
11
+ validates_presence_of :content
11
12
  validate :ensure_contextualized
12
13
 
14
+ before_create :mark_from_moderator!
13
15
  after_save :update_counters_cache!
14
16
 
15
17
  markdown_on :content
16
18
 
17
19
  # Visible messages are those that can be publicly seen
18
- # in forums. non-direct messages are never visible.
20
+ # in forums. Direct messages are never visible.
19
21
  scope :visible, -> () do
20
22
  where.not(deletion_motive: :self_deleted)
21
23
  .or(where(deletion_motive: nil))
@@ -49,19 +51,15 @@ class Message < ApplicationRecord
49
51
  end
50
52
 
51
53
  def from_initiator?
52
- sender_user == discussion&.initiator
54
+ sender == discussion&.initiator
53
55
  end
54
56
 
55
57
  def from_moderator?
56
- sender_user.moderator_here?
58
+ from_moderator || sender.moderator_here?
57
59
  end
58
60
 
59
61
  def from_user?(user)
60
- sender_user == user
61
- end
62
-
63
- def sender_user
64
- User.find_by(uid: sender)
62
+ sender == user
65
63
  end
66
64
 
67
65
  def authorized?(user)
@@ -74,9 +72,10 @@ class Message < ApplicationRecord
74
72
 
75
73
  def to_resource_h
76
74
  as_json(except: [:id, :type, :discussion_id, :approved, :approved_at, :approved_by_id,
77
- :not_actually_a_question, :deletion_motive, :deleted_at, :deleted_by_id],
75
+ :not_actually_a_question, :deletion_motive, :deleted_at, :deleted_by_id,
76
+ :from_moderator, :sender_id],
78
77
  include: {exercise: {only: [:bibliotheca_id]}})
79
- .merge(organization: Organization.current.name)
78
+ .merge(organization: Organization.current.name, sender: sender.uid)
80
79
  end
81
80
 
82
81
  def read!
@@ -103,6 +102,10 @@ class Message < ApplicationRecord
103
102
  approved? || from_moderator?
104
103
  end
105
104
 
105
+ def mark_from_moderator!
106
+ self.from_moderator = from_moderator?
107
+ end
108
+
106
109
  def update_counters_cache!
107
110
  discussion&.update_counters!
108
111
  end
@@ -122,7 +125,7 @@ class Message < ApplicationRecord
122
125
  def self.import_from_resource_h!(resource_h)
123
126
  if resource_h['submission_id'].present?
124
127
  assignment = Assignment.find_by(submission_id: resource_h['submission_id'])
125
- assignment&.receive_answer! sender: resource_h['message']['sender'],
128
+ assignment&.receive_answer! sender: User.locate!(resource_h['message']['sender']),
126
129
  content: resource_h['message']['content']
127
130
  end
128
131
  end
@@ -132,6 +135,10 @@ class Message < ApplicationRecord
132
135
  'message'
133
136
  end
134
137
 
138
+ def sender
139
+ super || User.locate!(self[:sender])
140
+ end
141
+
135
142
  private
136
143
 
137
144
  def approve!(user)
@@ -17,7 +17,7 @@ class OrganizationAccessMode::ReadOnly < OrganizationAccessMode::Base
17
17
  end
18
18
 
19
19
  def validate_discuss_here!(discussion)
20
- super(discussion) unless discussion&.initiator == user
20
+ super(discussion) unless discuss_here? && discussion&.initiator == user
21
21
  end
22
22
 
23
23
  def show_content?(content)
data/app/models/user.rb CHANGED
@@ -19,7 +19,7 @@ class User < ApplicationRecord
19
19
  has_many :assignments, foreign_key: :submitter_id
20
20
  has_many :indicators
21
21
  has_many :user_stats, class_name: 'UserStats'
22
- has_many :messages, -> { order(created_at: :desc) }, through: :assignments
22
+ has_many :direct_messages, -> { order(created_at: :desc) }, class_name: 'Message', source: :messages, through: :assignments
23
23
 
24
24
  has_many :submitted_exercises, through: :assignments, class_name: 'Exercise', source: :exercise
25
25
 
@@ -61,7 +61,7 @@ class User < ApplicationRecord
61
61
  end
62
62
 
63
63
  def messages_in_organization(organization = Organization.current)
64
- messages.where('assignments.organization': organization)
64
+ direct_messages.where('assignments.organization': organization)
65
65
  end
66
66
 
67
67
  def passed_submissions_count_in(organization)
@@ -141,9 +141,14 @@ class User < ApplicationRecord
141
141
  super.merge(image_url: profile_picture)
142
142
  end
143
143
 
144
- def verify_name!
145
- self.verified_first_name ||= first_name
146
- self.verified_last_name ||= last_name
144
+ def verify_name!(force: false)
145
+ if force
146
+ self.verified_first_name = first_name
147
+ self.verified_last_name = last_name
148
+ else
149
+ self.verified_first_name ||= first_name
150
+ self.verified_last_name ||= last_name
151
+ end
147
152
  save!
148
153
  end
149
154
 
@@ -157,8 +162,8 @@ class User < ApplicationRecord
157
162
  save_and_notify!
158
163
  end
159
164
 
160
- def detach!(role, course)
161
- make_ex_student_of! course.slug if student_of?(course.slug) && solved_any_exercises?(course.organization)
165
+ def detach!(role, course, deep: false)
166
+ make_ex_student_of! course.slug if !deep && student_of?(course.slug) && solved_any_exercises?(course.organization)
162
167
  remove_permission! role, course.slug
163
168
  save_and_notify!
164
169
  end
@@ -321,7 +326,7 @@ class User < ApplicationRecord
321
326
 
322
327
  target_assignments = assignments.where(location)
323
328
 
324
- messages.where(assignment: target_assignments).delete_all
329
+ direct_messages.where(assignment: target_assignments).delete_all
325
330
 
326
331
  target_assignments.delete_all
327
332
  indicators.where(location).delete_all
@@ -336,6 +341,10 @@ class User < ApplicationRecord
336
341
  ignored_notifications.include? notification.subject
337
342
  end
338
343
 
344
+ def forum_messages
345
+ Message.where(sender: self).or(Message.where('sender = ?', uid)).where.not(discussion_id: nil)
346
+ end
347
+
339
348
  private
340
349
 
341
350
  def welcome_to_new_organizations!
@@ -32,8 +32,8 @@ class UserStats < ApplicationRecord
32
32
 
33
33
  def messages_in_discussions_count(date_range = nil)
34
34
  date_filter = { created_at: date_range }.compact
35
- result = Message.joins(:discussion)
36
- .where({sender: user.uid, deletion_motive: nil, discussions: { organization: organization }}.merge(date_filter))
35
+ result = user.forum_messages.joins(:discussion)
36
+ .where({deletion_motive: nil, discussions: { organization: organization }}.merge(date_filter))
37
37
  .group(:approved)
38
38
  .count
39
39
  unapproved = result[false] || 0
@@ -0,0 +1,5 @@
1
+ class ReferenceSenderViaIdInMessages < ActiveRecord::Migration[5.1]
2
+ def change
3
+ add_reference :messages, :sender, index: true
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class AddFromModeratorToMessages < ActiveRecord::Migration[5.1]
2
+ def change
3
+ add_column :messages, :from_moderator, :boolean
4
+ end
5
+ end
@@ -1,6 +1,6 @@
1
1
  FactoryBot.define do
2
-
3
2
  factory :message do
4
3
  content { Faker::Lorem.sentence(word_count: 3) }
4
+ sender { create(:user) }
5
5
  end
6
6
  end
@@ -33,8 +33,4 @@ module Mumuki::Domain::Status::Discussion
33
33
  reachable_statuses_for_initiator(discussion)
34
34
  end
35
35
  end
36
-
37
- def as_json(_options={})
38
- to_s
39
- end
40
36
  end
@@ -31,10 +31,6 @@ module Mumuki::Domain::Status::Submission
31
31
  group.iconize
32
32
  end
33
33
 
34
- def as_json(_options={})
35
- to_s
36
- end
37
-
38
34
  def completed?
39
35
  solved?
40
36
  end
@@ -50,8 +46,4 @@ module Mumuki::Domain::Status::Submission
50
46
  def exp_given
51
47
  0
52
48
  end
53
-
54
- def dup
55
- self
56
- end
57
49
  end
@@ -25,6 +25,14 @@ module Mumuki::Domain::Status
25
25
  self.equal? parent.to_mumuki_status(other) rescue false
26
26
  end
27
27
 
28
+ def as_json(_options={})
29
+ to_s
30
+ end
31
+
32
+ def dup
33
+ self
34
+ end
35
+
28
36
  class_methods do
29
37
  def load(i)
30
38
  cast(i)
@@ -1,5 +1,5 @@
1
1
  module Mumuki
2
2
  module Domain
3
- VERSION = '9.18.0'
3
+ VERSION = '9.22.0'
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.18.0
4
+ version: 9.22.0
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-09-30 00:00:00.000000000 Z
11
+ date: 2021-11-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -306,6 +306,7 @@ files:
306
306
  - app/models/concerns/with_responsible_moderator.rb
307
307
  - app/models/concerns/with_scoped_queries.rb
308
308
  - app/models/concerns/with_scoped_queries/filter.rb
309
+ - app/models/concerns/with_scoped_queries/limit.rb
309
310
  - app/models/concerns/with_scoped_queries/page.rb
310
311
  - app/models/concerns/with_scoped_queries/sort.rb
311
312
  - app/models/concerns/with_slug.rb
@@ -687,6 +688,8 @@ files:
687
688
  - db/migrate/20210719145706_add_new_fields_to_notifications.rb
688
689
  - db/migrate/20210803175124_add_ignored_notifications_to_users.rb
689
690
  - db/migrate/20210929223144_add_authorization_requests_limit_to_exam_registration.rb
691
+ - db/migrate/20211004062332_reference_sender_via_id_in_messages.rb
692
+ - db/migrate/20211020224011_add_from_moderator_to_messages.rb
690
693
  - lib/mumuki/domain.rb
691
694
  - lib/mumuki/domain/area.rb
692
695
  - lib/mumuki/domain/engine.rb