Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Add "Link" custom field type

Oliver Günther1f73602839fb
diff --git a/app/forms/projects/custom_fields/form.rb b/app/forms/projects/custom_fields/form.rb
index 1508c068aae..46704f69082 100644
--- a/app/forms/projects/custom_fields/form.rb
+++ b/app/forms/projects/custom_fields/form.rb
@@ -81,7 +81,7 @@ module Projects::CustomFields
form_args = { custom_field:, object: @project, wrapper_id: @wrapper_id }
case custom_field.field_format
- when "string"
+ when "string", "link"
CustomFields::Inputs::String.new(builder, **form_args)
when "text"
CustomFields::Inputs::Text.new(builder, **form_args)
diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb
index 7b86c4c97c9..140ab414d3a 100644
--- a/app/models/custom_field.rb
+++ b/app/models/custom_field.rb
@@ -184,7 +184,7 @@ class CustomField < ApplicationRecord
return if value.blank?
case field_format
- when "string", "text", "list"
+ when "string", "text", "list", "link"
value
when "date"
begin
diff --git a/app/models/custom_value.rb b/app/models/custom_value.rb
index 0e331d84f54..d3b55b63bb0 100644
--- a/app/models/custom_value.rb
+++ b/app/models/custom_value.rb
@@ -97,7 +97,7 @@ class CustomValue < ApplicationRecord
def validate_format_of_value
if value.present? && custom_field.has_regexp? && !(value =~ Regexp.new(custom_field.regexp))
- errors.add(:value, :invalid)
+ errors.add(:value, :regex_match_failed, expression: custom_field.regexp)
end
rescue RegexpError => e
errors.add(:base, :regex_invalid)
diff --git a/app/models/custom_value/link_strategy.rb b/app/models/custom_value/link_strategy.rb
new file mode 100644
index 00000000000..04975fb25fb
--- /dev/null
+++ b/app/models/custom_value/link_strategy.rb
@@ -0,0 +1,49 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+class CustomValue::LinkStrategy < CustomValue::FormatStrategy
+ def typed_value
+ formatted_value
+ end
+
+ def parse_value(val)
+ parsed_url(val)&.to_s
+ end
+
+ def validate_type_of_value
+ unless parsed_url(value)&.absolute?
+ :invalid_url
+ end
+ end
+
+ private
+
+ def parsed_url(val)
+ Addressable::URI.heuristic_parse(val, scheme: "http")
+ end
+end
diff --git a/app/models/setting/aliases.rb b/app/models/setting/aliases.rb
index 848edd38ea2..cc008cf2833 100644
--- a/app/models/setting/aliases.rb
+++ b/app/models/setting/aliases.rb
@@ -39,5 +39,11 @@ class Setting
"http"
end
end
+
+ ##
+ # Host name without protocol
+ def host_without_protocol
+ Setting.host_name.split(":").first
+ end
end
end
diff --git a/config/initializers/custom_field_format.rb b/config/initializers/custom_field_format.rb
index 7e67798075c..22ad44f9663 100644
--- a/config/initializers/custom_field_format.rb
+++ b/config/initializers/custom_field_format.rb
@@ -34,44 +34,50 @@ OpenProject::CustomFieldFormat.map do |fields|
label: :label_text,
order: 2,
formatter: "CustomValue::FormattableStrategy")
+ fields.register OpenProject::CustomFieldFormat.new("link",
+ label: :label_link_url,
+ only: %w(WorkPackage Project),
+ order: 3,
+ formatter: "CustomValue::LinkStrategy")
fields.register OpenProject::CustomFieldFormat.new("int",
label: :label_integer,
- order: 3,
+ order: 4,
formatter: "CustomValue::IntStrategy")
fields.register OpenProject::CustomFieldFormat.new("float",
label: :label_float,
- order: 4,
+ order: 5,
formatter: "CustomValue::FloatStrategy")
fields.register OpenProject::CustomFieldFormat.new("list",
label: :label_list,
- order: 5,
+ order: 6,
formatter: "CustomValue::ListStrategy")
fields.register OpenProject::CustomFieldFormat.new("date",
label: :label_date,
- order: 6,
+ order: 7,
formatter: "CustomValue::DateStrategy")
fields.register OpenProject::CustomFieldFormat.new("bool",
label: :label_boolean,
- order: 7,
+ order: 8,
formatter: "CustomValue::BoolStrategy")
fields.register OpenProject::CustomFieldFormat.new("user",
label: Proc.new { User.model_name.human },
only: %w(WorkPackage TimeEntry
Version Project),
edit_as: "list",
- order: 8,
+ order: 9,
formatter: "CustomValue::UserStrategy")
fields.register OpenProject::CustomFieldFormat.new("version",
label: Proc.new { Version.model_name.human },
only: %w(WorkPackage TimeEntry
Version Project),
edit_as: "list",
- order: 9,
+ order: 10,
formatter: "CustomValue::VersionStrategy")
# This is an internal formatter used as a fallback in case a value is not found.
# Setting the label to nil in order to avoid it becoming available for selection as a custom value format.
fields.register OpenProject::CustomFieldFormat.new("empty",
label: nil,
- order: 10,
+ order: 11,
formatter: "CustomValue::EmptyStrategy")
+
end
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 8a7351f6ebc..db99370e8b9 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -855,6 +855,7 @@ Project attributes and sections are defined in the <a href=%{admin_settings_url}
not_an_iso_date: "is not a valid date. Required format: YYYY-MM-DD."
not_same_project: "doesn't belong to the same project."
odd: "must be odd."
+ regex_match_failed: "does not match the regular expression %{expression}."
regex_invalid: "could not be validated with the associated regular expression."
smaller_than_or_equal_to_max_length: "must be smaller than or equal to maximum length."
taken: "has already been taken."
@@ -2128,6 +2129,7 @@ Project attributes and sections are defined in the <a href=%{admin_settings_url}
label_learn_more: "Learn more"
label_less_or_equal: "<="
label_less_than_ago: "less than days ago"
+ label_link_url: "Link (URL)"
label_list: "List"
label_loading: "Loading..."
label_lock_user: "Lock user"
diff --git a/frontend/src/app/shared/components/fields/display/display-field-renderer.ts b/frontend/src/app/shared/components/fields/display/display-field-renderer.ts
index 138f063d921..d3b4c8cb716 100644
--- a/frontend/src/app/shared/components/fields/display/display-field-renderer.ts
+++ b/frontend/src/app/shared/components/fields/display/display-field-renderer.ts
@@ -20,6 +20,7 @@ export const requiredClassName = '-required';
export const readOnlyClassName = '-read-only';
export const placeholderClassName = '-placeholder';
export const displayClassName = 'inline-edit--display-field';
+export const displayTriggerLink = 'inline-edit--display-trigger';
export const editFieldContainerClass = 'inline-edit--container';
export class DisplayFieldRenderer<T extends HalResource = HalResource> {
diff --git a/frontend/src/app/shared/components/fields/display/display-field.initializer.ts b/frontend/src/app/shared/components/fields/display/display-field.initializer.ts
index e2cc679b507..c30605629e3 100644
--- a/frontend/src/app/shared/components/fields/display/display-field.initializer.ts
+++ b/frontend/src/app/shared/components/fields/display/display-field.initializer.ts
@@ -26,31 +26,66 @@
// See COPYRIGHT and LICENSE files for more details.
//++
-import { BooleanDisplayField } from 'core-app/shared/components/fields/display/field-types/boolean-display-field.module';
-import { CombinedDateDisplayField } from 'core-app/shared/components/fields/display/field-types/combined-date-display.field';
-import { CompoundProgressDisplayField } from 'core-app/shared/components/fields/display/field-types/compound-progress-display-field.module';
+import {
+ BooleanDisplayField,
+} from 'core-app/shared/components/fields/display/field-types/boolean-display-field.module';
+import {
+ CombinedDateDisplayField,
+} from 'core-app/shared/components/fields/display/field-types/combined-date-display.field';
+import {
+ CompoundProgressDisplayField,
+} from 'core-app/shared/components/fields/display/field-types/compound-progress-display-field.module';
import { DateDisplayField } from 'core-app/shared/components/fields/display/field-types/date-display-field.module';
-import { DateTimeDisplayField } from 'core-app/shared/components/fields/display/field-types/datetime-display-field.module';
-import { DaysDurationDisplayField } from 'core-app/shared/components/fields/display/field-types/days-duration-display-field.module';
+import {
+ DateTimeDisplayField,
+} from 'core-app/shared/components/fields/display/field-types/datetime-display-field.module';
+import {
+ DaysDurationDisplayField,
+} from 'core-app/shared/components/fields/display/field-types/days-duration-display-field.module';
import { DisplayFieldService } from 'core-app/shared/components/fields/display/display-field.service';
import { WorkDisplayField } from 'core-app/shared/components/fields/display/field-types/work-display-field.module';
import { FloatDisplayField } from 'core-app/shared/components/fields/display/field-types/float-display-field.module';
-import { FormattableDisplayField } from 'core-app/shared/components/fields/display/field-types/formattable-display-field.module';
-import { HighlightedResourceDisplayField } from 'core-app/shared/components/fields/display/field-types/highlighted-resource-display-field.module';
+import {
+ FormattableDisplayField,
+} from 'core-app/shared/components/fields/display/field-types/formattable-display-field.module';
+import {
+ HighlightedResourceDisplayField,
+} from 'core-app/shared/components/fields/display/field-types/highlighted-resource-display-field.module';
import { IdDisplayField } from 'core-app/shared/components/fields/display/field-types/id-display-field.module';
-import { IntegerDisplayField } from 'core-app/shared/components/fields/display/field-types/integer-display-field.module';
-import { LinkedWorkPackageDisplayField } from 'core-app/shared/components/fields/display/field-types/linked-work-package-display-field.module';
-import { MultipleUserFieldModule } from 'core-app/shared/components/fields/display/field-types/multiple-user-display-field.module';
-import { PlainFormattableDisplayField } from 'core-app/shared/components/fields/display/field-types/plain-formattable-display-field.module';
-import { ProjectStatusDisplayField } from 'core-app/shared/components/fields/display/field-types/project-status-display-field.module';
-import { ResourceDisplayField } from 'core-app/shared/components/fields/display/field-types/resource-display-field.module';
-import { ResourcesDisplayField } from 'core-app/shared/components/fields/display/field-types/resources-display-field.module';
+import {
+ IntegerDisplayField,
+} from 'core-app/shared/components/fields/display/field-types/integer-display-field.module';
+import {
+ LinkedWorkPackageDisplayField,
+} from 'core-app/shared/components/fields/display/field-types/linked-work-package-display-field.module';
+import {
+ MultipleUserFieldModule,
+} from 'core-app/shared/components/fields/display/field-types/multiple-user-display-field.module';
+import {
+ PlainFormattableDisplayField,
+} from 'core-app/shared/components/fields/display/field-types/plain-formattable-display-field.module';
+import {
+ ProjectStatusDisplayField,
+} from 'core-app/shared/components/fields/display/field-types/project-status-display-field.module';
+import {
+ ResourceDisplayField,
+} from 'core-app/shared/components/fields/display/field-types/resource-display-field.module';
+import {
+ ResourcesDisplayField,
+} from 'core-app/shared/components/fields/display/field-types/resources-display-field.module';
import { TextDisplayField } from 'core-app/shared/components/fields/display/field-types/text-display-field.module';
import { TypeDisplayField } from 'core-app/shared/components/fields/display/field-types/type-display-field.module';
import { UserDisplayField } from 'core-app/shared/components/fields/display/field-types/user-display-field.module';
-import { WorkPackageDisplayField } from 'core-app/shared/components/fields/display/field-types/work-package-display-field.module';
-import { WorkPackageIdDisplayField } from 'core-app/shared/components/fields/display/field-types/wp-id-display-field.module';
-import { WorkPackageSpentTimeDisplayField } from 'core-app/shared/components/fields/display/field-types/wp-spent-time-display-field.module';
+import {
+ WorkPackageDisplayField,
+} from 'core-app/shared/components/fields/display/field-types/work-package-display-field.module';
+import {
+ WorkPackageIdDisplayField,
+} from 'core-app/shared/components/fields/display/field-types/wp-id-display-field.module';
+import {
+ WorkPackageSpentTimeDisplayField,
+} from 'core-app/shared/components/fields/display/field-types/wp-spent-time-display-field.module';
+import { LinkDisplayField } from 'core-app/shared/components/fields/display/field-types/link-display-field.module';
export function initializeCoreDisplayFields(displayFieldService:DisplayFieldService) {
return () => {
@@ -80,6 +115,7 @@ export function initializeCoreDisplayFields(displayFieldService:DisplayFieldServ
.addFieldType(DateDisplayField, 'date', ['Date'])
.addFieldType(DateTimeDisplayField, 'datetime', ['DateTime'])
.addFieldType(BooleanDisplayField, 'boolean', ['Boolean'])
+ .addFieldType(LinkDisplayField, 'link', ['Link'])
.addFieldType(CompoundProgressDisplayField, 'progress', ['percentageDone'])
.addFieldType(LinkedWorkPackageDisplayField, 'work_package', ['WorkPackage'])
.addFieldType(IdDisplayField, 'id', ['id'])
diff --git a/frontend/src/app/shared/components/fields/display/field-types/link-display-field.module.ts b/frontend/src/app/shared/components/fields/display/field-types/link-display-field.module.ts
new file mode 100644
index 00000000000..978ef793ea8
--- /dev/null
+++ b/frontend/src/app/shared/components/fields/display/field-types/link-display-field.module.ts
@@ -0,0 +1,75 @@
+// -- copyright
+// OpenProject is an open source project management software.
+// Copyright (C) 2012-2024 the OpenProject GmbH
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License version 3.
+//
+// OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+// Copyright (C) 2006-2013 Jean-Philippe Lang
+// Copyright (C) 2010-2013 the ChiliProject Team
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+//
+// See COPYRIGHT and LICENSE files for more details.
+//++
+
+import { DisplayField } from 'core-app/shared/components/fields/display/display-field.module';
+import { debugLog } from 'core-app/shared/helpers/debug_output';
+import { displayTriggerLink } from 'core-app/shared/components/fields/display/display-field-renderer';
+
+export class LinkDisplayField extends DisplayField {
+ public render(element:HTMLElement, displayText:string):void {
+ if (this.value === null || this.value === undefined || this.value === '') {
+ element.textContent = this.placeholder;
+ return;
+ }
+
+ const link = document.createElement('a');
+ link.textContent = displayText;
+ link.href = this.value as string;
+ link.rel = 'noopener noreferrer';
+ link.target = this.target;
+
+ element.appendChild(link);
+
+ this.appendEditLink(element);
+ }
+
+ private appendEditLink(element:HTMLElement) {
+ if (this.resource.updateImmediately) {
+ const editLink = document.createElement('a');
+ editLink.classList.add('icon', 'icon-edit', displayTriggerLink);
+ editLink.setAttribute('href', '#');
+
+ element.appendChild(editLink);
+ }
+ }
+
+ private get target():string {
+ const origin = window.location.origin;
+
+ try {
+ const url = new URL(this.value as string, window.location.origin);
+ if (origin !== url.origin) {
+ return '_blank';
+ }
+ } catch (e) {
+ debugLog('Failed to parse origin for URL %O: %O', this.value, e);
+ }
+
+ return '';
+ }
+}
diff --git a/frontend/src/app/shared/components/fields/edit/field/editable-attribute-field.component.ts b/frontend/src/app/shared/components/fields/edit/field/editable-attribute-field.component.ts
index b74ded97b0e..b5a55de8bd6 100644
--- a/frontend/src/app/shared/components/fields/edit/field/editable-attribute-field.component.ts
+++ b/frontend/src/app/shared/components/fields/edit/field/editable-attribute-field.component.ts
@@ -26,7 +26,9 @@
// See COPYRIGHT and LICENSE files for more details.
//++
-import { HalResourceEditingService } from 'core-app/shared/components/fields/edit/services/hal-resource-editing.service';
+import {
+ HalResourceEditingService,
+} from 'core-app/shared/components/fields/edit/services/hal-resource-editing.service';
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
@@ -48,7 +50,7 @@ import { UntilDestroyedMixin } from 'core-app/shared/helpers/angular/until-destr
import { SchemaCacheService } from 'core-app/core/schemas/schema-cache.service';
import {
displayClassName,
- DisplayFieldRenderer,
+ DisplayFieldRenderer, displayTriggerLink,
editFieldContainerClass,
} from 'core-app/shared/components/fields/display/display-field-renderer';
import { States } from 'core-app/core/states/states.service';
@@ -168,7 +170,7 @@ export class EditableAttributeFieldComponent extends UntilDestroyedMixin impleme
// Skip activation if the user clicked on a link or within a macro
const target = jQuery(event.target as HTMLElement);
- if (target.closest('a,macro', this.displayContainer.nativeElement).length > 0) {
+ if (target.closest(`a:not(.${displayTriggerLink}),macro`, this.displayContainer.nativeElement).length > 0) {
return true;
}
diff --git a/frontend/src/stimulus/controllers/dynamic/admin/custom-fields.controller.ts b/frontend/src/stimulus/controllers/dynamic/admin/custom-fields.controller.ts
index 9a72661058a..c8dd3a30705 100644
--- a/frontend/src/stimulus/controllers/dynamic/admin/custom-fields.controller.ts
+++ b/frontend/src/stimulus/controllers/dynamic/admin/custom-fields.controller.ts
@@ -327,6 +327,11 @@ export default class CustomFieldsController extends Controller {
this.activate(this.possibleValuesTargets, false);
this.activate(this.textOrientationTargets);
break;
+ case 'link':
+ this.hide(...this.lengthTargets);
+ this.show(...this.regexpTargets, ...this.searchableTargets);
+ this.activate(this.possibleValuesTargets, false);
+ break;
default:
this.show(...this.lengthTargets, ...this.regexpTargets, ...this.searchableTargets);
this.activate(this.possibleValuesTargets, false);
diff --git a/lib/api/v3/queries/schemas/filter_dependency_representer_factory.rb b/lib/api/v3/queries/schemas/filter_dependency_representer_factory.rb
index bf1bcabe231..c0a72621569 100644
--- a/lib/api/v3/queries/schemas/filter_dependency_representer_factory.rb
+++ b/lib/api/v3/queries/schemas/filter_dependency_representer_factory.rb
@@ -98,7 +98,7 @@ module API
"API::V3::Queries::Schemas::BooleanFilterDependencyRepresenter"
when "user", "version", "float"
"API::V3::Queries::Schemas::#{format.camelize}FilterDependencyRepresenter"
- when "string"
+ when "string", "link"
"API::V3::Queries::Schemas::TextFilterDependencyRepresenter"
end
end
diff --git a/lib/api/v3/utilities/custom_field_injector.rb b/lib/api/v3/utilities/custom_field_injector.rb
index 23a3330fd18..229521e3b98 100644
--- a/lib/api/v3/utilities/custom_field_injector.rb
+++ b/lib/api/v3/utilities/custom_field_injector.rb
@@ -34,6 +34,7 @@ module API
"string" => "String",
"empty" => "String",
"text" => "Formattable",
+ "link" => "Link",
"int" => "Integer",
"float" => "Float",
"date" => "Date",
diff --git a/modules/overviews/app/components/project_custom_fields/sections/project_custom_fields/show_component.rb b/modules/overviews/app/components/project_custom_fields/sections/project_custom_fields/show_component.rb
index 06c7c5147cb..2faf8a508c7 100644
--- a/modules/overviews/app/components/project_custom_fields/sections/project_custom_fields/show_component.rb
+++ b/modules/overviews/app/components/project_custom_fields/sections/project_custom_fields/show_component.rb
@@ -49,6 +49,8 @@ module ProjectCustomFields
def render_value
case @project_custom_field.field_format
+ when "link"
+ render_link
when "text"
render_long_text
when "user"
@@ -86,6 +88,17 @@ module ProjectCustomFields
def render_avatar(user)
render(Users::AvatarComponent.new(user:, size: :mini))
end
+
+ def render_link
+ href = @project_custom_field_values&.first&.value
+ link = Addressable::URI.parse(href)
+ return href unless link
+
+ target = link.host == Setting.host_without_protocol ? "_top" : "_blank"
+ render(Primer::Beta::Link.new(href:, rel: "noopener noreferrer", target:)) do
+ href
+ end
+ end
end
end
end
diff --git a/spec/factories/custom_field_factory.rb b/spec/factories/custom_field_factory.rb
index 1c4616222b0..61c6c45deed 100644
--- a/spec/factories/custom_field_factory.rb
+++ b/spec/factories/custom_field_factory.rb
@@ -157,6 +157,10 @@ FactoryBot.define do
multi_value { true }
end
+ trait :link do
+ field_format { "link" }
+ end
+
factory :project_custom_field, class: "ProjectCustomField" do
project_custom_field_section
@@ -185,6 +189,7 @@ FactoryBot.define do
factory :list_project_custom_field, traits: [:list]
factory :version_project_custom_field, traits: [:version]
factory :user_project_custom_field, traits: [:user]
+ factory :link_project_custom_field, traits: [:link]
end
factory :user_custom_field, class: "UserCustomField"
@@ -214,6 +219,7 @@ FactoryBot.define do
factory :list_wp_custom_field, traits: [:list]
factory :version_wp_custom_field, traits: [:version]
factory :user_wp_custom_field, traits: [:user]
+ factory :link_wp_custom_field, traits: [:link]
end
factory :issue_custom_field, class: "WorkPackageCustomField" do
diff --git a/spec/features/admin/custom_fields/link_custom_field_spec.rb b/spec/features/admin/custom_fields/link_custom_field_spec.rb
new file mode 100644
index 00000000000..c5b06295946
--- /dev/null
+++ b/spec/features/admin/custom_fields/link_custom_field_spec.rb
@@ -0,0 +1,70 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require "spec_helper"
+
+RSpec.describe "Link custom fields edit", :js, :with_cuprite do
+ shared_let(:admin) { create(:admin) }
+ let(:cf_page) { Pages::CustomFields.new }
+
+ before do
+ login_as(admin)
+ visit custom_fields_path
+ end
+
+ it "can create and edit user custom fields" do
+ # Create CF
+ click_link "Create a new custom field"
+
+ wait_for_reload
+
+ fill_in "custom_field_name", with: "My Link CF"
+ select "Link (URL)", from: "custom_field_field_format"
+
+ expect(page).to have_no_field("custom_field_custom_options_attributes_0_value")
+
+ click_on "Save"
+
+ # Expect field to be created
+ cf = CustomField.last
+ expect(cf.name).to eq("My Link CF")
+ expect(cf.field_format).to eq 'link'
+
+ # Edit again
+ find("a", text: "My Link CF").click
+
+ expect(page).to have_no_field("custom_field_custom_options_attributes_0_value")
+ fill_in "custom_field_name", with: "My Link CF (edited)"
+
+ click_on "Save"
+
+ # Expect field to be saved
+ cf = CustomField.last
+ expect(cf.name).to eq("My Link CF (edited)")
+ end
+end
diff --git a/spec/features/projects/project_custom_fields/overview_page/dialog/inputs_spec.rb b/spec/features/projects/project_custom_fields/overview_page/dialog/inputs_spec.rb
index 0d4b5f53637..f86a22cf0ff 100644
--- a/spec/features/projects/project_custom_fields/overview_page/dialog/inputs_spec.rb
+++ b/spec/features/projects/project_custom_fields/overview_page/dialog/inputs_spec.rb
@@ -193,6 +193,22 @@ RSpec.describe "Edit project custom fields on project overview page", :js do
it_behaves_like "a custom field input"
end
+ describe "with link CF" do
+ let(:custom_field) { link_project_custom_field }
+ let(:default_value) { "https://openproject.org" }
+ let(:expected_blank_value) { "" }
+ let(:expected_initial_value) { "https://www.openproject.org" }
+ let(:field) { FormFields::Primerized::InputField.new(custom_field) }
+
+ it_behaves_like "a custom field input"
+
+ it "renders the custom field as a link" do
+ page.within_test_selector "project-custom-field-#{link_project_custom_field.id}" do
+ expect(page).to have_link("https://www.openproject.org", href: "https://www.openproject.org")
+ end
+ end
+ end
+
describe "with text CF" do
let(:custom_field) { text_project_custom_field }
let(:field) { FormFields::Primerized::EditorFormField.new(custom_field) }
diff --git a/spec/features/projects/project_custom_fields/overview_page/dialog/render_spec.rb b/spec/features/projects/project_custom_fields/overview_page/dialog/render_spec.rb
index 80523365044..ab2c95fb43b 100644
--- a/spec/features/projects/project_custom_fields/overview_page/dialog/render_spec.rb
+++ b/spec/features/projects/project_custom_fields/overview_page/dialog/render_spec.rb
@@ -121,7 +121,8 @@ RSpec.describe "Edit project custom fields on project overview page", :js do
expect(containers[2].text).to include("Integer field")
expect(containers[3].text).to include("Float field")
expect(containers[4].text).to include("Date field")
- expect(containers[5].text).to include("Text field")
+ expect(containers[5].text).to include("Link field")
+ expect(containers[6].text).to include("Text field")
end
boolean_project_custom_field.move_to_bottom
@@ -135,8 +136,9 @@ RSpec.describe "Edit project custom fields on project overview page", :js do
expect(containers[1].text).to include("Integer field")
expect(containers[2].text).to include("Float field")
expect(containers[3].text).to include("Date field")
- expect(containers[4].text).to include("Text field")
- expect(containers[5].text).to include("Boolean field")
+ expect(containers[4].text).to include("Link field")
+ expect(containers[5].text).to include("Text field")
+ expect(containers[6].text).to include("Boolean field")
end
end
diff --git a/spec/features/projects/project_custom_fields/overview_page/dialog/validation_spec.rb b/spec/features/projects/project_custom_fields/overview_page/dialog/validation_spec.rb
index 6652572d5a3..568c7e73155 100644
--- a/spec/features/projects/project_custom_fields/overview_page/dialog/validation_spec.rb
+++ b/spec/features/projects/project_custom_fields/overview_page/dialog/validation_spec.rb
@@ -59,7 +59,8 @@ RSpec.describe "Edit project custom fields on project overview page", :js do
expect(containers[2].text).to include("Integer field")
expect(containers[3].text).to include("Float field")
expect(containers[4].text).to include("Date field")
- expect(containers[5].text).to include("Text field")
+ expect(containers[5].text).to include("Link field")
+ expect(containers[6].text).to include("Text field")
expect(page).to have_no_text(boolean_project_custom_field_activated_in_other_project.name)
end
@@ -78,7 +79,8 @@ RSpec.describe "Edit project custom fields on project overview page", :js do
expect(containers[2].text).to include("Integer field")
expect(containers[3].text).to include("Float field")
expect(containers[4].text).to include("Date field")
- expect(containers[5].text).to include("Text field")
+ expect(containers[5].text).to include("Link field")
+ expect(containers[6].text).to include("Text field")
expect(page).to have_no_text(boolean_project_custom_field_activated_in_other_project.name)
end
diff --git a/spec/features/projects/project_custom_fields/overview_page/shared_context.rb b/spec/features/projects/project_custom_fields/overview_page/shared_context.rb
index 02ff40e0526..9f21d9613ac 100644
--- a/spec/features/projects/project_custom_fields/overview_page/shared_context.rb
+++ b/spec/features/projects/project_custom_fields/overview_page/shared_context.rb
@@ -127,6 +127,15 @@ RSpec.shared_context "with seeded projects, members and project custom fields" d
field
end
+ let!(:link_project_custom_field) do
+ field = create(:link_project_custom_field, projects: [project], name: "Link field",
+ project_custom_field_section: section_for_input_fields)
+
+ create(:custom_value, customized: project, custom_field: field, value: "https://www.openproject.org")
+
+ field
+ end
+
let!(:text_project_custom_field) do
field = create(:text_project_custom_field, projects: [project], name: "Text field",
project_custom_field_section: section_for_input_fields)
@@ -205,6 +214,7 @@ RSpec.shared_context "with seeded projects, members and project custom fields" d
integer_project_custom_field,
float_project_custom_field,
date_project_custom_field,
+ link_project_custom_field,
text_project_custom_field
]
end
diff --git a/spec/features/projects/project_custom_fields/overview_page/sidebar_spec.rb b/spec/features/projects/project_custom_fields/overview_page/sidebar_spec.rb
index 446a9d9d38a..dc7abb93548 100644
--- a/spec/features/projects/project_custom_fields/overview_page/sidebar_spec.rb
+++ b/spec/features/projects/project_custom_fields/overview_page/sidebar_spec.rb
@@ -82,14 +82,15 @@ RSpec.describe "Show project custom fields on project overview page", :js, :with
overview_page.within_custom_field_section_container(section_for_input_fields) do
fields = page.all(".op-project-custom-field-container")
- expect(fields.size).to eq(6)
+ expect(fields.size).to eq(7)
expect(fields[0].text).to include("Boolean field")
expect(fields[1].text).to include("String field")
expect(fields[2].text).to include("Integer field")
expect(fields[3].text).to include("Float field")
expect(fields[4].text).to include("Date field")
- expect(fields[5].text).to include("Text field")
+ expect(fields[5].text).to include("Link field")
+ expect(fields[6].text).to include("Text field")
end
overview_page.within_custom_field_section_container(section_for_select_fields) do
@@ -121,14 +122,15 @@ RSpec.describe "Show project custom fields on project overview page", :js, :with
overview_page.within_custom_field_section_container(section_for_input_fields) do
fields = page.all(".op-project-custom-field-container")
- expect(fields.size).to eq(6)
+ expect(fields.size).to eq(7)
expect(fields[0].text).to include("Boolean field")
expect(fields[1].text).to include("Integer field")
expect(fields[2].text).to include("Float field")
expect(fields[3].text).to include("Date field")
- expect(fields[4].text).to include("Text field")
- expect(fields[5].text).to include("String field")
+ expect(fields[4].text).to include("Link field")
+ expect(fields[5].text).to include("Text field")
+ expect(fields[6].text).to include("String field")
end
end
end
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 7443dc0953b..c8d4a0a4b33 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
@@ -236,4 +236,30 @@ RSpec.describe "custom field inplace editor", :js do
end
end
end
+
+ describe "link text" do
+ let(:custom_field) do
+ create(:link_wp_custom_field, :link, name: "My Link")
+ end
+ let(:initial_custom_values) { {} }
+
+ it "can set a link" do
+ # Activate the field
+ field.expect_state_text "-"
+ field.update "http://example.com"
+
+ field.expect_state_text "http://example.com"
+ expect(field.display_element).to have_css('a[href="http://example.com"]')
+
+ field.update "bogus", expect_failure: true
+
+ wp_page.expect_toast message: "My Link is not a valid URL", type: "error"
+
+ field.set_value "community.openproject.org"
+ field.save!
+
+ field.expect_state_text "http://community.openproject.org"
+ expect(field.display_element).to have_css('a[href="http://community.openproject.org"]')
+ end
+ end
end
diff --git a/spec/models/queries/work_packages/filter/custom_fields/custom_field_filter_spec.rb b/spec/models/queries/work_packages/filter/custom_fields/custom_field_filter_spec.rb
index 60e8b4b3a53..5d7235a6953 100644
--- a/spec/models/queries/work_packages/filter/custom_fields/custom_field_filter_spec.rb
+++ b/spec/models/queries/work_packages/filter/custom_fields/custom_field_filter_spec.rb
@@ -38,6 +38,7 @@ RSpec.describe Queries::WorkPackages::Filter::CustomFieldFilter do
let(:version_wp_custom_field) { build_stubbed(:version_wp_custom_field) }
let(:date_wp_custom_field) { build_stubbed(:date_wp_custom_field) }
let(:string_wp_custom_field) { build_stubbed(:string_wp_custom_field) }
+ let(:link_wp_custom_field) { build_stubbed(:link_wp_custom_field) }
let(:custom_field) { list_wp_custom_field }
let(:all_custom_fields) do
[list_wp_custom_field,
@@ -48,7 +49,8 @@ RSpec.describe Queries::WorkPackages::Filter::CustomFieldFilter do
user_wp_custom_field,
version_wp_custom_field,
date_wp_custom_field,
- string_wp_custom_field]
+ string_wp_custom_field,
+ link_wp_custom_field]
end
let(:query) { build_stubbed(:query, project:) }
let(:cf_accessor) { custom_field.column_name }
@@ -134,7 +136,7 @@ RSpec.describe Queries::WorkPackages::Filter::CustomFieldFilter do
end
describe "#type" do
- context "integer" do
+ describe "integer" do
let(:cf_accessor) { int_wp_custom_field.column_name }
it "is integer for an integer" do
@@ -143,7 +145,7 @@ RSpec.describe Queries::WorkPackages::Filter::CustomFieldFilter do
end
end
- context "float" do
+ describe "float" do
let(:cf_accessor) { float_wp_custom_field.column_name }
it "is integer for a float" do
@@ -152,7 +154,7 @@ RSpec.describe Queries::WorkPackages::Filter::CustomFieldFilter do
end
end
- context "text" do
+ describe "text" do
let(:cf_accessor) { text_wp_custom_field.column_name }
it "is text for a text" do
@@ -161,7 +163,7 @@ RSpec.describe Queries::WorkPackages::Filter::CustomFieldFilter do
end
end
- context "list optional" do
+ describe "list optional" do
let(:cf_accessor) { list_wp_custom_field.column_name }
it "is list_optional for a list" do
@@ -170,7 +172,7 @@ RSpec.describe Queries::WorkPackages::Filter::CustomFieldFilter do
end
end
- context "user" do
+ describe "user" do
let(:cf_accessor) { user_wp_custom_field.column_name }
it "is list_optional for a user" do
@@ -179,7 +181,7 @@ RSpec.describe Queries::WorkPackages::Filter::CustomFieldFilter do
end
end
- context "version" do
+ describe "version" do
let(:cf_accessor) { version_wp_custom_field.column_name }
it "is list_optional for a version" do
@@ -188,7 +190,7 @@ RSpec.describe Queries::WorkPackages::Filter::CustomFieldFilter do
end
end
- context "version" do
+ describe "date" do
let(:cf_accessor) { date_wp_custom_field.column_name }
it "is date for a date" do
@@ -197,7 +199,7 @@ RSpec.describe Queries::WorkPackages::Filter::CustomFieldFilter do
end
end
- context "bool" do
+ describe "bool" do
let(:cf_accessor) { bool_wp_custom_field.column_name }
it "is list for a bool" do
@@ -206,7 +208,7 @@ RSpec.describe Queries::WorkPackages::Filter::CustomFieldFilter do
end
end
- context "string" do
+ describe "string" do
let(:cf_accessor) { string_wp_custom_field.column_name }
it "is string for a string" do
@@ -214,6 +216,15 @@ RSpec.describe Queries::WorkPackages::Filter::CustomFieldFilter do
.to be(:string)
end
end
+
+ describe "link" do
+ let(:cf_accessor) { link_wp_custom_field.column_name }
+
+ it "is string for a link" do
+ expect(instance.type)
+ .to be(:string)
+ end
+ end
end
describe "#human_name" do
diff --git a/spec/support/edit_fields/edit_field.rb b/spec/support/edit_fields/edit_field.rb
index 19096f9a4dd..dba8ade9330 100644
--- a/spec/support/edit_fields/edit_field.rb
+++ b/spec/support/edit_fields/edit_field.rb
@@ -51,6 +51,14 @@ class EditField
context.find "#{@selector} #{display_selector}"
end
+ def display_trigger_element
+ if display_element.has_selector?(".inline-edit--display-trigger")
+ display_element.find(".inline-edit--display-trigger")
+ else
+ display_element
+ end
+ end
+
def input_element
context.find "#{@selector} #{input_selector}"
end
@@ -93,7 +101,7 @@ class EditField
retry_block(args: { tries: 2 }) do
unless active?
SeleniumHubWaiter.wait unless using_cuprite?
- scroll_to_and_click(display_element)
+ scroll_to_and_click(display_trigger_element)
SeleniumHubWaiter.wait unless using_cuprite?
end