scinote-web/app/models/comment.rb

64 lines
1.9 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
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)
2017-04-11 20:55:44 +08:00
.pluck(:id)
2016-02-12 23:52:43 +08:00
my_module_ids =
MyModule
.search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT)
2017-04-11 20:55:44 +08:00
.pluck(:id)
2016-02-12 23:52:43 +08:00
step_ids =
Step
.search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT)
2017-04-11 20:55:44 +08:00
.pluck(:id)
2016-02-12 23:52:43 +08:00
result_ids =
Result
.search(user, include_archived, nil, Constants::SEARCH_NO_LIMIT)
2017-04-11 20:55:44 +08:00
.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'
)
2017-05-05 22:41:23 +08:00
.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)
end
2016-02-12 23:52:43 +08:00
end
end