PR fixes [SCI-1274]

This commit is contained in:
Oleksii Kriuchykhin 2017-06-07 13:36:39 +02:00
parent 8b1190060e
commit ad8dd6d740
14 changed files with 69 additions and 87 deletions

View file

@ -322,6 +322,9 @@ function onClickAddRecord() {
saveAction = 'create';
var tr = document.createElement('tr');
if (table.column(1).visible() === false) {
table.column(1).visible(true);
}
$('table#repository-table thead tr').children('th').each(function() {
var th = $(this);
var td;
@ -562,34 +565,22 @@ function onClickSave() {
changeToViewMode();
updateButtons();
} else if (e.status === 400) {
if (data.init_fields) {
var init_fields = data.init_fields;
if (data.default_fields) {
var defaultFields = data.default_fields;
// Validate record name
if (init_fields.name) {
if (defaultFields.name) {
var input = $(selectedRecord).find('input[name = name]');
if (input) {
input.closest('.form-group').addClass('has-error');
input.parent().append("<span class='help-block'>" +
init_fields.name + '<br /></span>');
defaultFields.name + '<br /></span>');
}
}
}
// Validate new cells
$.each(data.new_repository_cells || [], function(key, val) {
$.each(val, function(key, val) {
var input = $(selectedRecord).find('input[name=' + key + ']');
if (input) {
input.closest('.form-group').addClass('has-error');
input.parent().append("<span class='help-block'>" +
val.value[0] + '<br /></span>');
}
});
});
// Validate existing cells
// Validate custom cells
$.each(data.repository_cells || [], function(key, val) {
$.each(val, function(key, val) {
var input = $(selectedRecord).find('input[name=' + key + ']');

View file

@ -397,7 +397,7 @@ class MyModulesController < ApplicationController
record.last_modified_by = current_user
record.save
records_names << record.name
MyModulesRepositoryRow.create!(
MyModuleRepositoryRow.create!(
my_module: @my_module,
repository_row: record,
assigned_by: current_user

View file

@ -81,7 +81,7 @@ class RepositoryColumnsController < ApplicationController
respond_to do |format|
format.json do
if @repository_column.destroy
RepositoryTable.update_state(
RepositoryTableState.update_state(
@del_repository_column,
params[:repository_column][:column_index],
current_user

View file

@ -11,14 +11,14 @@ class RepositoryRowsController < ApplicationController
before_action :check_destroy_permissions, only: :delete_records
def create
record = RepositoryRow.new(name: record_params[:name],
repository: @repository,
record = RepositoryRow.new(repository: @repository,
created_by: current_user,
last_modified_by: current_user)
errors = { default_fields: [],
custom_cells: [] }
repository_cells: [] }
record.transaction do
record.name = record_params[:name] unless record_params[:name].blank?
unless record.save
errors[:default_fields] = record.errors.messages
raise ActiveRecord::RecordInvalid
@ -38,7 +38,7 @@ class RepositoryRowsController < ApplicationController
}
)
unless cell_value.save
errors[:custom_cells] << {
errors[:repository_cells] << {
"#{cell.repository_column.id}": cell_value.errors.messages
}
raise ActiveRecord::RecordInvalid
@ -56,7 +56,7 @@ class RepositoryRowsController < ApplicationController
status: :ok
end
end
rescue ActiveRecord::RecordInvalid
rescue
respond_to do |format|
format.json { render json: errors, status: :bad_request }
end
@ -87,13 +87,13 @@ class RepositoryRowsController < ApplicationController
def update
errors = {
default_fields: [],
custom_cells: []
repository_cells: []
}
@record.transaction do
@record.name = record_params[:name]
@record.name = record_params[:name].blank? ? nil : record_params[:name]
unless @record.save
errors[:default_fields] = sample.errors.messages
errors[:default_fields] = @record.errors.messages
raise ActiveRecord::RecordInvalid
end
if params[:repository_cells]
@ -105,7 +105,7 @@ class RepositoryRowsController < ApplicationController
# Cell exists and new value present, so update value
existing.value.data = value
unless existing.value.save
errors[:custom_cells] << {
errors[:repository_cells] << {
"#{cell.repository_column_id}": existing.value.errors.messages
}
raise ActiveRecord::RecordInvalid
@ -127,7 +127,7 @@ class RepositoryRowsController < ApplicationController
}
)
unless value.save
errors[:custom_cells] << {
errors[:repository_cells] << {
"#{cell.repository_column_id}": value.errors.messages
}
raise ActiveRecord::RecordInvalid
@ -159,7 +159,7 @@ class RepositoryRowsController < ApplicationController
status: :ok
end
end
rescue ActiveRecord::RecordInvalid
rescue
respond_to do |format|
format.json { render json: errors, status: :bad_request }
end

View file

@ -2,14 +2,14 @@ class UserRepositoriesController < ApplicationController
before_action :load_vars
def save_table_state
repository_table = RepositoryTable.where(user: current_user,
table_state = RepositoryTableState.where(user: current_user,
repository: @repository).first
if repository_table
repository_table.update(state: params[:state])
if table_state
table_state.update(state: params[:state])
else
RepositoryTable.create(user: current_user,
repository: @repository,
state: params[:state])
RepositoryTableState.create(user: current_user,
repository: @repository,
state: params[:state])
end
respond_to do |format|
format.json do
@ -21,13 +21,8 @@ class UserRepositoriesController < ApplicationController
end
def load_table_state
table_state = RepositoryTable.load_state(current_user,
@repository).first
if table_state.nil?
RepositoryTable.create_state(current_user, @repository)
table_state = RepositoryTable.load_state(current_user,
@repository).first
end
table_state = RepositoryTableState.load_state(current_user,
@repository).first
respond_to do |format|
if table_state
format.json do

View file

@ -146,16 +146,8 @@ class RepositoryDatatable < AjaxDatatablesRails::Base
:created_by
)
.where(repository: @repository)
if @my_module
@assigned_rows = @my_module.repository_rows
# repository_rows.joins(
# "LEFT OUTER JOIN my_modules_repository_rows ON
# (repository_row.id = my_modules_repository_rows.repository_row_id AND
# (my_modules_repository_rows.my_module_id = #{@my_module.id} OR
# my_modules_repository_rows.id IS NULL))"
# )
end
@assigned_rows = @my_module.repository_rows if @my_module
# Make mappings of custom columns, so we have same id for every column
i = 5
@ -193,11 +185,11 @@ class RepositoryDatatable < AjaxDatatablesRails::Base
# nulls last on repository_cells association
direction = sort_null_direction(params[:order].values[0])
records.joins(
"LEFT OUTER JOIN my_modules_repository_rows ON
(repository_rows.id = my_modules_repository_rows.repository_row_id
AND (my_modules_repository_rows.my_module_id = #{@my_module.id} OR
my_modules_repository_rows.id IS NULL))"
).order("my_modules_repository_rows.id NULLS #{direction}")
"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}")
end
elsif sorting_by_custom_column
# Check if have to filter records first
@ -330,14 +322,12 @@ class RepositoryDatatable < AjaxDatatablesRails::Base
.split('.')
return model if model == ASSIGNED_SORT_COL
col = [model.constantize.table_name, column].join('.')
[model.constantize.table_name, column].join('.')
end
def generate_sortable_displayed_columns
sort_order = RepositoryTable.where(user: @user, repository: @repository)
.pluck(:state)
.first['ColReorder']
sort_order = RepositoryTableState.load_state(@user, @repository)
.first['ColReorder']
sort_order.shift
sort_order.map! { |i| (i.to_i - 1).to_s }

View file

@ -30,9 +30,9 @@ class MyModule < ActiveRecord::Base
has_many :my_module_antecessors, through: :inputs, source: :from, class_name: 'MyModule'
has_many :sample_my_modules, inverse_of: :my_module, :dependent => :destroy
has_many :samples, through: :sample_my_modules
has_many :my_modules_repository_rows,
has_many :my_module_repository_rows,
inverse_of: :my_module, dependent: :destroy
has_many :repository_rows, through: :my_modules_repository_rows
has_many :repository_rows, through: :my_module_repository_rows
has_many :user_my_modules, inverse_of: :my_module, :dependent => :destroy
has_many :users, through: :user_my_modules
has_many :activities, inverse_of: :my_module

View file

@ -1,4 +1,4 @@
class MyModulesRepositoryRow < ActiveRecord::Base
class MyModuleRepositoryRow < ActiveRecord::Base
belongs_to :assigned_by, foreign_key: 'assigned_by_id', class_name: 'User'
belongs_to :repository_row
belongs_to :my_module

View file

@ -3,7 +3,8 @@ class Repository < ActiveRecord::Base
belongs_to :created_by, foreign_key: :created_by_id, class_name: 'User'
has_many :repository_columns
has_many :repository_rows
has_many :repository_tables, inverse_of: :repository, dependent: :destroy
has_many :repository_table_states,
inverse_of: :repository, dependent: :destroy
auto_strip_attributes :name, nullify: false
validates :name,

View file

@ -18,6 +18,6 @@ class RepositoryColumn < ActiveRecord::Base
after_create :update_repository_table_state
def update_repository_table_state
RepositoryTable.update_state(self, nil, created_by)
RepositoryTableState.update_state(self, nil, created_by)
end
end

View file

@ -5,9 +5,9 @@ class RepositoryRow < ActiveRecord::Base
class_name: 'User'
has_many :repository_cells, dependent: :destroy
has_many :repository_columns, through: :repository_cells
has_many :my_modules_repository_rows,
has_many :my_module_repository_rows,
inverse_of: :repository_row, dependent: :destroy
has_many :my_modules, through: :my_modules_repository_rows
has_many :my_modules, through: :my_module_repository_rows
auto_strip_attributes :name, nullify: false
validates :name,

View file

@ -1,19 +1,24 @@
class RepositoryTable < ActiveRecord::Base
belongs_to :user, inverse_of: :repository_tables
belongs_to :repository, inverse_of: :repository_tables
class RepositoryTableState < ActiveRecord::Base
belongs_to :user, inverse_of: :repository_table_states
belongs_to :repository, inverse_of: :repository_table_states
validates :user, :repository, presence: true
scope :load_state, (lambda { |user, repository|
where(user: user, repository: repository).pluck(:state)
})
def self.load_state(user, repository)
table_state = where(user: user, repository: repository).pluck(:state)
if table_state.blank?
RepositoryTableState.create_state(user, repository)
table_state = where(user: user, repository: repository).pluck(:state)
end
table_state
end
def self.update_state(custom_column, column_index, user)
repository_table = RepositoryTable.where(
table_state = RepositoryTableState.where(
user: user,
repository: custom_column.repository
)
repository_state = repository_table.first['state']
repository_state = table_state.first['state']
if column_index
# delete column
repository_state['columns'].delete(column_index)
@ -40,7 +45,7 @@ class RepositoryTable < ActiveRecord::Base
REPOSITORY_TABLE_DEFAULT_STATE['columns'].first
repository_state['ColReorder'].insert(2, index)
end
repository_table.first.update(state: repository_state)
table_state.first.update(state: repository_state)
end
def self.create_state(user, repository)
@ -53,8 +58,8 @@ class RepositoryTable < ActiveRecord::Base
REPOSITORY_TABLE_DEFAULT_STATE['columns'].first
repository_state['ColReorder'] << (default_columns_num + index)
end
RepositoryTable.create(user: user,
repository: repository,
state: repository_state)
RepositoryTableState.create(user: user,
repository: repository,
state: repository_state)
end
end

View file

@ -49,7 +49,7 @@ class User < ActiveRecord::Base
has_many :samples, inverse_of: :user
has_many :samples_tables, inverse_of: :user, dependent: :destroy
has_many :repositories, inverse_of: :user
has_many :repository_tables, inverse_of: :user, dependent: :destroy
has_many :repository_table_states, inverse_of: :user, dependent: :destroy
has_many :steps, inverse_of: :user
has_many :custom_fields, inverse_of: :user
has_many :reports, inverse_of: :user
@ -177,8 +177,8 @@ class User < ActiveRecord::Base
class_name: 'Protocol',
foreign_key: 'restored_by_id',
inverse_of: :restored_by
has_many :assigned_repository_rows_my_modules,
class_name: 'RepositoryRowsMyModules',
has_many :assigned_repository_row_my_modules,
class_name: 'RepositoryRowMyModules',
foreign_key: 'assigned_by_id'
has_many :user_notifications, inverse_of: :user

View file

@ -61,7 +61,7 @@ class AddCustomRepositories < ActiveRecord::Migration
:users,
column: :last_modified_by_id
create_table :my_modules_repository_rows do |t|
create_table :my_module_repository_rows do |t|
t.integer :repository_row_id, index: true, null: false
t.integer :my_module_id, null: :false
t.integer :assigned_by_id, null: false
@ -70,9 +70,9 @@ class AddCustomRepositories < ActiveRecord::Migration
name: 'index_my_module_ids_repository_row_ids'
end
add_foreign_key :my_modules_repository_rows, :users, column: :assigned_by_id
add_foreign_key :my_module_repository_rows, :users, column: :assigned_by_id
create_table :repository_tables do |t|
create_table :repository_table_states do |t|
t.jsonb :state, null: false
t.references :user, index: true, null: false
t.references :repository, index: true, null: false