<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-4311613374961039922</id><updated>2011-12-27T10:34:11.957+06:00</updated><category term='SEO URL Rails'/><category term='format date in rails'/><category term='ruby on rails'/><category term='gravatar'/><category term='twitter status in rails'/><category term='format number in rails'/><category term='friendly_id'/><category term='avatar'/><category term='https'/><category term='exception'/><category term='time zone in rails'/><category term='ActionView::Helpers::NumberHelper'/><category term='twitter gem'/><category term='twitter feed in rails'/><category term='redirection to https'/><title type='text'>Fuad's Blog on Technology</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://fuadcse.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4311613374961039922/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://fuadcse.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Fuad Omar</name><uri>http://www.blogger.com/profile/11459190392904433194</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_f6bQr9hj8Bs/SXdkNg9PUyI/AAAAAAAAAHI/Vk9sB0OZyhI/S220/13082007135.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>12</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-4311613374961039922.post-9091604864623208810</id><published>2011-06-17T20:22:00.000+06:00</published><updated>2011-06-17T20:33:49.693+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='avatar'/><category scheme='http://www.blogger.com/atom/ns#' term='gravatar'/><category scheme='http://www.blogger.com/atom/ns#' term='ruby on rails'/><title type='text'>Display Avatar in Ruby on Rails application</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;Now a days displaying Avatar is a common trend in web technology. Avatar is graphical representation of an user. Thanks go to Tom Preston Werner for introducing &lt;a href="http://www.gravatar.com/"&gt;Gravatar&lt;/a&gt;&amp;nbsp;, a great web service which give globally recognized avatar. You just need to open up an account out their with an email address and upload your photo. Different websites, consuming Gravatar's service can display your photo if you register out there with the same email address.&lt;br /&gt;&lt;br /&gt;In this example I will show you a way to display Gravatar in your Ruby on Rails web application.&lt;br /&gt;&lt;br /&gt;First I will write a helper method which will return an image url for displaying Avatar. Gravater, id is simply the MD5 hexdigest of the email address. So the line returning avatar id will be:&lt;br /&gt;&lt;pre class="brush :ruby"&gt;gravatar_id = Digest::MD5.hexdigest(user.email.downcase)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;I would like to display the avatar as 48x48, which is the usual size of a twitter display image. Also, I want to show a default image url if avatar is not shown. For these two, we need to pass some extra parameters. The method will look like:&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush :ruby"&gt;def avatar_url(user)&lt;br /&gt;&amp;nbsp; &amp;nbsp; default_url = "#{root_url}images/default-image.jpg"&lt;br /&gt;&amp;nbsp; &amp;nbsp; if(user.present?)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; gravatar_id = Digest::MD5.hexdigest(user.email.downcase)&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return "http://gravatar.com/avatar/#{gravatar_id}.png?s=48&amp;amp;d=#{CGI.escape(default_url)}"&lt;br /&gt;&amp;nbsp; &amp;nbsp; else&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; return default_url&lt;br /&gt;&amp;nbsp;end&lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;In your view:&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush :ruby"&gt;&amp;lt;%= link_to(image_tag(avatar_url(user)), user_path(user.id))%&amp;gt;&lt;/pre&gt;&lt;br /&gt;The above line, will show the avatar and will take you to the show method of UsersController if you click on it.&lt;br /&gt;&lt;br /&gt;You can also choose from an user to display his avatar from gravatar or your local file system. All you need to do is to have a boolean field 'avatar' in your User table and re-arrange the logic in avatar_url method.&lt;br /&gt;&lt;br /&gt;It's that simple! Happy coding!!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4311613374961039922-9091604864623208810?l=fuadcse.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://fuadcse.blogspot.com/feeds/9091604864623208810/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4311613374961039922&amp;postID=9091604864623208810' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4311613374961039922/posts/default/9091604864623208810'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4311613374961039922/posts/default/9091604864623208810'/><link rel='alternate' type='text/html' href='http://fuadcse.blogspot.com/2011/06/display-avatar-in-ruby-on-rails.html' title='Display Avatar in Ruby on Rails application'/><author><name>Fuad Omar</name><uri>http://www.blogger.com/profile/11459190392904433194</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_f6bQr9hj8Bs/SXdkNg9PUyI/AAAAAAAAAHI/Vk9sB0OZyhI/S220/13082007135.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4311613374961039922.post-3577690019980697563</id><published>2011-03-05T22:13:00.001+06:00</published><updated>2011-06-17T22:35:12.102+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SEO URL Rails'/><category scheme='http://www.blogger.com/atom/ns#' term='friendly_id'/><title type='text'>SEO friendly URL in ruby on rails and friendly_id</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;div class="wlWriterEditableSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:1413afc0-3584-4a03-98bf-49a61f5131b6" style="display: inline; float: none; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/SEO" rel="tag"&gt;SEO&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Rails" rel="tag"&gt;Rails&lt;/a&gt;,&lt;a href="http://technorati.com/tags/SEO+in+Rails" rel="tag"&gt;SEO in Rails&lt;/a&gt;,&lt;a href="http://technorati.com/tags/SEO+friendly+URL+in+Rails" rel="tag"&gt;SEO friendly URL in Rails&lt;/a&gt;&lt;/div&gt;&lt;br/&gt;&lt;br /&gt;By default Rails generate URL with ID. In short &lt;span style="font-family: 'Courier New';"&gt;to_param&lt;/span&gt; method returns a string, which Action Pack uses for constructing an URL to this object. The default implementation returns records’ &lt;span style="font-family: 'Courier New';"&gt;id&lt;/span&gt; as a string.     &lt;br /&gt;Suppose, we have a &lt;span style="font-family: 'Courier New';"&gt;Category&lt;/span&gt; model. The &lt;span style="font-family: 'Courier New';"&gt;routes.rb&lt;/span&gt; will look like:   &lt;br /&gt;&lt;pre class="brush :ruby"&gt;map.resources :categories&lt;/pre&gt;In categories.rb controller:&lt;br /&gt;&lt;pre class="brush :ruby"&gt;@category = Category.find(1)&lt;/pre&gt;In view &lt;span style="font-family: 'Courier New';"&gt;index.html.erb&lt;/span&gt;: &lt;br /&gt;&lt;pre class="brush :ruby"&gt;&amp;lt;%=link_to(@category.title,category_path(@category))%&amp;gt;&lt;/pre&gt;This will generate the URL: &lt;span style="font-family: 'Courier New';"&gt;/category/1&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;While working on building a website &lt;a href="http://www.khanacademybangla.com/"&gt;Khan Academy In Bangla&lt;/a&gt; as a part of the translation project of &lt;a href="http://www.khanacademy.org/"&gt;Khan Academy&lt;/a&gt; , I realized that a SEO friendly URL is mandatory. In this website, I have categories like physics, biology or math and tutorials under each category. In order to make the user friendly URL I needed to overwrite to_param method in model.&lt;br /&gt;&lt;br /&gt;In order to make it SEO friendly, in Category.rb model I added the line:&lt;br /&gt;&lt;pre class="brush :ruby"&gt;def to_param&lt;br /&gt;&amp;nbsp; "#{id}-#{title.downcase.gsub(/[^a-zA-Z0-9]+/, '-').gsub(/-{2,}/, '-').gsub(/^-|-$/, '')}"&lt;br /&gt;end&lt;/pre&gt;The regular expression will remove any special character. It generates the url: &lt;span style="font-family: 'Courier New';"&gt;/category/1-biology&lt;/span&gt;. Looks better, right? &lt;br /&gt;Although the URL has become SEO friendly now but this method has some drawbacks. To summarize some:&lt;br /&gt;&lt;br /&gt;1. It appends the id with the name. If we remove ID, then maintaining uniqueness will be a problem&lt;br /&gt;2. If the title field is edited then the URL will be changed causing SEO to break.&lt;br /&gt;3. For a large application, it will not be feasible to overwrite to_param in tons of model.&lt;br /&gt;&lt;br /&gt;In this point, I found a nice plugin/gem to make all these pretty simple and it is &lt;a href="https://github.com/norman/friendly_id/"&gt;friedly_id&lt;/a&gt; . It will do all the work for you with some simple steps. For my system what I did were:&lt;br /&gt;1. Install the gem: &lt;br /&gt;&lt;pre class="brush :ruby"&gt;gem install friendly_id&lt;/pre&gt;2. In environment.rb include the gem. &lt;br /&gt;&lt;pre class="brush :ruby"&gt;require 'friendly_id'&lt;/pre&gt;3. Generate friendly_id by going to my application directory: &lt;br /&gt;&lt;pre class="brush :ruby"&gt;cd khanacademybangla&lt;br /&gt;script/generate friendly_id&lt;/pre&gt;4. Run the migration &lt;br /&gt;&lt;pre class="brush :ruby"&gt;rake db:migrate&lt;/pre&gt;5. Add has_friendly_id to the models which I am looking for to generate user friendly id &lt;br /&gt;&lt;pre class="brush :ruby"&gt;class Category &amp;lt; ActiveRecord::Base&lt;br /&gt;has_friendly_id :title, :use_slug =&amp;gt; true&lt;br /&gt;end&lt;/pre&gt;6. Run a rake task to generate slugs for existing records: &lt;br /&gt;&lt;pre class="brush :ruby"&gt;rake friendly_id:make_slugs MODEL=ModelName&lt;/pre&gt;For my example it was:&lt;br /&gt;&lt;pre class="brush :ruby"&gt;rake friendly_id:make_slugs MODEL=category&lt;/pre&gt;That’s it! Now we will be able to see a completely SEO friendly url: &lt;span style="font-family: 'Courier New';"&gt;/category/biology&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;I am yet to push my code to production! You need to wait till tomorrow to see it live.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4311613374961039922-3577690019980697563?l=fuadcse.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://fuadcse.blogspot.com/feeds/3577690019980697563/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4311613374961039922&amp;postID=3577690019980697563' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4311613374961039922/posts/default/3577690019980697563'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4311613374961039922/posts/default/3577690019980697563'/><link rel='alternate' type='text/html' href='http://fuadcse.blogspot.com/2011/03/seo-friendly-url-in-ruby-on-rails.html' title='SEO friendly URL in ruby on rails and friendly_id'/><author><name>Fuad Omar</name><uri>http://www.blogger.com/profile/11459190392904433194</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_f6bQr9hj8Bs/SXdkNg9PUyI/AAAAAAAAAHI/Vk9sB0OZyhI/S220/13082007135.jpg'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4311613374961039922.post-2880750096045918505</id><published>2010-01-13T10:17:00.001+06:00</published><updated>2010-01-13T10:45:10.249+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='twitter gem'/><category scheme='http://www.blogger.com/atom/ns#' term='twitter status in rails'/><category scheme='http://www.blogger.com/atom/ns#' term='twitter feed in rails'/><title type='text'>Displaying status feed of twitter in Rails</title><content type='html'>&lt;p&gt;&lt;div class="wlWriterEditableSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:ca366b6c-06c2-4ca1-b91b-99b4d74f942c" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/Ruby+on+Rails" rel="tag"&gt;Ruby on Rails&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Twitter+feed+in+rails" rel="tag"&gt;Twitter feed in rails&lt;/a&gt;,&lt;a href="http://technorati.com/tags/twitter+gem" rel="tag"&gt;twitter gem&lt;/a&gt;,&lt;a href="http://technorati.com/tags/HTTP+Auth+in+twitter" rel="tag"&gt;HTTP Auth in twitter&lt;/a&gt;&lt;/div&gt;&lt;/p&gt;&lt;p&gt;Couple of days ago I needed to show twitter status in a rails application. The specification was to show the last 5 status of a twitter account. Twitter itself has a good API. But I used twitter gem which left me to do just a couple of lines of code. &lt;/p&gt;&lt;p&gt;Twitter gem supports both OAuth and HTTP Auth. I used HTTP Auth for authentication. Let me dig in to code.&lt;/p&gt;&lt;p&gt;First of all, install &lt;a href="http://twitter.rubyforge.org/rdoc/" target="_blank"&gt;twitter gem.&lt;/a&gt;&lt;/p&gt;&lt;pre class="brush :ruby"&gt;gem install twitter&lt;/pre&gt;Load ruby gem to environment.rb.&lt;br /&gt;&lt;pre class="brush :ruby"&gt;require 'twitter'&lt;/pre&gt;&lt;p&gt;Then perform the authentication using HTTP Auth. You need to provide your user id and password of the twitter account. You should read this from a config file. For the simplicity I am placing it at code.&lt;/p&gt;&lt;pre class="brush :ruby"&gt;def twitter_feed   &lt;br /&gt;httpauth = Twitter::HTTPAuth.new(‘twitter_account_name’, ‘twitter_account_password’)    &lt;br /&gt;twitter_client = Twitter::Base.new(httpauth)&lt;br /&gt;@latest_status_feeds = twitter_client.user_timeline[0..4] if twitter_client.present?&lt;br /&gt;#it will show most recent 5 status. If you would like to show recent 20 don't put any bar.&lt;br /&gt;render :partial=&amp;gt;&amp;quot;twitter_feed&amp;quot;   &lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;&lt;p&gt;In twitter_feed partial you can display the text using some properties.&lt;/p&gt;&lt;pre class="brush :ruby"&gt;&amp;lt;h4&amp;gt;Latest Tweet&amp;lt;/h4&amp;gt;   &lt;br /&gt;&amp;lt;%if @latest_status_feeds.present?%&amp;gt;    &lt;br /&gt;&amp;#160; &amp;lt;%&amp;#160; @latest_status_feeds.each do | @latest_status_feed|%&amp;gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;p&amp;gt;&amp;quot;&amp;lt;%= @latest_status_feed.text %&amp;gt;&amp;quot;&amp;lt;br /&amp;gt;&amp;lt;span class=&amp;quot;time&amp;quot; style=&amp;quot;font-size:11px;font-style:italic;&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;%= convert_date(@latest_status_feed.created_at.to_date) %&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/p&amp;gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;    &lt;br /&gt;&amp;#160; &amp;lt;%end%&amp;gt;    &lt;br /&gt;&amp;lt;%end%&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;p&gt;This will show the last 5 status from your twitter feed.&lt;/p&gt;&lt;p&gt;If you would like to show the twitter feeds of your friends, the following tweak is needed to be made:&lt;/p&gt;&lt;pre class="brush :ruby"&gt;&amp;#160;&amp;#160;&amp;#160; @friends_feeds =&amp;#160;&amp;#160;&amp;#160;&amp;#160; twitter_client.friends_timeline &lt;/pre&gt;&lt;p&gt;You can display from the view in the same way! &lt;/p&gt;&lt;p&gt;That's it! Pretty simple, yeah? Happy coding.&lt;/P&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4311613374961039922-2880750096045918505?l=fuadcse.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://fuadcse.blogspot.com/feeds/2880750096045918505/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4311613374961039922&amp;postID=2880750096045918505' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4311613374961039922/posts/default/2880750096045918505'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4311613374961039922/posts/default/2880750096045918505'/><link rel='alternate' type='text/html' href='http://fuadcse.blogspot.com/2010/01/displaying-status-of-twitter-feed-in.html' title='Displaying status feed of twitter in Rails'/><author><name>Fuad Omar</name><uri>http://www.blogger.com/profile/11459190392904433194</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_f6bQr9hj8Bs/SXdkNg9PUyI/AAAAAAAAAHI/Vk9sB0OZyhI/S220/13082007135.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4311613374961039922.post-3475058946301482158</id><published>2009-12-31T17:57:00.001+07:00</published><updated>2009-12-31T19:32:15.535+07:00</updated><title type='text'>Presentation on bad smells in code</title><content type='html'>&lt;p&gt;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.&lt;/p&gt;&lt;a title="View Bad Smell in Codes-1 on Scribd" href="http://www.scribd.com/doc/24112859/Bad-Smell-in-Codes-1" style="margin: 12px auto 6px auto; font-family: Helvetica,Arial,Sans-serif; font-style: normal; font-variant: normal; font-weight: normal; font-size: 14px; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; display: block; text-decoration: underline;"&gt;Bad Smell in Codes-1&lt;/a&gt; &lt;object codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" id="doc_267779635668753" name="doc_267779635668753" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" align="middle" height="500" width="450" &gt;  &lt;param name="movie" value="http://d1.scribdassets.com/ScribdViewer.swf?document_id=24112859&amp;access_key=key-2ayzc3yavv1y9w4magip&amp;page=1&amp;version=1&amp;viewMode=slideshow"&gt;&lt;param name="quality" value="high"&gt;&lt;param name="play" value="true"&gt;&lt;param name="loop" value="true"&gt;&lt;param name="scale" value="showall"&gt;&lt;param name="wmode" value="opaque"&gt;&lt;param name="devicefont" value="false"&gt;&lt;param name="bgcolor" value="#ffffff"&gt;&lt;param name="menu" value="true"&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;param name="allowScriptAccess" value="always"&gt;&lt;param name="salign" value=""&gt;&lt;param name="mode" value="slideshow"&gt;&lt;embed src="http://d1.scribdassets.com/ScribdViewer.swf?document_id=24112859&amp;access_key=key-2ayzc3yavv1y9w4magip&amp;page=1&amp;version=1&amp;viewMode=slideshow" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" play="true" loop="true" scale="showall" wmode="opaque" devicefont="false" bgcolor="#ffffff" name="doc_267779635668753_object" menu="true" allowfullscreen="true" allowscriptaccess="always" salign="" type="application/x-shockwave-flash" align="middle" mode="slideshow" height="500" width="450"&gt;&lt;/embed&gt; &lt;/object&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4311613374961039922-3475058946301482158?l=fuadcse.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://fuadcse.blogspot.com/feeds/3475058946301482158/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4311613374961039922&amp;postID=3475058946301482158' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4311613374961039922/posts/default/3475058946301482158'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4311613374961039922/posts/default/3475058946301482158'/><link rel='alternate' type='text/html' href='http://fuadcse.blogspot.com/2009/12/presentation-on-bad-smells-in-code.html' title='Presentation on bad smells in code'/><author><name>Fuad Omar</name><uri>http://www.blogger.com/profile/11459190392904433194</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_f6bQr9hj8Bs/SXdkNg9PUyI/AAAAAAAAAHI/Vk9sB0OZyhI/S220/13082007135.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4311613374961039922.post-4807592032758880969</id><published>2009-12-30T22:31:00.001+07:00</published><updated>2009-12-31T10:59:02.657+07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='format date in rails'/><category scheme='http://www.blogger.com/atom/ns#' term='ruby on rails'/><title type='text'>Formatting date in Rails</title><content type='html'>&lt;p&gt;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.&lt;/p&gt;&lt;p&gt;I have used &lt;strong&gt;strftime&lt;/strong&gt; function of &lt;strong&gt;ActiveSupport&lt;/strong&gt; class. &lt;/p&gt;&lt;pre class="brush :ruby"&gt;current_time = Time.now #today is 30 December, 2009&lt;br /&gt;current_time.strftime(&amp;quot;%m-%d-%y&amp;quot;) # &amp;quot;12-30-09&amp;quot;&lt;br /&gt;current_time.strftime(&amp;quot;%b %d, %Y %I:%M%p&amp;quot;) # &amp;quot;Dec 30, 2009 10:23PM&amp;quot;&lt;br /&gt;current_time.strftime(&amp;quot;%A %d, %B %Y&amp;quot;) # &amp;quot;Wednesday 30, December 2009&amp;quot;&lt;br /&gt;current_time.strftime(&amp;quot;%A, %d %B %Y %I:%M%p&amp;quot;) # &amp;quot;Wednesday, 30 December 2009 10:33PM&amp;quot;&lt;br /&gt;current_time.strftime(&amp;quot;%Y-%m-%d %H:%M:%S&amp;quot;) # &amp;quot;2009-12-30 22:33:55&amp;quot;&lt;br /&gt;current_time.strftime(&amp;quot;%b %d, %Y&amp;quot;) #=&amp;gt; &amp;quot;Dec 30, 2009&amp;quot;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;By this time you may have understood the meaning of this format. Here is the explanation:&lt;br /&gt;&lt;br /&gt;%Y – 2009 – Year with century&lt;br /&gt;&lt;br /&gt;%y – 09 – Year without century&lt;br /&gt;&lt;br /&gt;%B – January – Full month name&lt;br /&gt;&lt;br /&gt;%b – Jan – Short month name&lt;br /&gt;&lt;br /&gt;%m – 12 – Month number&lt;br /&gt;&lt;br /&gt;%A- Wednesday-Full weekday name&lt;br /&gt;&lt;br /&gt;%a- Wed-Short weekday name&lt;br /&gt;&lt;br /&gt;%H –22 – Hour&lt;br /&gt;&lt;br /&gt;%M –33 – Minute&lt;br /&gt;&lt;br /&gt;%S –55 - Second&lt;br /&gt;&lt;br /&gt;%p – AM or PM&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4311613374961039922-4807592032758880969?l=fuadcse.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://fuadcse.blogspot.com/feeds/4807592032758880969/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4311613374961039922&amp;postID=4807592032758880969' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4311613374961039922/posts/default/4807592032758880969'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4311613374961039922/posts/default/4807592032758880969'/><link rel='alternate' type='text/html' href='http://fuadcse.blogspot.com/2009/12/formatting-date-in-rails.html' title='Formatting date in Rails'/><author><name>Fuad Omar</name><uri>http://www.blogger.com/profile/11459190392904433194</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_f6bQr9hj8Bs/SXdkNg9PUyI/AAAAAAAAAHI/Vk9sB0OZyhI/S220/13082007135.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4311613374961039922.post-8646762592729371414</id><published>2009-12-29T18:05:00.001+07:00</published><updated>2011-03-05T23:39:31.997+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ActionView::Helpers::NumberHelper'/><category scheme='http://www.blogger.com/atom/ns#' term='ruby on rails'/><category scheme='http://www.blogger.com/atom/ns#' term='format number in rails'/><title type='text'>Some useful number helpers in rails</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;In my previous blog on Rails I discussed about some useful text helpers for rails based on my work experience on &lt;a href="http://www.scrumpad.com/companies/new?referrer=FO-00010"&gt;Scrumpad&lt;/a&gt;. 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!&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;1. number_to_currency&lt;/strong&gt;&lt;br /&gt;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 &lt;br /&gt;&lt;pre class="brush :ruby"&gt;number_to_currency(1234.506)&amp;nbsp; # $1234.51&lt;br /&gt;number_to_currency(1234.506, :precision=&amp;gt;3)&amp;nbsp; # $1234.506&lt;br /&gt;number_to_currency(1234.506, :precision=&amp;gt;3, :unit=&amp;gt;”&amp;amp;pound”)&amp;nbsp; # &amp;amp;pound1234.506&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;strong&gt;2. number_to_human_size&lt;/strong&gt;&lt;br /&gt;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. &lt;br /&gt;&lt;br /&gt;&lt;pre class="brush :ruby"&gt;number_to_human_size(123) # 123 Bytes&lt;br /&gt;number_to_human_size(1234) # 123 KB&lt;br /&gt;number_to_human_size(1234567) # 1.2 MB&lt;/pre&gt;&lt;br /&gt;It can be used with precision and separator, too.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush :ruby"&gt;number_to_human_size(1234567, :precision=&amp;gt;2, :separator=&amp;gt;’,’) # 1,18 MB&lt;/pre&gt;&lt;br /&gt;&lt;strong&gt;3. number_to_percentage&lt;/strong&gt;&lt;br /&gt;It formats a number as percentage strings.&amp;nbsp; At some places discount is calculated dynamically based on number of active users. Here comes a useful helper.&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush :ruby"&gt;number_to_percentage(100) #100%&lt;/pre&gt;&lt;br /&gt;&lt;strong&gt;4. number_to_phone&lt;/strong&gt;&lt;br /&gt;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.&lt;br /&gt;&lt;br /&gt;Let’s give some example:&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush :ruby"&gt;number_to_phone(1115678) # 111-5678&lt;br /&gt;number_to_phone(1114567890, :area_code=&amp;gt;true) # (111) 456-7890&lt;br /&gt;number_to_phone(1114567890, :area_code=&amp;gt;true, :extension=&amp;gt;999) # (111) 456-7890 x 999&lt;br /&gt;number_to_phone(1114567890, :country_code=&amp;gt;1 # +1- 111 - 456-7890&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So, based upon your requirement you can show the user phone number.&lt;br /&gt;&lt;br /&gt;That’s it for today. I will come shortly with more helper that I usually use! Happy coding :-)&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4311613374961039922-8646762592729371414?l=fuadcse.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://fuadcse.blogspot.com/feeds/8646762592729371414/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4311613374961039922&amp;postID=8646762592729371414' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4311613374961039922/posts/default/8646762592729371414'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4311613374961039922/posts/default/8646762592729371414'/><link rel='alternate' type='text/html' href='http://fuadcse.blogspot.com/2009/12/some-useful-number-helpers.html' title='Some useful number helpers in rails'/><author><name>Fuad Omar</name><uri>http://www.blogger.com/profile/11459190392904433194</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_f6bQr9hj8Bs/SXdkNg9PUyI/AAAAAAAAAHI/Vk9sB0OZyhI/S220/13082007135.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4311613374961039922.post-4943383646556030403</id><published>2009-12-24T21:49:00.001+07:00</published><updated>2009-12-29T18:22:01.110+07:00</updated><title type='text'>Some useful text helpers in Rails (Part - 1)</title><content type='html'>&lt;div class="wlWriterEditableSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:bf6f3c5b-9c24-4bf2-89df-23e150c82ab6" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/Ruby+on+Rails" rel="tag"&gt;Ruby on Rails&lt;/a&gt;,&lt;a href="http://technorati.com/tags/ActionView%3a%3aHelpers%3a%3aTextHelpers" rel="tag"&gt;ActionView::Helpers::TextHelpers&lt;/a&gt;,&lt;a href="http://technorati.com/tags/text+helpers+in+rails" rel="tag"&gt;text helpers in rails&lt;/a&gt;&lt;/div&gt;&lt;p&gt;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 ( &lt;a href="http://www.scrumpad.com"&gt;scrumpad&lt;/a&gt; )&amp;#160; I found many places where texts need to be formatted and Rail’s helper came like a great boon. Let’s share some helpers!&lt;/p&gt;&lt;p&gt;&lt;strong&gt;1.&amp;#160; auto_link&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Suppose your text contains both url and email. auto_link will convert it to as links.&lt;/p&gt;&lt;pre class="brush :ruby"&gt;auto_link(“Go to www.scrumpad.com and meet fuad@code71.com”)&lt;/pre&gt;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:&lt;/p&gt;&lt;pre class="brush :ruby"&gt;auto_link(“Go to www.scrumpad.com and meet fuad@code71.com” , :link=&amp;gt;:urls)&lt;/p&gt;&lt;p&gt;auto_link(“Go to www.scrumpad.com and meet fuad@code71.com” , :link=&amp;gt;:email_address)&lt;/pre&gt;&lt;p&gt;&amp;#160;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;2. cycle&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;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”&lt;/p&gt;suppose you have two css classes named “even” and “odd”. You can make the alternate rows to alternate colors by:&lt;br /&gt;&lt;pre class="brush :ruby"&gt;&amp;lt;tr class=”&amp;lt;%= cycle(“even”, “odd”)&amp;gt;”&amp;lt;td&amp;gt;Data&amp;lt;/td&amp;gt; &amp;lt;/tr&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;3. word_wrap&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;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.&lt;/p&gt;&lt;pre class="brush :ruby"&gt;word_wrap(“A brown colored fox jumped over the lazy dog”, :line_width=&amp;gt;5)&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;4.&amp;#160; truncate&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;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.&lt;/p&gt;&lt;pre class="brush :ruby"&gt;truncate(“Scrumpad, a agile project management tools by Code71”, 19) &lt;/pre&gt;&lt;p&gt;Output will be something like&amp;#160; “Scrumpad, an agile…”&lt;/p&gt;&lt;p&gt;If you want to provide your text instead of “…” you should write it as:&lt;/p&gt;&lt;pre class="brush :ruby"&gt;truncate(“Scrumpad, a agile project management tools by Code71”, 19, “….(More)”) &lt;/pre&gt;&lt;p&gt;Output will be like “Scrumpad, an agile…(More)”&lt;/p&gt;&lt;p&gt;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 :-) )&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4311613374961039922-4943383646556030403?l=fuadcse.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://fuadcse.blogspot.com/feeds/4943383646556030403/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4311613374961039922&amp;postID=4943383646556030403' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4311613374961039922/posts/default/4943383646556030403'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4311613374961039922/posts/default/4943383646556030403'/><link rel='alternate' type='text/html' href='http://fuadcse.blogspot.com/2009/12/some-useful-text-helper-in-rails-part-1.html' title='Some useful text helpers in Rails (Part - 1)'/><author><name>Fuad Omar</name><uri>http://www.blogger.com/profile/11459190392904433194</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_f6bQr9hj8Bs/SXdkNg9PUyI/AAAAAAAAAHI/Vk9sB0OZyhI/S220/13082007135.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4311613374961039922.post-311734378925311736</id><published>2009-08-19T19:11:00.001+07:00</published><updated>2009-08-19T19:15:49.186+07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby on rails'/><category scheme='http://www.blogger.com/atom/ns#' term='exception'/><title type='text'>Showing success/failure message for CRUD in rails</title><content type='html'>How do you know whether a CRUD is successful in rails?How do you show an user friendly error message based on different error? Here is a brief description:  &lt;br /&gt;&lt;br /&gt;&lt;pre class="brush :ruby"&gt;&lt;br /&gt;#create  &lt;br /&gt;&lt;br /&gt;def create     &lt;br /&gt;@is_success = false     &lt;br /&gt;@build = Build.new(params[:build])     &lt;br /&gt;begin       &lt;br /&gt;@build.save!       &lt;br /&gt;@is_success = true     &lt;br /&gt;rescue Exception=&gt;ex       &lt;br /&gt;@message = "Build cannot be saved because #{ex.message} "         &lt;br /&gt;end &lt;br /&gt;end  &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Now from view based on the value of @is_success you can deliver user friendly message.   What in case of update? The code will look somehow different. :-)  &lt;br /&gt;&lt;pre class="brush :ruby"&gt;&lt;br /&gt;def update     &lt;br /&gt;@build = Build.find(params[:id])     &lt;br /&gt;@is_success = @build.update_attributes!(params[:build]) &lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;   &lt;br /&gt;Now comes destroy!&lt;br /&gt;If the destroy() is succesful then frozen method will return true! That is how you can be sure whether an object is deleted!  &lt;br /&gt;&lt;br /&gt;&lt;pre class="brush :ruby"&gt;&lt;br /&gt;def destroy     &lt;br /&gt;@is_success = false     &lt;br /&gt;@build = Build.find(params[:id])     &lt;br /&gt;@build.destroy()     &lt;br /&gt;@is_success = @build.frozen? &lt;br /&gt;end  &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;That's it! Simple!! Happy coding :-)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4311613374961039922-311734378925311736?l=fuadcse.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://fuadcse.blogspot.com/feeds/311734378925311736/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4311613374961039922&amp;postID=311734378925311736' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4311613374961039922/posts/default/311734378925311736'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4311613374961039922/posts/default/311734378925311736'/><link rel='alternate' type='text/html' href='http://fuadcse.blogspot.com/2009/08/showing-successfailure-message-for-crud.html' title='Showing success/failure message for CRUD in rails'/><author><name>Fuad Omar</name><uri>http://www.blogger.com/profile/11459190392904433194</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_f6bQr9hj8Bs/SXdkNg9PUyI/AAAAAAAAAHI/Vk9sB0OZyhI/S220/13082007135.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4311613374961039922.post-2274471799509466500</id><published>2009-01-12T10:50:00.001+06:00</published><updated>2009-01-12T12:01:06.359+06:00</updated><title type='text'>Using subversion client with Net Beans 6.5 and Tortoise 1.5.x</title><content type='html'>&lt;p&gt;I recently upgraded my Net Beans to it’s 6.5 version and Tortoise to 1.5.x. Since then my subversion client is not working and showing an error message as “please get a newer Subversion client”. The fix is the following one:&lt;/p&gt;  &lt;p&gt;Go to Tools=&amp;gt;Plugins=&amp;gt;Available Plugins and install Subvesion 1.5 for windows bridge.&lt;/p&gt;  &lt;p&gt;If it does not fix your problem then,&lt;/p&gt;  &lt;p&gt;Go to &lt;em&gt;Tools=&amp;gt; Options=&amp;gt; Miscellaneous=&amp;gt; Versioning=&amp;gt; Subversion&lt;/em&gt;. You will find an input field to specify&lt;em&gt; SVN home folder.&lt;/em&gt; You can specify where you installed your latest Subversion client software here&lt;/p&gt;  &lt;div class="wlWriterEditableSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:420d61c5-1741-4b72-a905-8c1f278f5fd9" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/net+beans+6.5" rel="tag"&gt;net beans 6.5&lt;/a&gt;,&lt;a href="http://technorati.com/tags/svn+client" rel="tag"&gt;svn client&lt;/a&gt;&lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4311613374961039922-2274471799509466500?l=fuadcse.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://fuadcse.blogspot.com/feeds/2274471799509466500/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4311613374961039922&amp;postID=2274471799509466500' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4311613374961039922/posts/default/2274471799509466500'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4311613374961039922/posts/default/2274471799509466500'/><link rel='alternate' type='text/html' href='http://fuadcse.blogspot.com/2009/01/using-subversion-client-with-net-beans.html' title='Using subversion client with Net Beans 6.5 and Tortoise 1.5.x'/><author><name>Fuad Omar</name><uri>http://www.blogger.com/profile/11459190392904433194</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_f6bQr9hj8Bs/SXdkNg9PUyI/AAAAAAAAAHI/Vk9sB0OZyhI/S220/13082007135.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4311613374961039922.post-2987389065740080044</id><published>2009-01-10T20:13:00.001+06:00</published><updated>2009-08-21T21:44:20.602+07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby on rails'/><category scheme='http://www.blogger.com/atom/ns#' term='time zone in rails'/><title type='text'>Setting user defined time zone in Rails 2.1</title><content type='html'>&lt;p&gt;Time zone support in rails is now easier than ever. You can set your time zone dynamically based on users information.&lt;/p&gt;  &lt;p&gt;Here are the steps:&lt;/p&gt; &lt;br /&gt;In configuration file, config/environment.rb add the following line&lt;br /&gt;&lt;pre class="brush :ruby"&gt;config.time_zone = &amp;quot;UTC&amp;quot;&lt;/pre&gt;&lt;br /&gt;You can prompt from user for their time zone and save it in the database for future reference. For the population of time zones in the dropdown list add the following line in you *.rhtml file:&lt;br /&gt;&lt;pre class="brush :ruby"&gt;&lt;br /&gt;&amp;lt;%= time_zone_select &amp;quot;user&amp;quot;, &amp;quot;time_zone&amp;quot;, TimeZone.all%&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Save this data in the database field.&lt;br /&gt;When the user will be logged in fetch the time_zone from database and set it at Time.zone.&lt;br /&gt;&lt;pre class="brush :ruby"&gt;def set_timezone(time_zone)&lt;br /&gt;   Time.zone = time_zone&lt;br /&gt;end&lt;/pre&gt;&lt;br /&gt;To persist it every where make sure to call it:&lt;br /&gt;&lt;pre class="brush :ruby"&gt;set_timezone(@logged_in_user.time_zone)&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;That’s it!&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4311613374961039922-2987389065740080044?l=fuadcse.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://fuadcse.blogspot.com/feeds/2987389065740080044/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4311613374961039922&amp;postID=2987389065740080044' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4311613374961039922/posts/default/2987389065740080044'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4311613374961039922/posts/default/2987389065740080044'/><link rel='alternate' type='text/html' href='http://fuadcse.blogspot.com/2009/01/setting-user-defined-time-zone-in-rails.html' title='Setting user defined time zone in Rails 2.1'/><author><name>Fuad Omar</name><uri>http://www.blogger.com/profile/11459190392904433194</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_f6bQr9hj8Bs/SXdkNg9PUyI/AAAAAAAAAHI/Vk9sB0OZyhI/S220/13082007135.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4311613374961039922.post-8100352405044590853</id><published>2008-12-03T12:01:00.000+06:00</published><updated>2009-12-31T11:05:59.388+07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby on rails'/><category scheme='http://www.blogger.com/atom/ns#' term='https'/><category scheme='http://www.blogger.com/atom/ns#' term='redirection to https'/><title type='text'>Redirecting a http request to https in Rails</title><content type='html'>&lt;p&gt;In my previous blog i instructed how to install a SSL certificate in server. In this tutorial I will provide you some insights on how to redirect a http request to a https.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;we need two methods:&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;1. One is for going to from http to https:&lt;br /&gt;&lt;pre class="brush :ruby"&gt;def require_https  &lt;br /&gt;redirect_to :protocol =&amp;gt; "&lt;a href="https://%22/"&gt;https://"&lt;/a&gt; unless (request.ssl? or local_request? or request.post? or ENV["RAILS_ENV"]=="development")   &lt;br /&gt;end&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;2. The other is the reverse of this one, that is going from https to http:&lt;/p&gt;&lt;pre class="brush :ruby"&gt;def require_http  &lt;br /&gt;redirect_to :protocol =&amp;gt; "&lt;a href="http://%22/"&gt;http://"&lt;/a&gt; if (request.ssl?)   &lt;br /&gt;end&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Now you can call these two function where it is necessary like:&lt;br /&gt;&lt;pre class="brush :ruby"&gt;before_filter :require_https&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;In this point you may get an error like “Infinite Redirection loop”. In order to solve this problem add the following line at&lt;br /&gt;&lt;pre class="brush :ruby"&gt;RequestHeader set X_FORWARDED_PROTO 'https'&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So the config file should be something like this:&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush :ruby"&gt;&lt;VirtualHost *:443&gt;&lt;br /&gt;RequestHeader set X_FORWARDED_PROTO 'https'  &lt;br /&gt;SSLEngine on    &lt;br /&gt;SSLCertificateFile /etc/apache2/SSL_Files/abc.crt    &lt;br /&gt;SSLCertificateKeyFile /etc/apache2/SSL_Files/abc.key    &lt;br /&gt;SSLCertificateChainFile /etc/apache2/SSL_Files/gd_bundle.crt    &lt;br /&gt;&lt;/VirtualHost&gt;&lt;br /&gt;&lt;/pre&gt;&lt;p&gt;Now any request coming to http should be redirected to https.&lt;/p&gt;&lt;p&gt;Hope this will serve your purpose. Happy coding!!   &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4311613374961039922-8100352405044590853?l=fuadcse.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://fuadcse.blogspot.com/feeds/8100352405044590853/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4311613374961039922&amp;postID=8100352405044590853' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4311613374961039922/posts/default/8100352405044590853'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4311613374961039922/posts/default/8100352405044590853'/><link rel='alternate' type='text/html' href='http://fuadcse.blogspot.com/2009/01/redirecting-http-request-to-https-in.html' title='Redirecting a http request to https in Rails'/><author><name>Fuad Omar</name><uri>http://www.blogger.com/profile/11459190392904433194</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_f6bQr9hj8Bs/SXdkNg9PUyI/AAAAAAAAAHI/Vk9sB0OZyhI/S220/13082007135.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4311613374961039922.post-6644194279681315779</id><published>2008-12-01T18:35:00.000+06:00</published><updated>2009-01-11T13:32:47.426+06:00</updated><title type='text'>Steps for installing SSL certificate in Linux</title><content type='html'>&lt;p&gt;Here is a complete process of installing SSL certificate in Linux box which run in Amazon EC2 instance and served by Apache. I will also provide suggestion to show how homepage can be served in https while invoking the default page.&lt;/p&gt;  &lt;p&gt;I tested with:&lt;/p&gt;  &lt;p&gt;1. Linux version 2.6.16 (Red Hat 4.0.1-5)&lt;/p&gt;  &lt;p&gt;2. Apache 2&lt;/p&gt;  &lt;p&gt;You will need an installed copy of OpenSSL in the box. It is installed by default with Apache2. If you do not have it installed you should install it from &lt;a title="http://www.openssl.org/" href="http://www.openssl.org/"&gt;http://www.openssl.org/&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;So here are the steps:&lt;/p&gt;  &lt;p&gt;1. Generating a private key:&lt;/p&gt;  &lt;p&gt;To create a private key Go to /etc/apache2/conf.d and use the following command:&lt;/p&gt;  &lt;p&gt;/usr/bin/openssl genrsa –rand /dev/urandom –out /etc/apache2/conf.d/server.key&lt;/p&gt;  &lt;p&gt;Hence a private key file named “server.key” will be generated at the /etc/apache2/conf.d&lt;/p&gt;  &lt;p&gt;2. Generating CSR (certificate signing request):&lt;/p&gt;  &lt;p&gt;For creating a file named server.csr at the same directory the following command should be written:&lt;/p&gt;  &lt;p&gt;/usr/bin/openssl req –new –key /etc/apache2/conf.d/server.key –out /etc/apache2/conf.d/server.csr&lt;/p&gt;  &lt;p&gt;After proving this command some  information should be provided. A CSR file will be generated after this one.&lt;/p&gt;  &lt;p&gt;3. Creating a Certificate: &lt;/p&gt;  &lt;p&gt;You can generate a Commercially Signed Certificate file from any Commercially Signed Authority by providing the private key and csr.&lt;/p&gt;  &lt;p&gt;4. Installing the certificate:&lt;/p&gt;  &lt;p&gt;Go to /etc/apache2/sites-available/default and add the following configuration:&lt;/p&gt;  &lt;p&gt;&amp;lt;VirtualHost *:443&amp;gt;  &lt;br /&gt; SSLEngine on   &lt;br /&gt; SSLCertificateFile /etc/apache2/SSL_Files/abc.crt   &lt;br /&gt; SSLCertificateKeyFile /etc/apache2/SSL_Files/abc.key   &lt;br /&gt; SSLCertificateChainFile /etc/apache2/SSL_Files/gd_bundle.crt   &lt;br /&gt;&amp;lt;/VirtualHost&amp;gt;&lt;/p&gt;  &lt;p&gt;Make sure that port 443 is open.&lt;/p&gt;  &lt;p&gt;That’s it!!Now your certificate is installed in the server.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4311613374961039922-6644194279681315779?l=fuadcse.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://fuadcse.blogspot.com/feeds/6644194279681315779/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4311613374961039922&amp;postID=6644194279681315779' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4311613374961039922/posts/default/6644194279681315779'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4311613374961039922/posts/default/6644194279681315779'/><link rel='alternate' type='text/html' href='http://fuadcse.blogspot.com/2009/01/steps-for-installing-ssl-certificate-in.html' title='Steps for installing SSL certificate in Linux'/><author><name>Fuad Omar</name><uri>http://www.blogger.com/profile/11459190392904433194</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://3.bp.blogspot.com/_f6bQr9hj8Bs/SXdkNg9PUyI/AAAAAAAAAHI/Vk9sB0OZyhI/S220/13082007135.jpg'/></author><thr:total>1</thr:total></entry></feed>
