fix hound

This commit is contained in:
zmagod 2018-03-09 14:43:12 +01:00
parent 4f951f6679
commit 0baa7a1f99
4 changed files with 95 additions and 59 deletions

View file

@ -13,15 +13,18 @@ module RepositoryDatatableHelper
'2': escape_input(record.name),
'3': I18n.l(record.created_at, format: :full),
'4': escape_input(record.created_by.full_name),
'recordEditUrl':
Rails.application.routes.url_helpers
.edit_repository_repository_row_path(repository,
record.id),
'recordUpdateUrl':
Rails.application.routes.url_helpers
.repository_repository_row_path(repository, record.id),
'recordInfoUrl':
Rails.application.routes.url_helpers.repository_row_path(record.id)
'recordEditUrl': Rails.application.routes.url_helpers
.edit_repository_repository_row_path(
repository,
record.id
),
'recordUpdateUrl': Rails.application.routes.url_helpers
.repository_repository_row_path(
repository,
record.id
),
'recordInfoUrl': Rails.application.routes.url_helpers
.repository_row_path(record.id)
}
# Add custom columns

View file

@ -14,7 +14,8 @@ class RepositoryDatatableService
private
def create_columns_mappings
# Make mappings of custom columns, so we have same id for every column
# Make mappings of custom columns, so we have same id for every
# column
i = 5
@mappings = {}
@repository.repository_columns.order(:id).each do |column|
@ -24,9 +25,8 @@ class RepositoryDatatableService
end
def process_query
contitions = build_conditions(@params)
order_obj = contitions[:order_by_column]
search_value = contitions[:search_value]
order_obj = build_conditions(@params)[:order_by_column]
search_value = build_conditions(@params)[:search_value]
if search_value.present?
@repository_rows = sort_rows(order_obj, search(search_value))
else
@ -53,7 +53,8 @@ class RepositoryDatatableService
else
@assigned_rows = repository_rows.joins(
'INNER JOIN my_module_repository_rows ON
(repository_rows.id = my_module_repository_rows.repository_row_id)'
(repository_rows.id =
my_module_repository_rows.repository_row_id)'
)
end
repository_rows
@ -61,8 +62,10 @@ class RepositoryDatatableService
def search(value)
includes_json = {
repository_cells: [:repository_text_value,
repository_list_value: :repository_list_item ]
repository_cells: [
:repository_text_value,
repository_list_value: :repository_list_item
]
}
RepositoryRow .left_outer_joins(:created_by)
.left_outer_joins(includes_json)
@ -98,7 +101,9 @@ class RepositoryDatatableService
end
def sort_rows(column_obj, records)
dir = %w[DESC ASC].find { |dir| dir == column_obj[:dir].upcase } || 'ASC'
dir = %w(DESC ASC).find do |dir|
dir == column_obj[:dir].upcase
end || 'ASC'
column_index = column_obj[:column]
col_order = @repository.repository_table_states
.find_by_user_id(@user.id)
@ -106,17 +111,22 @@ class RepositoryDatatableService
column_id = col_order[column_index].to_i
if sortable_columns[column_id - 1] == 'assigned'
return records if @my_module && @params[:assigned] == 'assigned'
if @my_module && @params[:assigned] == 'assigned'
return records
end
if @my_module
# Depending on the sort, order nulls first or
# nulls last on repository_cells association
return 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))"
(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 #{sort_null_direction(dir)}"
"my_module_repository_rows.id NULLS
#{sort_null_direction(dir)}"
)
else
return sort_assigned_records(records, dir)
@ -127,12 +137,16 @@ class RepositoryDatatableService
return records unless type
return select_type(type.data_type, records, id, dir)
else
return records.order("#{sortable_columns[column_id - 1]} #{dir}")
return records.order(
"#{sortable_columns[column_id - 1]} #{dir}"
)
end
end
def sort_assigned_records(records, direction)
assigned = records.joins(:my_module_repository_rows).distinct.pluck(:id)
assigned = records.joins(:my_module_repository_rows)
.distinct
.pluck(:id)
unassigned = records.where.not(id: assigned).pluck(:id)
if direction == 'ASC'
ids = assigned + unassigned
@ -149,10 +163,14 @@ class RepositoryDatatableService
end
def select_type(type, records, id, dir)
return filter_by_text_value(
records, id, dir) if type == 'RepositoryTextValue'
return filter_by_list_value(
records, id, dir) if type == 'RepositoryListValue'
case type
when 'RepositoryTextValue'
filter_by_text_value(records, id, dir)
when 'RepositoryListValue'
filter_by_list_value(records, id, dir)
else
records
end
end
def sort_null_direction(val)
@ -160,7 +178,7 @@ class RepositoryDatatableService
end
def filter_by_text_value(records, id, dir)
return records.joins(
records.joins(
"LEFT OUTER JOIN (SELECT repository_cells.repository_row_id,
repository_text_values.data AS value
FROM repository_cells
@ -172,7 +190,7 @@ class RepositoryDatatableService
end
def filter_by_list_value(records, id, dir)
return records.joins(
records.joins(
"LEFT OUTER JOIN (SELECT repository_cells.repository_row_id,
repository_list_items.data AS value
FROM repository_cells

View file

@ -98,7 +98,6 @@ RSpec.describe RepositoryListValue, type: :model do
it 'retuns only the the item related to the list' do
repository_row_two = create :repository_row, name: 'New row'
repository_list_value_two =
create :repository_list_value, repository_cell_attributes: {
repository_column: repository_column,
repository_row: repository_row_two
@ -113,8 +112,7 @@ RSpec.describe RepositoryListValue, type: :model do
end
it 'returns an empty string if no item selected' do
list_item = create :repository_list_item,
data: 'my item',
create :repository_list_item, data: 'my item',
repository: repository,
repository_column: repository_column
expect(repository_list_value.reload.data).to be_nil

View file

@ -4,10 +4,13 @@ describe RepositoryDatatableService do
let!(:team) { create :team }
let!(:user) { create :user, email: 'user_one@asdf.com' }
let!(:repository) do
create :repository, name: 'my repo', created_by: user, team: team
create :repository, name: 'my repo',
created_by: user,
team: team
end
let!(:repository_column) do
create :repository_column, name: 'My column', data_type: :RepositoryListValue
create :repository_column, name: 'My column',
data_type: :RepositoryListValue
end
let!(:repository_state) do
RepositoryTableState.create(
@ -29,14 +32,16 @@ describe RepositoryDatatableService do
last_modified_by: user
end
let!(:list_item) do
create :repository_list_item, data: 'bug',
create :repository_list_item,
data: 'bug',
repository: repository,
repository_column: repository_column,
created_by: user,
last_modified_by: user
end
let!(:repository_list_value) do
create :repository_list_value, repository_list_item: list_item,
create :repository_list_value,
repository_list_item: list_item,
created_by: user,
last_modified_by: user,
repository_cell_attributes: {
@ -47,15 +52,21 @@ describe RepositoryDatatableService do
context 'object' do
let(:params) do
{ order: { 0 => { column: '2', dir: 'asc' } }, search: { value: 'row' } }
{ order: { 0 => { column: '2', dir: 'asc' } },
search: { value: 'row' } }
end
let(:subject) do
RepositoryDatatableService.new(repository, params, user)
end
let(:subject) { RepositoryDatatableService.new(repository, params, user) }
describe '#build_conditions/1' do
it 'parsers the contitions' do
contitions = subject.send(:build_conditions, params)
expect(contitions[:search_value]).to eq 'row'
expect(contitions[:order_by_column]).to eq({ column: 2, dir: 'asc' })
expect(contitions[:order_by_column]).to eq(
{ column: 2, dir: 'asc' }
)
end
end
@ -83,7 +94,9 @@ describe RepositoryDatatableService do
it 'is ordered by row name asc' do
params = { order: { 0 => { column: '2', dir: 'asc' } },
search: { value: '' } }
subject = RepositoryDatatableService.new(repository, params, user)
subject = RepositoryDatatableService.new(repository,
params,
user)
expect(subject.repository_rows.first.name).to eq 'A row'
expect(subject.repository_rows.last.name).to eq 'B row'
end
@ -91,7 +104,9 @@ describe RepositoryDatatableService do
it 'is ordered by row name desc' do
params = { order: { 0 => { column: '2', dir: 'desc' } },
search: { value: '' } }
subject = RepositoryDatatableService.new(repository, params, user)
subject = RepositoryDatatableService.new(repository,
params,
user)
expect(subject.repository_rows.first.name).to eq 'B row'
expect(subject.repository_rows.last.name).to eq 'A row'
end
@ -108,7 +123,9 @@ describe RepositoryDatatableService do
it 'returns only the searched entity' do
params = { order: { 0 => { column: '2', dir: 'desc' } },
search: { value: 'test' } }
subject = RepositoryDatatableService.new(repository, params, user)
subject = RepositoryDatatableService.new(repository,
params,
user)
expect(subject.repository_rows.first.name).to eq 'test'
expect(subject.repository_rows.count).to eq 1
end