Fixes #294 - Possibility to specify the order of objects.

This commit is contained in:
Bola Ahmed Buari 2021-10-26 15:26:16 +02:00 committed by Rolf Schmidt
parent 5c333430ca
commit cdd2980a2a
6 changed files with 47 additions and 6 deletions

View file

@ -6,8 +6,8 @@ class App.ObjectManagerAttribute extends App.Model
{ name: 'name', display: 'Name', tag: 'input', type: 'text', limit: 100, null: false },
{ name: 'display', display: 'Display', tag: 'input', type: 'text', limit: 100, null: false },
{ name: 'object', display: 'Object', tag: 'input', readonly: 1 },
{ name: 'position', display: 'Position', tag: 'input', readonly: 1 },
{ name: 'active', display: 'Active', tag: 'active', default: true },
{ name: 'data_type', display: 'Format', tag: 'object_manager_attribute', null: false },
{ name: 'updated_at', display: 'Updated', tag: 'datetime', readonly: 1 },
{ name: 'position', display: 'Position', tag: 'integer', type: 'number', limit: 100, null: true },
]

View file

@ -55,6 +55,7 @@
<th class=""><%- @T('Display') %></th>
<th class=""><%- @T('Name') %></th>
<th class="" style="width: 200px;"><%- @T('Type') %></th>
<th><%- @T('Position') %></th>
<th class="" style="width: 140px;"><%- @T('Action') %></th>
</tr>
</thead>
@ -64,6 +65,7 @@
<td><%= item.display %></td>
<td><%= item.name %></td>
<td><%= item.data_type %></td>
<td><%= item.position %></td>
<td>
<% if item.to_create is true: %>
<%- @T('will be created') %>

View file

@ -29,7 +29,7 @@ class ObjectManagerAttributesController < ApplicationController
)
raise Exceptions::UnprocessableEntity, 'already exists' if exists
add_attribute_using_params(permitted_params.merge(position: 1550), status: :created)
add_attribute_using_params(permitted_params, status: :created)
end
# PUT /object_manager_attributes/1

View file

@ -367,6 +367,12 @@ possible types
return record
end
# add maximum position only for new records with blank position
if !record && data[:position].blank?
maximum_position = where(object_lookup_id: data[:object_lookup_id]).maximum(:position)
data[:position] = maximum_position.present? ? maximum_position + 1 : 1
end
# do not allow to overwrite certain attributes
if !force
data[:editable] = true

View file

@ -1039,7 +1039,7 @@ RSpec.describe 'ObjectManager Attributes', type: :request do
end
context 'position handling', authenticated_as: -> { admin } do
let(:params) do
let(:base_params) do
{
name: "customerdescription_#{SecureRandom.uuid.tr('-', '_')}",
object: 'Ticket',
@ -1060,15 +1060,35 @@ RSpec.describe 'ObjectManager Attributes', type: :request do
before { post '/api/v1/object_manager_attributes', params: params, as: :json }
context 'when creating a new attribute' do
it 'defaults to 1550' do
expect(new_attribute_object.position).to eq 1550
let(:params) { base_params }
context 'with no position attribute provided' do
let(:maximum_position) do
ObjectManager::Attribute
.for_object(params[:object])
.maximum(:position)
end
it 'defaults to the maximum available position' do
expect(new_attribute_object.position).to eq maximum_position
end
end
context 'with a position attribute given' do
let(:position) { 50 }
let(:params) { base_params.merge(position: position) }
it 'defaults to given position' do
expect(new_attribute_object.position).to eq position
end
end
end
context 'when updating an existing attribute' do
let(:alternative_position) { 123 }
let(:alternative_display) { 'another description' }
let(:alternative_params) { params.deep_dup.update(display: alternative_display) }
let(:params) { base_params }
let(:alternative_params) { base_params.merge(display: alternative_display) }
before do
new_attribute_object.update! position: alternative_position

View file

@ -29,6 +29,19 @@ RSpec.describe 'System > Objects', type: :system do
end
end
context 'when creating a new attribute' do
before do
visit '/#system/object_manager'
find('.js-new').click
end
it 'has the position feild with no default value' do
within '.modal' do
expect(page).to have_field('position', with: '')
end
end
end
context 'when creating but then discarding fields again' do
before do
visit '/#system/object_manager'