From: <sv...@ca...> - 2006-01-25 15:25:33
|
User: hammett Date: 2006/01/25 10:19 AM Modified: /trunk/MonoRail/ Changes.txt /trunk/MonoRail/Castle.MonoRail.ActiveRecordSupport/ ARDataBindAttribute.cs, ARDataBinder.cs /trunk/MonoRail/Castle.MonoRail.Framework.Tests/Helpers/ FormHelperTestCase.cs /trunk/MonoRail/Castle.MonoRail.Framework/Helpers/ FormHelper.cs Log: Applied Roelof Blom's patches. - Added ARDataBindAttribute.NullWhenPrimaryKey which forces the databinder to ignore and do not load a record if the retrieved PK has the value assigned to this property (kudos to Roelof Blom <roe...@gm...>) File Changes: Directory: /trunk/MonoRail/Castle.MonoRail.ActiveRecordSupport/ =============================================================== File [modified]: ARDataBindAttribute.cs Delta lines: +21 -23 =================================================================== --- trunk/MonoRail/Castle.MonoRail.ActiveRecordSupport/ARDataBindAttribute.cs 2006-01-25 14:59:17 UTC (rev 1624) +++ trunk/MonoRail/Castle.MonoRail.ActiveRecordSupport/ARDataBindAttribute.cs 2006-01-25 15:19:59 UTC (rev 1625) @@ -26,10 +26,12 @@ [AttributeUsage(AttributeTargets.Parameter), Serializable] public class ARDataBindAttribute : DataBindAttribute, IParameterBinder { -// private bool validate; -// private bool autoPersist; + private static readonly object NullWhenPrimaryKeyEmpty = new object(); + private bool autoLoad; - + + private object nullWhenPrimaryKey = NullWhenPrimaryKeyEmpty; + public ARDataBindAttribute(String prefix) : base (prefix) { } @@ -40,35 +42,31 @@ set { autoLoad = value; } } - /// <summary> - /// When true performs automatic validation of any class - /// that inherit from <see cref="Castle.ActiveRecord.ActiveRecordValidationBase"/> - /// </summary> -// public bool Validate -// { -// get { return validate; } -// set { validate = value; } -// } - - /// <summary> - /// When true automatically saves any record - /// that inherit from <see cref="Castle.ActiveRecord.ActiveRecordBase"/> - /// </summary> -// public bool AutoPersist -// { -// get { return autoPersist; } -// set { autoPersist = value; } -// } + public object NullWhenPrimaryKey + { + get { return nullWhenPrimaryKey; } + set { nullWhenPrimaryKey = value; } + } - public override object Bind(SmartDispatcherController controller, ParameterInfo parameterInfo) + public override object Bind(SmartDispatcherController controller, ParameterInfo parameterInfo) { ARDataBinder binder = new ARDataBinder(); ConfigureBinder(binder, controller); binder.AutoLoad = AutoLoad; + + if (IsNullWhenPrimaryKeySet) + { + binder.NullWhenPrimaryKey = NullWhenPrimaryKey; + } return binder.BindObject(parameterInfo.ParameterType, Prefix, Exclude, Allow, ResolveParams(controller)); } + + protected internal bool IsNullWhenPrimaryKeySet + { + get { return NullWhenPrimaryKey != NullWhenPrimaryKeyEmpty; } + } } } File [modified]: ARDataBinder.cs Delta lines: +52 -39 =================================================================== --- trunk/MonoRail/Castle.MonoRail.ActiveRecordSupport/ARDataBinder.cs 2006-01-25 14:59:17 UTC (rev 1624) +++ trunk/MonoRail/Castle.MonoRail.ActiveRecordSupport/ARDataBinder.cs 2006-01-25 15:19:59 UTC (rev 1625) @@ -39,18 +39,14 @@ { protected internal static readonly object[] EmptyArg = new object[0]; - private bool autoLoad, validate, persistchanges; + private bool autoLoad, persistchanges; + + private object nullWhenPrimaryKey; public ARDataBinder() : base() { } -// public bool Validate -// { -// get { return validate; } -// set { validate = value; } -// } - public bool PersistChanges { get { return persistchanges; } @@ -63,6 +59,12 @@ set { autoLoad = value; } } + public object NullWhenPrimaryKey + { + get { return nullWhenPrimaryKey; } + set { nullWhenPrimaryKey = value; } + } + protected override object CreateInstance(Type instanceType, String paramPrefix, IBindingDataSourceNode node) { if (node == null) @@ -99,7 +101,14 @@ object id = ObtainPKValue(model, node, paramPrefix, out pkModel); - instance = SupportingUtils.FindByPK(instanceType, id); + if (nullWhenPrimaryKey != null && id.Equals(NullWhenPrimaryKey)) + { + instance = null; + } + else + { + instance = SupportingUtils.FindByPK(instanceType, id); + } } else { @@ -139,6 +148,10 @@ protected override bool PerformCustomBinding(object instance, string prefix, IBindingDataSourceNode node) { + if (nullWhenPrimaryKey != null && instance == null) + { + return true; + } object stackInstance = InstanceOnStack; if (stackInstance == null) @@ -237,10 +250,10 @@ if (model == null) return; - if (validate) - { - ValidateInstances(instance); - } +// if (validate) +// { +// ValidateInstances(instance); +// } if (persistchanges) { @@ -272,33 +285,33 @@ } } - private void ValidateInstances(object instances) - { - Type instanceType = instances.GetType(); - ActiveRecordValidationBase[] records = null; +// private void ValidateInstances(object instances) +// { +// Type instanceType = instances.GetType(); +// ActiveRecordValidationBase[] records = null; +// +// if (instanceType.IsArray) +// { +// records = instances as ActiveRecordValidationBase[]; +// } +// else if (typeof(ActiveRecordValidationBase).IsAssignableFrom(instanceType)) +// { +// records = new ActiveRecordValidationBase[] {(ActiveRecordValidationBase) instances}; +// } +// +// if (records != null) +// { +// foreach(ActiveRecordValidationBase record in records) +// { +// if (!record.IsValid()) +// { +// throw new RailsException("Error validating {0} {1}", +// record.GetType().Name, string.Join("\n", record.ValidationErrorMessages)); +// } +// } +// } +// } - if (instanceType.IsArray) - { - records = instances as ActiveRecordValidationBase[]; - } - else if (typeof(ActiveRecordValidationBase).IsAssignableFrom(instanceType)) - { - records = new ActiveRecordValidationBase[] {(ActiveRecordValidationBase) instances}; - } - - if (records != null) - { - foreach(ActiveRecordValidationBase record in records) - { - if (!record.IsValid()) - { - throw new RailsException("Error validating {0} {1}", - record.GetType().Name, string.Join("\n", record.ValidationErrorMessages)); - } - } - } - } - protected void SaveManyMappings(object instance, ActiveRecordModel model, IBindingDataSourceNode node) { foreach(HasManyModel hasManyModel in model.HasMany) @@ -417,4 +430,4 @@ return null; } } -} \ No newline at end of file +} Directory: /trunk/MonoRail/Castle.MonoRail.Framework/Helpers/ ============================================================= File [modified]: FormHelper.cs Delta lines: +7 -2 =================================================================== --- trunk/MonoRail/Castle.MonoRail.Framework/Helpers/FormHelper.cs 2006-01-25 14:59:17 UTC (rev 1624) +++ trunk/MonoRail/Castle.MonoRail.Framework/Helpers/FormHelper.cs 2006-01-25 15:19:59 UTC (rev 1625) @@ -183,14 +183,19 @@ public String Select(String target, IEnumerable dataSource, IDictionary attributes) { + object selectedValue = ObtainValue(target); + + return Select(target, selectedValue, dataSource, attributes); + } + + public String Select(String target, object selectedValue, IEnumerable dataSource, IDictionary attributes) + { String id = target.Replace('.', '_'); StringBuilder sb = new StringBuilder(); StringWriter sbWriter = new StringWriter(sb); HtmlTextWriter writer = new HtmlTextWriter(sbWriter); - object selectedValue = ObtainValue(target); - String firstOption = null; String valueProperty = null; String textProperty = null; Directory: /trunk/MonoRail/Castle.MonoRail.Framework.Tests/Helpers/ =================================================================== File [modified]: FormHelperTestCase.cs Delta lines: +2 -1 =================================================================== --- trunk/MonoRail/Castle.MonoRail.Framework.Tests/Helpers/FormHelperTestCase.cs 2006-01-25 14:59:17 UTC (rev 1624) +++ trunk/MonoRail/Castle.MonoRail.Framework.Tests/Helpers/FormHelperTestCase.cs 2006-01-25 15:19:59 UTC (rev 1625) @@ -34,7 +34,7 @@ [SetUp] public void Init() { - CultureInfo en = CultureInfo.CreateSpecificCulture( "en" ); + CultureInfo en = CultureInfo.CreateSpecificCulture("en"); Thread.CurrentThread.CurrentCulture = en; Thread.CurrentThread.CurrentUICulture = en; @@ -125,6 +125,7 @@ public void SimplisticSelect() { ArrayList list = new ArrayList(); + list.Add("cat1"); list.Add("cat2"); Directory: /trunk/MonoRail/ =========================== File [modified]: Changes.txt Delta lines: +4 -0 =================================================================== --- trunk/MonoRail/Changes.txt 2006-01-25 14:59:17 UTC (rev 1624) +++ trunk/MonoRail/Changes.txt 2006-01-25 15:19:59 UTC (rev 1625) @@ -73,6 +73,10 @@ - Introduced WindsorMonoRailSectionHandler which has the support for statements similar to those supported by windsor's XmlInterpreter + +- Added ARDataBindAttribute.NullWhenPrimaryKey which forces the databinder to + ignore and do not load a record if the retrieved PK has the value assigned to this + property (kudos to Roelof Blom <roe...@gm...>) Beta 5 ====== |