2019-05-08 15:24:11 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-23 21:19:08 +08:00
|
|
|
class Result < ApplicationRecord
|
2019-02-26 18:01:15 +08:00
|
|
|
include ArchivableModel
|
|
|
|
include SearchableModel
|
|
|
|
include SearchableByNameModel
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2016-09-21 21:35:23 +08:00
|
|
|
auto_strip_attributes :name, nullify: false
|
2016-02-12 23:52:43 +08:00
|
|
|
validates :user, :my_module, presence: true
|
2016-10-05 23:45:20 +08:00
|
|
|
validates :name, length: { maximum: Constants::NAME_MAX_LENGTH }
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2019-06-10 18:19:10 +08:00
|
|
|
belongs_to :user, inverse_of: :results
|
2019-05-08 15:24:11 +08:00
|
|
|
belongs_to :last_modified_by, foreign_key: 'last_modified_by_id', class_name: 'User', optional: true
|
|
|
|
belongs_to :archived_by, foreign_key: 'archived_by_id', class_name: 'User', optional: true
|
|
|
|
belongs_to :restored_by, foreign_key: 'restored_by_id', class_name: 'User', optional: true
|
|
|
|
belongs_to :my_module, inverse_of: :results
|
2018-11-14 18:57:53 +08:00
|
|
|
has_one :result_asset, inverse_of: :result, dependent: :destroy
|
2016-02-12 23:52:43 +08:00
|
|
|
has_one :asset, through: :result_asset
|
2018-11-14 18:57:53 +08:00
|
|
|
has_one :result_table, inverse_of: :result, dependent: :destroy
|
2016-02-12 23:52:43 +08:00
|
|
|
has_one :table, through: :result_table
|
2018-11-14 18:57:53 +08:00
|
|
|
has_one :result_text, inverse_of: :result, dependent: :destroy
|
2017-03-08 20:18:20 +08:00
|
|
|
has_many :result_comments, foreign_key: :associated_id, dependent: :destroy
|
2016-02-12 23:52:43 +08:00
|
|
|
has_many :report_elements, inverse_of: :result, dependent: :destroy
|
|
|
|
|
|
|
|
accepts_nested_attributes_for :result_text
|
|
|
|
accepts_nested_attributes_for :asset
|
|
|
|
accepts_nested_attributes_for :table
|
|
|
|
|
2017-05-05 22:41:23 +08:00
|
|
|
def self.search(user,
|
|
|
|
include_archived,
|
|
|
|
query = nil,
|
|
|
|
page = 1,
|
|
|
|
_current_team = nil,
|
|
|
|
options = {})
|
2016-02-12 23:52:43 +08:00
|
|
|
module_ids =
|
|
|
|
MyModule
|
2016-10-05 23:45:20 +08:00
|
|
|
.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
|
|
|
|
2017-05-05 22:41:23 +08:00
|
|
|
new_query =
|
|
|
|
Result
|
2016-07-21 19:11:15 +08:00
|
|
|
.distinct
|
2017-05-05 22:41:23 +08:00
|
|
|
.joins('LEFT JOIN result_texts ON results.id = result_texts.result_id')
|
|
|
|
.where('results.my_module_id IN (?)', module_ids)
|
|
|
|
.where_attributes_like(['results.name', 'result_texts.text'],
|
|
|
|
query, options)
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2019-08-21 17:15:57 +08:00
|
|
|
new_query = new_query.where('results.archived = ?', false) unless include_archived
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
# Show all results if needed
|
2016-10-05 23:45:20 +08:00
|
|
|
if page == Constants::SEARCH_NO_LIMIT
|
2016-02-12 23:52:43 +08:00
|
|
|
new_query
|
|
|
|
else
|
|
|
|
new_query
|
2016-10-05 23:45:20 +08:00
|
|
|
.limit(Constants::SEARCH_LIMIT)
|
|
|
|
.offset((page - 1) * Constants::SEARCH_LIMIT)
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-26 18:01:15 +08:00
|
|
|
def self.viewable_by_user(user, teams)
|
|
|
|
where(my_module: MyModule.viewable_by_user(user, teams))
|
|
|
|
end
|
|
|
|
|
2019-03-28 20:43:50 +08:00
|
|
|
def navigable?
|
|
|
|
!my_module.archived? && my_module.navigable?
|
|
|
|
end
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
def space_taken
|
|
|
|
is_asset ? result_asset.space_taken : 0
|
|
|
|
end
|
|
|
|
|
2016-10-05 23:45:20 +08:00
|
|
|
def last_comments(last_id = 1, per_page = Constants::COMMENTS_SEARCH_LIMIT)
|
2016-10-13 17:05:11 +08:00
|
|
|
last_id = Constants::INFINITY if last_id <= 1
|
2017-03-08 20:18:20 +08:00
|
|
|
comments = ResultComment.joins(:result)
|
|
|
|
.where(results: { id: id })
|
|
|
|
.where('comments.id < ?', last_id)
|
|
|
|
.order(created_at: :desc)
|
|
|
|
.limit(per_page)
|
2016-09-20 23:06:07 +08:00
|
|
|
comments.reverse
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def is_text
|
2019-08-21 17:15:57 +08:00
|
|
|
result_text.present?
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def is_table
|
2019-08-21 17:15:57 +08:00
|
|
|
table.present?
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def is_asset
|
2019-08-21 17:15:57 +08:00
|
|
|
asset.present?
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
2018-02-19 21:47:36 +08:00
|
|
|
def unlocked?(result)
|
|
|
|
if result.is_asset
|
|
|
|
!result.asset.locked?
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
2019-08-21 17:15:57 +08:00
|
|
|
|
|
|
|
def editable?
|
|
|
|
return false if is_asset && asset.file.metadata['asset_type'] == 'marvinjs'
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|