Skip to content

A platform for community discussion. Free, open, simple.

DEV: Pass upload id from object site setting (#36665)

Isaac Janzen36a158989bef
diff --git a/frontend/discourse/admin/components/schema-setting/types/upload.gjs b/frontend/discourse/admin/components/schema-setting/types/upload.gjs
index 5cfd81fd7b2..bc8d0c61585 100644
--- a/frontend/discourse/admin/components/schema-setting/types/upload.gjs
+++ b/frontend/discourse/admin/components/schema-setting/types/upload.gjs
@@ -11,7 +11,7 @@ export default class SchemaSettingTypeUpload extends Component {
@action
uploadDone(upload) {
this.uploadUrl = upload.url;
- this.args.onChange(upload.url);
+ this.args.onChange(upload.id);
}
@action
diff --git a/lib/schema_settings_object_validator.rb b/lib/schema_settings_object_validator.rb
index fbbc4f0a061..58ad66d344d 100644
--- a/lib/schema_settings_object_validator.rb
+++ b/lib/schema_settings_object_validator.rb
@@ -80,13 +80,6 @@ class SchemaSettingsObjectValidator
end
def validate
- # Convert all upload URLs to IDs before validation
- @properties.each do |property_name, property_attributes|
- next if property_attributes[:type] != "upload"
- normalize_upload_value(property_name, property_attributes)
- end
-
- # Validate all properties
@properties.each do |property_name, property_attributes|
if property_attributes[:type] == "objects"
validate_child_objects(
@@ -108,15 +101,6 @@ class SchemaSettingsObjectValidator
private
- def normalize_upload_value(property_name, property_attributes)
- value = @object[property_name]
- return unless value.is_a?(String) && value.present?
-
- if upload = Upload.get_from_url(value)
- @object[property_name] = upload.id
- end
- end
-
def validate_child_objects(objects, property_name:, schema:)
return if objects.blank?
@@ -153,7 +137,6 @@ class SchemaSettingsObjectValidator
when "integer", "topic", "post"
value.is_a?(Integer)
when "upload"
- # Upload URLs are converted to IDs in normalize_upload_value
value.is_a?(Integer)
when "float"
value.is_a?(Float) || value.is_a?(Integer)
diff --git a/spec/lib/schema_settings_object_validator_spec.rb b/spec/lib/schema_settings_object_validator_spec.rb
index 9e6e7cbc806..afa256ef567 100644
--- a/spec/lib/schema_settings_object_validator_spec.rb
+++ b/spec/lib/schema_settings_object_validator_spec.rb
@@ -725,15 +725,14 @@ RSpec.describe SchemaSettingsObjectValidator do
)
end
- it "should convert upload URL to ID and validate successfully" do
+ it "should validate upload ID successfully" do
upload = Fabricate(:upload)
schema = { name: "section", properties: { upload_property: { type: "upload" } } }
- validator = described_class.new(schema: schema, object: { upload_property: upload.url })
+ validator = described_class.new(schema: schema, object: { upload_property: upload.id })
errors = validator.validate
expect(errors).to eq({})
- expect(validator.instance_variable_get(:@object)[:upload_property]).to eq(upload.id)
end
it "should return the right hash of error messages when value is an invalid URL string" do
@@ -801,7 +800,7 @@ RSpec.describe SchemaSettingsObjectValidator do
expect(queries.length).to eq(1)
end
- it "should convert multiple upload URLs to IDs and validate successfully" do
+ it "should validate multiple upload IDs successfully" do
upload1 = Fabricate(:upload)
upload2 = Fabricate(:upload)
schema = {
@@ -816,13 +815,38 @@ RSpec.describe SchemaSettingsObjectValidator do
},
}
- object = { upload_property_1: upload1.url, upload_property_2: upload2.url }
+ object = { upload_property_1: upload1.id, upload_property_2: upload2.id }
validator = described_class.new(schema: schema, object: object)
errors = validator.validate
expect(errors).to eq({})
- expect(validator.instance_variable_get(:@object)[:upload_property_1]).to eq(upload1.id)
- expect(validator.instance_variable_get(:@object)[:upload_property_2]).to eq(upload2.id)
+ end
+
+ it "should accept and store upload IDs" do
+ upload1 = Fabricate(:upload)
+ upload2 = Fabricate(:upload)
+ schema = {
+ name: "section",
+ properties: {
+ upload_field_1: {
+ type: "upload",
+ },
+ upload_field_2: {
+ type: "upload",
+ },
+ },
+ }
+
+ objects = [{ upload_field_1: upload1.id, upload_field_2: upload2.id }]
+
+ # Validate
+ errors = described_class.validate_objects(schema: schema, objects: objects)
+ expect(errors).to eq([])
+
+ expect(objects.first[:upload_field_1]).to eq(upload1.id)
+ expect(objects.first[:upload_field_2]).to eq(upload2.id)
+ expect(objects.first[:upload_field_1]).to be_a(Integer)
+ expect(objects.first[:upload_field_2]).to be_a(Integer)
end
end