From 68c2f5511188f8a19bc8024903e600cc076efa69 Mon Sep 17 00:00:00 2001 From: leekelleher Date: Wed, 17 Feb 2016 13:30:43 +0000 Subject: [PATCH] Added support to ArchetypePublishedContent for Umbraco extension methods For the `IPublishedContent` implementation, added support for `GetIndex()`, so that extension methods, such as `IsFirst()`, `IsLast()`, etc will work as expected. Unfortunately we wont be able to support the `Siblings()`, (or `FollowingSibling`, `PrecedingSibling`) extension methods, as these perform an internal query to the node's parent's children - which in the context of Archetype data is not possible access. Added unit-tests to support this enhancement. --- .../ArchetypePublishedContentTests.cs | 61 +++++++++++++++++++ .../ArchetypeFieldsetModelExtensions.cs | 14 ++++- .../Models/ArchetypePublishedContent.cs | 9 ++- .../Models/ArchetypePublishedContentSet.cs | 5 +- 4 files changed, 84 insertions(+), 5 deletions(-) diff --git a/app/Umbraco/Archetype.Tests/PublishedContent/ArchetypePublishedContentTests.cs b/app/Umbraco/Archetype.Tests/PublishedContent/ArchetypePublishedContentTests.cs index 27198cf..2fa05d5 100644 --- a/app/Umbraco/Archetype.Tests/PublishedContent/ArchetypePublishedContentTests.cs +++ b/app/Umbraco/Archetype.Tests/PublishedContent/ArchetypePublishedContentTests.cs @@ -91,5 +91,66 @@ namespace Archetype.Tests.PublishedContent Assert.Throws(code); } + + [Test] + public void ArchetypePublishedContent_GetIndex() + { + var contentSet = _archetype.ToPublishedContentSet(); + var i = 0; + + foreach (var content in contentSet) + { + Assert.AreEqual(content.GetIndex(), i); + i++; + } + } + + [Test] + public void ArchetypePublishedContentSet_IsFirstIsLast() + { + var contentSet = _archetype.ToPublishedContentSet(); + + foreach (var content in contentSet) + { + var value = content.GetPropertyValue("boxHeadline"); + + Assert.That(value == "Box 1 Title", Is.EqualTo(content.IsFirst())); + Assert.That(value == "Another Box", Is.EqualTo(content.IsLast())); + } + } + + [Test] + public void ArchetypePublishedContentSet_PreviousNext() + { + var contentSet = _archetype.ToPublishedContentSet(); + var list = contentSet.ToList(); + + Assert.That(list.Count, Is.EqualTo(2)); + + var first = list[0]; + var last = list[1]; + + var next = first.Next(); + Assert.AreEqual(next, last); + + var prev = last.Previous(); + Assert.AreEqual(prev, first); + } + + [Test, ExpectedException(typeof(NullReferenceException))] + public void ArchetypePublishedContentSet_Siblings() + { + var contentSet = _archetype.ToPublishedContentSet(); + var content = contentSet.FirstOrDefault(); + + var siblings = content.Siblings(); + Assert.That(siblings, Is.Not.Null); + + var following = content.FollowingSibling(); + Assert.That(following, Is.Not.Null); + + var preceding = content.PrecedingSibling(); + Assert.That(preceding, Is.Not.Null); + } } } \ No newline at end of file diff --git a/app/Umbraco/Umbraco.Archetype/Extensions/ArchetypeFieldsetModelExtensions.cs b/app/Umbraco/Umbraco.Archetype/Extensions/ArchetypeFieldsetModelExtensions.cs index 1cf6933..1ccb442 100644 --- a/app/Umbraco/Umbraco.Archetype/Extensions/ArchetypeFieldsetModelExtensions.cs +++ b/app/Umbraco/Umbraco.Archetype/Extensions/ArchetypeFieldsetModelExtensions.cs @@ -1,4 +1,5 @@ -using Archetype.Models; +using System.Linq; +using Archetype.Models; using Umbraco.Core.Models; namespace Archetype.Extensions @@ -9,5 +10,16 @@ namespace Archetype.Extensions { return new ArchetypePublishedContent(fieldset); } + + public static IPublishedContent ToPublishedContent(this ArchetypeFieldsetModel fieldset, ArchetypeModel archetype) + { + var contentSet = archetype.ToPublishedContentSet(); + + var first = contentSet + .Cast() + .FirstOrDefault(x => x.ArchetypeFieldset == fieldset); + + return first ?? new ArchetypePublishedContent(fieldset); + } } } \ No newline at end of file diff --git a/app/Umbraco/Umbraco.Archetype/Models/ArchetypePublishedContent.cs b/app/Umbraco/Umbraco.Archetype/Models/ArchetypePublishedContent.cs index b691431..b22b850 100644 --- a/app/Umbraco/Umbraco.Archetype/Models/ArchetypePublishedContent.cs +++ b/app/Umbraco/Umbraco.Archetype/Models/ArchetypePublishedContent.cs @@ -11,14 +11,17 @@ namespace Archetype.Models { private ArchetypeFieldsetModel _fieldset; + private IEnumerable _parent; + private readonly Dictionary _properties; - public ArchetypePublishedContent(ArchetypeFieldsetModel fieldset) + public ArchetypePublishedContent(ArchetypeFieldsetModel fieldset, ArchetypePublishedContentSet parent = null) { if (fieldset == null) throw new ArgumentNullException("fieldset"); _fieldset = fieldset; + _parent = parent ?? Enumerable.Empty(); _properties = fieldset.Properties .ToDictionary( @@ -39,7 +42,7 @@ namespace Archetype.Models public IEnumerable ContentSet { - get { return Enumerable.Empty(); } + get { return _parent; } } public PublishedContentType ContentType @@ -74,7 +77,7 @@ namespace Archetype.Models public int GetIndex() { - return default(int); + return _parent.IndexOf(this); } public IPublishedProperty GetProperty(string alias, bool recurse) diff --git a/app/Umbraco/Umbraco.Archetype/Models/ArchetypePublishedContentSet.cs b/app/Umbraco/Umbraco.Archetype/Models/ArchetypePublishedContentSet.cs index 5a529d9..88a9397 100644 --- a/app/Umbraco/Umbraco.Archetype/Models/ArchetypePublishedContentSet.cs +++ b/app/Umbraco/Umbraco.Archetype/Models/ArchetypePublishedContentSet.cs @@ -16,9 +16,12 @@ namespace Archetype.Models this.ArchetypeModel = archetype; + var count = archetype.Fieldsets.Count(); + _items = archetype.Fieldsets .Where(x => x.Disabled == false) - .Select(x => new ArchetypePublishedContent(x)); + .Select(x => new ArchetypePublishedContent(x, this)) + .ToArray(); } internal ArchetypeModel ArchetypeModel { get; private set; }