Fixing shifted content versionning

This commit is contained in:
Sebastien Ros
2014-03-27 06:52:05 -07:00
parent 14674a9908
commit 1ca5fa5483
3 changed files with 56 additions and 34 deletions

View File

@@ -49,12 +49,12 @@ namespace Orchard.Users.Models {
}
public UserStatus RegistrationStatus {
get { return Retrieve(x => x.RegistrationStatus); }
get { return Retrieve(x => x.RegistrationStatus, UserStatus.Approved); }
set { Store(x => x.RegistrationStatus, value); }
}
public UserStatus EmailStatus {
get { return Retrieve(x => x.EmailStatus); }
get { return Retrieve(x => x.EmailStatus, UserStatus.Approved); }
set { Store(x => x.EmailStatus, value); }
}
}

View File

@@ -116,6 +116,13 @@ namespace Orchard.ContentManagement {
return InfosetHelper.Retrieve(this, targetExpression, defaultExpression);
}
protected TProperty Retrieve<TProperty>(
Expression<Func<TRecord, TProperty>> targetExpression,
TProperty defaultValue) {
return InfosetHelper.Retrieve(this, targetExpression, (Func<TRecord, TProperty>)(x => defaultValue));
}
protected ContentPart<TRecord> Store<TProperty>(
Expression<Func<TRecord, TProperty>> targetExpression,
TProperty value) {

View File

@@ -2,6 +2,7 @@
using System.Linq.Expressions;
using System.Xml.Linq;
using Orchard.ContentManagement.FieldStorage.InfosetStorage;
using Orchard.ContentManagement.Records;
using Orchard.Utility;
namespace Orchard.ContentManagement {
@@ -34,6 +35,50 @@ namespace Orchard.ContentManagement {
return el == null ? default(TProperty) : el.Attr<TProperty>(name);
}
public static TProperty Retrieve<TPart, TRecord, TProperty>(this TPart contentPart,
Expression<Func<TRecord, TProperty>> targetExpression)
where TPart : ContentPart<TRecord> {
var getter = ReflectionHelper<TRecord>.GetGetter(targetExpression);
return contentPart.Retrieve(targetExpression, getter);
}
public static TProperty Retrieve<TPart, TRecord, TProperty>(this TPart contentPart,
Expression<Func<TRecord, TProperty>> targetExpression,
Delegate defaultExpression)
where TPart : ContentPart<TRecord> {
var propertyInfo = ReflectionHelper<TRecord>.GetPropertyInfo(targetExpression);
var name = propertyInfo.Name;
var infosetPart = contentPart.As<InfosetPart>();
var versioned = typeof(ContentPartVersionRecord).IsAssignableFrom(typeof(TRecord));
if (infosetPart == null) {
// Property has never been stored. Get it from the default expression and store that.
var defaultValue = defaultExpression == null
? default(TProperty)
: (TProperty)defaultExpression.DynamicInvoke(contentPart.Record);
contentPart.Store(name, defaultValue, versioned);
return defaultValue;
}
else {
var infoset = versioned ? infosetPart.VersionInfoset.Element : infosetPart.Infoset.Element;
var el = infoset.Element(contentPart.GetType().Name);
if (el == null || el.Attribute(name) == null) {
var defaultValue = defaultExpression == null
? default(TProperty)
: (TProperty)defaultExpression.DynamicInvoke(contentPart.Record);
contentPart.Store(name, defaultValue, versioned);
return defaultValue;
}
return el.Attr<TProperty>(name);
}
}
public static void Store<TPart, TProperty>(this TPart contentPart,
Expression<Func<TPart, TProperty>> targetExpression,
TProperty value, bool versioned = false) where TPart : ContentPart {
@@ -66,37 +111,6 @@ namespace Orchard.ContentManagement {
partElement.Attr(name, value);
}
public static TProperty Retrieve<TPart, TRecord, TProperty>(this TPart contentPart,
Expression<Func<TRecord, TProperty>> targetExpression)
where TPart : ContentPart<TRecord> {
var getter = ReflectionHelper<TRecord>.GetGetter(targetExpression);
return contentPart.Retrieve(targetExpression, getter);
}
public static TProperty Retrieve<TPart, TRecord, TProperty>(this TPart contentPart,
Expression<Func<TRecord, TProperty>> targetExpression,
Delegate defaultExpression)
where TPart : ContentPart<TRecord> {
var propertyInfo = ReflectionHelper<TRecord>.GetPropertyInfo(targetExpression);
var name = propertyInfo.Name;
var infosetPart = contentPart.As<InfosetPart>();
var el = infosetPart == null
? null
: infosetPart.Infoset.Element.Element(contentPart.GetType().Name);
if (el == null || el.Attribute(name) == null) {
// Property has never been stored. Get it from the default expression and store that.
var defaultValue = defaultExpression == null
? default(TProperty)
: (TProperty)defaultExpression.DynamicInvoke(contentPart.Record);
contentPart.Store(name, defaultValue);
return defaultValue;
}
return el.Attr<TProperty>(name);
}
public static void Store<TPart, TRecord, TProperty>(this TPart contentPart,
Expression<Func<TRecord, TProperty>> targetExpression,
TProperty value)
@@ -104,8 +118,9 @@ namespace Orchard.ContentManagement {
var propertyInfo = ReflectionHelper<TRecord>.GetPropertyInfo(targetExpression);
var name = propertyInfo.Name;
var versioned = typeof(ContentPartVersionRecord).IsAssignableFrom(typeof(TRecord));
propertyInfo.SetValue(contentPart.Record, value, null);
contentPart.Store(name, value);
contentPart.Store(name, value, versioned);
}
}
}