Add support of repository snapshots to assigned items section [SCI-6439] (#3828)

This commit is contained in:
Alex Kriuchykhin 2022-02-11 10:46:19 +01:00 committed by GitHub
parent 1efe3ddd37
commit 79c32e30ad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 67 additions and 33 deletions

View file

@ -209,11 +209,11 @@ $.fn.dataTable.render.RepositoryConsumedStockValue = function(data) {
if (data.value) {
if (data.consumption_managable) {
return `<a href="${data.updateStockConsumptionUrl}" class="manage-repository-consumed-stock-value-link stock-value-view-render">
${data.value.consumed_stock_formatted} ${data.value.unit}
${data.value.consumed_stock} ${data.value.unit || ''}
</a>`;
}
return `<span class="stock-value-view-render">
${data.value.consumed_stock_formatted} ${data.value.unit}
${data.value.consumed_stock} ${data.value.unit || ''}
</span>`;
}
if (data.stock_present && data.consumption_managable) {

View file

@ -18,6 +18,7 @@ class MyModuleRepositorySnapshotsController < ApplicationController
@columns_mappings = datatable_service.mappings
if params[:simple_view]
repository_rows = datatable_service.repository_rows
@repository = @repository_snapshot
rows_view = 'repository_rows/simple_view_index.json'
else
repository_rows =

View file

@ -89,24 +89,35 @@ module RepositoryDatatableHelper
stock_present = record.repository_stock_cell.present?
# Always disabled in a simple view
stock_managable = false
consumption_managable = stock_present && can_update_my_module_stock_consumption?(my_module)
consumption_managable =
stock_present && record.repository.is_a?(Repository) && can_update_my_module_stock_consumption?(my_module)
row['1'] = stock_present ? display_cell_value(record.repository_stock_cell, record.repository.team) : {}
row['1'][:stock_managable] = stock_managable
row['2'] = {
stock_present: stock_present,
consumption_managable: consumption_managable,
updateStockConsumptionUrl: Rails.application.routes.url_helpers.consume_modal_my_module_repository_path(
my_module,
record.repository,
row_id: record.id
)
consumption_managable: consumption_managable
}
if record.consumed_stock.present?
row['2'][:value] = {
consumed_stock_formatted: record.consumed_stock,
unit: record.repository_stock_value.repository_stock_unit_item&.data
}
if record.repository.is_a?(RepositorySnapshot)
if record.repository_stock_consumption_value.present?
row['2'][:value] = {
consumed_stock: record.repository_stock_consumption_value.amount,
unit: record.repository_stock_consumption_value.repository_stock_unit_item&.data
}
end
else
if consumption_managable
row['2'][:updateStockConsumptionUrl] =
Rails.application.routes.url_helpers.consume_modal_my_module_repository_path(
my_module, record.repository, row_id: record.id
)
end
if record.consumed_stock.present?
row['2'][:value] = {
consumed_stock: record.consumed_stock,
unit: record.repository_stock_value&.repository_stock_unit_item&.data
}
end
end
end

View file

@ -211,14 +211,6 @@ class Repository < RepositoryBase
.destroy_all
end
def self.stock_management_enabled?
true
end
def has_stock_management?
self.class.stock_management_enabled? && repository_columns.stock_type.exists?
end
private
def sync_name_with_snapshots

View file

@ -27,6 +27,14 @@ class RepositoryBase < ApplicationRecord
# Not discarded
default_scope -> { kept }
def self.stock_management_enabled?
true
end
def has_stock_management?
self.class.stock_management_enabled? && repository_columns.stock_type.exists?
end
def cell_preload_includes
cell_includes = []
repository_columns.pluck(:data_type).each do |data_type|

View file

@ -34,9 +34,7 @@ class RepositoryRow < ApplicationRecord
repository_date: 'RepositoryDateValue',
repository_date_time_range: 'RepositoryDateTimeRangeValue',
repository_time_range: 'RepositoryTimeRangeValue',
repository_date_range: 'RepositoryDateRangeValue',
repository_stock: 'RepositoryStockValue',
repository_stock_consumption: 'RepositoryStockConsumptionValue'
repository_date_range: 'RepositoryDateRangeValue'
}.each do |relation, class_name|
has_many "#{relation}_cells".to_sym, -> { where(value_type: class_name) }, class_name: 'RepositoryCell',
inverse_of: :repository_row
@ -44,10 +42,33 @@ class RepositoryRow < ApplicationRecord
source: :value, source_type: class_name
end
has_one :repository_stock_cell, -> { where(value_type: 'RepositoryStockValue') }, class_name: 'RepositoryCell',
inverse_of: :repository_row
has_one :repository_stock_value, class_name: 'RepositoryStockValue', through: :repository_stock_cell,
source: :value, source_type: 'RepositoryStockValue'
has_one :repository_stock_cell,
lambda {
joins(:repository_column)
.where(repository_columns: { data_type: 'RepositoryStockValue' })
.where(value_type: 'RepositoryStockValue')
},
class_name: 'RepositoryCell',
inverse_of: :repository_row
has_one :repository_stock_value, class_name: 'RepositoryStockValue',
through: :repository_stock_cell,
source: :value,
source_type: 'RepositoryStockValue'
# Only in snapshots
has_one :repository_stock_consumption_cell,
lambda {
joins(:repository_column)
.where(repository_columns: { data_type: 'RepositoryStockConsumptionValue' })
.where(value_type: 'RepositoryStockValue')
},
class_name: 'RepositoryCell',
inverse_of: :repository_row
has_one :repository_stock_consumption_value,
class_name: 'RepositoryStockConsumptionValue',
through: :repository_stock_consumption_cell,
source: :value,
source_type: 'RepositoryStockValue'
has_many :repository_columns, through: :repository_cells
has_many :my_module_repository_rows,

View file

@ -66,9 +66,10 @@ module Repositories
end
def create_stock_consumption_cell_snapshot!(repository_row, row_snapshot)
return if repository_row.repository_stock_value.blank?
my_module_repository_row =
repository_row.my_module_repository_rows.find { |mrr| mrr.my_module_id == @repository_snapshot.my_module_id }
return if my_module_repository_row.stock_consumption.blank?
stock_unit_item =
@repository_snapshot.repository_stock_consumption_column
@ -79,7 +80,7 @@ module Repositories
repository_column: @repository_snapshot.repository_stock_consumption_column,
repository_row: row_snapshot
},
amount: my_module_repository_row.stock_consumption,
amount: my_module_repository_row.stock_consumption.to_d,
repository_stock_unit_item: stock_unit_item
)
end

View file

@ -28,12 +28,12 @@
data-name-column-id="<%= assigned_repository_simple_view_name_column_id(repository) %>"
>
<table class="table hidden repository-table"
data-stock-management="<%= !repository.is_a?(RepositorySnapshot) && repository.has_stock_management? %>"
data-stock-management="<%= repository.has_stock_management? %>"
data-stock-consumption-editable="<%= can_update_my_module_stock_consumption?(@my_module) %>">
<thead>
<tr>
<th class="row-name"><%= t("repositories.table.row_name") %></th>
<% if !repository.is_a?(RepositorySnapshot) && repository.has_stock_management? %>
<% if repository.has_stock_management? %>
<th class="row-stock" data-columns-visible="false"><%= repository.repository_stock_column.name %></th>
<th class="row-consumption" data-columns-visible="false"><%= t("repositories.table.row_consumption") %></th>
<% end %>