Implement content.FirstChild
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using NUnit.Framework;
|
||||
@@ -31,7 +32,50 @@ namespace Umbraco.Tests.PublishedContent
|
||||
return GetNode(id).AsDynamic();
|
||||
}
|
||||
|
||||
[Test]
|
||||
[Test]
|
||||
public void FirstChild()
|
||||
{
|
||||
var content = GetDynamicNode(1173);
|
||||
|
||||
var x = content.FirstChild();
|
||||
Assert.IsNotNull(x);
|
||||
Assert.IsInstanceOf<DynamicPublishedContent>(x);
|
||||
Assert.AreEqual(1174, x.Id);
|
||||
|
||||
x = content.FirstChild("CustomDocument");
|
||||
Assert.IsNotNull(x);
|
||||
Assert.IsInstanceOf<DynamicPublishedContent>(x);
|
||||
Assert.AreEqual(1177, x.Id);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Children()
|
||||
{
|
||||
var content = GetDynamicNode(1173);
|
||||
|
||||
var l = content.Children;
|
||||
Assert.AreEqual(4, l.Count());
|
||||
|
||||
// works - but not by calling the extension method
|
||||
// because the binder will in fact re-route to the property first
|
||||
l = content.Children();
|
||||
Assert.AreEqual(4, l.Count());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ChildrenOfType()
|
||||
{
|
||||
var content = GetDynamicNode(1173);
|
||||
|
||||
var l = content.Children;
|
||||
Assert.AreEqual(4, l.Count());
|
||||
|
||||
// fails - because it fails to find extension methods?
|
||||
l = content.Children("CustomDocument");
|
||||
Assert.AreEqual(2, l.Count());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Custom_Extension_Methods()
|
||||
{
|
||||
var asDynamic = GetDynamicNode(1173);
|
||||
|
||||
@@ -27,5 +27,15 @@ namespace Umbraco.Web.Dynamics
|
||||
{
|
||||
return source.Items.OrderBy(x => Guid.NewGuid());
|
||||
}
|
||||
|
||||
public static DynamicPublishedContentList Children(this DynamicPublishedContent content)
|
||||
{
|
||||
return content.Children;
|
||||
}
|
||||
|
||||
public static DynamicPublishedContentList Children(this DynamicPublishedContent content, string contentTypeAlias)
|
||||
{
|
||||
return new DynamicPublishedContentList(content.PublishedContent.Children().OfTypes(contentTypeAlias));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1254,6 +1254,16 @@ namespace Umbraco.Web.Models
|
||||
{
|
||||
get { return _children ?? (_children = new DynamicPublishedContentList(PublishedContent.Children)); }
|
||||
}
|
||||
|
||||
public DynamicPublishedContent FirstChild()
|
||||
{
|
||||
return Children.FirstOrDefault<DynamicPublishedContent>();
|
||||
}
|
||||
|
||||
public DynamicPublishedContent FirstChild(string alias)
|
||||
{
|
||||
return Children.FirstOrDefault<IPublishedContent>(x => x.DocumentTypeAlias == alias) as DynamicPublishedContent;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
@@ -1641,6 +1641,22 @@ namespace Umbraco.Web
|
||||
return content.Children().OfType<T>();
|
||||
}
|
||||
|
||||
public static IPublishedContent FirstChild(this IPublishedContent content)
|
||||
{
|
||||
return content.Children().First();
|
||||
}
|
||||
|
||||
public static IPublishedContent FirstChild(this IPublishedContent content, Func<IPublishedContent, bool> predicate)
|
||||
{
|
||||
return content.Children(predicate).FirstOrDefault();
|
||||
}
|
||||
|
||||
public static IPublishedContent FirstChild<T>(this IPublishedContent content)
|
||||
where T : class, IPublishedContent
|
||||
{
|
||||
return content.Children<T>().FirstOrDefault();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the children of the content in a DataTable.
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user