Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature order discussions by similarity #224

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion app/models/application_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def self.organic_on(*selectors)
def self.active_between(start_date_field, end_date_field, **options)
define_singleton_method(:active) do |actually_filter=true|
if actually_filter
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)
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.current)
else
all
end
Expand Down
4 changes: 2 additions & 2 deletions app/models/assignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def visible_status
def persist_submission!(submission)
transaction do
update! submission_id: submission.id
update! submitted_at: DateTime.current
update! submitted_at: Time.current
update_submissions_count!
update_last_submission!
end
Expand Down Expand Up @@ -281,6 +281,6 @@ def update_submissions_count!
end

def update_last_submission!
submitter.update!(last_submission_date: DateTime.current, last_exercise: exercise)
submitter.update!(last_submission_date: Time.current, last_exercise: exercise)
end
end
2 changes: 1 addition & 1 deletion app/models/concerns/with_messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def send_question!(question)
end

def build_message(body)
messages.build({date: DateTime.current, submission_id: submission_id}.merge(body))
messages.build({date: Time.current, submission_id: submission_id}.merge(body))
end

def has_messages?
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/with_reminders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def build_reminder

def remind!
build_reminder.deliver_now
update! last_reminded_date: Time.now
update! last_reminded_date: Time.current
end

def should_remind?
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/with_responsible_moderator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def can_toggle_responsible?(user)
private

def responsible!(moderator)
update! responsible_moderator_at: Time.now, responsible_moderator_by: moderator
update! responsible_moderator_at: Time.current, responsible_moderator_by: moderator
end

def no_responsible!
Expand Down
6 changes: 3 additions & 3 deletions app/models/concerns/with_scoped_queries/sort.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def self.add_queriable_attributes_to(klass, attributes)

def self.sort_method_for(klass, scope, field, direction)
if klass.column_names.include? field
scope.public_send(:reorder, "#{klass.table_name}.#{field} #{direction}")
scope.reorder "#{klass.table_name}.#{field} #{direction}"
else
scope.public_send("order_by_#{field}", direction)
end
Expand All @@ -39,8 +39,8 @@ def opposite(direction)
[:asc, :desc].find { |it| it != dir }
end

def sorting_filters
sorting_fields.product([:asc, :desc]).map do |it|
def sorting_filters(except: [])
(sorting_fields - except).product([:asc, :desc]).map do |it|
"#{it.first}_#{it.second}"
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/with_soft_deletion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module WithSoftDeletion
end

def soft_delete!(motive, deleter)
update! deletion_motive: motive, deleted_by: deleter, deleted_at: Time.now
update! deletion_motive: motive, deleted_by: deleter, deleted_at: Time.current
end

def deleted?
Expand Down
3 changes: 1 addition & 2 deletions app/models/concerns/with_terms_acceptance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ def term_acceptance_field_for(role)
end

def accept_terms!(terms)
update! unaccepted_terms_scopes_in(terms).to_h { |scope| [term_acceptance_field_for(scope), Time.now] }
update! unaccepted_terms_scopes_in(terms).to_h { |scope| [term_acceptance_field_for(scope), Time.current] }
end

end

2 changes: 1 addition & 1 deletion app/models/concerns/with_timed_enablement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module WithTimedEnablement
extend ActiveSupport::Concern

def enabled?
enabled_range.cover? DateTime.current
enabled_range.cover? Time.current
end

def enabled_range
Expand Down
4 changes: 4 additions & 0 deletions app/models/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ def fork_to!(organization, syncer, quiet: false)
def public?
!private?
end

def contextualize_for(_scope, _user, _organization)
scope
end
end
4 changes: 2 additions & 2 deletions app/models/course.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Course < ApplicationRecord
resource_fields :slug, :shifts, :code, :days, :period, :description, :period_start, :period_end

def current_invitation
invitations.where('expiration_date > ?', Time.now).first
invitations.where('expiration_date > ?', Time.current).first
end

def import_from_resource_h!(resource_h)
Expand Down Expand Up @@ -49,7 +49,7 @@ def infer_period_range!
period =~ /^(\d{4})?/
year = $1.to_i

return nil unless year.between? 2014, (DateTime.now.year + 1)
return nil unless year.between? 2014, (Time.current.year + 1)

self.period_start = DateTime.new(year).beginning_of_year
self.period_end = DateTime.new(year).end_of_year
Expand Down
60 changes: 48 additions & 12 deletions app/models/discussion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,13 @@ class Discussion < ApplicationRecord

markdown_on :description

sortable :responses_count, :upvotes_count, :created_at, default: :created_at_desc
sortable :responses_count, :upvotes_count, :created_at, :similarity, default: :created_at_desc
filterable :status, :language, :requires_attention
pageable

delegate :language, to: :item
delegate :to_discussion_status, to: :status

scope :for_user, -> (user) do
if user.try(:moderator_here?)
all
else
where.not(status: :closed).where.not(status: :pending_review).or(where(initiator: user))
end
end

def visible_messages
messages.visible
end
Expand All @@ -54,7 +46,7 @@ def try_solve!
def navigable_content_in(_)
nil
end

def target
self
end
Expand Down Expand Up @@ -128,7 +120,7 @@ def update_status!(status, user)
if reachable_status_for?(user, status)
update! status: status,
status_updated_by: user,
status_updated_at: Time.now
status_updated_at: Time.current

no_responsible! if responsible?(user)
end
Expand Down Expand Up @@ -165,7 +157,11 @@ def subscribe_initiator!
def extra_preview_html
# FIXME this is buggy, because the extra
# may have changed since the submission of this discussion
exercise.assignment_for(initiator).extra_preview_html
assignment.extra_preview_html
end

def assignment
exercise.find_assignment_for(initiator, organization)
end

def update_counters!
Expand All @@ -182,6 +178,46 @@ def self.debatable_for(klazz, params)
klazz.constantize.find(debatable_id)
end

def self.in_context_of(content, user, organization = Organization.current)
organization_discussions = content.discussions_in_organization(organization)
if user&.moderator_of? organization
organization_discussions
else
content.contextualize_for(
organization_discussions
.where.not(status: :closed)
.where.not(status: :pending_review)
.or(where(initiator: user)),
user,
organization)
end
end

def self.contextual_sorting_filters
Discussion.sorting_filters(except: [:similarity])
end

def self.in_context_of_assignment(assignment)
relation = all
relation.instance_variable_set(:@assignment, assignment)

class << relation
def contextual_sorting_filters
Discussion.sorting_filters
end

def order_by_similarity(direction)
order(
"submission_status = #{@assignment.status.to_i} #{direction}",
"test_results = #{ActiveRecord::Base.connection.quote(@assignment.test_results_before_type_cast)} #{direction}",
"expectation_results = #{ActiveRecord::Base.connection.quote(@assignment.expectation_results_before_type_cast)} #{direction}"
)
end
end

relation
end

private

def messages_by_updated_at(direction = :desc)
Expand Down
6 changes: 3 additions & 3 deletions app/models/exam.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def used_in?(organization)
end

def enabled_for?(user)
enabled_range_for(user).cover? DateTime.current
enabled_range_for(user).cover? Time.current
end

def in_progress_for?(user)
Expand Down Expand Up @@ -179,8 +179,8 @@ def self.adapt_json_values(exam)
exam[:organization_id] = Organization.current.id
exam[:course_id] = Course.locate!(exam[:course].to_s).id
exam[:users] = User.where(uid: exam[:uids])
exam[:start_time] = exam[:start_time].to_time
exam[:end_time] = exam[:end_time].to_time
exam[:start_time] = exam[:start_time].in_time_zone
exam[:end_time] = exam[:end_time].in_time_zone
exam[:classroom_id] = exam[:eid] if exam[:eid].present?
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/exam_authorization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class ExamAuthorization < ApplicationRecord
belongs_to :exam

def start!
update!(started: true, started_at: Time.now) unless started?
update!(started: true, started_at: Time.current) unless started?
end

end
2 changes: 1 addition & 1 deletion app/models/exam_registration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ExamRegistration < ApplicationRecord

alias_attribute :name, :description

scope :should_process, -> { where(processed: false).where(arel_table[:end_time].lt(Time.now)) }
scope :should_process, -> { where(processed: false).where(arel_table[:end_time].lt(Time.current)) }

def authorization_criterion
@authorization_criterion ||= ExamRegistration::AuthorizationCriterion.parse(authorization_criterion_type, authorization_criterion_value)
Expand Down
5 changes: 5 additions & 0 deletions app/models/exercise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ def solvable?
is_a? ::Problem
end

def contextualize_for(scope, user, organization)
assignment = find_assignment_for(user, organization)
assignment ? scope.in_context_of_assignment(assignment) : scope
end

private

def evaluation_class
Expand Down
2 changes: 1 addition & 1 deletion app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def self.import_from_resource_h!(resource_h)
private

def approve!(user)
update! approved: true, approved_at: Time.now, approved_by: user
update! approved: true, approved_at: Time.current, approved_by: user
end

def disapprove!
Expand Down
2 changes: 1 addition & 1 deletion app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Organization < ApplicationRecord

has_many :certificate_programs

validates_presence_of :contact_email, :locale
validates_presence_of :contact_email, :locale, :time_zone
validates_presence_of :welcome_email_template, if: :greet_new_users?
validates :name, uniqueness: true,
presence: true,
Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def placeholder_image_url

def age
if birthdate.present?
@age ||= Time.now.round_years_since(birthdate.to_time)
@age ||= Time.current.round_years_since(birthdate.in_time_zone)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/mumuki/domain/organization/profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def open_graph_image_url
end

def time_zone
@time_zone || 'Buenos Aires'
@time_zone
end

end
4 changes: 2 additions & 2 deletions lib/mumuki/domain/organization/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ def forum_discussions_minimal_role
end

def disabled_from=(disabled_from)
@disabled_from = disabled_from&.to_time
@disabled_from = disabled_from&.in_time_zone
end

def in_preparation_until=(in_preparation_until)
@in_preparation_until = in_preparation_until&.to_time
@in_preparation_until = in_preparation_until&.in_time_zone
end

def disabled?
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/organization_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@
it { expect(subject.logo_url).to eq 'https://mumuki.io/logo-alt-large.png' }
it { expect(subject.banner_url).to eq 'https://mumuki.io/logo-alt-large.png' }
it { expect(subject.favicon_url).to eq '/favicon.ico' }
it { expect(subject.breadcrumb_image_url).to eq nil }
it { expect(subject.breadcrumb_image_url).to be nil }
it { expect(subject.open_graph_image_url).to eq 'http://localmumuki.io/logo-alt.png' }
it { expect(subject.time_zone).to eq 'Buenos Aires' }
it { expect(subject.time_zone).to be nil }
end

describe Mumuki::Domain::Organization::Profile do
Expand Down
Loading