Refactor assigned rows

This commit is contained in:
Urban Rotnik 2020-05-18 12:56:10 +02:00
parent 9536939b3b
commit 2b493ce6f6
5 changed files with 16 additions and 36 deletions

View file

@ -402,15 +402,9 @@ class MyModule < ApplicationRecord
# Generate the repository rows belonging to this module
# in JSON form, suitable for display in handsontable.js
def repository_json_hot(repository_id, order)
def repository_json_hot(repository, order)
data = []
r = RepositoryBase.find(repository_id)
rows = if r.is_a?(RepositorySnapshot)
r.repository_rows.includes(:created_by).order(created_at: order)
else
repository_rows.includes(:created_by).where(repository_id: repository_id).order(created_at: order)
end
rows = repository.assigned_rows(self).includes(:created_by).order(created_at: order)
rows.find_each do |row|
row_json = []
row_json << row.id
@ -430,29 +424,7 @@ class MyModule < ApplicationRecord
{ data: data, headers: headers }
end
def repository_json(repository_id, order, user)
headers = [
I18n.t('repositories.table.id'),
I18n.t('repositories.table.row_name'),
I18n.t('repositories.table.added_on'),
I18n.t('repositories.table.added_by')
]
repository = Repository.find_by_id(repository_id)
return false unless repository
repository.repository_columns.order(:id).each do |column|
headers.push(column.name)
end
params = { assigned: 'assigned', search: {}, order: { values: { column: '1', dir: order } } }
records = RepositoryDatatableService.new(repository,
params,
user,
self)
{ headers: headers, data: records }
end
def repository_docx_json(repository_id)
def repository_docx_json(repository)
headers = [
I18n.t('repositories.table.id'),
I18n.t('repositories.table.row_name'),
@ -460,7 +432,6 @@ class MyModule < ApplicationRecord
I18n.t('repositories.table.added_by')
]
custom_columns = []
repository = Repository.find_by(id: repository_id)
return false unless repository
repository.repository_columns.order(:id).each do |column|
@ -468,7 +439,7 @@ class MyModule < ApplicationRecord
custom_columns.push(column.id)
end
records = repository_rows.where(repository_id: repository_id).select(:id, :name, :created_at, :created_by_id)
records = repository.assigned_rows(self).select(:id, :name, :created_at, :created_by_id)
{ headers: headers, rows: records, custom_columns: custom_columns }
end

View file

@ -212,4 +212,8 @@ class Repository < RepositoryBase
def sync_name_with_snapshots
repository_snapshots.update(name: name)
end
def assigned_rows(my_module)
repository_rows.joins(:my_module_repository_rows).where(my_module_repository_rows: { my_module_id: my_module.id })
end
end

View file

@ -19,6 +19,10 @@ class RepositorySnapshot < RepositoryBase
Constants::REPOSITORY_SNAPSHOT_TABLE_DEFAULT_STATE['length']
end
def assigned_rows(_my_module)
repository_rows
end
private
def only_one_selected_for_my_module

View file

@ -6,10 +6,11 @@ module Reports::Docx::DrawMyModuleRepository
return unless my_module
repository_id = subject['id']['repository_id']
repository_data = my_module.repository_docx_json(repository_id)
repository = ::RepositoryBase.find(repository_id)
repository_data = my_module.repository_docx_json(repository)
return false unless repository_data[:rows].any?
repository = ::Repository.find(repository_id)
table = prepare_row_columns(repository_data)
@docx.p

View file

@ -2,7 +2,7 @@
<% repository ||= my_module.selected_snapshot(element_id) || Repository.find(element_id) %>
<% if order.blank? and @order.present? then order = @order end %>
<% timestamp = Time.current + 1.year - 1.days %>
<% rows_json = my_module.repository_json_hot(repository.id, order) %>
<% rows_json = my_module.repository_json_hot(repository, order) %>
<div class="report-element report-module-repository-element" data-sort-hot="1" data-ts="<%= timestamp.to_i %>" data-type="my_module_repository" data-id='{ "my_module_id": <%= my_module.id %>, "repository_id": <%= repository.id %> }' data-scroll-id="<%= "#{my_module.id}_#{repository.id}" %>" data-order="<%= order == :asc ? "asc" : "desc" %>" data-name="<%= repository.name %>" data-icon-class="fas fa-list-alt">
<div class="report-element-header">
<div class="row">