New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

U4-7318 Core property value converters #1570

Merged
merged 11 commits into from Nov 3, 2016

Added dropdown list multiple value converter

  • Loading branch information...
jamescoxhead committed Nov 1, 2016
commit cee92518ea0b6d61d4b449073ae02a9c31ad1350
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core.Models.PublishedContent;
namespace Umbraco.Core.PropertyEditors.ValueConverters
{
public class DropdownListMultipleValueConverter : PropertyValueConverterBase, IPropertyValueConverterMeta
{
public override bool IsConverter(PublishedPropertyType propertyType)
{
return propertyType.PropertyEditorAlias.InvariantEquals(Constants.PropertyEditors.DropDownListMultipleAlias);
}
public override object ConvertSourceToObject(PublishedPropertyType propertyType, object source, bool preview)
{
var sourceString = (source ?? "").ToString();
if (string.IsNullOrEmpty(sourceString))
return Enumerable.Empty<string>();
var values =
sourceString.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(v => v.Trim());
return values;
}
public Type GetPropertyValueType(PublishedPropertyType propertyType)
{
return typeof(IEnumerable<string>);
}
public PropertyCacheLevel GetPropertyCacheLevel(PublishedPropertyType propertyType,
PropertyCacheValue cacheValue)
{
return PropertyCacheLevel.Content;
}
}
}
@@ -498,6 +498,7 @@
<Compile Include="PropertyEditors\DecimalValidator.cs" />
<Compile Include="PropertyEditors\PropertyEditorValueTypes.cs" />
<Compile Include="PropertyEditors\ValueConverters\CheckboxListValueConverter.cs" />
<Compile Include="PropertyEditors\ValueConverters\DropdownListMultipleValueConverter.cs" />
<Compile Include="PropertyEditors\ValueConverters\EmailAddressValueConverter.cs" />
<Compile Include="PropertyEditors\ValueConverters\GridValueConverter.cs" />
<Compile Include="PropertyEditors\ValueConverters\DecimalValueConverter.cs" />
@@ -70,5 +70,18 @@ public void CanConvertCheckboxListPropertyEditor(object value, IEnumerable<strin
Assert.AreEqual(expected, result);
}
[TestCase("apples", new[] {"apples"})]
[TestCase("apples,oranges", new[] {"apples", "oranges"})]
[TestCase("apples , oranges, pears ", new[] {"apples", "oranges", "pears"})]
[TestCase("", new string[] {})]
[TestCase(null, new string[] {})]
public void CanConvertDropdownListMultiplePropertyEditor(object value, IEnumerable<string> expected)
{
var converter = new DropdownListMultipleValueConverter();
var result = converter.ConvertSourceToObject(null, value, false);
Assert.AreEqual(expected, result);
}
}
}
ProTip! Use n and p to navigate between commits in a pull request.