internal static bool IsFilterableColumn(IMetaColumn column, IPrincipal user) { DisplayAttribute attribute = column.Attributes.FirstOrDefault <DisplayAttribute>(); if ((attribute != null) && attribute.GetAutoGenerateFilter().HasValue) { return(attribute.GetAutoGenerateFilter().Value); } if (!string.IsNullOrEmpty(column.FilterUIHint)) { return(true); } if (!column.Scaffold) { return(false); } if (column.IsCustomProperty) { return(false); } IMetaForeignKeyColumn column2 = column as IMetaForeignKeyColumn; if (column2 != null) { return(column2.ParentTable.CanRead(user)); } return((column.ColumnType == typeof(bool)) || (column.GetEnumType() != null)); }
public void AutoGenerateFilter_Get_NotSet_ThrowsInvalidOperationException() { DisplayAttribute attribute = new DisplayAttribute(); Assert.Throws <InvalidOperationException>(() => attribute.AutoGenerateFilter); Assert.Null(attribute.GetAutoGenerateFilter()); }
public void AutoGenerateFilter_Get_Set(bool value) { DisplayAttribute attribute = new DisplayAttribute(); attribute.AutoGenerateFilter = value; Assert.Equal(value, attribute.AutoGenerateFilter); Assert.Equal(value, attribute.GetAutoGenerateFilter()); }
public void DisplayAttribute_AutoGenerateFilter_Can_Be_Set_And_Retrieved() { DisplayAttribute attr = new DisplayAttribute { AutoGenerateFilter = true }; Assert.IsTrue(attr.AutoGenerateFilter, "AutoGenerateFilter should be true after setting it to true"); Assert.IsTrue(attr.GetAutoGenerateFilter().Value, "GetAutoGenerateFilter should be true after setting it to true"); attr = new DisplayAttribute { AutoGenerateFilter = false }; Assert.IsFalse(attr.AutoGenerateFilter, "AutoGenerateFilter should be false after setting it to false"); Assert.IsFalse(attr.GetAutoGenerateFilter().Value, "GetAutoGenerateFilter should be false after setting it to false"); }
public void OrderAndAutoGenerateProperties_Success() { var display = new DisplayAttribute() { Order = 1, AutoGenerateField = true, AutoGenerateFilter = false }; Assert.AreEqual(1, display.Order); Assert.AreEqual(1, display.GetOrder()); Assert.AreEqual(true, display.AutoGenerateField); Assert.AreEqual(true, display.GetAutoGenerateField()); Assert.AreEqual(false, display.AutoGenerateFilter); Assert.AreEqual(false, display.GetAutoGenerateFilter()); }
public static DisplayAttribute Copy(this DisplayAttribute srcAttribute) { if (srcAttribute == null) { return(null); } var copy = new DisplayAttribute { Name = srcAttribute.Name, GroupName = srcAttribute.GroupName, Description = srcAttribute.Description, ResourceType = srcAttribute.ResourceType, ShortName = srcAttribute.ShortName, Prompt = srcAttribute.Prompt }; var order = srcAttribute.GetOrder(); if (order != null) { copy.Order = order.Value; } var autoGenerateField = srcAttribute.GetAutoGenerateField(); if (autoGenerateField != null) { copy.AutoGenerateField = autoGenerateField.Value; } var autoGenerateFilter = srcAttribute.GetAutoGenerateFilter(); if (autoGenerateFilter != null) { copy.AutoGenerateFilter = autoGenerateFilter.Value; } return(copy); }
public static DisplayAttribute Copy(this DisplayAttribute attribute) { if (attribute == null) { return(null); } // DisplayAttribute is sealed, so it's safe to copy. var copy = new DisplayAttribute { Name = attribute.Name, GroupName = attribute.GroupName, Description = attribute.Description, ResourceType = attribute.ResourceType, ShortName = attribute.ShortName, Prompt = attribute.Prompt, Order = attribute.GetOrder() ?? 10000, AutoGenerateField = attribute.GetAutoGenerateField() ?? true, AutoGenerateFilter = attribute.GetAutoGenerateFilter() ?? true }; return(copy); }
public bool?GetAutoGenerateFilter() { return(_innerAttribute.GetAutoGenerateFilter()); }
public override AttributeDeclaration GetAttributeDeclaration(Attribute attribute) { DisplayAttribute displayAttribute = (DisplayAttribute)attribute; AttributeDeclaration attributeDeclaration = new AttributeDeclaration(typeof(DisplayAttribute)); // By convention, the attribute parameters are not validated until an attempt is made to // access the resources. We do the following probe merely to trigger this validation process. // An InvalidOperationException will be thrown if the attribute is ill-formed. Type attributeType = attribute.GetType(); try { displayAttribute.GetName(); } catch (InvalidOperationException ex) { throw new AttributeBuilderException(ex, attributeType, "Name"); } try { displayAttribute.GetShortName(); } catch (InvalidOperationException ex) { throw new AttributeBuilderException(ex, attributeType, "ShortName"); } try { displayAttribute.GetDescription(); } catch (InvalidOperationException ex) { throw new AttributeBuilderException(ex, attributeType, "Description"); } try { displayAttribute.GetPrompt(); } catch (InvalidOperationException ex) { throw new AttributeBuilderException(ex, attributeType, "Prompt"); } // Add AutoGenerateField if (displayAttribute.GetAutoGenerateField().HasValue) { attributeDeclaration.NamedParameters.Add("AutoGenerateField", displayAttribute.AutoGenerateField); } // Add AutoGenerateFilter if (displayAttribute.GetAutoGenerateFilter().HasValue) { attributeDeclaration.NamedParameters.Add("AutoGenerateFilter", displayAttribute.AutoGenerateFilter); } // Add Description if (!string.IsNullOrEmpty(displayAttribute.Description)) { attributeDeclaration.NamedParameters.Add("Description", displayAttribute.Description); } // Add GroupName if (!string.IsNullOrEmpty(displayAttribute.GroupName)) { attributeDeclaration.NamedParameters.Add("GroupName", displayAttribute.GroupName); } // Add Name if (!string.IsNullOrEmpty(displayAttribute.Name)) { attributeDeclaration.NamedParameters.Add("Name", displayAttribute.Name); } // Add Order if (displayAttribute.GetOrder().HasValue) { attributeDeclaration.NamedParameters.Add("Order", displayAttribute.Order); } // Add Prompt if (!string.IsNullOrEmpty(displayAttribute.Prompt)) { attributeDeclaration.NamedParameters.Add("Prompt", displayAttribute.Prompt); } // Add ResourceType if (displayAttribute.ResourceType != null) { attributeDeclaration.NamedParameters.Add("ResourceType", displayAttribute.ResourceType); } // Add ShortName if (!string.IsNullOrEmpty(displayAttribute.ShortName)) { attributeDeclaration.NamedParameters.Add("ShortName", displayAttribute.ShortName); } return(attributeDeclaration); }
public void AutoGenerateFilter_Get_NotSet_ThrowsInvalidOperationException() { DisplayAttribute attribute = new DisplayAttribute(); Assert.Throws<InvalidOperationException>(() => attribute.AutoGenerateFilter); Assert.Null(attribute.GetAutoGenerateFilter()); }
protected override ModelMetadata CreateMetadata(IEnumerable <Attribute> attributes, Type containerType, Func <object> modelAccessor, Type modelType, string propertyName) { var modelMetadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName); DisplayAttribute da = attributes.OfType <DisplayAttribute>().FirstOrDefault(); if (da != null) { var autoGenerate = da.GetAutoGenerateFilter(); if (autoGenerate.HasValue) { modelMetadata.AdditionalValues["AutoGenerateFilter"] = autoGenerate.Value; } else { modelMetadata.AdditionalValues["AutoGenerateFilter"] = false; } } modelMetadata.ShowForDisplay = true; modelMetadata.ShowForEdit = true; //var renderModeAttribute = attributes.OfType<RenderModeAttribute>(); //if (renderModeAttribute.Any()) //{ // var renderMode = renderModeAttribute.First().RenderMode; // switch (renderMode) // { // case RenderMode.DisplayModeOnly: // modelMetadata.ShowForDisplay = true; // modelMetadata.ShowForEdit = false; // break; // case RenderMode.EditModeOnly: // modelMetadata.ShowForDisplay = false; // modelMetadata.ShowForEdit = true; // break; // case RenderMode.None: // modelMetadata.ShowForDisplay = false; // modelMetadata.ShowForEdit = false; // break; // } //} //var lookupMetaData = attributes.OfType<LookupAttribute>().FirstOrDefault(); //if (lookupMetaData != null) // modelMetadata.AdditionalValues.Add("LookupMetadata", lookupMetaData); //var lookupAdditionalMetaData = attributes.OfType<LookupAdditionalAttribute>().FirstOrDefault(); //if (lookupAdditionalMetaData != null) // modelMetadata.AdditionalValues.Add("LookupAdditionalMetadata", lookupAdditionalMetaData); //var multiLookupMetaData = attributes.OfType<MultiLookupAttribute>().FirstOrDefault(); //if (multiLookupMetaData != null) // modelMetadata.AdditionalValues.Add("MultiLookupMetadata", multiLookupMetaData); //var styleMetaData = attributes.OfType<StyleAttribute>().FirstOrDefault(); //if (styleMetaData != null) // modelMetadata.AdditionalValues.Add("StyleMetadata", styleMetaData); //string maxValue = null; //string minValue = null; ////decimal //var decimalMinValue = attributes.OfType<MinValueDecimalAttribute>().FirstOrDefault(); //if (decimalMinValue != null && !String.IsNullOrWhiteSpace(decimalMinValue.MinValue)) // minValue = decimalMinValue.MinValue; //var decimalMaxValue = attributes.OfType<MaxValueDecimalAttribute>().FirstOrDefault(); //if (decimalMaxValue != null && !String.IsNullOrWhiteSpace(decimalMaxValue.MaxValue)) // maxValue = decimalMaxValue.MaxValue; ////integer //var integerMinValue = attributes.OfType<MinValueIntegerAttribute>().FirstOrDefault(); //if (integerMinValue != null && !String.IsNullOrWhiteSpace(integerMinValue.MinValue)) // minValue = integerMinValue.MinValue; //var integerMaxValue = attributes.OfType<MaxValueIntegerAttribute>().FirstOrDefault(); //if (integerMaxValue != null && !String.IsNullOrWhiteSpace(integerMaxValue.MaxValue)) // maxValue = integerMaxValue.MaxValue; ////date //var dateMinValue = attributes.OfType<MinValueDateTimeAttribute>().FirstOrDefault(); //if (dateMinValue != null && !String.IsNullOrWhiteSpace(dateMinValue.MinValue)) // minValue = dateMinValue.MinValue; //var dateMaxValue = attributes.OfType<MaxValueDateTimeAttribute>().FirstOrDefault(); //if (dateMaxValue != null && !String.IsNullOrWhiteSpace(dateMaxValue.MaxValue)) // maxValue = dateMaxValue.MaxValue; //modelMetadata.AdditionalValues.Add("MinValue", minValue); //modelMetadata.AdditionalValues.Add("MaxValue", maxValue); var keyAttribute = attributes.OfType <KeyAttribute>().FirstOrDefault(); if (keyAttribute != null) { modelMetadata.AdditionalValues.Add("IsKey", true); } var stringLengthAttribute = attributes.OfType <StringLengthAttribute>().FirstOrDefault(); if (stringLengthAttribute != null) { modelMetadata.AdditionalValues.Add("MaxLength", stringLengthAttribute.MaximumLength); modelMetadata.AdditionalValues.Add("MinLength", stringLengthAttribute.MinimumLength); } var maxLengthAttribute = attributes.OfType <MaxLengthAttribute>().FirstOrDefault(); if (maxLengthAttribute != null) { modelMetadata.AdditionalValues.Add("MaxLength", maxLengthAttribute.Length); } var minLengthAttribute = attributes.OfType <MinLengthAttribute>().FirstOrDefault(); if (minLengthAttribute != null) { modelMetadata.AdditionalValues.Add("MinLength", minLengthAttribute.Length); } var regularExpressionAttribute = attributes.OfType <RegularExpressionAttribute>().FirstOrDefault(); if (regularExpressionAttribute != null) { modelMetadata.AdditionalValues.Add("Regex", regularExpressionAttribute.Pattern); } return(modelMetadata); }