2017-06-23 21:19:08 +08:00
|
|
|
class RepositoryRow < ApplicationRecord
|
2017-06-23 22:06:11 +08:00
|
|
|
include SearchableModel
|
|
|
|
|
2017-06-28 21:21:32 +08:00
|
|
|
belongs_to :repository, optional: true
|
|
|
|
belongs_to :created_by,
|
|
|
|
foreign_key: :created_by_id,
|
|
|
|
class_name: 'User',
|
|
|
|
optional: true
|
|
|
|
belongs_to :last_modified_by,
|
|
|
|
foreign_key: :last_modified_by_id,
|
|
|
|
class_name: 'User',
|
|
|
|
optional: true
|
2017-05-16 21:27:36 +08:00
|
|
|
has_many :repository_cells, dependent: :destroy
|
|
|
|
has_many :repository_columns, through: :repository_cells
|
2017-06-07 19:36:39 +08:00
|
|
|
has_many :my_module_repository_rows,
|
2017-06-06 23:35:29 +08:00
|
|
|
inverse_of: :repository_row, dependent: :destroy
|
2017-06-07 19:36:39 +08:00
|
|
|
has_many :my_modules, through: :my_module_repository_rows
|
2017-05-16 21:27:36 +08:00
|
|
|
|
|
|
|
auto_strip_attributes :name, nullify: false
|
|
|
|
validates :name,
|
|
|
|
presence: true,
|
|
|
|
length: { maximum: Constants::NAME_MAX_LENGTH }
|
|
|
|
validates :created_by, presence: true
|
2017-06-23 22:06:11 +08:00
|
|
|
|
|
|
|
def self.search(repository, query, page = 1, options)
|
|
|
|
new_query = distinct
|
|
|
|
.joins(:created_by)
|
|
|
|
.joins(
|
|
|
|
"LEFT OUTER JOIN (
|
|
|
|
SELECT repository_cells.repository_row_id,
|
|
|
|
repository_text_values.data AS text_value,
|
|
|
|
to_char(repository_date_values.data, 'DD.MM.YYYY HH24:MI')
|
|
|
|
AS date_value
|
|
|
|
FROM repository_cells
|
|
|
|
INNER JOIN repository_text_values
|
|
|
|
ON repository_text_values.id = repository_cells.value_id
|
|
|
|
FULL OUTER JOIN repository_date_values
|
|
|
|
ON repository_date_values.id = repository_cells.value_id
|
|
|
|
) AS values
|
|
|
|
ON values.repository_row_id = repository_rows.id"
|
|
|
|
)
|
|
|
|
.where_attributes_like(
|
|
|
|
['repository_rows.name', 'users.full_name',
|
|
|
|
'values.text_value', 'values.date_value'],
|
|
|
|
query, options
|
|
|
|
)
|
|
|
|
|
|
|
|
if repository
|
|
|
|
new_query = new_query
|
|
|
|
.preload(
|
|
|
|
:repository_columns,
|
|
|
|
:created_by,
|
|
|
|
repository_cells: :value
|
|
|
|
)
|
|
|
|
.where(repository: repository)
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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
|
|
|
|
end
|
2017-05-16 21:27:36 +08:00
|
|
|
end
|