scinote-web/app/models/comment.rb

65 lines
2.5 KiB
Ruby
Raw Normal View History

2017-06-23 21:19:08 +08:00
class Comment < ApplicationRecord
2016-02-12 23:52:43 +08:00
include SearchableModel
auto_strip_attributes :message, nullify: false
validates :message,
presence: true,
length: { maximum: Constants::TEXT_MAX_LENGTH }
2016-02-12 23:52:43 +08:00
validates :user, presence: true
2019-05-09 23:10:02 +08:00
belongs_to :user, inverse_of: :comments
belongs_to :last_modified_by, foreign_key: 'last_modified_by_id', class_name: 'User', optional: true
2016-02-12 23:52:43 +08:00
2023-05-05 21:47:53 +08:00
scope :unseen_by, ->(user) { where('? = ANY (unseen_by)', user.id) }
2016-02-12 23:52:43 +08:00
def self.search(
user,
include_archived,
query = nil,
2017-05-05 22:41:23 +08:00
page = 1,
_current_team = nil,
options = {}
2016-02-12 23:52:43 +08:00
)
project_ids = Project.search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT)
.pluck(:id)
my_module_ids = MyModule.search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT)
.pluck(:id)
step_ids = Step.search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT)
.pluck(:id)
result_ids = Result.search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT)
.pluck(:id)
2016-07-21 19:11:15 +08:00
new_query = Comment.distinct
.joins(:user)
.where(
'(comments.associated_id IN (?) AND comments.type = ?) OR ' \
'(comments.associated_id IN (?) AND comments.type = ?) OR ' \
'(comments.associated_id IN (?) AND comments.type = ?) OR ' \
'(comments.associated_id IN (?) AND comments.type = ?)',
project_ids, 'ProjectComment',
my_module_ids, 'TaskComment',
step_ids, 'StepComment',
result_ids, 'ResultComment'
)
.where_attributes_like(['message', 'users.full_name'], query, options)
2016-02-12 23:52:43 +08:00
2016-10-07 00:36:55 +08:00
# Show all results if needed
if page == Constants::SEARCH_NO_LIMIT
new_query
else
new_query.limit(Constants::SEARCH_LIMIT).offset((page - 1) * Constants::SEARCH_LIMIT)
2016-10-07 00:36:55 +08:00
end
2016-02-12 23:52:43 +08:00
end
def self.mark_as_seen_by(user, commentable)
# rubocop:disable Rails/SkipsModelValidations
all.where('? = ANY (unseen_by)', user.id).update_all("unseen_by = array_remove(unseen_by, #{user.id.to_i}::bigint)")
# Because we want the number of unseen comments to affect the cache of project
# and experiment lists, we need to set the updated_at of Project or Experiment.
commentable.touch if commentable.class.in? [Project, Experiment]
# rubocop:enable Rails/SkipsModelValidations
end
2016-02-12 23:52:43 +08:00
end