Chorn Sokun's Weblog

Conquer inner fear, push it to the limit!

Posts Tagged ‘brail

Brail Daily Use Macros #2

with 2 comments

Imagine you had to associate multiple skill to your “Profile”

Multiple skill

Suppose we have the following Profile & Skill class

class Profile {
  public virtual int Id { get; set; }
  public virtual string Name { get; set; }
  //.... other properties as necessary
  public virtual IList<Skill> Skills { get;set; }
}

// a lookup class per say
class Skill {
  public virtual int Id { get;set; }
  public virtual Profile { get; set; }
  public virtual string Title { get; set; }
}

Assume that we know how to apply AR attribute on these two class make it persist able to a database. The important thing is Profile has many Skills that all you have to concentrate on right now.

Ok ! today recipe is to generate UI with checkbox which allow user to tick and save the choices without any hassle. With the help of FormHelper method YES, WE CAN

<%
list = Form.CreateCheckboxFieldList("p.Skills", skills, {@text:'Title', @value:'Id'})
// we need a loop
for item in list:
%>
  ${list.Item()}  ${item.ToString()}
<%
end
%>

Don’t panic let me explain a bit ${list.Item()} will generate individual html checkbox where ${item.ToString()} will generate a label for each one.

Advertisements

Written by Chorn Sokun

March 16, 2009 at 5:00 pm

Brail DateTime – StripTime() Extension Method

leave a comment »

Do you agree DateTime is the most annoying thing to deal with? .Net doesn’t have Date type where I can just store date value only. Oh bother why not define your own type? blame me I am lazy coder.

I have and object that hold date value which can be null, when it time to display the value I pass it from Controller -> View

But when the view render date field it render like this: 09/11/2008 12:00:00 AM What ???!!! oh yeah that is DateTime not Date.

What I can do (since I am using Brail View Engine) <% emp.DateOfEntry.ToString(“dd/MM/yyyy”) %> and I should get 09/11/2008 that fine right ;)

But I got problem with <% emp.DateOfResignation.ToString(“dd/MM/yyyy”) %> the app will blow cuz the employee is still work so the DateOfResignation is null.

Here come my very first Boo Extension method Views\CommonScripts\StripTime.brail:

import System
[Boo.Lang.ExtensionAttribute]
static def StripTime(val as duck) as string:
  try:
    return string.Empty if val is null
    return val.ToString("dd/MM/yyyy") if val.GetType().Name == "DateTime"
  except e as System.MissingMethodException:
    return string.Empty
  end
end

This code block is inspired from Ayende’s post.

And this is how I use it <% emp.DateOfResignation.StripTime() %> it will return empty string is null detected otherwise it will strip out 12:00:00 AM, isn’t that cool?

Written by Chorn Sokun

June 9, 2008 at 10:22 am

Posted in MonoRail, Tips & Tricks

Tagged with ,

Life without intellisence

leave a comment »

Without an intellisence feature my programming life become doom :( I just feel I need to get additional eyes to read between the line :))

Can you tell the different between these lines of code?

${Url.Link(‘Main page’, {@controller=’Home’, @action=’Index’})}
${Url.Link(‘Main page’, {@controller:’Home’, @action:’Index’})}

and these two:

At the end of the day it just make me laugh @ myself :))

Now that should be the first trick to get into the castle.
The next step is to keep searching for the Excalibor

Come with me @
http://www.castleproject.org ; http://using.castleproject.org

Written by Chorn Sokun

August 15, 2007 at 5:17 pm

Posted in Castle, MonoRail

Tagged with