Skip to content
This repository has been archived by the owner on Dec 13, 2021. It is now read-only.

Commit

Permalink
Renamed UmbracoPropertyPrefix attribute to UmbracoProperties and adde…
Browse files Browse the repository at this point in the history
…d support for class level recursive flag settings
  • Loading branch information
mattbrailsford committed Jun 11, 2015
1 parent 1eaf9e1 commit d97118e
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,25 @@
using System;

/// <summary>
/// The Umbraco property prefix attribute.
/// The Umbraco properties attribute.
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class UmbracoPropertyPrefixAttribute : Attribute
public class UmbracoPropertiesAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="UmbracoPropertyAttribute"/> class.
/// Initializes a new instance of the <see cref="UmbracoPropertiesAttribute"/> class.
/// </summary>
/// <param name="prefix">
/// The property name.
/// </param>
public UmbracoPropertyPrefixAttribute(
string prefix)
{
this.Prefix = prefix;
}
public UmbracoPropertiesAttribute()
{ }

/// <summary>
/// Gets or sets the prefix.
/// </summary>
public string Prefix { get; set; }

/// <summary>
/// Gets or sets whether the properties should be retrieved recursively up the tree.
/// </summary>
public bool Recursive { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,31 @@ public override object ResolveValue(ITypeDescriptorContext context, UmbracoPrope
{
var defaultValue = attribute.DefaultValue;

var recursive = attribute.Recursive;
var propName = context.PropertyDescriptor != null ? context.PropertyDescriptor.Name : string.Empty;
var altPropName = "";

// Check for umbraco properties attribute on class
if (context.PropertyDescriptor != null)
{
var prefixAttr = context.PropertyDescriptor.ComponentType
.GetCustomAttribute<UmbracoPropertyPrefixAttribute>();
if (prefixAttr != null)
var classAttr = context.PropertyDescriptor.ComponentType
.GetCustomAttribute<UmbracoPropertiesAttribute>();
if (classAttr != null)
{
altPropName = propName;
propName = prefixAttr.Prefix + propName;
// Apply the prefix
if (!string.IsNullOrWhiteSpace(classAttr.Prefix))
{
altPropName = propName;
propName = classAttr.Prefix + propName;
}

// Apply global recursive setting
recursive |= classAttr.Recursive;
}
}

var umbracoPropertyName = attribute.PropertyName ?? propName;
var altUmbracoPropertyName = attribute.AltPropertyName ?? altPropName;
var recursive = attribute.Recursive;

var content = context.Instance as IPublishedContent;
if (content == null)
Expand Down
2 changes: 1 addition & 1 deletion src/Our.Umbraco.Ditto/Our.Umbraco.Ditto.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
<Compile Include="Attributes\DittoIgnoreAttribute.cs" />
<Compile Include="Attributes\DittoValueResolverAttribute.cs" />
<Compile Include="Attributes\UmbracoDictionaryAttribute.cs" />
<Compile Include="Attributes\UmbracoPropertyPrefixAttribute.cs" />
<Compile Include="Attributes\UmbracoPropertiesAttribute.cs" />
<Compile Include="Attributes\UmbracoPropertyAttribute.cs" />
<Compile Include="Common\EnumerableInvocations.cs" />
<Compile Include="Common\MethodBaseCacheItem.cs" />
Expand Down
9 changes: 8 additions & 1 deletion tests/Our.Umbraco.Ditto.Tests/Mocks/ContentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class ContentBuilder
{
private int _id = 1234;
private string _name = "Name";
private IPublishedContent _parent;
private List<IPublishedContent> _children = new List<IPublishedContent>();
private List<IPublishedContentProperty> _properties = new List<IPublishedContentProperty>();

Expand Down Expand Up @@ -49,9 +50,15 @@ public ContentBuilder AddChild(IPublishedContent child)
return this;
}

public ContentBuilder AddParent(IPublishedContent parent)
{
_parent = parent;
return this;
}

public IPublishedContent Build()
{
return new PublishedContentMock(_id, _name, _children, _properties);
return new PublishedContentMock(_id, _name, _parent, _children, _properties);
}
}
}
68 changes: 42 additions & 26 deletions tests/Our.Umbraco.Ditto.Tests/Mocks/PublishedContentMock.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace Our.Umbraco.Ditto.Tests.Mocks
using System.Collections.ObjectModel;
using NUnit.Framework;

namespace Our.Umbraco.Ditto.Tests.Mocks
{
using System;
using System.Collections.Generic;
Expand All @@ -15,15 +18,23 @@
/// </summary>
public class PublishedContentMock : IPublishedContent
{
public PublishedContentMock()
{
Properties = new Collection<IPublishedContentProperty>();
Children = new List<IPublishedContent>();
}

public PublishedContentMock(
int id,
string name,
IPublishedContent _parent,
IEnumerable<IPublishedContent> children,
ICollection<IPublishedContentProperty> properties)
{
Properties = properties;
Id = id;
Name = name;
Parent = Parent;
Children = children;
}

Expand All @@ -39,56 +50,61 @@ public IPublishedContentProperty GetProperty(string alias)

public IPublishedContentProperty GetProperty(string alias, bool recurse)
{
return Properties.SingleOrDefault(p => p.Alias.InvariantEquals(alias));
var prop = Properties.SingleOrDefault(p => p.Alias.InvariantEquals(alias));
if (prop == null && recurse && Parent != null)
{
return Parent.GetProperty(alias, recurse);
}
return prop;
}

public IEnumerable<IPublishedContent> ContentSet { get; private set; }
public IEnumerable<IPublishedContent> ContentSet { get; set; }

public PublishedContentType ContentType { get; private set; }
public PublishedContentType ContentType { get; set; }

public int Id { get; private set; }
public int Id { get; set; }

public int TemplateId { get; private set; }
public int TemplateId { get; set; }

public int SortOrder { get; private set; }
public int SortOrder { get; set; }

public string Name { get; private set; }
public string Name { get; set; }

public string UrlName { get; private set; }
public string UrlName { get; set; }

public string DocumentTypeAlias { get; private set; }
public string DocumentTypeAlias { get; set; }

public int DocumentTypeId { get; private set; }
public int DocumentTypeId { get; set; }

public string WriterName { get; private set; }
public string WriterName { get; set; }

public string CreatorName { get; private set; }
public string CreatorName { get; set; }

public int WriterId { get; private set; }
public int WriterId { get; set; }

public int CreatorId { get; private set; }
public int CreatorId { get; set; }

public string Path { get; private set; }
public string Path { get; set; }

public DateTime CreateDate { get; private set; }
public DateTime CreateDate { get; set; }

public DateTime UpdateDate { get; private set; }
public DateTime UpdateDate { get; set; }

public Guid Version { get; private set; }
public Guid Version { get; set; }

public int Level { get; private set; }
public int Level { get; set; }

public string Url { get; private set; }
public string Url { get; set; }

public PublishedItemType ItemType { get; private set; }
public PublishedItemType ItemType { get; set; }

public bool IsDraft { get; private set; }
public bool IsDraft { get; set; }

public IPublishedContent Parent { get; private set; }
public IPublishedContent Parent { get; set; }

public IEnumerable<IPublishedContent> Children { get; private set; }
public IEnumerable<IPublishedContent> Children { get; set; }

public ICollection<IPublishedContentProperty> Properties { get; private set; }
public ICollection<IPublishedContentProperty> Properties { get; set; }

public object this[string alias]
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,28 @@

public class PublishedContentPropertyMock : IPublishedContentProperty
{
public PublishedContentPropertyMock()
{ }

public PublishedContentPropertyMock(string alias, object value, bool hasValue)
{
HasValue = hasValue;
Alias = alias;
Value = value;
}

public string PropertyTypeAlias { get; private set; }
public string PropertyTypeAlias { get; set; }

public bool HasValue { get; private set; }
public bool HasValue { get; set; }

public object DataValue { get; private set; }
public object DataValue { get; set; }

public object Value { get; private set; }
public object Value { get; set; }

public object XPathValue { get; private set; }
public object XPathValue { get; set; }

public string Alias { get; private set; }
public string Alias { get; set; }

public Guid Version { get; private set; }
public Guid Version { get; set; }
}
}
2 changes: 1 addition & 1 deletion tests/Our.Umbraco.Ditto.Tests/Models/PrefixedModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Our.Umbraco.Ditto.Tests.Models
{
[UmbracoPropertyPrefix("Site")]
[UmbracoProperties(Prefix = "Site", Recursive = true)]
public class PrefixedModel
{
public string Name { get; set; }
Expand Down
31 changes: 29 additions & 2 deletions tests/Our.Umbraco.Ditto.Tests/PublishedContentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void Content_To_String()
}

[Test]
public void Prefix_Attribute_Resolves_Prefixed_Properties()
public void Can_Resolve_Prefixed_Properties()
{
var prop1 = PropertyBuilder.Default("siteName", "Name").Build();
var prop2 = PropertyBuilder.Default("siteDescription", "Description").Build();
Expand All @@ -129,7 +129,7 @@ public void Prefix_Attribute_Resolves_Prefixed_Properties()
}

[Test]
public void Umbraco_Property_Attribute_Overrides_Prefix_Attribute()
public void Umbraco_Property_Attribute_Overrides_Prefix()
{
var prop1 = PropertyBuilder.Default("siteUnprefixedProp", "Site Unprefixed").Build();
var prop2 = PropertyBuilder.Default("unprefixedProp", "Unprefixed").Build();
Expand All @@ -143,5 +143,32 @@ public void Umbraco_Property_Attribute_Overrides_Prefix_Attribute()

Assert.That(converted.UnprefixedProp, Is.EqualTo("Unprefixed"));
}

[Test]
public void Can_Resolve_Recursive_Properties_Via_Umbraco_Properties_Attribute()
{
var childContent = new PublishedContentMock();
var parentContent = new PublishedContentMock
{
Properties = new[]
{
new PublishedContentPropertyMock
{
Alias = "description",
Value = "Description"
}
},
Children = new[]
{
childContent
}
};

childContent.Parent = parentContent;

var converted = childContent.As<PrefixedModel>();

Assert.That(converted.Description, Is.EqualTo("Description"));
}
}
}

0 comments on commit d97118e

Please sign in to comment.