Make repositories more extendable [SCI-3528]

This commit is contained in:
Oleksii Kriuchykhin 2019-06-06 17:28:59 +02:00
parent d553fee2b3
commit 8920d38a02
8 changed files with 54 additions and 77 deletions

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class RepositoryAssetValue < ApplicationRecord class RepositoryAssetValue < ApplicationRecord
belongs_to :created_by, belongs_to :created_by,
foreign_key: :created_by_id, foreign_key: :created_by_id,
@ -15,6 +17,9 @@ class RepositoryAssetValue < ApplicationRecord
validates :asset, :repository_cell, presence: true validates :asset, :repository_cell, presence: true
SORTABLE_COLUMN_NAME = 'assets.file_file_name'
SORTABLE_VALUE_INCLUDE = { repository_asset_value: :asset }.freeze
def formatted def formatted
asset.file_file_name asset.file_file_name
end end

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class RepositoryDateValue < ApplicationRecord class RepositoryDateValue < ApplicationRecord
belongs_to :created_by, belongs_to :created_by,
foreign_key: :created_by_id, foreign_key: :created_by_id,
@ -14,6 +16,9 @@ class RepositoryDateValue < ApplicationRecord
validates :data, validates :data,
presence: true presence: true
SORTABLE_COLUMN_NAME = 'repository_date_values.data'
SORTABLE_VALUE_INCLUDE = :repository_date_value
def formatted def formatted
data data
end end

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class RepositoryListValue < ApplicationRecord class RepositoryListValue < ApplicationRecord
belongs_to :repository_list_item belongs_to :repository_list_item
belongs_to :created_by, belongs_to :created_by,
@ -17,6 +19,9 @@ class RepositoryListValue < ApplicationRecord
.repository_list_items .repository_list_items
end) end)
SORTABLE_COLUMN_NAME = 'repository_list_items.data'
SORTABLE_VALUE_INCLUDE = { repository_list_value: :repository_list_item }.freeze
def formatted def formatted
data.to_s data.to_s
end end

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class RepositoryTextValue < ApplicationRecord class RepositoryTextValue < ApplicationRecord
belongs_to :created_by, belongs_to :created_by,
foreign_key: :created_by_id, foreign_key: :created_by_id,
@ -15,6 +17,9 @@ class RepositoryTextValue < ApplicationRecord
presence: true, presence: true,
length: { maximum: Constants::TEXT_MAX_LENGTH } length: { maximum: Constants::TEXT_MAX_LENGTH }
SORTABLE_COLUMN_NAME = 'repository_text_values.data'
SORTABLE_VALUE_INCLUDE = :repository_text_value
def formatted def formatted
data data
end end

View file

@ -10,7 +10,7 @@ module RepositoryActions
end end
def call def call
self.send("duplicate_#{@cell.value_type.underscore}") __send__("duplicate_#{@cell.value_type.split('::').last.underscore}")
end end
private private

View file

@ -119,9 +119,18 @@ class RepositoryDatatableService
end end
elsif sortable_columns[column_id - 1] == 'repository_cell.value' elsif sortable_columns[column_id - 1] == 'repository_cell.value'
id = @mappings.key(column_id.to_s) id = @mappings.key(column_id.to_s)
type = RepositoryColumn.find_by_id(id) sorting_column = RepositoryColumn.find_by_id(id)
return records unless type return records unless sorting_column
return select_type(type.data_type, records, id, dir)
sorting_data_type = sorting_column.data_type.constantize
cells = RepositoryCell.joins(sorting_data_type::SORTABLE_VALUE_INCLUDE)
.where('repository_cells.repository_column_id': sorting_column.id)
.select("repository_cells.repository_row_id,
#{sorting_data_type::SORTABLE_COLUMN_NAME} AS value")
records.joins("LEFT OUTER JOIN (#{cells.to_sql}) AS values ON values.repository_row_id = repository_rows.id")
.order("values.value #{dir}")
elsif sortable_columns[column_id - 1] == 'users.full_name' elsif sortable_columns[column_id - 1] == 'users.full_name'
# We don't need join user table, because it already joined in fetch_row method # We don't need join user table, because it already joined in fetch_row method
return records.order("users.full_name #{dir}") return records.order("users.full_name #{dir}")
@ -151,61 +160,7 @@ class RepositoryDatatableService
records.order(order_by_index) records.order(order_by_index)
end end
def select_type(type, records, id, dir)
case type
when 'RepositoryTextValue'
filter_by_text_value(records, id, dir)
when 'RepositoryListValue'
filter_by_list_value(records, id, dir)
when 'RepositoryAssetValue'
filter_by_asset_value(records, id, dir)
else
records
end
end
def sort_null_direction(val) def sort_null_direction(val)
val == 'ASC' ? 'LAST' : 'FIRST' val == 'ASC' ? 'LAST' : 'FIRST'
end end
def filter_by_asset_value(records, id, dir)
records.joins(
"LEFT OUTER JOIN (SELECT repository_cells.repository_row_id,
assets.file_file_name AS value
FROM repository_cells
INNER JOIN repository_asset_values
ON repository_asset_values.id = repository_cells.value_id
INNER JOIN assets
ON repository_asset_values.asset_id = assets.id
WHERE repository_cells.repository_column_id = #{id}) AS values
ON values.repository_row_id = repository_rows.id"
).order("values.value #{dir}")
end
def filter_by_text_value(records, id, dir)
records.joins(
"LEFT OUTER JOIN (SELECT repository_cells.repository_row_id,
repository_text_values.data AS value
FROM repository_cells
INNER JOIN repository_text_values
ON repository_text_values.id = repository_cells.value_id
WHERE repository_cells.repository_column_id = #{id}) AS values
ON values.repository_row_id = repository_rows.id"
).order("values.value #{dir}")
end
def filter_by_list_value(records, id, dir)
records.joins(
"LEFT OUTER JOIN (SELECT repository_cells.repository_row_id,
repository_list_items.data AS value
FROM repository_cells
INNER JOIN repository_list_values
ON repository_list_values.id = repository_cells.value_id
INNER JOIN repository_list_items
ON repository_list_values.repository_list_item_id =
repository_list_items.id
WHERE repository_cells.repository_column_id = #{id}) AS values
ON values.repository_row_id = repository_rows.id"
).order("values.value #{dir}")
end
end end

View file

@ -14,7 +14,7 @@ module RepositoryImportParser
def get_value(value, record_row) def get_value(value, record_row)
return unless @column return unless @column
send("new_#{@column.data_type.underscore}", value, record_row) __send__("new_#{@column.data_type.split('::').last.underscore}", value, record_row)
end end
private private

View file

@ -25,24 +25,26 @@
<%= f.text_field :name %> <%= f.text_field :name %>
<%= f.form_group :data_type, label: { text: t('libraries.manange_modal_column.colum_type') } do %> <%= f.form_group :data_type, label: { text: t('libraries.manange_modal_column.colum_type') } do %>
<br /> <br />
<%= f.radio_button :data_type, <span id="repository-column-types-list">
'RepositoryTextValue'.freeze, <%= f.radio_button :data_type,
label: t('libraries.manange_modal_column.labels.text'), 'RepositoryTextValue'.freeze,
inline: true, label: t('libraries.manange_modal_column.labels.text'),
checked: checked?(@repository_column, 'RepositoryTextValue'.freeze), inline: true,
disabled: disabled?(@repository_column, 'RepositoryTextValue'.freeze) %> checked: checked?(@repository_column, 'RepositoryTextValue'.freeze),
<%= f.radio_button :data_type, disabled: disabled?(@repository_column, 'RepositoryTextValue'.freeze) %>
'RepositoryAssetValue'.freeze, <%= f.radio_button :data_type,
label: t('libraries.manange_modal_column.labels.file'), 'RepositoryAssetValue'.freeze,
inline: true, label: t('libraries.manange_modal_column.labels.file'),
checked: checked?(@repository_column, 'RepositoryAssetValue'.freeze), inline: true,
disabled: disabled?(@repository_column, 'RepositoryAssetValue'.freeze) %> checked: checked?(@repository_column, 'RepositoryAssetValue'.freeze),
<%= f.radio_button :data_type, disabled: disabled?(@repository_column, 'RepositoryAssetValue'.freeze) %>
'RepositoryListValue'.freeze, <%= f.radio_button :data_type,
label: t('libraries.manange_modal_column.labels.dropdown'), 'RepositoryListValue'.freeze,
inline: true, label: t('libraries.manange_modal_column.labels.dropdown'),
checked: checked?(@repository_column, 'RepositoryListValue'.freeze), inline: true,
disabled: disabled?(@repository_column, 'RepositoryListValue'.freeze) %> checked: checked?(@repository_column, 'RepositoryListValue'.freeze),
disabled: disabled?(@repository_column, 'RepositoryListValue'.freeze) %>
</span>
<% end %> <% end %>
<input class="form-control" <input class="form-control"
data-role="tagsinput" data-role="tagsinput"