31 December 2009

Presentation on bad smells in code

Most of you must have read the famous book on “Refactoring” written by Martin Fowler. Based on this book and and my fellow colleague Rashed Kibria presented at the BASIS, titled as “Bad Smells in code (Part-1)”. We have shared the document. You can take a look at it.

Bad Smell in Codes-1

30 December 2009

Formatting date in Rails

The thing with which we deal everyday in our programming life is “Date”. This small, delicate stuff needs to be displayed in different formats in different places! In Rails there are many useful helpers to format date. Hence I am jolting down some so that I can take look quickly if I need one of them to use. You can take a look at it, too.

I have used strftime function of ActiveSupport class.

current_time = Time.now #today is 30 December, 2009
current_time.strftime("%m-%d-%y") # "12-30-09"
current_time.strftime("%b %d, %Y %I:%M%p") # "Dec 30, 2009 10:23PM"
current_time.strftime("%A %d, %B %Y") # "Wednesday 30, December 2009"
current_time.strftime("%A, %d %B %Y %I:%M%p") # "Wednesday, 30 December 2009 10:33PM"
current_time.strftime("%Y-%m-%d %H:%M:%S") # "2009-12-30 22:33:55"
current_time.strftime("%b %d, %Y") #=> "Dec 30, 2009"


By this time you may have understood the meaning of this format. Here is the explanation:

%Y – 2009 – Year with century

%y – 09 – Year without century

%B – January – Full month name

%b – Jan – Short month name

%m – 12 – Month number

%A- Wednesday-Full weekday name

%a- Wed-Short weekday name

%H –22 – Hour

%M –33 – Minute

%S –55 - Second

%p – AM or PM

29 December 2009

Some useful number helpers in rails

In my previous blog on Rails I discussed about some useful text helpers for rails based on my work experience on Scrumpad. Now I would like to shade on some useful number helpers in rails which made my life easy during the project work. Let’s dive into some samples!


1. number_to_currency
Software developers do the complex thing and forget about small ones. Sometimes after design discussions, writing a lot of lines of codes with complex logic create a number. Say, bill of a particular company for the month of January! When you will deliver the result to your product owner with pride, he will take a look at the number (you are expecting a pat on your back ) and ignoring your days of work may start talking with an unsatisfied face that “You forgot the currency. Is it in pound? I will be damn rich, then”. Now you come down to the earth! What a silly thing it was using number helpers in rails!! These methods are from ActionView::Helpers::NumberHelper
number_to_currency(1234.506)  # $1234.51
number_to_currency(1234.506, :precision=>3)  # $1234.506
number_to_currency(1234.506, :precision=>3, :unit=>”&pound”)  # &pound1234.506

2. number_to_human_size
A lot of systems deal with user interaction now a days. Attachment is an important part of it. Users like to upload files like images, texts, documents and so on. Beside a beautiful icon of that attachment, it is nice to mention the size of that file. It can be easily done by rails using this function.

number_to_human_size(123) # 123 Bytes
number_to_human_size(1234) # 123 KB
number_to_human_size(1234567) # 1.2 MB

It can be used with precision and separator, too.

number_to_human_size(1234567, :precision=>2, :separator=>’,’) # 1,18 MB

3. number_to_percentage
It formats a number as percentage strings.  At some places discount is calculated dynamically based on number of active users. Here comes a useful helper.

number_to_percentage(100) #100%

4. number_to_phone
It formats a phone number to US phone number format. Once can customize it, too. It takes area code, country code, extension as option hash.

Let’s give some example:

number_to_phone(1115678) # 111-5678
number_to_phone(1114567890, :area_code=>true) # (111) 456-7890
number_to_phone(1114567890, :area_code=>true, :extension=>999) # (111) 456-7890 x 999
number_to_phone(1114567890, :country_code=>1 # +1- 111 - 456-7890

So, based upon your requirement you can show the user phone number.

That’s it for today. I will come shortly with more helper that I usually use! Happy coding :-)

24 December 2009

Some useful text helpers in Rails (Part - 1)

One of the most important features of Rails is it’s rich helper. Sometimes these are life savers because you don’t have to write your own code and very useful if you can use it in a clever way. While developing scrumpad ( scrumpad )  I found many places where texts need to be formatted and Rail’s helper came like a great boon. Let’s share some helpers!

1.  auto_link

Suppose your text contains both url and email. auto_link will convert it to as links.

auto_link(“Go to www.scrumpad.com and meet fuad@code71.com”)
It will place links automatically at the appropriate places. If you want to make the sites as url but not the mail, you can pass the option too like:

auto_link(“Go to www.scrumpad.com and meet fuad@code71.com” , :link=>:urls)

auto_link(“Go to www.scrumpad.com and meet fuad@code71.com” , :link=>:email_address)

 

2. cycle

While providing alternate colors for tables for style purpose, we often put logic in the code to determine the even and odd row and places the color alternatively. This can be handled by a nice helper named “cycle”

suppose you have two css classes named “even” and “odd”. You can make the alternate rows to alternate colors by:
<tr class=”<%= cycle(“even”, “odd”)>”<td>Data</td> </tr>

3. word_wrap

Yes! You can guess it by it’s name. If you want to wrap a long line with (or without) a specified width you just call this helper.

word_wrap(“A brown colored fox jumped over the lazy dog”, :line_width=>5)

4.  truncate

At times we wish to show the users a part of our long text and replace the lest with “…”. Rails makes it easy for us as usual.

truncate(“Scrumpad, a agile project management tools by Code71”, 19) 

Output will be something like  “Scrumpad, an agile…”

If you want to provide your text instead of “…” you should write it as:

truncate(“Scrumpad, a agile project management tools by Code71”, 19, “….(More)”) 

Output will be like “Scrumpad, an agile…(More)”

That’s all from today. I will come back to you guys with more. In the meantime you can try these out (If you haven’t tried these yet :-) )