<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Zan's Collection of anything useful &#187; Rails</title>
	<atom:link href="http://liangzan.net/index.php/category/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://liangzan.net</link>
	<description></description>
	<lastBuildDate>Mon, 26 Jul 2010 10:27:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Test for blank elements with Culerity-Cucumber</title>
		<link>http://liangzan.net/index.php/2010/04/07/test-for-blank-elements-with-culerity-cucumber/</link>
		<comments>http://liangzan.net/index.php/2010/04/07/test-for-blank-elements-with-culerity-cucumber/#comments</comments>
		<pubDate>Wed, 07 Apr 2010 07:13:30 +0000</pubDate>
		<dc:creator>zan</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[cucumber culerity rails]]></category>

		<guid isPermaLink="false">http://liangzan.net/?p=69</guid>
		<description><![CDATA[How to test for blank elements when using culerity]]></description>
			<content:encoded><![CDATA[<p>When you need to test for blank elements on Culerity, it is better to parse the html. Using Celerity helper methods such as $browser.button will return a Culerity::RemoteObjectProxy object. It does not tell you if it found an element or not. It is a limitation of Culerity. To work around it try this:</p>
<p><script src="http://gist.github.com/358616.js"></script></p>
<p>You can use the above code in the step definitions. </p>
]]></content:encoded>
			<wfw:commentRss>http://liangzan.net/index.php/2010/04/07/test-for-blank-elements-with-culerity-cucumber/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Selectively load Ruby Facets modules</title>
		<link>http://liangzan.net/index.php/2010/01/12/selectively-load-ruby-facets-modules/</link>
		<comments>http://liangzan.net/index.php/2010/01/12/selectively-load-ruby-facets-modules/#comments</comments>
		<pubDate>Tue, 12 Jan 2010 09:43:39 +0000</pubDate>
		<dc:creator>zan</dc:creator>
				<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://liangzan.net/?p=62</guid>
		<description><![CDATA[Ruby facets has a lot of useful libraries. Sometimes you only want to load the ones that you need. Here&#8217;s a little snippet that you can use Rails won&#8217;t load facets as the &#8216;lib&#8217; param is false. We then load the libraries manually through initializers. This helps when facets unwittingly clobbers some of the other [...]]]></description>
			<content:encoded><![CDATA[<p>Ruby <a href="http://facets.rubyforge.org/">facets</a> has a lot of useful libraries. Sometimes you only want to load the ones that you need. Here&#8217;s a little snippet that you can use</p>
<p><script src="http://gist.github.com/275055.js"></script></p>
<p>Rails won&#8217;t load <a href="http://facets.rubyforge.org/">facets</a> as the &#8216;lib&#8217; param is false. We then load the libraries manually through initializers. This helps when <a href="http://facets.rubyforge.org/">facets</a> unwittingly clobbers some of the other gems you are using.</p>
]]></content:encoded>
			<wfw:commentRss>http://liangzan.net/index.php/2010/01/12/selectively-load-ruby-facets-modules/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Testing PDFs with Celerity-Culerity and Cucumber</title>
		<link>http://liangzan.net/index.php/2009/12/11/testing-pdfs-with-celerity-culerity-and-cucumber/</link>
		<comments>http://liangzan.net/index.php/2009/12/11/testing-pdfs-with-celerity-culerity-and-cucumber/#comments</comments>
		<pubDate>Fri, 11 Dec 2009 08:42:41 +0000</pubDate>
		<dc:creator>zan</dc:creator>
				<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://liangzan.net/?p=57</guid>
		<description><![CDATA[Here&#8217;re the code to test the content of the pdfs. But first you need the pdf-reader gem. The feature file is above. Add the code above as a helper file. And lastly, the steps. Remember to add a @pdf tag to your feature files.]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;re the code to test the content of the pdfs. But first you need the <a href="http://github.com/yob/pdf-reader">pdf-reader</a> gem.</p>
<p><script src="http://gist.github.com/254067.js"></script></p>
<p>The feature file is above.</p>
<p><script src="http://gist.github.com/254065.js"></script></p>
<p>Add the code above as a helper file.</p>
<p><script src="http://gist.github.com/254064.js"></script></p>
<p>And lastly, the steps. Remember to add a @pdf tag to your feature files. </p>
]]></content:encoded>
			<wfw:commentRss>http://liangzan.net/index.php/2009/12/11/testing-pdfs-with-celerity-culerity-and-cucumber/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using Cucumber to test Paperclip file attachments</title>
		<link>http://liangzan.net/index.php/2009/08/31/using-cucumber-to-test-paperclip-file-attachments/</link>
		<comments>http://liangzan.net/index.php/2009/08/31/using-cucumber-to-test-paperclip-file-attachments/#comments</comments>
		<pubDate>Mon, 31 Aug 2009 15:23:45 +0000</pubDate>
		<dc:creator>zan</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[cucumber]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://liangzan.net/?p=51</guid>
		<description><![CDATA[I was using thoughtbot&#8217;s paperclip to attach files. I realised that paperclip only provide shoulda macros for test::unit. No cucumber macros? In the end, I got to write it. Its very simple. The feature is shown here: Define your steps: As celerity does not have an attach_file method, I got to write my own. You [...]]]></description>
			<content:encoded><![CDATA[<p>I was using thoughtbot&#8217;s <a href="http://github.com/thoughtbot/paperclip/tree/master">paperclip</a> to attach files. I realised that paperclip only provide <a href="http://github.com/thoughtbot/shoulda/tree/master">shoulda</a> macros for test::unit. No <a href="https://github.com/aslakhellesoy/cucumber/tree">cucumber</a> macros? In the end, I got to write it. Its very simple.</p>
<p>The feature is shown here: <script src="http://gist.github.com/178507.js"></script></p>
<p>Define your steps:  <script src="http://gist.github.com/178505.js"></script></p>
<p>As <a href="https://github.com/jarib/celerity/tree">celerity</a> does not have an <strong>attach_file</strong> method, I got to write my own. You could see it&#8217;s not hard at all. Remember to define the <strong>browser</strong> variable.</p>
<p>Last, we have some hooks to clean up the test upload files. Remember to place an <strong>@upload</strong> tag in your scenario.</p>
<p><script src="http://gist.github.com/178501.js"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://liangzan.net/index.php/2009/08/31/using-cucumber-to-test-paperclip-file-attachments/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Email_Spec Integrated with Action_Mailer_Cache_Delivery</title>
		<link>http://liangzan.net/index.php/2009/08/20/email_spec-integrated-with-action_mailer_cache_delivery/</link>
		<comments>http://liangzan.net/index.php/2009/08/20/email_spec-integrated-with-action_mailer_cache_delivery/#comments</comments>
		<pubDate>Thu, 20 Aug 2009 06:47:41 +0000</pubDate>
		<dc:creator>zan</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://liangzan.net/?p=49</guid>
		<description><![CDATA[For people using the action mailer cache delivery plugin, you can now add the email_spec plugin. The email_spec plugin provides many wonderful helpers to test your emails. Both plugins are now integrated. To use them you need to install both my forks of the plugins: action_mailer_cache_delivery email_spec Install them as plugins following the same instructions [...]]]></description>
			<content:encoded><![CDATA[<p>For people using the action mailer cache delivery plugin, you can now add the email_spec plugin. The email_spec plugin provides many wonderful helpers to test your emails. Both plugins are now integrated. To use them you need to install both my forks of the plugins:</p>
<ul>
<li><a href="http://github.com/liangzan/action_mailer_cache_delivery/tree/master">action_mailer_cache_delivery</a></li>
<li> <a href="http://github.com/liangzan/email-spec/tree/master">email_spec</a></li>
</ul>
<p>Install them as plugins following the same instructions from the README. Don&#8217;t forget to change the git url!</p>
]]></content:encoded>
			<wfw:commentRss>http://liangzan.net/index.php/2009/08/20/email_spec-integrated-with-action_mailer_cache_delivery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Persistent Cookie Authentication Gem Documentation</title>
		<link>http://liangzan.net/index.php/2008/07/07/persistent-cookie-authentication-gem-documentation/</link>
		<comments>http://liangzan.net/index.php/2008/07/07/persistent-cookie-authentication-gem-documentation/#comments</comments>
		<pubDate>Mon, 07 Jul 2008 03:27:20 +0000</pubDate>
		<dc:creator>zan</dc:creator>
				<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://liangzan.net/?p=35</guid>
		<description><![CDATA[What are persistent cookies? What are persistent cookies used for? A persistent cookie is no different from other cookies. What is different is that it is used to identify users. With identification, it enables a website to remember you on subsequent visits, speeding up or enhancing your experience of services or functions offered. For example, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://liangzan.net/wp-content/uploads/2008/07/cookiemonster.jpg"></p>
<p style="text-align: center"><img src="http://liangzan.net/wp-content/uploads/2008/07/cookiemonster.jpg" title="cookie-monster" alt="cookie-monster" /></p>
<p></a></p>
<h2>What are persistent cookies? What are persistent cookies used for?</h2>
<p>A persistent cookie is no different from other cookies. What is different is that it is used to identify users. With identification, it enables a website to remember you on subsequent visits, speeding up or enhancing your experience of services or functions offered.  For example, a website may offer its contents in different languages. On your first visit, you may choose to have the content delivered in French and the site may record that preference in a persistent cookie set on your browser. When you revisit that site it will use the cookie to ensure that the content is delivered in French.</p>
<h2>Why do I need it?</h2>
<p>The biggest advantage is we can skip the registration step. We use persistent cookies to identify users. Users can be anonymous first time visitors or registered users. When we&#8217;re able to identify them(especially anonymous users), we can keep data like what items the user has placed on her cart.  For e-commerce sites, you may not want to force the first time anonymous users to register when they try to place items in the cart. Some may be turned off and go away. By using persistent cookies to identify them, we can save which items they want without making them go through the registration process. You can make them register when its time to make payment.</p>
<h2>How do I use them?</h2>
<p>To use persistent cookies effectively, we&#8217;re segregating the web site into 3 levels of access.</p>
<ul>
<li>Public &#8211; anyone can go in. Examples would include your index page and FAQ</li>
<li>Protected &#8211; cookie authenticated.</li>
<li>Private &#8211; user must log in.</li>
</ul>
<p>In your controllers, place a before_filter to identify which actions is private/protected</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">before_filter <span style="color:#ff3333; font-weight:bold;">:cookie_required</span>, <span style="color:#ff3333; font-weight:bold;">:only</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:some_actions</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">before_filter <span style="color:#ff3333; font-weight:bold;">:login_required</span>, <span style="color:#ff3333; font-weight:bold;">:only</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:some_actions</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

<p>Use :cookie_required for protected actions, and :login_required for private areas.</p>
<h2>How do I identify the users?</h2>
<p>If you have seen through the code, there&#8217;re actually 3 models controlling the authentication system: identity, login_cookies and users.</p>
<ul>
<li>Identity &#8211; the unique identifier used to identify users. All users only have one identity, be it anonymous or registered users.</li>
<li>Login_cookie &#8211; the cookies used for identifying the users.</li>
<li>User &#8211; for identifying registered users.</li>
</ul>
<p>One unique identity can have many Cookies, but only one User. So when you want to link to the user, use the Identity model instead. For example, you want to create a relationship where a user has many books:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Identity <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
has_one <span style="color:#ff3333; font-weight:bold;">:user</span>
has_many <span style="color:#ff3333; font-weight:bold;">:login_cookies</span>
has_many <span style="color:#ff3333; font-weight:bold;">:books</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Whenever your controllers need to link to the user, do it through the identity instead.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#0066ff; font-weight:bold;">@identity</span>.<span style="color:#9900CC;">user</span></pre></div></div>

<h2>What if the user&#8217;s browser did not enable cookies?</h2>
<p>Good question. If that happens, we will treat the protected region as private automatically. There&#8217;s no need for you to handle it. The code is created to degrade gracefully.</p>
<h2>Implementation of persistent cookie authentication</h2>
<p>The implementation is as suggested by <a href="http://jaspan.com/improved_persistent_login_cookie_best_practice">Barry Jaspan</a> and <a href="http://fishbowl.pastiche.org/2004/01/19/persistent_login_cookie_best_practice">Charles Miller</a>. In fact, <a href="http://drupal.org">Drupal</a>&#8216;s Persistent Login Module follows this set of implementation.</p>
<h2>Special cases</h2>
<p>There&#8217;s a special case you need to take note. For example, an anonymous user comes to your site, and place a few items on the cart. And then she registers as a new user. Will the data in her shopping cart be lost?  The gem will ensure that her cookie entry and her newly registered user entry will be transferred to the same identity. But as I do not know how your website will be implemented, I did not implement the merging of the data from her cart. You&#8217;ll need to add your code to transfer the data to her new identity. An example of the implementation is here. You&#8217;ll need to modify the merge_identity function from user_controller.rb  From app/controllers/user_controller.rb</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> mergeIdentity<span style="color:#006600; font-weight:bold;">&#40;</span>anonymousCookieValue, knownIdentityID<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">if</span> anonymousCookieValue == <span style="color:#0000FF; font-weight:bold;">nil</span> <span style="color:#006600; font-weight:bold;">||</span> knownIdentityID == <span style="color:#0000FF; font-weight:bold;">nil</span>
<span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">false</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">false</span> <span style="color:#9966CC; font-weight:bold;">if</span> !LoginCookie.<span style="color:#9900CC;">isAnonymous</span><span style="color:#006600; font-weight:bold;">&#40;</span>anonymousCookieValue<span style="color:#006600; font-weight:bold;">&#41;</span>
loginValue = LoginCookie.<span style="color:#9900CC;">getCookieLogin</span><span style="color:#006600; font-weight:bold;">&#40;</span>anonymousCookieValue<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#getting the both anonymous user's identity and the known identity</span>
anonymousCookie = LoginCookie.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:first</span>, <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;login = ?&quot;</span>, loginValue<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
anonymousIdentity = anonymousCookie.<span style="color:#9900CC;">identity</span>
knownIdentity = Identity.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>knownIdentityID<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">false</span> <span style="color:#9966CC; font-weight:bold;">if</span> anonymousIdentity == <span style="color:#0000FF; font-weight:bold;">nil</span> <span style="color:#006600; font-weight:bold;">||</span> knownIdentity == <span style="color:#0000FF; font-weight:bold;">nil</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#transferring the cookies</span>
anonymousIdentity.<span style="color:#9900CC;">login_cookies</span>.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>cookie<span style="color:#006600; font-weight:bold;">|</span>
cookie.<span style="color:#9900CC;">identity_id</span> = knownIdentity.<span style="color:#9900CC;">id</span>
cookie.<span style="color:#9900CC;">save</span>
<span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#transferring the cards</span>
anonymousIdentity.<span style="color:#9900CC;">cards</span>.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>card<span style="color:#006600; font-weight:bold;">|</span>
card.<span style="color:#9900CC;">identity_id</span> = knownIdentity.<span style="color:#9900CC;">id</span>
card.<span style="color:#9900CC;">save</span>
<span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#finally send the identity to its grave</span>
anonymousIdentity.<span style="color:#9900CC;">destroy</span>
&nbsp;
<span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">true</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This is about all you will need to know to implement the persistent cookie authentication system effectively. Any bugs or unclear points, feel free to contact me.</p>
]]></content:encoded>
			<wfw:commentRss>http://liangzan.net/index.php/2008/07/07/persistent-cookie-authentication-gem-documentation/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Persistent Cookie Authentication Generator</title>
		<link>http://liangzan.net/index.php/2008/07/06/persistent-cookie-authentication-generator/</link>
		<comments>http://liangzan.net/index.php/2008/07/06/persistent-cookie-authentication-generator/#comments</comments>
		<pubDate>Sun, 06 Jul 2008 22:38:35 +0000</pubDate>
		<dc:creator>zan</dc:creator>
				<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://liangzan.net/?p=34</guid>
		<description><![CDATA[This is a Rails gem that I wrote, or rather extracted from the codebase from hushyhushy.com. OK, what is PersistentCookie Authentication Generator? This gem is a code generator.  This generator creates an authentication system with persistent cookie management. Feature include a model which uses SHA1 encryption and salted hashes for passwords a controller with signup, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://liangzan.net/wp-content/uploads/2008/07/rails.png"></a></p>
<p style="text-align: center"><a href="http://liangzan.net/wp-content/uploads/2008/07/rails.png"><img src="http://liangzan.net/wp-content/uploads/2008/07/rails.png" title="Rails" alt="Rails" /></a></p>
<p>This is a <a href="http://www.rubyonrails.org">Rails</a> gem that I wrote, or rather extracted from the codebase from <a href="http://www.hushyhushy.com">hushyhushy.com</a>. OK, what is <a href="http://rubyforge.org/projects/per-cookie-auth/">PersistentCookie Authentication Generator</a>?</p>
<p>This gem is a code generator.  This generator creates an <a href="http://wiki.rubyonrails.com/rails/pages/Authentication">authentication system</a> with persistent cookie management.</p>
<p>Feature include</p>
<ul>
<li>a model which uses SHA1 encryption and salted hashes for passwords</li>
<li>a controller with signup, login, welcome and logoff actions</li>
<li>gmail smtp server integration</li>
<li>account creation that requires account verification via email</li>
<li>supports forgotten and changed passwords</li>
<li>a mixin which lets you easily add advanced authentication features to your abstract base controller</li>
<li>extensive unit and functional test cases to make sure nothing breaks.</li>
<li>token based authentication</li>
<li>persistent cookie management that allows anonymous users to be authenticated via cookies</li>
</ul>
<p>Actually the code is heavily modified from another well known gem: <a href="http://rubyforge.org/projects/salted-login/">salted hash login generator</a></p>
<p>To put it simply, it is</p>
<p align="center"><em>persistent_cookie_authentication = <a href="http://wiki.rubyonrails.org/rails/pages/SaltedHashLoginGenerator">salted_hash_login</a> &#8211; <a href="http://rubyforge.org/projects/localization">localization</a> + persistent_cookie_management</em></p>
<p>The code is written with an emphasis on simplicity. No effort is made from me to write clever or <a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY(don&#8217;t repeat yourself)</a> code. The intention is to dumb it down so that people using it could understand it. As Joel says, <a href="http://www.joelonsoftware.com/articles/LeakyAbstractions.html">abstraction is bad.</a></p>
<p><a href="http://liangzan.net/wp-content/uploads/2008/07/keep-it-simple-stupid-kiss.png"></a></p>
<p style="text-align: center"><a href="http://liangzan.net/wp-content/uploads/2008/07/keep-it-simple-stupid-kiss.png"><img src="http://liangzan.net/wp-content/uploads/2008/07/keep-it-simple-stupid-kiss.png" title="kiss" alt="kiss" /></a></p>
<p>Here&#8217;s the installation instructions:</p>
<p>To install, run this line on the shell</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">gem <span style="color: #c20cb9; font-weight: bold;">install</span> persistent_cookie_authentication_generator</pre></div></div>

<p>From the directory of your Rails app, run this from the shell</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">ruby script<span style="color: #000000; font-weight: bold;">/</span>generate persistent_cookie_authentication</pre></div></div>

<p>Change your config/database.yml settings. Then update your database by running</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">rake db:migrate</pre></div></div>

<p>From app/controllers/application.rb, add this within application.rb</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">include</span> UserSystem</pre></div></div>

<p>Example <a href="http://liangzan.net/wp-content/uploads/2008/07/applicationrb.txt" class="attachmentlink">application.rb</a></p>
<p>From config/environment.rb, add this at the end of the file</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'environments/user_environment'</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'smtp_tls'</span></pre></div></div>

<p>Example <a href="http://liangzan.net/wp-content/uploads/2008/07/environmentrb.txt" class="attachmentlink">environment.rb</a></p>
<p>From config/environments/, add this to the end of both development.rb and production.rb</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#6666ff; font-weight:bold;">ActionMailer::Base</span>.<span style="color:#9900CC;">smtp_settings</span> = <span style="color:#006600; font-weight:bold;">&#123;</span>:address =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">&quot;smtp.gmail.com&quot;</span>,
<span style="color:#ff3333; font-weight:bold;">:port</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">&quot;587&quot;</span>,
<span style="color:#ff3333; font-weight:bold;">:domain</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">&quot;localhost.localdomain&quot;</span>,
<span style="color:#ff3333; font-weight:bold;">:authentication</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#ff3333; font-weight:bold;">:plain</span>,
<span style="color:#ff3333; font-weight:bold;">:user_name</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">&quot;yourgmailusername&quot;</span>,
<span style="color:#ff3333; font-weight:bold;">:password</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">&quot;yourgmailpassword&quot;</span>
<span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>Example <a href="http://liangzan.net/wp-content/uploads/2008/07/developmentrb.txt" class="attachmentlink">development.rb</a></p>
<p>From config/initializers/inflections.rb, modify your inflections.rb to look like this</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC00FF; font-weight:bold;">Inflector</span>.<span style="color:#9900CC;">inflections</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>inflect<span style="color:#006600; font-weight:bold;">|</span><span style="color:#008000; font-style:italic;">#   inflect.plural /^(ox)$/i, '1en'</span>
<span style="color:#008000; font-style:italic;">#   inflect.singular /^(ox)en/i, '1'</span>
<span style="color:#008000; font-style:italic;">#   inflect.irregular 'person', 'people'</span>
<span style="color:#008000; font-style:italic;">#   inflect.uncountable %w( fish sheep )</span>
inflect.<span style="color:#9900CC;">irregular</span> <span style="color:#996600;">'LoginCookie'</span>, <span style="color:#996600;">'LoginCookies'</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>To test, run this from your shell</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">rake db:<span style="color: #7a0874; font-weight: bold;">test</span>:clone</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">rake test</pre></div></div>

<p>From config/user_environment.rb, change the name of your Rails app to your liking</p>
<p>That&#8217;s it. Another <a href="http://liangzan.net/?p=35">post</a> will cover how to use persistent login cookies. Bug reports please contact me directly at my email address.</p>
<p>*Update: Here&#8217;s the slides I&#8217;ve presented at the Singapore Ruby Meeting</p>
<div style="width:425px;text-align:left" id="__ss_703906"><a style="font:14px Helvetica,Arial,Sans-serif;display:block;margin:12px 0 3px 0;text-decoration:underline;" href="http://www.slideshare.net/liangzan/persistent-authentication-cookie-generator-gem-presentation?type=powerpoint" title="Persistent Authentication Cookie Generator Gem">Persistent Authentication Cookie Generator Gem</a><object style="margin:0px" width="425" height="355"><param name="movie" value="http://static.slideshare.net/swf/ssplayer2.swf?doc=persistent-cookie-authentication-gem-1225329059548006-8&#038;stripped_title=persistent-authentication-cookie-generator-gem-presentation" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed src="http://static.slideshare.net/swf/ssplayer2.swf?doc=persistent-cookie-authentication-gem-1225329059548006-8&#038;stripped_title=persistent-authentication-cookie-generator-gem-presentation" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"></embed></object>
<div style="font-size:11px;font-family:tahoma,arial;height:26px;padding-top:2px;">View SlideShare <a style="text-decoration:underline;" href="http://www.slideshare.net/liangzan/persistent-authentication-cookie-generator-gem-presentation?type=powerpoint" title="View Persistent Authentication Cookie Generator Gem on SlideShare">presentation</a> or <a style="text-decoration:underline;" href="http://www.slideshare.net/upload?type=powerpoint">Upload</a> your own.</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://liangzan.net/index.php/2008/07/06/persistent-cookie-authentication-generator/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>InvalidAuthenticityToken and Session Expiry</title>
		<link>http://liangzan.net/index.php/2008/06/29/invalidauthenticitytoken-and-session-expiry/</link>
		<comments>http://liangzan.net/index.php/2008/06/29/invalidauthenticitytoken-and-session-expiry/#comments</comments>
		<pubDate>Sun, 29 Jun 2008 03:58:33 +0000</pubDate>
		<dc:creator>zan</dc:creator>
				<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://liangzan.net/?p=33</guid>
		<description><![CDATA[This is something that I&#8217;ve discovered as I was building hushyhushy. After I upgraded to Rails 2, I began using authenticity tokens. So far so good. But in the course of testing the app, I would encounter the infamous &#8216;InvalidAuthenticityToken Error&#8217;. That&#8217;s weird! It usually happened in the middle of testing the app, and after [...]]]></description>
			<content:encoded><![CDATA[<p>This is something that I&#8217;ve discovered as I was building <a href="http://www.hushyhushy.com">hushyhushy</a>. After I upgraded to Rails 2, I began using authenticity tokens. So far so good. But in the course of testing the app, I would encounter the infamous &#8216;InvalidAuthenticityToken Error&#8217;. That&#8217;s weird! It usually happened in the middle of testing the app, and after some time too. It means that my authentication token was working fine, but somehow or rather it just failed in mid air. Then I realised the reason after some investigation.</p>
<p>The reason is that my session expired. The session id changed. The authentication token is tied to the session id. So it became invalid. I did set my sessions to expire. I hope this helps for those who encountered the same problems as me.</p>
<p>The obvious solutions that I can think of is:</p>
<ul>
<li>prevent the session from expiring</li>
<li>lengthen the session expiration time</li>
<li>reset the authentication token</li>
</ul>
<p>Here&#8217;s how you would set the session expiration time in config/environment.rb</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"> <span style="color:#6666ff; font-weight:bold;">ActionController::Base</span>.<span style="color:#9900CC;">session_options</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:session_expires</span><span style="color:#006600; font-weight:bold;">&#93;</span> = 10.<span style="color:#9900CC;">minutes</span>.<span style="color:#9900CC;">from_now</span></pre></td></tr></table></div>

<p>The best solution is to reset the authentication token. I tried doing that with form_authentication_token(), but I wasn&#8217;t too successful. I&#8217;ll try again and update this post if I am successful.</p>
]]></content:encoded>
			<wfw:commentRss>http://liangzan.net/index.php/2008/06/29/invalidauthenticitytoken-and-session-expiry/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Postman Rails Plugin</title>
		<link>http://liangzan.net/index.php/2008/06/27/postman-rails-plugin/</link>
		<comments>http://liangzan.net/index.php/2008/06/27/postman-rails-plugin/#comments</comments>
		<pubDate>Fri, 27 Jun 2008 23:42:17 +0000</pubDate>
		<dc:creator>zan</dc:creator>
				<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://liangzan.net/?p=31</guid>
		<description><![CDATA[This plugin is calculates the shipping rate for a given location and weight. It is useful for cases where the postal service in your country doesn&#8217;t provide APIs for calculating shipping rates. This is actually extracted code from my website hushyhushy.com. I needed some sort of a calculator for shipping rates. But there isn&#8217;t any. [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://liangzan.net/wp-content/uploads/2008/06/postman_pat.jpg"></a></p>
<p style="text-align: center"><a href="http://liangzan.net/wp-content/uploads/2008/06/postman_pat.jpg"><img src="http://liangzan.net/wp-content/uploads/2008/06/postman_pat.jpg" title="postman" alt="postman" /></a></p>
<p>This plugin is calculates the shipping rate for a given location and weight. It is useful for cases where the postal service in your country doesn&#8217;t provide APIs for calculating shipping rates.</p>
<p>This is actually extracted code from my website <a href="http://www.hushyhushy.com">hushyhushy.com</a>. I needed some sort of a calculator for shipping rates. But there isn&#8217;t any. Those available are meant for USPS, Fedex, UPS, you know the Big &amp; Expensive mail carriers. Not suitable for poor folks like me. They have nice APIs which makes writing your own calculator code superfluous.</p>
<p>The code is ridiculously simple. I even find it embarrassing. Well, why code when you have the database doing all the heavy lifting for you?</p>
<p>Here&#8217;s the project URL: <a href="http://code.google.com/p/rails-postman/">rails-postman</a></p>
<p>This is my first <a href="http://www.rubyonrails.org/">Rails</a> plugin. So if you find any bugs, please yell at me! Do forgive me after that. Hahaha&#8230; Anyway, there&#8217;s another plugin coming from the hushyhushy code. That one is more complicated. But definitely more useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://liangzan.net/index.php/2008/06/27/postman-rails-plugin/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Setting up a VPS on Ubuntu Dapper 6</title>
		<link>http://liangzan.net/index.php/2007/11/11/setting-up-a-vps-on-ubuntu-dapper-6/</link>
		<comments>http://liangzan.net/index.php/2007/11/11/setting-up-a-vps-on-ubuntu-dapper-6/#comments</comments>
		<pubDate>Sun, 11 Nov 2007 06:55:05 +0000</pubDate>
		<dc:creator>zan</dc:creator>
				<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://liangzan.net/?p=7</guid>
		<description><![CDATA[Here&#8217;s a summary of where I found my how-tos when set up my VPS to host my sites: ShippingTogether, and my own site. My web host is Slicehost. It&#8217;s cheap and good. I&#8217;d definitely give my recommendation for them if you want a VPS. If you&#8217;re wondering whether to choose between a my VPS or [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a summary of where I found my how-tos when set up my <a href="http://en.wikipedia.org/wiki/Virtual_private_server">VPS</a> to host my sites: <a href="http://www.shippingtogether.com">ShippingTogether</a>, and my own <a href="http://liangzan.net">site</a>. My web host is <a href="http://www.slicehost.com">Slicehost</a>. It&#8217;s cheap and good. I&#8217;d definitely give my recommendation for them if you want a <a href="http://en.wikipedia.org/wiki/Virtual_private_server">VPS</a>.</p>
<p>If you&#8217;re wondering whether to choose between a my <a href="http://en.wikipedia.org/wiki/Virtual_private_server">VPS</a> or <a href="http://en.wikipedia.org/wiki/Shared_web_hosting_service">shared hosting</a>, here&#8217;s why I chose VPS in the end: the many deployment options available. In most shared hosting plans, you&#8217;ll be limited to just 1 mongrel instance. Whereas for <a href="http://en.wikipedia.org/wiki/Virtual_private_server">VPS</a>, you&#8217;re free to configure your rails app in any way you deem best.</p>
<ul>
<li><a href="http://wiki.slicehost.com/doku.php?id=get_started_with_your_new_ubuntu_slice">Initial configuration</a></li>
<li><a href="http://www.cyberciti.biz/faq/ubuntu-vsftpd-ftp-service-server/">VSFTP</a></li>
<li><a href="http://www.urbanpuddle.com/articles/2007/05/09/install-ruby-on-rails-on-ubuntu-feisty-fawn">Ruby on Rails &#8211; Mongrel &#8211; MySQL &#8211; NginX &#8211; PHP</a></li>
</ul>
<h3>Ruby Gem Error</h3>
<p><code>Could not find rubygems-update (&gt; 0) in any repository</code></p>
<p>If you see this error, fret not, there&#8217;s a <a href="http://armyofevilrobots.com/node/418">solution</a> for it</p>
<p>Here&#8217;s what I found out. You need to run these 2 commands in succession for gems to work.</p>
<p><code><br />
gem update --system</code></p>
<p>gem update<br />
No idea why. If you run only one of them, the system will keep showing the same errors. You should be able to use gem to install rails after running the 2 commands.</p>
<p>Below are 2 gems that always give problems. The solutions are below.</p>
<ul>
<li><a href="http://expressica.com/2007/07/04/how-to-install-rmagick-gem-on-linuxubuntu/">Installing RMagick Gem</a></li>
<li><a href="http://brantinteractive.com/index.php/?p=79">Installing MySQL Gem</a></li>
</ul>
<h3>NginX Caveat</h3>
<p>I compiled my NginX from source. For those who chose the same method, note that you will need to install a few extra libraries for NginX as well.</p>
<p><code><br />
libpcre3<br />
libpcre3-dev<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://liangzan.net/index.php/2007/11/11/setting-up-a-vps-on-ubuntu-dapper-6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
