diff --git a/app/assets/javascripts/app/models/object_manager_attribute.coffee b/app/assets/javascripts/app/models/object_manager_attribute.coffee index 3857e9472..6346d5714 100644 --- a/app/assets/javascripts/app/models/object_manager_attribute.coffee +++ b/app/assets/javascripts/app/models/object_manager_attribute.coffee @@ -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 }, ] diff --git a/app/assets/javascripts/app/views/object_manager/index.jst.eco b/app/assets/javascripts/app/views/object_manager/index.jst.eco index b984dc0c0..3190716d8 100644 --- a/app/assets/javascripts/app/views/object_manager/index.jst.eco +++ b/app/assets/javascripts/app/views/object_manager/index.jst.eco @@ -55,6 +55,7 @@ <%- @T('Display') %> <%- @T('Name') %> <%- @T('Type') %> + <%- @T('Position') %> <%- @T('Action') %> @@ -64,6 +65,7 @@ <%= item.display %> <%= item.name %> <%= item.data_type %> + <%= item.position %> <% if item.to_create is true: %> <%- @T('will be created') %> diff --git a/app/controllers/object_manager_attributes_controller.rb b/app/controllers/object_manager_attributes_controller.rb index 864cc2a4d..b645d7df7 100644 --- a/app/controllers/object_manager_attributes_controller.rb +++ b/app/controllers/object_manager_attributes_controller.rb @@ -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 diff --git a/app/models/object_manager/attribute.rb b/app/models/object_manager/attribute.rb index 503a2e9f0..303dbccbd 100644 --- a/app/models/object_manager/attribute.rb +++ b/app/models/object_manager/attribute.rb @@ -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 diff --git a/spec/requests/integration/object_manager_attributes_spec.rb b/spec/requests/integration/object_manager_attributes_spec.rb index f53e1a901..9c5c8cc13 100644 --- a/spec/requests/integration/object_manager_attributes_spec.rb +++ b/spec/requests/integration/object_manager_attributes_spec.rb @@ -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 diff --git a/spec/system/system/object_manager_spec.rb b/spec/system/system/object_manager_spec.rb index d69ef5eb1..9ce519ca7 100644 --- a/spec/system/system/object_manager_spec.rb +++ b/spec/system/system/object_manager_spec.rb @@ -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'