Skip to content

Commit

Permalink
Refactoring required token logic
Browse files Browse the repository at this point in the history
Move display logic into ExpressionParser.DisplayFor to reduce hits on
expression parsing.
Added a Global Setting for the token and whether or not to use it.
  • Loading branch information
zackpudil committed May 12, 2013
1 parent e15b764 commit 75614b0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 31 deletions.
6 changes: 2 additions & 4 deletions src/FluentKnockoutHelpers.Core/Builders/Builder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,9 @@ public virtual IHtmlString GetExpressionTextFor<TProp>(Expression<Func<TModel, T
/// <typeparam name="TProp"></typeparam>
/// <param name="propExpr"></param>
/// <returns></returns>
public virtual StringReturningBuilder<TModel> LabelFor<TProp>(Expression<Func<TModel, TProp>> propExpr, bool displayStarIfRequired = true)
public virtual StringReturningBuilder<TModel> LabelFor<TProp>(Expression<Func<TModel, TProp>> propExpr)
{
var required = displayStarIfRequired && (!ExpressionParser.ExpressionCanBeAssignedNull(propExpr) || ExpressionParser.ExpressionHasAttribute(propExpr, typeof(RequiredAttribute)));

return ElementSelfClosing("label", (required ? "*" : "") + DisplayNameFor(propExpr).ToString()).Attr("for", ExpressionParser.GetExpressionText(propExpr));
return ElementSelfClosing("label", DisplayNameFor(propExpr).ToString()).Attr("for", ExpressionParser.GetExpressionText(propExpr));
}

#region Bound ___ For
Expand Down
20 changes: 20 additions & 0 deletions src/FluentKnockoutHelpers.Core/GlobalSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace FluentKnockoutHelpers.Core
public static class GlobalSettings
{
private static IJsonSerializer _jsonSerializer = new DefaultJsonSerializer();
private static string _requiredToken = "*";
private static bool _useRequiredToken = true;

/// <summary>
/// Supply a custom IJsonSerializer to use something other than the default JSON.NET. Ex: ServiceStack
Expand All @@ -17,5 +19,23 @@ public static IJsonSerializer JsonSerializer
get { return _jsonSerializer; }
set { _jsonSerializer = value; }
}

/// <summary>
/// Supply a custom required token to use something other than the default "*"
/// </summary>
public static string RequiredToken
{
get { return _requiredToken; }
set { _requiredToken = value; }
}

/// <summary>
/// Set whether or not to use a required token on the display name for non-nullable/required properties
/// </summary>
public static bool UseRequiredToken
{
get { return _useRequiredToken; }
set { _useRequiredToken = value; }
}
}
}
30 changes: 3 additions & 27 deletions src/FluentKnockoutHelpers.Core/Utility/ExpressionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,36 +49,12 @@ public static string DisplayNameFor<TParameter, TValue>(Expression<Func<TParamet
var propertyName = memberExpression.Member.Name;
var propMetadata = metaData.Properties.FirstOrDefault(p => p.PropertyName == propertyName);

var requiredToken = (GlobalSettings.UseRequiredToken && propMetadata.IsRequired ? GlobalSettings.RequiredToken : String.Empty);

if (propMetadata == null)
return CamelCaseSpacer(propertyName);

return propMetadata.DisplayName ?? CamelCaseSpacer(propMetadata.PropertyName);
}

/// <summary>
/// Determine if the property in the Lambda Expression can be assigned a null
/// </summary>
/// <param name="expr">Expression containing property to check</param>
/// <returns></returns>
public static bool ExpressionCanBeAssignedNull(LambdaExpression expr)
{
var type = ToMemberExpression(expr).Type;
return !type.IsValueType || (Nullable.GetUnderlyingType(type) != null);
}

/// <summary>
/// Determine if the property in the Lambda Expression has a paramater attribute type on it.
/// </summary>
/// <param name="expr">Expression containing the property</param>
/// <param name="type">The type of attribute to check for</param>
/// <returns></returns>
public static bool ExpressionHasAttribute(LambdaExpression expr, Type type)
{
/*TODO:
* I'm assuming that the first paramater of the expression is the Class that contains the property to check.
* May not be the best way of doing business, ifyaknowwhatimsaying.
*/
return expr.Parameters[0].Type.GetProperty(GetExpressionText(expr)).GetCustomAttributes(type, false).Length != 0;
return requiredToken + (propMetadata.DisplayName ?? CamelCaseSpacer(propMetadata.PropertyName));
}

//FirstName => First Name
Expand Down

0 comments on commit 75614b0

Please sign in to comment.