Skip to content

Commit 9c820cc

Browse files
committed
Get inplaceEditField for customField dynamically by the format instead of registring them all directly
1 parent adb88c9 commit 9c820cc

File tree

8 files changed

+27
-58
lines changed

8 files changed

+27
-58
lines changed

app/components/open_project/common/inplace_edit_field_component.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ def initialize(model:,
6060
end
6161

6262
def field_class
63-
OpenProject::InplaceEdit::FieldRegistry.fetch(attribute)
63+
if custom_field?
64+
OpenProject::InplaceEdit::FieldRegistry.fetch_for_custom_field_format(custom_field&.field_format)
65+
else
66+
OpenProject::InplaceEdit::FieldRegistry.fetch(attribute)
67+
end
6468
end
6569

6670
def edit_field_component(form)
@@ -212,8 +216,7 @@ def required?
212216
# For custom fields, check the is_required attribute
213217
custom_field&.is_required || false
214218
else
215-
# For regular model attributes, check ActiveRecord validations
216-
model.class.validators_on(attribute).any?(ActiveRecord::Validations::PresenceValidator)
219+
false
217220
end
218221
end
219222

app/components/open_project/common/inplace_edit_fields/display_fields/display_fields_component.sass

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
padding: var(--base-size-4) var(--base-size-8)
44
border: 1px solid transparent
55
border-radius: var(--borderRadius-medium)
6+
@include text-shortener(false)
67

78
&:hover, &:focus
89
border-color: var(--borderColor-default)

app/components/open_project/common/inplace_edit_fields/display_fields/link_input_component.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ def render_link(href)
5151
link = Addressable::URI.parse(href)
5252
return href unless link
5353

54-
target = link.host == Setting.host_without_protocol ? "_top" : "_blank"
55-
render(Primer::Beta::Link.new(href:, rel: "noopener noreferrer", target:)) do
54+
render(Primer::Beta::Link.new(href:, rel: "noopener noreferrer")) do
5655
href
5756
end
5857
end

app/models/custom_field.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class CustomField < ApplicationRecord
8888
validates :has_comment, absence: true, unless: :can_have_comment?
8989

9090
before_validation :check_searchability
91-
after_create :register_inplace_edit_component
91+
9292
after_destroy :destroy_help_text
9393

9494
# make sure int, float, date, and bool are not searchable
@@ -482,8 +482,4 @@ def destroy_help_text
482482
.where(attribute_name:)
483483
.destroy_all
484484
end
485-
486-
def register_inplace_edit_component
487-
OpenProject::InplaceEdit::FieldRegistry.register_custom_field(id, field_format)
488-
end
489485
end

config/initializers/inplace_edit_fields.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,6 @@
5353

5454
OpenProject::InplaceEdit::FieldRegistry.register_custom_field_format_mappings(custom_field_format_mappings)
5555

56-
if CustomField.table_exists?
57-
CustomField.pluck(:id, :field_format).each do |id, field_format|
58-
OpenProject::InplaceEdit::FieldRegistry.register_custom_field(id, field_format)
59-
end
60-
end
61-
6256
# Register the update handler per model
6357
OpenProject::InplaceEdit::UpdateRegistry.register(Project,
6458
handler: OpenProject::InplaceEdit::Handlers::ProjectUpdate,

lib/open_project/inplace_edit/field_registry.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,22 @@ def register_custom_field_format_mappings(mappings)
4444
@custom_field_format_mappings = mappings
4545
end
4646

47-
def register_custom_field(id, field_format)
48-
component = @custom_field_format_mappings[field_format]
49-
register("custom_field_#{id}", component) if component
50-
end
51-
5247
def fetch(attribute_name)
5348
@registry.fetch(attribute_name.to_s) { Common::InplaceEditFields::TextInputComponent }
5449
end
5550

51+
def fetch_for_custom_field_format(field_format)
52+
return Common::InplaceEditFields::TextInputComponent if field_format.nil?
53+
54+
@custom_field_format_mappings.fetch(field_format.to_s) { Common::InplaceEditFields::TextInputComponent }
55+
end
56+
5657
@default = new
5758

5859
class << self
5960
attr_reader :default
6061

61-
delegate :register, :fetch, :register_custom_field_format_mappings, :register_custom_field, to: :@default
62+
delegate :register, :fetch, :fetch_for_custom_field_format, :register_custom_field_format_mappings, to: :@default
6263
end
6364
end
6465
end

spec/lib/open_project/inplace_edit/field_registry_spec.rb

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,33 +63,32 @@
6363
end
6464

6565
describe "#register_custom_field_format_mappings" do
66-
it "stores format-to-component mappings used by register_custom_field" do
66+
it "stores format-to-component mappings used by fetch_for_custom_field_format" do
6767
text_component = Class.new
6868
registry.register_custom_field_format_mappings("text" => text_component)
6969

70-
registry.register_custom_field(42, "text")
71-
72-
expect(registry.fetch("custom_field_42")).to eq(text_component)
70+
expect(registry.fetch_for_custom_field_format("text")).to eq(text_component)
7371
end
7472
end
7573

76-
describe "#register_custom_field" do
74+
describe "#fetch_for_custom_field_format" do
7775
let(:text_component) { Class.new }
7876

7977
before do
8078
registry.register_custom_field_format_mappings("text" => text_component)
8179
end
8280

83-
it "registers the correct component for the given field format" do
84-
registry.register_custom_field(1, "text")
85-
86-
expect(registry.fetch("custom_field_1")).to eq(text_component)
81+
it "returns the correct component for the given field format" do
82+
expect(registry.fetch_for_custom_field_format("text")).to eq(text_component)
8783
end
8884

89-
it "does nothing when the format has no mapping" do
90-
registry.register_custom_field(2, "unknown_format")
85+
it "falls back to TextInputComponent when the format has no mapping" do
86+
expect(registry.fetch_for_custom_field_format("unknown_format"))
87+
.to eq(OpenProject::Common::InplaceEditFields::TextInputComponent)
88+
end
9189

92-
expect(registry.fetch("custom_field_2"))
90+
it "falls back to TextInputComponent when field_format is nil" do
91+
expect(registry.fetch_for_custom_field_format(nil))
9392
.to eq(OpenProject::Common::InplaceEditFields::TextInputComponent)
9493
end
9594
end

spec/models/custom_field_spec.rb

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -742,28 +742,4 @@
742742
end
743743
end
744744
end
745-
746-
describe "after_create callback" do
747-
it "registers the custom field in the inplace edit field registry" do
748-
custom_field = build(:custom_field, field_format: "string")
749-
750-
allow(OpenProject::InplaceEdit::FieldRegistry).to receive(:register_custom_field)
751-
752-
custom_field.save!
753-
754-
expect(OpenProject::InplaceEdit::FieldRegistry)
755-
.to have_received(:register_custom_field)
756-
.with(anything, "string")
757-
end
758-
759-
it "does not re-register when updated" do
760-
custom_field = create(:custom_field, field_format: "string")
761-
762-
allow(OpenProject::InplaceEdit::FieldRegistry).to receive(:register_custom_field)
763-
764-
custom_field.update!(name: "Updated name")
765-
766-
expect(OpenProject::InplaceEdit::FieldRegistry).not_to have_received(:register_custom_field)
767-
end
768-
end
769745
end

0 commit comments

Comments
 (0)