Skip to content

Commit

Permalink
Add WithPart<> and WithSettings<> builder methods (OrchardCMS#15426)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek authored and urbanit committed Mar 18, 2024
1 parent 2402dfd commit 00caf71
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,37 @@ public ContentPartFieldDefinitionBuilder MergeSettings(JsonObject settings)
// If existing settings do not exist, create.
if (existingJObject == null)
{
existingJObject = JObject.FromObject(new T(), ContentBuilderSettings.IgnoreDefaultValuesSerializer);
existingJObject = ToJsonObject(new T());
_settings[typeof(T).Name] = existingJObject;
}

var settingsToMerge = existingJObject.ToObject<T>();
setting(settingsToMerge);
_settings[typeof(T).Name] = JObject.FromObject(settingsToMerge, ContentBuilderSettings.IgnoreDefaultValuesSerializer);
_settings[typeof(T).Name] = ToJsonObject(settingsToMerge);
return this;
}

public ContentPartFieldDefinitionBuilder WithSettings<T>(T settings)
{
ArgumentNullException.ThrowIfNull(settings);

var jObject = JObject.FromObject(settings, ContentBuilderSettings.IgnoreDefaultValuesSerializer);
_settings[typeof(T).Name] = jObject;
_settings[typeof(T).Name] = ToJsonObject(settings);

return this;
}

public ContentPartFieldDefinitionBuilder WithSettings<T>() where T : class, new()
{
_settings[typeof(T).Name] = ToJsonObject(new T());

return this;
}

public abstract ContentPartFieldDefinitionBuilder OfType(ContentFieldDefinition fieldDefinition);
public abstract ContentPartFieldDefinitionBuilder OfType(string fieldType);
public abstract ContentPartFieldDefinition Build();

private static JsonObject ToJsonObject(object obj)
=> JObject.FromObject(obj, ContentBuilderSettings.IgnoreDefaultValuesSerializer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ public ContentTypeDefinitionBuilder WithPart<TPart>(string name) where TPart : C
public ContentTypeDefinitionBuilder WithPart<TPart>(string name, Action<ContentTypePartDefinitionBuilder> configuration) where TPart : ContentPart
=> WithPart(name, new ContentPartDefinition(typeof(TPart).Name), configuration);

public ContentTypeDefinitionBuilder WithPart<TPart>(Action<ContentTypePartDefinitionBuilder> configuration) where TPart : ContentPart
=> WithPart(typeof(TPart).Name, configuration);

public Task<ContentTypeDefinitionBuilder> WithPartAsync(string name, string partName, Func<ContentTypePartDefinitionBuilder, Task> configurationAsync)
=> WithPartAsync(name, new ContentPartDefinition(partName), configurationAsync);

Expand Down

0 comments on commit 00caf71

Please sign in to comment.