diff --git a/app/controllers/repository_rows_controller.rb b/app/controllers/repository_rows_controller.rb index 9526bcc71..7fabc0f5b 100644 --- a/app/controllers/repository_rows_controller.rb +++ b/app/controllers/repository_rows_controller.rb @@ -20,8 +20,8 @@ class RepositoryRowsController < ApplicationController record.transaction do record.name = record_params[:name] unless record_params[:name].blank? errors[:default_fields] = record.errors.messages unless record.save - if params[:repository_cells] - params[:repository_cells].each do |key, value| + if cell_params + cell_params.each do |key, value| column = @repository.repository_columns.detect do |c| c.id == key.to_i end @@ -93,8 +93,8 @@ class RepositoryRowsController < ApplicationController @record.transaction do @record.name = record_params[:name].blank? ? nil : record_params[:name] errors[:default_fields] = @record.errors.messages unless @record.save - if params[:repository_cells] - params[:repository_cells].each do |key, value| + if cell_params + cell_params.each do |key, value| existing = @record.repository_cells.detect do |c| c.repository_column_id == key.to_i end @@ -136,7 +136,7 @@ class RepositoryRowsController < ApplicationController end # Clean up empty cells, not present in updated record @record.repository_cells.each do |cell| - cell.value.destroy unless params[:repository_cells] + cell.value.destroy unless cell_params .key?(cell.repository_column_id.to_s) end else @@ -169,8 +169,8 @@ class RepositoryRowsController < ApplicationController def delete_records deleted_count = 0 - if params[:selected_rows] - params[:selected_rows].each do |row_id| + if selected_params + selected_params.each do |row_id| row = @repository.repository_rows.find_by_id(row_id) if row && can_delete_repository_record(row) row.destroy && deleted_count += 1 @@ -178,9 +178,9 @@ class RepositoryRowsController < ApplicationController end if deleted_count.zero? flash = t('repositories.destroy.no_deleted_records_flash', - other_records_number: params[:selected_rows].count) - elsif deleted_count != params[:selected_rows].count - not_deleted_count = params[:selected_rows].count - deleted_count + other_records_number: selected_params.count) + elsif deleted_count != selected_params.count + not_deleted_count = selected_params.count - deleted_count flash = t('repositories.destroy.contains_other_records_flash', records_number: deleted_count, other_records_number: not_deleted_count) @@ -231,7 +231,15 @@ class RepositoryRowsController < ApplicationController end def record_params - params.require(:repository_row).permit(:name) + params.require(:repository_row).permit(:name).to_h + end + + def cell_params + params.permit(repository_cells: {}).to_h[:repository_cells] + end + + def selected_params + params.permit(selected_rows: []).to_h[:selected_rows] end def record_annotation_notification(record, cell, old_text = nil) diff --git a/app/datatables/repository_datatable.rb b/app/datatables/repository_datatable.rb index b1d274a90..f75c641fa 100644 --- a/app/datatables/repository_datatable.rb +++ b/app/datatables/repository_datatable.rb @@ -183,8 +183,7 @@ class RepositoryDatatable < CustomDatatable # number of samples/all samples it's dependant upon sort_record query def fetch_records records = get_raw_records - records = filter_records(records) if dt_params[:search].present? && - dt_params[:search][:value].present? + records = filter_records(records) if dt_params[:search].present? records = sort_records(records) if order_params.present? records = paginate_records(records) unless dt_params[:length].present? && dt_params[:length] == '-1' @@ -196,7 +195,10 @@ class RepositoryDatatable < CustomDatatable # NOTE: Function assumes the provided records/rows are only from the current # repository! def filter_records(repo_rows) + return repo_rows unless dt_params[:search].present? && + dt_params[:search][:value].present? search_val = dt_params[:search][:value] + filtered_rows = repo_rows.find_by_sql( "SELECT DISTINCT repository_rows.* FROM repository_rows @@ -230,12 +232,12 @@ class RepositoryDatatable < CustomDatatable # Override default sort method if needed def sort_records(records) - if params[:order].present? && params[:order].length == 1 - if sort_column(params[:order].values[0]) == ASSIGNED_SORT_COL + if order_params.present? + if sort_column(order_params) == ASSIGNED_SORT_COL # If "assigned" column is sorted when viewing assigned items - return records if @my_module && params[:assigned] == 'assigned' + return records if @my_module && dt_params[:assigned] == 'assigned' # If "assigned" column is sorted - direction = sort_null_direction(params[:order].values[0]) + direction = sort_null_direction(order_params) if @my_module # Depending on the sort, order nulls first or # nulls last on repository_cells association @@ -246,40 +248,26 @@ class RepositoryDatatable < CustomDatatable my_module_repository_rows.id IS NULL))" ).order("my_module_repository_rows.id NULLS #{direction}") else - sort_assigned_records(records, params[:order].values[0]['dir']) + sort_assigned_records(records, order_params['dir']) end elsif sorting_by_custom_column ci = sortable_displayed_columns[ - params[:order].values[0][:column].to_i - 1 + order_params['column'].to_i - 1 ] column_id = @columns_mappings.key((ci.to_i + 1).to_s) - dir = sort_direction(params[:order].values[0]) + dir = sort_direction(order_params) records.joins( - "LEFT OUTER JOIN my_module_repository_rows ON - (repository_rows.id = my_module_repository_rows.repository_row_id - AND (my_module_repository_rows.my_module_id = #{@my_module.id} OR - my_module_repository_rows.id IS NULL))" - ).order("my_module_repository_rows.id NULLS #{direction}") + "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 = #{column_id}) AS values + ON values.repository_row_id = repository_rows.id" + ).order("values.value #{dir}") else - records.joins( - 'LEFT OUTER JOIN my_module_repository_rows ON - (repository_rows.id = my_module_repository_rows.repository_row_id)' - ).order("my_module_repository_rows.id NULLS #{direction}") + super(records) end - elsif sorting_by_custom_column - ci = sortable_displayed_columns[order_params[:column].to_i - 1] - column_id = @columns_mappings.key((ci.to_i + 1).to_s) - dir = sort_direction(order_params) - - 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 = #{column_id}) AS values - ON values.repository_row_id = repository_rows.id" - ).order("values.value #{dir}") else super(records) end @@ -335,10 +323,10 @@ class RepositoryDatatable < CustomDatatable ids = unassigned + assigned end - order_by_index = ActiveRecord::Base.send( + order_by_index = ActiveRecord::Base.__send__( :sanitize_sql_array, ["position((',' || repository_rows.id || ',') in ?)", - ids.join(',') + ','] + ids.join(',') + ','] ) records.order(order_by_index) end diff --git a/app/models/repository.rb b/app/models/repository.rb index b82a8688e..230f45f66 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -6,6 +6,7 @@ class Repository < ApplicationRecord foreign_key: :created_by_id, class_name: 'User', optional: true + has_many :repository_columns has_many :repository_rows has_many :repository_table_states, inverse_of: :repository, dependent: :destroy diff --git a/app/models/repository_text_value.rb b/app/models/repository_text_value.rb index 2d22744c7..194e809dd 100644 --- a/app/models/repository_text_value.rb +++ b/app/models/repository_text_value.rb @@ -7,7 +7,7 @@ class RepositoryTextValue < ApplicationRecord foreign_key: :last_modified_by_id, class_name: 'User', optional: true - has_one :repository_cell, as: :value, dependent: :destroy + has_one :repository_cell, as: :value, dependent: :destroy, inverse_of: :value accepts_nested_attributes_for :repository_cell validates :repository_cell, presence: true