From b3f3ed8f43ebbbd1bffa5b1ed7c78c87f2e86789 Mon Sep 17 00:00:00 2001 From: Dombi Attila <83396+dombesz@users.noreply.github.com> Date: Thu, 19 Jan 2023 21:20:33 +0200 Subject: [PATCH] refactor: Extract customField1 json format into the CustomField#attribute_name method --- app/models/custom_field.rb | 4 +- app/views/customizable/_form.html.erb | 2 +- lib/api/v3/utilities/custom_field_injector.rb | 18 ++++----- .../v3/utilities/custom_field_sum_injector.rb | 4 +- modules/bim/spec/features/bcf/create_spec.rb | 10 ++--- .../custom_field_filters_spec.rb | 16 ++++---- .../time_entry_representer_parsing_spec.rb | 4 +- .../time_entries/create_form_resource_spec.rb | 2 +- .../time_entries/update_form_resource_spec.rb | 2 +- .../requests/api/time_entry_resource_spec.rb | 4 +- .../spec/features/project_details_spec.rb | 10 ++--- .../my/time_entries_current_user_spec.rb | 2 +- .../multi_user_custom_field_spec.rb | 2 +- .../multi_value_custom_field_spec.rb | 8 ++-- .../custom_fields/custom_field_spec.rb | 2 +- .../work_packages/details/date_editor_spec.rb | 2 +- .../relations/hierarchy_custom_fields_spec.rb | 2 +- .../details/relations/hierarchy_spec.rb | 2 +- .../work_packages/edit_work_package_spec.rb | 2 +- .../new/new_work_package_spec.rb | 6 +-- .../table/edit_work_packages_spec.rb | 4 +- .../create_work_packages_spec.rb | 4 +- .../table/queries/filter_spec.rb | 10 ++--- .../table/queries/user_cf_filter_spec.rb | 10 ++--- .../work_packages/table/switch_types_spec.rb | 10 ++--- .../filters/query_filter_representer_spec.rb | 2 +- ...filter_instance_schema_representer_spec.rb | 2 +- .../utilities/custom_field_injector_spec.rb | 2 +- .../version_schema_representer_spec.rb | 2 +- spec/models/custom_field_spec.rb | 38 ++++++++++--------- .../projects/copy/copy_form_resource_spec.rb | 4 +- .../v3/projects/copy/copy_resource_spec.rb | 2 +- .../v3/projects/create_form_resource_spec.rb | 4 +- .../api/v3/projects/create_resource_spec.rb | 2 +- .../v3/projects/update_form_resource_spec.rb | 4 +- .../api/v3/projects/update_resource_spec.rb | 4 +- .../api/v3/queries/create_form_api_spec.rb | 2 +- .../filters/query_filters_resource_spec.rb | 2 +- .../api/v3/user/create_form_resource_spec.rb | 4 +- .../v3/versions/create_form_resource_spec.rb | 4 +- .../v3/versions/update_form_resource_spec.rb | 4 +- .../form/work_package_form_resource_spec.rb | 2 +- .../v3/work_packages/update_resource_spec.rb | 4 +- spec/support/form_fields/form_field.rb | 2 +- .../work_packages/abstract_work_package.rb | 2 +- 45 files changed, 120 insertions(+), 114 deletions(-) diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb index 54cd5d98d9..ad5571159c 100644 --- a/app/models/custom_field.rb +++ b/app/models/custom_field.rb @@ -232,7 +232,9 @@ class CustomField < ApplicationRecord where(is_filter: true) end - def attribute_name + def attribute_name(format = nil) + return "customField#{id}" if format == :camel_case + "custom_field_#{id}" end diff --git a/app/views/customizable/_form.html.erb b/app/views/customizable/_form.html.erb index cb357bc845..9f15c092b7 100644 --- a/app/views/customizable/_form.html.erb +++ b/app/views/customizable/_form.html.erb @@ -53,7 +53,7 @@ See COPYRIGHT and LICENSE files for more details. } if help_text - options[:help_text] = help_text.merge(attribute: "customField#{custom_field.id}") + options[:help_text] = help_text.merge(attribute: custom_field.attribute_name(:camel_case)) end cf_form.cf_form_field(options) diff --git a/lib/api/v3/utilities/custom_field_injector.rb b/lib/api/v3/utilities/custom_field_injector.rb index de76cab693..4895ff7d7d 100644 --- a/lib/api/v3/utilities/custom_field_injector.rb +++ b/lib/api/v3/utilities/custom_field_injector.rb @@ -127,12 +127,12 @@ module API private - def property_name(id) - "customField#{id}".to_sym + def property_name(custom_field) + custom_field.attribute_name(:camel_case).to_sym end def inject_version_schema(custom_field) - @class.schema_with_allowed_collection property_name(custom_field.id), + @class.schema_with_allowed_collection property_name(custom_field), type: resource_type(custom_field), name_source: ->(*) { custom_field.name }, values_callback: ->(*) { @@ -150,7 +150,7 @@ module API end def inject_user_schema(custom_field) - @class.schema_with_allowed_link property_name(custom_field.id), + @class.schema_with_allowed_link property_name(custom_field), type: resource_type(custom_field), name_source: ->(*) { custom_field.name }, required: custom_field.is_required, @@ -159,7 +159,7 @@ module API def inject_list_schema(custom_field) @class.schema_with_allowed_collection( - property_name(custom_field.id), + property_name(custom_field), type: resource_type(custom_field), name_source: ->(*) { custom_field.name }, values_callback: list_schemas_values_callback(custom_field), @@ -170,7 +170,7 @@ module API end def inject_basic_schema(custom_field) - @class.schema property_name(custom_field.id), + @class.schema property_name(custom_field), type: resource_type(custom_field), name_source: ->(*) { custom_field.name }, required: custom_field.is_required, @@ -182,7 +182,7 @@ module API end def inject_link_value(custom_field) - name = property_name(custom_field.id) + name = property_name(custom_field) expected_namespace = NAMESPACE_MAP[custom_field.field_format] link = LinkValueGetter.link_for custom_field @@ -196,7 +196,7 @@ module API end @class.send(method, - property_name(custom_field.id), + property_name(custom_field), link:, setter:, getter:) @@ -244,7 +244,7 @@ module API def inject_property_value(custom_field) @class.property custom_field.attribute_name.to_sym, - as: property_name(custom_field.id), + as: property_name(custom_field), getter: property_value_getter_for(custom_field), setter: property_value_setter_for(custom_field), render_nil: true diff --git a/lib/api/v3/utilities/custom_field_sum_injector.rb b/lib/api/v3/utilities/custom_field_sum_injector.rb index d5c1addd47..5b3c94fe24 100644 --- a/lib/api/v3/utilities/custom_field_sum_injector.rb +++ b/lib/api/v3/utilities/custom_field_sum_injector.rb @@ -35,7 +35,7 @@ module API end def inject_basic_schema(custom_field) - @class.schema property_name(custom_field.id), + @class.schema property_name(custom_field), type: TYPE_MAP[custom_field.field_format], name_source: ->(*) { custom_field.name }, required: false, @@ -44,7 +44,7 @@ module API end def inject_property_value(custom_field) - @class.property property_name(custom_field.id), + @class.property property_name(custom_field), getter: property_value_getter_for(custom_field), setter: property_value_setter_for(custom_field), render_nil: true, diff --git a/modules/bim/spec/features/bcf/create_spec.rb b/modules/bim/spec/features/bcf/create_spec.rb index d1b349085c..7bf4a65e90 100644 --- a/modules/bim/spec/features/bcf/create_spec.rb +++ b/modules/bim/spec/features/bcf/create_spec.rb @@ -13,12 +13,12 @@ describe 'Create BCF', let(:index_page) { Pages::IfcModels::ShowDefault.new(project) } let(:permissions) { %i[view_ifc_models view_linked_issues manage_bcf add_work_packages edit_work_packages view_work_packages] } let!(:status) { create(:default_status) } - let!(:priority) { create :priority, is_default: true } + let!(:priority) { create(:priority, is_default: true) } let(:user) do - create :user, + create(:user, member_in_project: project, - member_with_permissions: permissions + member_with_permissions: permissions) end let!(:model) do @@ -67,7 +67,7 @@ describe 'Create BCF', type_field.activate! type_field.set_value type_with_cf.name - cf_field = create_page.edit_field(:"customField#{integer_cf.id}") + cf_field = create_page.edit_field(integer_cf.attribute_name(:camel_case).to_sym) cf_field.set_value(815) create_page.save! @@ -141,7 +141,7 @@ describe 'Create BCF', end context 'when starting on the details page of an existing work package' do - let(:work_package) { create :work_package, project: } + let(:work_package) { create(:work_package, project:) } before do visit bcf_project_frontend_path(project, "details/#{work_package.id}") diff --git a/modules/boards/spec/features/action_boards/custom_field_filters_spec.rb b/modules/boards/spec/features/action_boards/custom_field_filters_spec.rb index 10aabd512d..8459bf6670 100644 --- a/modules/boards/spec/features/action_boards/custom_field_filters_spec.rb +++ b/modules/boards/spec/features/action_boards/custom_field_filters_spec.rb @@ -47,16 +47,16 @@ describe 'Custom field filter in boards', js: true do edit_work_packages view_work_packages manage_public_queries] end - let!(:priority) { create :default_priority } - let!(:open_status) { create :default_status, name: 'Open' } - let!(:closed_status) { create :status, is_closed: true, name: 'Closed' } + let!(:priority) { create(:default_priority) } + let!(:open_status) { create(:default_status, name: 'Open') } + let!(:closed_status) { create(:status, is_closed: true, name: 'Closed') } let!(:work_package) do - wp = build :work_package, - project: project, - type: type, + wp = build(:work_package, + project:, + type:, subject: 'Foo', - status: open_status + status: open_status) wp.custom_field_values = { custom_field.id => %w[B].map { |s| custom_value_for(s) } @@ -105,7 +105,7 @@ describe 'Custom field filter in boards', js: true do filters.add_filter_by(custom_field.name, 'is', ['A', 'B'], - "customField#{custom_field.id}") + custom_field.attribute_name(:camel_case)) board_page.expect_changed diff --git a/modules/costs/spec/lib/api/v3/time_entries/time_entry_representer_parsing_spec.rb b/modules/costs/spec/lib/api/v3/time_entries/time_entry_representer_parsing_spec.rb index d2c4e155dd..6a33b3be29 100644 --- a/modules/costs/spec/lib/api/v3/time_entries/time_entry_representer_parsing_spec.rb +++ b/modules/costs/spec/lib/api/v3/time_entries/time_entry_representer_parsing_spec.rb @@ -72,7 +72,7 @@ describe API::V3::TimeEntries::TimeEntryRepresenter, 'parsing' do "href" => api_v3_paths.work_package(work_package2.id) }, - "customField#{user_custom_field.id}" => { + user_custom_field.attribute_name(:camel_case) => { "href" => api_v3_paths.user(user2.id) } }, @@ -81,7 +81,7 @@ describe API::V3::TimeEntries::TimeEntryRepresenter, 'parsing' do "raw" => "some comment" }, "spentOn" => "2017-07-28", - "customField#{test_custom_field.id}" => { + test_custom_field.attribute_name(:camel_case) => { "raw" => "some text" } } diff --git a/modules/costs/spec/requests/api/time_entries/create_form_resource_spec.rb b/modules/costs/spec/requests/api/time_entries/create_form_resource_spec.rb index 4049f8a302..191c39fa50 100644 --- a/modules/costs/spec/requests/api/time_entries/create_form_resource_spec.rb +++ b/modules/costs/spec/requests/api/time_entries/create_form_resource_spec.rb @@ -125,7 +125,7 @@ describe API::V3::TimeEntries::CreateFormAPI, content_type: :json do comment: { raw: "some comment" }, - "customField#{custom_field.id}": { + custom_field.attribute_name(:camel_case) => { raw: 'some cf text' } } diff --git a/modules/costs/spec/requests/api/time_entries/update_form_resource_spec.rb b/modules/costs/spec/requests/api/time_entries/update_form_resource_spec.rb index 247b85517d..923ab34400 100644 --- a/modules/costs/spec/requests/api/time_entries/update_form_resource_spec.rb +++ b/modules/costs/spec/requests/api/time_entries/update_form_resource_spec.rb @@ -93,7 +93,7 @@ describe API::V3::TimeEntries::UpdateFormAPI, content_type: :json do comment: { raw: "some comment" }, - "customField#{custom_field.id}": { + custom_field.attribute_name(:camel_case) => { raw: 'some cf text' } } diff --git a/modules/costs/spec/requests/api/time_entry_resource_spec.rb b/modules/costs/spec/requests/api/time_entry_resource_spec.rb index 55836543f8..e8c7558ade 100644 --- a/modules/costs/spec/requests/api/time_entry_resource_spec.rb +++ b/modules/costs/spec/requests/api/time_entry_resource_spec.rb @@ -392,7 +392,7 @@ describe 'API v3 time_entry resource' do raw: "some comment" }, spentOn: "2017-07-28", - "customField#{custom_field.id}": { + custom_field.attribute_name(:camel_case) => { raw: 'some cf text' } } @@ -485,7 +485,7 @@ describe 'API v3 time_entry resource' do hours: 'PT5H', comment: "some comment", spentOn: "2017-07-28", - "customField#{custom_field.id}": { + custom_field.attribute_name(:camel_case) => { raw: 'some cf text' } } diff --git a/modules/dashboards/spec/features/project_details_spec.rb b/modules/dashboards/spec/features/project_details_spec.rb index 8e8cb2c304..e9a1ce85a0 100644 --- a/modules/dashboards/spec/features/project_details_spec.rb +++ b/modules/dashboards/spec/features/project_details_spec.rb @@ -147,7 +147,7 @@ describe 'Project details widget on dashboard', js: true do .to have_content("#{user_cf.name}\n#{other_user.name.split.map(&:first).join}\n#{other_user.name}") # The fields are not editable - field = EditField.new dashboard_page, "customField#{bool_cf.id}" + field = EditField.new dashboard_page, bool_cf.attribute_name(:camel_case) field.expect_read_only field.activate! expect_open: false end @@ -158,16 +158,16 @@ describe 'Project details widget on dashboard', js: true do let(:current_user) { editing_user } it 'can edit the custom fields' do - int_field = EditField.new dashboard_page, "customField#{int_cf.id}" + int_field = EditField.new dashboard_page, int_cf.attribute_name(:camel_case) change_cf_value int_field, "5", "3" - string_field = EditField.new dashboard_page, "customField#{string_cf.id}" + string_field = EditField.new dashboard_page, string_cf.attribute_name(:camel_case) change_cf_value string_field, 'Some small text', 'Some new text' - text_field = TextEditorField.new dashboard_page, "customField#{text_cf.id}" + text_field = TextEditorField.new dashboard_page, text_cf.attribute_name(:camel_case) change_cf_value text_field, 'Some long text', 'Some very long text' - user_field = SelectField.new dashboard_page, "customField#{user_cf.id}" + user_field = SelectField.new dashboard_page, user_cf.attribute_name(:camel_case) change_cf_value user_field, other_user.name, editing_user.name end end diff --git a/modules/my_page/spec/features/my/time_entries_current_user_spec.rb b/modules/my_page/spec/features/my/time_entries_current_user_spec.rb index abf93648cc..d75df2503b 100644 --- a/modules/my_page/spec/features/my/time_entries_current_user_spec.rb +++ b/modules/my_page/spec/features/my/time_entries_current_user_spec.rb @@ -111,7 +111,7 @@ describe 'My page time entries current user widget spec', js: true, with_mail: f let(:my_page) do Pages::My::Page.new end - let(:cf_field) { TextEditorField.new(page, "customField#{custom_field.id}") } + let(:cf_field) { TextEditorField.new(page, custom_field.attribute_name(:camel_case)) } let(:time_logging_modal) { Components::TimeLoggingModal.new } before do diff --git a/spec/features/custom_fields/multi_user_custom_field_spec.rb b/spec/features/custom_fields/multi_user_custom_field_spec.rb index b390e7186f..047ced2f20 100644 --- a/spec/features/custom_fields/multi_user_custom_field_spec.rb +++ b/spec/features/custom_fields/multi_user_custom_field_spec.rb @@ -6,7 +6,7 @@ describe "multi select custom values", js: true do let(:current_user) { admin } let(:wp_page) { Pages::FullWorkPackage.new work_package } let(:cf_edit_field) do - field = wp_page.edit_field "customField#{custom_field.id}" + field = wp_page.edit_field custom_field.attribute_name(:camel_case) field.field_type = 'create-autocompleter' field end diff --git a/spec/features/custom_fields/multi_value_custom_field_spec.rb b/spec/features/custom_fields/multi_value_custom_field_spec.rb index 56b6af36d9..176930578b 100644 --- a/spec/features/custom_fields/multi_value_custom_field_spec.rb +++ b/spec/features/custom_fields/multi_value_custom_field_spec.rb @@ -10,7 +10,7 @@ describe "multi select custom values", js: true do let(:group_by) { Components::WorkPackages::GroupBy.new } let(:sort_by) { Components::WorkPackages::SortBy.new } let(:user) { create :admin } - let(:cf_frontend) { "customField#{custom_field.id}" } + let(:cf_frontend) { custom_field.attribute_name(:camel_case) } let(:project) { create :project, types: [type] } let(:multi_value) { true } @@ -30,7 +30,7 @@ describe "multi select custom values", js: true do end def table_edit_field(work_package) - field = wp_table.edit_field work_package, "customField#{custom_field.id}" + field = wp_table.edit_field work_package, custom_field.attribute_name(:camel_case) field.field_type = 'create-autocompleter' field end @@ -62,7 +62,7 @@ describe "multi select custom values", js: true do describe 'in single view' do let(:edit_field) do - field = wp_page.edit_field "customField#{custom_field.id}" + field = wp_page.edit_field custom_field.attribute_name(:camel_case) field.field_type = 'create-autocompleter' field end @@ -147,7 +147,7 @@ describe "multi select custom values", js: true do # Open split view split_view = wp_table.open_split_view work_package - field = SelectField.new(split_view.container, "customField#{custom_field.id}") + field = SelectField.new(split_view.container, custom_field.attribute_name(:camel_case)) field.activate! field.unset_value "ham", multi: true diff --git a/spec/features/work_packages/details/custom_fields/custom_field_spec.rb b/spec/features/work_packages/details/custom_fields/custom_field_spec.rb index 3a1da5a7de..45ba8fb433 100644 --- a/spec/features/work_packages/details/custom_fields/custom_field_spec.rb +++ b/spec/features/work_packages/details/custom_fields/custom_field_spec.rb @@ -19,7 +19,7 @@ describe 'custom field inplace editor', js: true do end let(:wp_page) { Pages::SplitWorkPackage.new(work_package) } - let(:property_name) { "customField#{custom_field.id}" } + let(:property_name) { custom_field.attribute_name(:camel_case) } let(:field) { wp_page.edit_field(property_name) } before do diff --git a/spec/features/work_packages/details/date_editor_spec.rb b/spec/features/work_packages/details/date_editor_spec.rb index 995d84e87d..cc30a5e2ce 100644 --- a/spec/features/work_packages/details/date_editor_spec.rb +++ b/spec/features/work_packages/details/date_editor_spec.rb @@ -276,7 +276,7 @@ describe 'date inplace editor', ) end - let(:cf_field) { EditField.new page, :"customField#{date_cf.id}" } + let(:cf_field) { EditField.new page, date_cf.attribute_name(:camel_case) } let(:datepicker) { Components::Datepicker.new } let(:create_page) { Pages::FullWorkPackageCreate.new(project:) } diff --git a/spec/features/work_packages/details/relations/hierarchy_custom_fields_spec.rb b/spec/features/work_packages/details/relations/hierarchy_custom_fields_spec.rb index 4bc58a61db..bd60237c1c 100644 --- a/spec/features/work_packages/details/relations/hierarchy_custom_fields_spec.rb +++ b/spec/features/work_packages/details/relations/hierarchy_custom_fields_spec.rb @@ -56,7 +56,7 @@ describe 'creating a child directly after the wp itself was created', js: true d subject.set_value 'My subject' # Set CF - cf = wp_page.edit_field "customField#{custom_field.id}" + cf = wp_page.edit_field custom_field.attribute_name(:camel_case) cf.set_value '42' # Save WP diff --git a/spec/features/work_packages/details/relations/hierarchy_spec.rb b/spec/features/work_packages/details/relations/hierarchy_spec.rb index 0780376938..acd94df975 100644 --- a/spec/features/work_packages/details/relations/hierarchy_spec.rb +++ b/spec/features/work_packages/details/relations/hierarchy_spec.rb @@ -120,7 +120,7 @@ shared_examples 'work package relations tab', js: true, selenium: true do wp_page.expect_toast message: "#{custom_field.name} can't be blank.", type: 'error' - cf_field = wp_page.edit_field("customField#{custom_field.id}") + cf_field = wp_page.edit_field(custom_field.attribute_name(:camel_case)) cf_field.expect_active! cf_field.expect_value('') diff --git a/spec/features/work_packages/edit_work_package_spec.rb b/spec/features/work_packages/edit_work_package_spec.rb index 4b58bf9e74..d4eba6bdf9 100644 --- a/spec/features/work_packages/edit_work_package_spec.rb +++ b/spec/features/work_packages/edit_work_package_spec.rb @@ -219,7 +219,7 @@ describe 'edit work package', wp_page.expect_toast message: "#{custom_field.name} can't be blank.", type: 'error' - cf_field = wp_page.edit_field("customField#{custom_field.id}") + cf_field = wp_page.edit_field(custom_field.attribute_name(:camel_case)) cf_field.expect_active! cf_field.expect_value('') end diff --git a/spec/features/work_packages/new/new_work_package_spec.rb b/spec/features/work_packages/new/new_work_package_spec.rb index ff6fa0811c..9dc8ecba7e 100644 --- a/spec/features/work_packages/new/new_work_package_spec.rb +++ b/spec/features/work_packages/new/new_work_package_spec.rb @@ -193,12 +193,12 @@ describe 'new work package', js: true do it do ids = custom_fields.map(&:id) - cf1 = find(".customField#{ids.first} input") + cf1 = find(".#{custom_fields.first.attribute_name(:camel_case)} input") expect(cf1).not_to be_nil - expect(page).to have_selector(".customField#{ids.last} ng-select") + expect(page).to have_selector(".#{custom_fields.last.attribute_name(:camel_case)} ng-select") - cf = wp_page.edit_field "customField#{ids.last}" + cf = wp_page.edit_field custom_fields.last.attribute_name(:camel_case) cf.field_type = 'create-autocompleter' cf.openSelectField cf.set_value 'foo' diff --git a/spec/features/work_packages/table/edit_work_packages_spec.rb b/spec/features/work_packages/table/edit_work_packages_spec.rb index c070ad9cbb..099bb04252 100644 --- a/spec/features/work_packages/table/edit_work_packages_spec.rb +++ b/spec/features/work_packages/table/edit_work_packages_spec.rb @@ -177,10 +177,10 @@ describe 'Inline editing work packages', js: true do expect(page).to have_selector('th a', text: cf_text_name.upcase) expect(wp_table.row(work_package)).to have_selector('.wp-table--cell-container.-error', count: 2) - cf_text = wp_table.edit_field(work_package, "customField#{custom_fields.last.id}") + cf_text = wp_table.edit_field(work_package, custom_fields.last.attribute_name(:camel_case)) cf_text.update('my custom text', expect_failure: true) - cf_list = wp_table.edit_field(work_package, "customField#{custom_fields.first.id}") + cf_list = wp_table.edit_field(work_package, custom_fields.first.attribute_name(:camel_case)) cf_list.field_type = 'create-autocompleter' cf_list.openSelectField cf_list.set_value('bar') diff --git a/spec/features/work_packages/table/inline_create/create_work_packages_spec.rb b/spec/features/work_packages/table/inline_create/create_work_packages_spec.rb index e46ed1e5dd..cf5bb0ce90 100644 --- a/spec/features/work_packages/table/inline_create/create_work_packages_spec.rb +++ b/spec/features/work_packages/table/inline_create/create_work_packages_spec.rb @@ -94,7 +94,7 @@ describe 'inline create work package', js: true do let(:cf_list) do create(:list_wp_custom_field, is_for_all: true, is_filter: true) end - let(:cf_accessor_frontend) { "customField#{cf_list.id}" } + let(:cf_accessor_frontend) { cf_list.attribute_name(:camel_case) } let(:types) { [type, cf_type] } let(:type) { create(:type_standard) } let(:cf_type) { create(:type, custom_fields: [cf_list]) } @@ -136,7 +136,7 @@ describe 'inline create work package', js: true do created_wp = WorkPackage.last - cf_field = wp_table.edit_field(created_wp, :"customField#{cf_list.id}") + cf_field = wp_table.edit_field(created_wp, cf_list.attribute_name(:camel_case)) cf_field.expect_text(cf_list.custom_options.second.name) end end diff --git a/spec/features/work_packages/table/queries/filter_spec.rb b/spec/features/work_packages/table/queries/filter_spec.rb index 9e8bdee433..8eac540d7d 100644 --- a/spec/features/work_packages/table/queries/filter_spec.rb +++ b/spec/features/work_packages/table/queries/filter_spec.rb @@ -253,7 +253,7 @@ describe 'filter work packages', js: true do filters.add_filter_by(list_cf.name, 'is not', list_cf.custom_options.last.value, - "customField#{list_cf.id}") + list_cf.attribute_name(:camel_case)) loading_indicator_saveguard wp_table.expect_work_package_listed work_package_with_list_value @@ -261,7 +261,7 @@ describe 'filter work packages', js: true do wp_table.save_as('Some query name') - filters.remove_filter "customField#{list_cf.id}" + filters.remove_filter list_cf.attribute_name(:camel_case) loading_indicator_saveguard wp_table.expect_work_package_listed work_package_with_list_value, work_package_with_anti_list_value @@ -331,7 +331,7 @@ describe 'filter work packages', js: true do filters.add_filter_by(string_cf.name, 'is', ['G+H'], - "customField#{string_cf.id}") + string_cf.attribute_name(:camel_case)) loading_indicator_saveguard wp_table.expect_work_package_listed work_package_plus @@ -339,7 +339,7 @@ describe 'filter work packages', js: true do wp_table.save_as('Some query name') - filters.remove_filter "customField#{string_cf.id}" + filters.remove_filter string_cf.attribute_name(:camel_case) loading_indicator_saveguard wp_table.expect_work_package_listed work_package_plus, work_package_and @@ -362,7 +362,7 @@ describe 'filter work packages', js: true do filters.set_filter(string_cf, 'is', ['A&B'], - "customField#{string_cf.id}") + string_cf.attribute_name(:camel_case)) loading_indicator_saveguard wp_table.expect_work_package_listed work_package_and diff --git a/spec/features/work_packages/table/queries/user_cf_filter_spec.rb b/spec/features/work_packages/table/queries/user_cf_filter_spec.rb index ccffbac489..acf4a6b297 100644 --- a/spec/features/work_packages/table/queries/user_cf_filter_spec.rb +++ b/spec/features/work_packages/table/queries/user_cf_filter_spec.rb @@ -97,7 +97,7 @@ describe 'Work package filtering by user custom field', js: true do # Filtering by user - filters.add_filter_by(user_cf.name, 'is', [other_user.name], "customField#{user_cf.id}") + filters.add_filter_by(user_cf.name, 'is', [other_user.name], user_cf.attribute_name(:camel_case)) wp_table.ensure_work_package_not_listed!(work_package_placeholder, work_package_group) wp_table.expect_work_package_listed(work_package_user) @@ -116,16 +116,16 @@ describe 'Work package filtering by user custom field', js: true do # Filtering by placeholder - filters.remove_filter "customField#{user_cf.id}" - filters.add_filter_by(user_cf.name, 'is', [placeholder_user.name], "customField#{user_cf.id}") + filters.remove_filter user_cf.attribute_name(:camel_case) + filters.add_filter_by(user_cf.name, 'is', [placeholder_user.name], user_cf.attribute_name(:camel_case)) wp_table.ensure_work_package_not_listed!(work_package_user, work_package_group) wp_table.expect_work_package_listed(work_package_placeholder) # Filtering by group - filters.remove_filter "customField#{user_cf.id}" - filters.add_filter_by(user_cf.name, 'is', [group.name], "customField#{user_cf.id}") + filters.remove_filter user_cf.attribute_name(:camel_case) + filters.add_filter_by(user_cf.name, 'is', [group.name], user_cf.attribute_name(:camel_case)) wp_table.ensure_work_package_not_listed!(work_package_user, work_package_placeholder) wp_table.expect_work_package_listed(work_package_group) diff --git a/spec/features/work_packages/table/switch_types_spec.rb b/spec/features/work_packages/table/switch_types_spec.rb index 9674a8ed63..e8a82c5ad3 100644 --- a/spec/features/work_packages/table/switch_types_spec.rb +++ b/spec/features/work_packages/table/switch_types_spec.rb @@ -49,8 +49,8 @@ describe 'Switching types in work package table', js: true do end let(:type_field) { wp_table.edit_field(work_package, :type) } - let(:text_field) { wp_table.edit_field(work_package, :"customField#{cf_text.id}") } - let(:req_text_field) { wp_table.edit_field(work_package, :"customField#{cf_req_text.id}") } + let(:text_field) { wp_table.edit_field(work_package, cf_text.attribute_name(:camel_case)) } + let(:req_text_field) { wp_table.edit_field(work_package, cf_req_text.attribute_name(:camel_case)) } before do login_as(user) @@ -163,8 +163,8 @@ describe 'Switching types in work package table', js: true do context 'switching to single view' do let(:wp_split) { wp_table.open_split_view(work_package) } let(:type_field) { wp_split.edit_field(:type) } - let(:text_field) { wp_split.edit_field(:"customField#{cf_text.id}") } - let(:req_text_field) { wp_split.edit_field(:"customField#{cf_req_text.id}") } + let(:text_field) { wp_split.edit_field(cf_text.attribute_name(:camel_case)) } + let(:req_text_field) { wp_split.edit_field(cf_req_text.attribute_name(:camel_case)) } it 'allows editing and cancelling the new required fields' do wp_split @@ -301,7 +301,7 @@ describe 'Switching types in work package table', js: true do let!(:priority) { create :priority, is_default: true } let(:cf_edit_field) do - field = wp_page.edit_field "customField#{custom_field.id}" + field = wp_page.edit_field custom_field.attribute_name(:camel_case) field.field_type = 'create-autocompleter' field end diff --git a/spec/lib/api/v3/queries/filters/query_filter_representer_spec.rb b/spec/lib/api/v3/queries/filters/query_filter_representer_spec.rb index dcb2921be4..5766fea97d 100644 --- a/spec/lib/api/v3/queries/filters/query_filter_representer_spec.rb +++ b/spec/lib/api/v3/queries/filters/query_filter_representer_spec.rb @@ -93,7 +93,7 @@ describe API::V3::Queries::Filters::QueryFilterRepresenter do describe '_links' do it_behaves_like 'has a titled link' do let(:link) { 'self' } - let(:href) { api_v3_paths.query_filter "customField#{custom_field.id}" } + let(:href) { api_v3_paths.query_filter custom_field.attribute_name(:camel_case) } let(:title) { custom_field.name } end end diff --git a/spec/lib/api/v3/queries/schemas/query_filter_instance_schema_representer_spec.rb b/spec/lib/api/v3/queries/schemas/query_filter_instance_schema_representer_spec.rb index c448f4c168..6d384426b9 100644 --- a/spec/lib/api/v3/queries/schemas/query_filter_instance_schema_representer_spec.rb +++ b/spec/lib/api/v3/queries/schemas/query_filter_instance_schema_representer_spec.rb @@ -131,7 +131,7 @@ describe API::V3::Queries::Schemas::QueryFilterInstanceSchemaRepresenter do let(:filter) { custom_field_filter } it_behaves_like 'links to and embeds allowed values directly' do - let(:hrefs) { [api_v3_paths.query_filter("customField#{custom_field.id}")] } + let(:hrefs) { [api_v3_paths.query_filter(custom_field.attribute_name(:camel_case))] } end end end diff --git a/spec/lib/api/v3/utilities/custom_field_injector_spec.rb b/spec/lib/api/v3/utilities/custom_field_injector_spec.rb index 87819a9426..d06afe906e 100644 --- a/spec/lib/api/v3/utilities/custom_field_injector_spec.rb +++ b/spec/lib/api/v3/utilities/custom_field_injector_spec.rb @@ -31,7 +31,7 @@ require 'spec_helper' describe API::V3::Utilities::CustomFieldInjector do include API::V3::Utilities::PathHelper - let(:cf_path) { "customField#{custom_field.id}" } + let(:cf_path) { custom_field.attribute_name(:camel_case) } let(:field_format) { 'bool' } let(:custom_field) do build(:custom_field, diff --git a/spec/lib/api/v3/versions/schemas/version_schema_representer_spec.rb b/spec/lib/api/v3/versions/schemas/version_schema_representer_spec.rb index 037afe95ce..07df4236b7 100644 --- a/spec/lib/api/v3/versions/schemas/version_schema_representer_spec.rb +++ b/spec/lib/api/v3/versions/schemas/version_schema_representer_spec.rb @@ -152,7 +152,7 @@ describe API::V3::Versions::Schemas::VersionSchemaRepresenter do end describe 'int custom field' do - let(:path) { "customField#{custom_field.id}" } + let(:path) { custom_field.attribute_name(:camel_case) } it_behaves_like 'has basic schema properties' do let(:type) { 'Integer' } diff --git a/spec/models/custom_field_spec.rb b/spec/models/custom_field_spec.rb index a962b6845a..f4a9557873 100644 --- a/spec/models/custom_field_spec.rb +++ b/spec/models/custom_field_spec.rb @@ -30,11 +30,11 @@ require 'spec_helper' describe CustomField do before do - CustomField.destroy_all + described_class.destroy_all end - let(:field) { build :custom_field } - let(:field2) { build :custom_field } + let(:field) { build(:custom_field) } + let(:field2) { build(:custom_field) } describe '#name' do it { is_expected.to validate_presence_of(:name) } @@ -189,6 +189,12 @@ describe CustomField do subject { field.attribute_name } it { is_expected.to eq("custom_field_#{field.id}") } + + context 'when a format is provided' do + subject { field.attribute_name(:camel_case) } + + it { is_expected.to eq("customField#{field.id}") } + end end describe '#attribute_getter' do @@ -242,7 +248,7 @@ describe CustomField do context 'for something that responds to project' do it 'is a list of name, id pairs' do - object = OpenStruct.new project: project + object = OpenStruct.new(project:) expect(field.possible_values_options(object)) .to match_array [[user1.name, user1.id.to_s], @@ -321,7 +327,7 @@ describe CustomField do describe '#possible_values' do context 'on a list custom field' do - let(:field) { CustomField.new field_format: "list" } + let(:field) { described_class.new field_format: "list" } context 'on providing an array' do before do @@ -361,7 +367,7 @@ describe CustomField do describe 'nested attributes for custom options' do let(:option) { build(:custom_option) } let(:options) { [option] } - let(:field) { build :custom_field, field_format: 'list', custom_options: options } + let(:field) { build(:custom_field, field_format: 'list', custom_options: options) } before do field.save! @@ -398,7 +404,7 @@ describe CustomField do describe '#multi_value_possible?' do context 'with a wp list cf' do - let(:field) { build_stubbed :list_wp_custom_field } + let(:field) { build_stubbed(:list_wp_custom_field) } it 'is true' do expect(field) @@ -407,7 +413,7 @@ describe CustomField do end context 'with a wp user cf' do - let(:field) { build_stubbed :user_wp_custom_field } + let(:field) { build_stubbed(:user_wp_custom_field) } it 'is true' do expect(field) @@ -416,7 +422,7 @@ describe CustomField do end context 'with a wp int cf' do - let(:field) { build_stubbed :int_wp_custom_field } + let(:field) { build_stubbed(:int_wp_custom_field) } it 'is true' do expect(field) @@ -425,7 +431,7 @@ describe CustomField do end context 'with a project list cf' do - let(:field) { build_stubbed :list_project_custom_field } + let(:field) { build_stubbed(:list_project_custom_field) } it 'is true' do expect(field) @@ -434,7 +440,7 @@ describe CustomField do end context 'with a project user cf' do - let(:field) { build_stubbed :user_project_custom_field } + let(:field) { build_stubbed(:user_project_custom_field) } it 'is true' do expect(field) @@ -443,7 +449,7 @@ describe CustomField do end context 'with a project int cf' do - let(:field) { build_stubbed :int_project_custom_field } + let(:field) { build_stubbed(:int_project_custom_field) } it 'is true' do expect(field) @@ -452,7 +458,7 @@ describe CustomField do end context 'with a time_entry user cf' do - let(:field) { build_stubbed :time_entry_custom_field, field_format: 'user' } + let(:field) { build_stubbed(:time_entry_custom_field, field_format: 'user') } it 'is true' do expect(field) @@ -461,7 +467,7 @@ describe CustomField do end context 'with a time_entry list cf' do - let(:field) { build_stubbed :time_entry_custom_field, field_format: 'list' } + let(:field) { build_stubbed(:time_entry_custom_field, field_format: 'list') } it 'is true' do expect(field) @@ -475,9 +481,7 @@ describe CustomField do field.save! field.destroy - - expect(CustomField.where(id: field.id).exists?) - .to be_falsey + expect(described_class.where(id: field.id)).not_to exist end end end diff --git a/spec/requests/api/v3/projects/copy/copy_form_resource_spec.rb b/spec/requests/api/v3/projects/copy/copy_form_resource_spec.rb index 30f776ef0b..e8c0213173 100644 --- a/spec/requests/api/v3/projects/copy/copy_form_resource_spec.rb +++ b/spec/requests/api/v3/projects/copy/copy_form_resource_spec.rb @@ -112,12 +112,12 @@ describe API::V3::Projects::Copy::CreateFormAPI, content_type: :json do { name: 'My copied project', identifier: 'foobar', - "customField#{text_custom_field.id}": { + text_custom_field.attribute_name(:camel_case) => { raw: "CF text" }, statusExplanation: { raw: "A magic dwells in each beginning." }, _links: { - "customField#{list_custom_field.id}": { + list_custom_field.attribute_name(:camel_case) => { href: api_v3_paths.custom_option(list_custom_field.custom_options.first.id) }, status: { diff --git a/spec/requests/api/v3/projects/copy/copy_resource_spec.rb b/spec/requests/api/v3/projects/copy/copy_resource_spec.rb index ebd3ef239e..7000aae30a 100644 --- a/spec/requests/api/v3/projects/copy/copy_resource_spec.rb +++ b/spec/requests/api/v3/projects/copy/copy_resource_spec.rb @@ -90,7 +90,7 @@ describe API::V3::Projects::Copy::CopyAPI, content_type: :json do let(:params) do { name: 'My copied project', identifier: 'my-copied-project', - "customField#{text_custom_field.id}": { + text_custom_field.attribute_name(:camel_case) => { raw: "CF text" } } end diff --git a/spec/requests/api/v3/projects/create_form_resource_spec.rb b/spec/requests/api/v3/projects/create_form_resource_spec.rb index 796f30357d..c9e473eaff 100644 --- a/spec/requests/api/v3/projects/create_form_resource_spec.rb +++ b/spec/requests/api/v3/projects/create_form_resource_spec.rb @@ -91,12 +91,12 @@ describe API::V3::Projects::CreateFormAPI, content_type: :json do { identifier: 'new_project_identifier', name: 'Project name', - "customField#{text_custom_field.id}": { + text_custom_field.attribute_name(:camel_case) => { raw: "CF text" }, statusExplanation: { raw: "A magic dwells in each beginning." }, _links: { - "customField#{list_custom_field.id}": { + list_custom_field.attribute_name(:camel_case) => { href: api_v3_paths.custom_option(list_custom_field.custom_options.first.id) }, status: { diff --git a/spec/requests/api/v3/projects/create_resource_spec.rb b/spec/requests/api/v3/projects/create_resource_spec.rb index c859b2cd81..86df9c40db 100644 --- a/spec/requests/api/v3/projects/create_resource_spec.rb +++ b/spec/requests/api/v3/projects/create_resource_spec.rb @@ -121,7 +121,7 @@ describe 'API v3 Project resource create', content_type: :json do { identifier: 'new_project_identifier', name: 'Project name', - "customField#{custom_field.id}": { + custom_field.attribute_name(:camel_case) => { raw: "CF text" } }.to_json diff --git a/spec/requests/api/v3/projects/update_form_resource_spec.rb b/spec/requests/api/v3/projects/update_form_resource_spec.rb index 643b6224d4..be960d7899 100644 --- a/spec/requests/api/v3/projects/update_form_resource_spec.rb +++ b/spec/requests/api/v3/projects/update_form_resource_spec.rb @@ -162,12 +162,12 @@ describe API::V3::Projects::UpdateFormAPI, content_type: :json do { identifier: 'new_project_identifier', name: 'Project name', - "customField#{text_custom_field.id}": { + text_custom_field.attribute_name(:camel_case) => { raw: "new CF text" }, statusExplanation: { raw: 'Something goes awry.' }, _links: { - "customField#{list_custom_field.id}": { + list_custom_field.attribute_name(:camel_case) => { href: api_v3_paths.custom_option(list_custom_field.custom_options.last.id) }, status: { diff --git a/spec/requests/api/v3/projects/update_resource_spec.rb b/spec/requests/api/v3/projects/update_resource_spec.rb index 70239ffcd7..039f248b32 100644 --- a/spec/requests/api/v3/projects/update_resource_spec.rb +++ b/spec/requests/api/v3/projects/update_resource_spec.rb @@ -94,7 +94,7 @@ describe 'API v3 Project resource update', content_type: :json do context 'with a custom field' do let(:body) do { - "customField#{custom_field.id}": { + custom_field.attribute_name(:camel_case) => { raw: "CF text" } } @@ -352,7 +352,7 @@ describe 'API v3 Project resource update', content_type: :json do let(:body) do { active: false, - "customField#{custom_field.id}": { + custom_field.attribute_name(:camel_case) => { raw: "CF text" } } diff --git a/spec/requests/api/v3/queries/create_form_api_spec.rb b/spec/requests/api/v3/queries/create_form_api_spec.rb index dfc17b41c1..c6e894471d 100644 --- a/spec/requests/api/v3/queries/create_form_api_spec.rb +++ b/spec/requests/api/v3/queries/create_form_api_spec.rb @@ -580,7 +580,7 @@ describe "POST /api/v3/queries/form" do let(:path_with_cf) do uri = Addressable::URI.parse(path) uri.query = { - filters: [{ "customField#{custom_field.id}": { operator: "=", values: ["ABC"] } }] + filters: [{ custom_field.attribute_name(:camel_case) => { operator: "=", values: ["ABC"] } }] }.to_query uri.to_s diff --git a/spec/requests/api/v3/queries/filters/query_filters_resource_spec.rb b/spec/requests/api/v3/queries/filters/query_filters_resource_spec.rb index e3558b7344..ed93295b85 100644 --- a/spec/requests/api/v3/queries/filters/query_filters_resource_spec.rb +++ b/spec/requests/api/v3/queries/filters/query_filters_resource_spec.rb @@ -81,7 +81,7 @@ describe 'API v3 Query Filter resource' do context 'custom field filter' do let(:list_wp_custom_field) { create(:list_wp_custom_field) } - let(:filter_name) { "customField#{list_wp_custom_field.id}" } + let(:filter_name) { list_wp_custom_field.attribute_name(:camel_case) } it 'succeeds' do expect(last_response.status) diff --git a/spec/requests/api/v3/user/create_form_resource_spec.rb b/spec/requests/api/v3/user/create_form_resource_spec.rb index 7411ceeff7..ad0bd4ade4 100644 --- a/spec/requests/api/v3/user/create_form_resource_spec.rb +++ b/spec/requests/api/v3/user/create_form_resource_spec.rb @@ -139,9 +139,9 @@ describe API::V3::Users::CreateFormAPI, content_type: :json do { email: 'cfuser@example.com', status: 'invited', - "customField#{custom_field.id}": "A custom value", + custom_field.attribute_name(:camel_case) => "A custom value", _links: { - "customField#{list_custom_field.id}": { + list_custom_field.attribute_name(:camel_case) => { href: custom_option_href } } diff --git a/spec/requests/api/v3/versions/create_form_resource_spec.rb b/spec/requests/api/v3/versions/create_form_resource_spec.rb index 5422a29258..44bbc4b170 100644 --- a/spec/requests/api/v3/versions/create_form_resource_spec.rb +++ b/spec/requests/api/v3/versions/create_form_resource_spec.rb @@ -117,7 +117,7 @@ describe API::V3::Versions::CreateFormAPI, content_type: :json do description: { raw: 'A new description' }, - "customField#{int_cf.id}": 5, + int_cf.attribute_name(:camel_case) => 5, startDate: "2018-01-01", endDate: "2018-01-09", status: "closed", @@ -126,7 +126,7 @@ describe API::V3::Versions::CreateFormAPI, content_type: :json do definingProject: { href: api_v3_paths.project(project.id) }, - "customField#{list_cf.id}": { + list_cf.attribute_name(:camel_case) => { href: api_v3_paths.custom_option(list_cf.custom_options.first.id) } } diff --git a/spec/requests/api/v3/versions/update_form_resource_spec.rb b/spec/requests/api/v3/versions/update_form_resource_spec.rb index 151b502f2c..dc3cd8b03e 100644 --- a/spec/requests/api/v3/versions/update_form_resource_spec.rb +++ b/spec/requests/api/v3/versions/update_form_resource_spec.rb @@ -147,13 +147,13 @@ describe API::V3::Versions::UpdateFormAPI, content_type: :json do description: { raw: 'A new description' }, - "customField#{int_cf.id}": 5, + int_cf.attribute_name(:camel_case) => 5, startDate: "2018-01-01", endDate: "2018-01-09", status: "closed", sharing: "descendants", _links: { - "customField#{list_cf.id}": { + list_cf.attribute_name(:camel_case) => { href: api_v3_paths.custom_option(list_cf.custom_options.first.id) } } diff --git a/spec/requests/api/v3/work_packages/form/work_package_form_resource_spec.rb b/spec/requests/api/v3/work_packages/form/work_package_form_resource_spec.rb index 42fed0bf61..808e098d0f 100644 --- a/spec/requests/api/v3/work_packages/form/work_package_form_resource_spec.rb +++ b/spec/requests/api/v3/work_packages/form/work_package_form_resource_spec.rb @@ -764,7 +764,7 @@ describe 'API v3 Work package form resource', with_mail: false do create(:work_package_custom_field, field_format: 'text') end - let(:cf_param) { { "customField#{custom_field.id}" => nil } } + let(:cf_param) { { custom_field.attribute_name(:camel_case) => nil } } let(:params) { valid_params.merge(cf_param) } before do diff --git a/spec/requests/api/v3/work_packages/update_resource_spec.rb b/spec/requests/api/v3/work_packages/update_resource_spec.rb index 5fe8c53699..712623ffe4 100644 --- a/spec/requests/api/v3/work_packages/update_resource_spec.rb +++ b/spec/requests/api/v3/work_packages/update_resource_spec.rb @@ -349,7 +349,7 @@ describe 'API v3 Work package resource', context 'valid type changing custom fields' do let(:custom_field) { create(:work_package_custom_field) } - let(:custom_field_parameter) { { "customField#{custom_field.id}": true } } + let(:custom_field_parameter) { { custom_field.attribute_name(:camel_case) => true } } let(:params) { valid_params.merge(type_parameter).merge(custom_field_parameter) } before do @@ -430,7 +430,7 @@ describe 'API v3 Work package resource', context 'with a custom field defined on the target project' do let(:member_permissions) { %i[move_work_packages edit_work_packages] } let(:custom_field) { create(:work_package_custom_field) } - let(:custom_field_parameter) { { "customField#{custom_field.id}": true } } + let(:custom_field_parameter) { { custom_field.attribute_name(:camel_case) => true } } let(:params) { valid_params.merge(project_parameter).merge(custom_field_parameter) } before do diff --git a/spec/support/form_fields/form_field.rb b/spec/support/form_fields/form_field.rb index e80e253a4f..a5c2d03d93 100644 --- a/spec/support/form_fields/form_field.rb +++ b/spec/support/form_fields/form_field.rb @@ -26,7 +26,7 @@ module FormFields def property_name if property.is_a? CustomField - "customField#{property.id}" + property.attribute_name(:camel_case) else property.to_s end diff --git a/spec/support/pages/work_packages/abstract_work_package.rb b/spec/support/pages/work_packages/abstract_work_package.rb index 8a3ae2e3b7..25db78e608 100644 --- a/spec/support/pages/work_packages/abstract_work_package.rb +++ b/spec/support/pages/work_packages/abstract_work_package.rb @@ -54,7 +54,7 @@ module Pages end def custom_edit_field(custom_field) - edit_field("customField#{custom_field.id}").tap do |field| + edit_field(custom_field.attribute_name(:camel_case)).tap do |field| if custom_field.list? field.field_type = 'create-autocompleter' end