diff --git a/app/assets/javascripts/app/controllers/object_manager.coffee b/app/assets/javascripts/app/controllers/object_manager.coffee index 1e4d0f6e9..2ddd69270 100644 --- a/app/assets/javascripts/app/controllers/object_manager.coffee +++ b/app/assets/javascripts/app/controllers/object_manager.coffee @@ -39,6 +39,21 @@ treeParams = (e, params) -> params.data_option.options = tree params +setSelectDefaults = (el) -> + data_type = el.find('select[name=data_type]').val() + return if data_type isnt 'select' && data_type isnt 'boolean' + + el.find('.js-value, .js-valueTrue, .js-valueFalse').each(-> + element = $(@) + return true if element.val() + + if element.hasClass('js-valueTrue') || element.hasClass('js-valueFalse') + element.val(element.attr('placeholder')) + else + key_value = element.closest('tr').find('.js-key').val() + element.val(key_value) + ) + class ObjectManager extends App.ControllerTabs requiredPermission: 'admin.object' constructor: -> @@ -179,6 +194,8 @@ class Items extends App.ControllerSubContent class New extends App.ControllerGenericNew onSubmit: (e) => + setSelectDefaults(@el) + params = @formParam(e.target) params = treeParams(e, params) @@ -242,6 +259,8 @@ class Edit extends App.ControllerGenericEdit @controller.form onSubmit: (e) => + setSelectDefaults(@el) + params = @formParam(e.target) params = treeParams(e, params) diff --git a/spec/system/system/object_manager_spec.rb b/spec/system/system/object_manager_spec.rb index c0acbc340..8b9a6a51b 100644 --- a/spec/system/system/object_manager_spec.rb +++ b/spec/system/system/object_manager_spec.rb @@ -65,4 +65,52 @@ RSpec.describe 'Admin Panel > Objects', type: :system, authenticated_as: true do expect(ObjectManager::Attribute.last.data_option).to eq(expected_data_options) end + + it 'checks smart defaults for select field' do + page.find('.js-new').click + + fill_in 'Name', with: 'select1' + find('input[name=display]').set('select1') + + page.find('select[name=data_type]').select('Select') + + page.first('div.js-add').click + page.first('div.js-add').click + page.first('div.js-add').click + + counter = 0 + page.all('.js-key').each do |field| + field.set(counter) + counter += 1 + end + + page.all('.js-value')[-2].set('special 2') + page.find('.js-submit').click + + expected_data_options = { + '0' => '0', + '1' => '1', + '2' => 'special 2', + } + + expect(ObjectManager::Attribute.last.data_option['options']).to eq(expected_data_options) + end + + it 'checks smart defaults for boolean field' do + page.find('.js-new').click + + fill_in 'Name', with: 'bool1' + find('input[name=display]').set('bool1') + + page.find('select[name=data_type]').select('Boolean') + page.find('.js-valueFalse').set('HELL NOO') + page.find('.js-submit').click + + expected_data_options = { + true => 'yes', + false => 'HELL NOO', + } + + expect(ObjectManager::Attribute.last.data_option['options']).to eq(expected_data_options) + end end