<?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>Year of Moo</title>
	<atom:link href="http://yearofmoo.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://yearofmoo.com</link>
	<description></description>
	<lastBuildDate>Sat, 21 Jan 2012 05:18:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Rails 3.2 and Active Record Store</title>
		<link>http://yearofmoo.com/2012/01/rails-3-2-and-active-record-store/</link>
		<comments>http://yearofmoo.com/2012/01/rails-3-2-and-active-record-store/#comments</comments>
		<pubDate>Sat, 21 Jan 2012 05:04:33 +0000</pubDate>
		<dc:creator>matsko</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://yearofmoo.com/?p=890</guid>
		<description><![CDATA[What exactly is Active Record Store and how to use it with Rails 3.2 Rails 3.2 was released earlier today and it provides a variety of new features including: Active Reload, Faster Dev Mode, Tagged Logger and, of coarse, Active Record Store. When Active Record store was released I was a bit curious as to [...]]]></description>
			<content:encoded><![CDATA[<h3>What exactly is Active Record Store and how to use it with Rails 3.2</h3>
<p>Rails 3.2 was released earlier today and it provides a variety of new features including: <em>Active Reload</em>, <em>Faster Dev Mode</em>, <em>Tagged Logger</em> and, of coarse, <em>Active Record Store</em>. When Active Record store was released I was a bit curious as to how it works since <em>I only saw the same example on every blog out there</em> and the example itself just wasn&#8217;t doing it for me. Therefore this article will briefly explain how to use Active Record Store and how the contents of are stored in the database.<br />
<span id="more-890"></span></p>
<p><h3>Last Updated</h3>
<p>This article was first published on <em>January 20th 2012</em> and was last updated on <em>January 20th 2012</em>.
</p>
<p><h3>Active Record Store</h3>
<p>Active Record Store is <em>used for storing additional attributes within a database record</em> without the need to have to create additional attributes (columns in the database table) to store these values. The contents of these &#8220;additional attributes&#8221; <em>are stored within the scope of an existing attribute and as serialized as a JSON object</em>. Once fetched, <em>they&#8217;re unserialized and used as regular attributes</em>. Here&#8217;s the basic code for using Active Record Store:</p>
<div class="gist-container" data-src="https://gist.github.com/1651321.js?file=gistfile1.rb">
Loading&#8230;
</div>
<p><em>Active Record Store</em> is a great idea for when you need to store optional data into a database record. One thing to keep in mind, however, is <em>this does go against the 1st rule of normalization</em>. This database storage method also makes database searching of the record unpractical when a search is issued on the storage column.
</p>
<p><h3>More about Rails 3.2</h3>
<p>Some of the other things I noticed about 3.2 was that the development mode is alot faster. Rails startup and migration creation are much faster. Routes also load and parse faster. Other features such as <em>Explain Queries</em>, <em>Compression of Page Cached Files</em> and <em>Tagged Logging</em> are among the many new features provided by rails.
</p>
<p><h3>Some Street Cred</h3>
<p>Here are some more links to follow up on Rails 3.2</p>
<ol class="links">
<li><a class="em ignore-xview" target="_blank" href="http://weblog.rubyonrails.org/2012/1/20/rails-3-2-0-faster-dev-mode-routing-explain-queries-tagged-logger-store">http://weblog.rubyonrails.org/2012/1/20/rails-3-2-0-faster-dev-mode-routing-explain-queries-tagged-logger-store</a></li>
<li><a class="em ignore-xview" target="_blank"  href="http://guides.rubyonrails.org/3_2_release_notes.html">http://guides.rubyonrails.org/3_2_release_notes.html</a></li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://yearofmoo.com/2012/01/rails-3-2-and-active-record-store/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails Page Caching and Internationalization</title>
		<link>http://yearofmoo.com/2011/12/rails-page-caching-and-internationalization/</link>
		<comments>http://yearofmoo.com/2011/12/rails-page-caching-and-internationalization/#comments</comments>
		<pubDate>Fri, 30 Dec 2011 04:29:54 +0000</pubDate>
		<dc:creator>matsko</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://yearofmoo.com/?p=863</guid>
		<description><![CDATA[How to hack your Rails application to configure how page caches are stored Rails offers an effective caching solution for caching pages using page_caching. This stores the entire output of a request into a publicly accessible file which is then picked up by the web server first before the request is made to Rails. The [...]]]></description>
			<content:encoded><![CDATA[<h3>How to hack your Rails application to configure how page caches are stored</h3>
<p>Rails offers an effective caching solution for caching pages using page_caching. This stores the entire output of a request into a <em>publicly accessible file</em> which is then picked up by the web server first before the request is made to Rails. The Rails default method to set the path of the is something that doesn&#8217;t take <em>the locale</em> nor the <em>subdomain</em> into consideration. So if you have a subdomain that is bound to the same Rails server, then the page caching will not append any of that to the name of the cached page file. This blog article will explain how to change this behaviour.<br />
<span id="more-863"></span></p>
<p><h3>Last Updated</h3>
<p>This page was last updated on <strong class="em">December 29th 2011</strong> and was first published on <strong class="em">December 29th 2011</strong>
</p>
<p><h3>How (where) Rails stores its page cached files</h3>
<p>The page that Rails (up to and including its latest version Rails 3.1) stores its page caches are in <em>/public folder of the project root</em>. This configuration can be configured within the <em>application.rb</em> file by setting the following configuration:</p>
<pre><code>config.action_controller.page_cache_directory = <em>Rails.root.to_s + "/public"</em></code></pre>
</p>
<p>When a <em>caches_page</em> directive is set within a controller for a particular action then the page caching sets up the <em>path of the URL to be the path of the cache file</em>. This can be changed within some function overriding.</p>
<p><h3>How to override it</h3>
<p>In your <em>app/controllers/application_controller.rb</em> set the following code to change the way that the file is written. Here is the default:</p>
<div class="gist-container" data-src="https://gist.github.com/1537728.js?file=gistfile1.rb">
Loading&#8230;
</div>
</p>
<p>
Now lets imagine if you were to include the <em>locale</em> into the mix:</p>
<div class="gist-container" data-src="https://gist.github.com/1537740.js?file=gistfile1.rb">
Loading&#8230;
</div>
<p>And we can also include the <em>subdomain</em>. Lets imagine that <em>www is the default locale &#8230; English (en)</em> and anything else is <em>non-www</em>. The only issue is that the <em>page_cache_path method is a class-level method (static)</em> so the request object won&#8217;t be recognized by the class method. We&#8217;ll need to add a class variable as a before filter:</p>
<div class="gist-container" data-src="https://gist.github.com/1537768.js?file=gistfile1.rb">
Loading&#8230;
</div>
</p>
<p><h3>Configuring your HTTP Server</h3>
<p>So far I&#8217;ve only done this with NGINX. What needs to be done is to configure your <em>NGINX server to recognize that the page cached file is there before it fires up the Rails request</em>. Here is my configuration file:</p>
<div class="gist-container" data-src="https://gist.github.com/1537815.js?file=gistfile1.txt">
Loading&#8230;
</div>
</p>
<p>It will take alot of trial and error with getting NGINX to fully recognize your page cached files. Therefore, its better to install NGINX locally and change the HOSTS configuration a proper domain to point on your computer and perfect it that way. Keep in mind that NGINX does not allow <em>AND</em> and <em>OR</em> pre-conditions or <em>nested</em> if statements so you&#8217;ll need to use concatenation and variable checking to get something complex to work.</p>
]]></content:encoded>
			<wfw:commentRss>http://yearofmoo.com/2011/12/rails-page-caching-and-internationalization/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting started with MooTools Behavior.js</title>
		<link>http://yearofmoo.com/2011/12/getting-started-with-mootools-behavior-js/</link>
		<comments>http://yearofmoo.com/2011/12/getting-started-with-mootools-behavior-js/#comments</comments>
		<pubDate>Fri, 23 Dec 2011 17:53:15 +0000</pubDate>
		<dc:creator>matsko</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://yearofmoo.com/?p=849</guid>
		<description><![CDATA[A simple tutorial that explains the steps to get you started with Behavior.js MooTools offers an elegant solution to dealing with messy, unusable JavaScript code that is used to build simple, disposable UI effects and elements, that should normally be abstracted from your application code, with the use of a library called Behavior.js. The library [...]]]></description>
			<content:encoded><![CDATA[<h3>A simple tutorial that explains the steps to get you started with Behavior.js</h3>
<p>MooTools offers an elegant solution to dealing with messy, unusable JavaScript code that is used to build simple, disposable UI effects and elements, that should normally be abstracted from your application code, with the use of a library called <a href="https://github.com/anutron/behavior" class="em">Behavior.js</a>. The library contains two main tools: <em>Behaviour</em> which is used for mapping layout elements to classes (plugins) and <em>Delegator</em> which is used to relate the options identified in HTML elements to predefined, reusable filters (subroutines).</p>
<p>This article will go through the steps involved with getting this awesome plugin working with your application.</p>
<p><span id="more-849"></span></p>
<p><h3>Last Updated</h3>
<p>This page was last updated on <strong class="em">December 24rd 2011</strong> and was first published on <strong class="em">December 23rd 2011</strong>
</p>
<p><h3>In a Nutshell</h3>
<p><em>Behavior</em> scans the page looking for any elements that contain the <em>data-behavior</em> attribute and then it will fire the assigned filter for any of the class(es) defined within the attribute value. This is what it looks like:</p>
<div class="gist-container" data-src="https://gist.github.com/1514759.js?file=gistfile1.html">
loading&#8230;
</div>
<p>In this example, Behavior will scan the page (once the Behavior.attach() method is fired) and then look for any elements that match the selector (<em>data-behavior=&#8221;Tip&#8221;</em>) and then will append the options fetched from data-tip-options and built the <em>api</em> variable from that. It is up to you as to how you wish to instantiate the plugin (or perform a series of operations).
</p>
<p>
<em>Delegator</em> is pretty similar. It too looks for an attribute selector (<em>data-trigger=&#8221;..&#8221;</em>) and applies the associated options to the function call. The function call is registered with the Delegator:</p>
<div class="gist-container" data-src="https://gist.github.com/1514809.js?file=gistfile1.html">
Loading&#8230;
</div>
<p>The code is here pretty straightforward and it follows the flow of the <em>Behavior</em> example.
</p>
<p><h3>How to set this up</h3>
<p>First you&#8217;ll need to <a href="https://github.com/anutron/behavior" class="ignore-xview" target="_blank">clone the Git repository</a> or <a href="http://dev.clientcide.com/" class="ignore-xview" target="_blank">download the source</a>. Next extract all the files into a folder accessible within your web application. Include the following files (in the following order):</p>
<ol class="files">
<li class="em">./Source/Element.Data.js</li>
<li class="em">./Source/BehaviorAPI.js</li>
<li class="em">./Source/Behavior.js</li>
<li class="em">./Source/Behavior.Startup.js</li>
<li class="em">./Source/Delegator.js</li>
</ol>
<p>
Next, <em>define the filters that you want for your behavior operations</em> with the following code:</p>
<div class="gist-container" data-src="https://gist.github.com/1514830.js?file=gistfile1.js">
Loading&#8230;
</div>
</p>
<p>Then <em>register the Delegator events</em> for the items that you wish to apply delegation operations on:</p>
<div class="gist-container" data-src="https://gist.github.com/1514845.js?file=gistfile1.js">
Loading&#8230;
</div>
</p>
<p>
Once all the filters and delegation callbacks have been created, now any HTML element that contains the <em>data-behavior</em> or <em>data-trigger</em> attributes that match a <em>behavior filter</em> or <em>delegation registration function</em> will be ready. They won&#8217;t fire automatically, so you&#8217;ll need to define a run method:</p>
<div class="gist-container" data-src="https://gist.github.com/1514852.js?file=gistfile1.js">
Loading&#8230;
</div>
</p>
<p><h3>Anything else?</h3>
<p>Not so much. This toolkit is really sweet. Once you start using it you&#8217;ll see how the majority of your <em>page or element class-specific</em> code will trim itself down. The UI interactivity can be fully abstracted from the application core. I will blog more and more about this later on once I start to use the library within my own projects.</p>
]]></content:encoded>
			<wfw:commentRss>http://yearofmoo.com/2011/12/getting-started-with-mootools-behavior-js/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Use Gistex to load up Github Gist Snippets</title>
		<link>http://yearofmoo.com/2011/12/use-gistex-to-load-up-github-gist-snippets/</link>
		<comments>http://yearofmoo.com/2011/12/use-gistex-to-load-up-github-gist-snippets/#comments</comments>
		<pubDate>Sun, 18 Dec 2011 20:49:57 +0000</pubDate>
		<dc:creator>matsko</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://yearofmoo.com/?p=833</guid>
		<description><![CDATA[Gistex is an easy to use tool to spawn up Github Gist snippets using AJAX The other day I decided on embedding Github Gist code snippets into yearofmoo instead of regular home-grown code blockquote areas. Gist is awesome, but the standard Javascript embeds do not work with a pure ajax website (like this one). So [...]]]></description>
			<content:encoded><![CDATA[<h3>Gistex is an easy to use tool to spawn up Github Gist snippets using AJAX</h3>
<p>The other day I decided on embedding <a href="https://gist.github.com" class="ignore-xview" target="_blank">Github Gist code snippets</a> into yearofmoo instead of regular home-grown code blockquote areas. Gist is awesome, but the standard Javascript embeds do not work with a pure ajax website (like this one). So the only way to get them to work was todo some voodoo JavaScript and then &#8230; voila &#8230; <a href="/Gistex" class="em">Gistex</a> was born.<br />
<span id="more-833"></span></p>
<p><h3>Last Updated</h3>
<p>This page was last updated on <strong class="em">December 18th 2011</strong> and was first published on <strong class="em">December 18th 2011</strong>
</p>
<p><h3>Gistex?</h3>
<p>Yes Gistex. It loads a Github Gist snippet, creates the HTML and pastes it into the DOM element that you wish to apply it to. This is great for AJAX applications and lazy loading. It is also supported in all browers and is <em>NOT</em> dependent on any JavaScript framework. The Gistex class is very simple, and provides support for numerous callbacks and options.
</p>
<p><h3>Where can I get it?</h3>
<p>Gistex is hosted here on yearofmoo.</p>
<p><a href="/Gistex">Click here to view the documentation and demos</a>.</p>
<p>
]]></content:encoded>
			<wfw:commentRss>http://yearofmoo.com/2011/12/use-gistex-to-load-up-github-gist-snippets/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Current state of onhashchange &amp; HTML5 history</title>
		<link>http://yearofmoo.com/2011/10/current-state-of-onhashchange-and-html5-history/</link>
		<comments>http://yearofmoo.com/2011/10/current-state-of-onhashchange-and-html5-history/#comments</comments>
		<pubDate>Mon, 24 Oct 2011 14:46:13 +0000</pubDate>
		<dc:creator>matsko</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://yearofmoo.com/?p=742</guid>
		<description><![CDATA[How widely supported are these URL changing techniques in modern browsers? The onhashchange event is a popular event and has been widely looked into by yearofmoo. The MooTools-onhashchange plugin offered by yearofmoo takes care of this task effectively. The onhashchange event can be used in combination with a hash-prefixed URL token, known as a hashbang, [...]]]></description>
			<content:encoded><![CDATA[<h3>How widely supported are these URL changing techniques in modern browsers?</h3>
<p>The onhashchange event is a popular event and has been widely looked into by yearofmoo. The <a href="/onhashchange/" class="em">MooTools-onhashchange</a> plugin offered by yearofmoo takes care of this task effectively. The onhashchange event can be used in combination with a hash-prefixed URL token, known as a hashbang, to effectively inform search engines of indexed ajax content.</p>
<p>The HTML5 history API is only offered by modern browsers and offers more features than the onhashchange event. The only issue is that not all browsers fully support it yet since it is a new HTML5 JavaScript API. In addition, unlike with the onhashchange event, <em>there is no way to emulate or hack non-supporting browsers to offer popstate support</em>.</p>
<p>But how widely supported are the two features?</p>
<p><span id="more-742"></span></p>
<p><h3>Last Updated</h3>
<p>This page was last updated on <em>Oct 24th 2011</em> and was first published on <em>Oct 24th 2011</em>
</p>
<p><h3>The onhashchange event vs the History API</h3>
<p>HTML5 history is by far more extensive, user-friendly and profound than the onhashchange event (in regards to dynamic URL changing). The page fragment (or hash tag) of a URL was designed for in page anchoring and not for page to page addressing (which is what the history API does). So how do each of these two compare? Here are some comparsions:</p>
<table class="feature-matrix options-matrix">
<thead>
<tr>
<th>Feature</th>
<th>onhashchange</th>
<th>History API</th>
</tr>
</thead>
<tbody>
<tr>
<td>URL Changing</td>
<td class="pass">Supported</td>
<td class="pass">Supported</td>
</tr>
<tr>
<td>URL Path Changing</td>
<td class="fail">Not Supported</td>
<td class="pass">Supported</td>
</tr>
<tr>
<td>URL Changing from browser</td>
<td class="pass">Supported</td>
<td class="fail">Not Supported</td>
</tr>
<tr>
<td>Back and Forward Events</td>
<td>Emulated, but indistinguishable.</td>
<td class="pass">Supported</td>
</tr>
<tr>
<td>Stack Control</td>
<td class="fail">Not Supported</td>
<td class="pass">Supported</td>
</tr>
<tr>
<td>Variables passed</td>
<td>Can be emulated</td>
<td class="pass">Supported</td>
</tr>
<tr>
<td>Temporary URL Changes</td>
<td class="fail">Not Supported</td>
<td class="pass">Supported</td>
</tr>
</tbody>
</table>
<p><h3>Browser Support</h3>
<p>The onhashchange event is <em>widely supported by all browsers</em> (other than IE6 and IE7). As of now, no extra code is required to handle the event for modern browsers. IE6 and IE7 are so far phased out now that they should not be supported for new web apps &#8230; so why offer the onhashchange event for them? The history API, however, is not yet fully supported by all browsers until IE10 fully hits the scene.</p>
<p>Here is a complete matrix of which browsers support which:</p>
<table class="feature-matrix options-matrix">
<thead>
<tr>
<th>Browser</th>
<th>onhashchange</th>
<th>History API</th>
</tr>
</thead>
<tbody>
<tr>
<td>Firefox 7</td>
<td class="pass">Supported</td>
<td class="pass">Supported</td>
</tr>
<tr>
<td>Safari 5</td>
<td class="pass">Supported</td>
<td class="pass">Supported</td>
</tr>
<tr>
<td>Chrome 14</td>
<td class="pass">Supported</td>
<td class="pass">Supported</td>
</tr>
<tr>
<td>Opera 11.0</td>
<td class="pass">Supported</td>
<td class="fail">Not Supported</td>
</tr>
<tr>
<td>Opera 11.50</td>
<td class="pass">Supported</td>
<td class="pass">Supported</td>
</tr>
<tr>
<td>IE10</td>
<td class="pass">Supported</td>
<td class="pass">Supported</td>
</tr>
<tr>
<td>IE8 &amp; IE9</td>
<td class="pass">Supported</td>
<td class="fail">Not Supported</td>
</tr>
<tr>
<td>IE6 &amp; IE7</td>
<td class="fail">Not Supported</td>
<td class="fail">Not Supported</td>
</tr>
<tr>
<td>Android Browser v2.3</td>
<td class="pass">Supported</td>
<td class="pass">Supported</td>
</tr>
<tr>
<td>Android Browser v4</td>
<td class="pass">Supported</td>
<td class="fail">Not Supported</td>
</tr>
<tr>
<td>iOS 5 Safari</td>
<td class="pass">Supported</td>
<td class="pass">Supported</td>
</tr>
</tbody>
</table>
<p><h3>What should I use?</h3>
<p>You should feel comfortable to use both, but be sure to favour use of the history API over to the onhashchange event due to the extra features that are provided.
</p>
<p><h3>Any Plugins?</h3>
<p>A useful plugin on the <a href="/popstate-hashing/" class="em">popstate-hashing.js plugin offered by yearofmoo</a>. It basically sets up one event to handle the URL changing via the history API first and then via the onhashchange event second. This way, the browser and the developer need not to worry about any extra cases of what todo when changing the URL of a web page dynamically. <a href="/popstate-hashing/" class="em">Click here to view the popstatehashing plugin</a>
</p>
<p><h3>History API + onhashchange</h3>
<p>The nice thing is that all of the browsers that support the history API support the onhashchange event. So in a few years time, when IE10+ is very common and other IE browsers have been phased out, developers can use both the History API and the onhashchange event together for some truly awesome web page effects. Imagine if you have your top level page changes triggered by a popstate event (using the History API) and then lower level changes, such as pagination and optional, expanding areas triggered by a hash tag change (using the onhashchange event). This way, all areas of a website can be accessed directly from a URL, but your website can be better tailored to make use of more specific events with less code.
</p>
<p><h3>Some Links</h3>
<p>Thanks guys! :]</p>
<ul class="links">
<li><a target="_blank" class="em ignore-xview"  href="http://caniuse.com/#search=history">http://caniuse.com/#search=history</a></li>
<li><a target="_blank" class="em ignore-xview"  href="http://ie.microsoft.com/testdrive/Info/ReleaseNotes/Default.html">http://ie.microsoft.com/testdrive/Info/ReleaseNotes/Default.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://yearofmoo.com/2011/10/current-state-of-onhashchange-and-html5-history/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails page caching in a separate directory</title>
		<link>http://yearofmoo.com/2011/10/rails-page-caching-in-a-separate-directory/</link>
		<comments>http://yearofmoo.com/2011/10/rails-page-caching-in-a-separate-directory/#comments</comments>
		<pubDate>Sat, 22 Oct 2011 06:54:55 +0000</pubDate>
		<dc:creator>matsko</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://yearofmoo.com/?p=700</guid>
		<description><![CDATA[How to setup Apache or NGINX to be aware of where rails stores page caches Rails offers a really powerful caching technique where the contents of an entire page load will be stored into a HTML file. This is incredibly useful for performance enhancements for big and small Rails apps. One issue, however, is that [...]]]></description>
			<content:encoded><![CDATA[<h3>How to setup Apache or NGINX to be aware of where rails stores page caches</h3>
<p>Rails offers a really powerful caching technique where the contents of an entire page load will be stored into a HTML file. This is incredibly useful for performance enhancements for big and small Rails apps.</p>
<p>One issue, however, is that when you setup page caching into a different directory which isn&#8217;t the public folder root, then the generated HTML files won&#8217;t be picked up by Apache, Nginx, or whichever HTTP daemon you&#8217;re webserver is running since the direct path is different. Therefore, to get this to work properly, some tweaking needs to be done on the HTTP daemon so that it recognizes the caching directory path.</p>
<p>The nice thing is that when the caching files are in a subfolder within the public root folder then management of the cache as a whole becomes much easier. This is explained in detail <a href="http://www.fngtps.com/2006/lazy-sweeping-the-rails-page-cache/" target="_blank">here</a>.</p>
<p>This article will quickly explain how to setup Rails and the HTTP daemon (Apache or Nginx) to properly behave with the cache change.</p>
<p><span id="more-700"></span></p>
<p><h3>Last Updated</h3>
<p>This blog entry was last updated on <em>October 22nd 2011</em> and was first published on <em>December 28th 2011</em>
</p>
<p><h3>What exactly is the issue?</h3>
<p>Apache or Nginx won&#8217;t notice if a page is cached because the URL to access that cached file is different from the actual URL that accesses the non-cached page.</p>
<pre><code><em># Non-Cached</em>
/about/some/sub/page

<em># When cached it will get stored in</em>
/cache/about/some/sub/page.html</code></pre>
<p>The path is different and caching won&#8217;t work right away so we&#8217;ll need to configure the HTTP daemon.
</p>
<p>So how do we fix this problem?</p>
<p><h3>Configure Rails First</h3>
<p>Be sure to add this to your <em>production.rb</em> (or <em>application.rb</em>) file.</p>
<pre><code>config.action_controller.page_cache_directory = <em>Rails.root.to_s + "/public/cache/"</em></code></pre>
<p>Now all of your page cache files will be stored under <em>/public/cache/PATH</em>
</p>
<p><h3>URL Rewriting does the trick</h3>
<p>Using mod_rewrite for apache and the built-in URL rewriting offered by NGINX, the cached directory can be recognized directly from a &#8220;non-cached&#8221; URL. The thing to keep in mind is that <em>if a cached file does not exist then the URL rewrite will still apply itself, so its important to consider the rewrite conditions so that the http server is aware of this</em>.
</p>
<p>Here are the settings <em>(keep in mind that this is for a cached directory of /public/cache)</em>:</p>
<h4>Apache</h4>
<p>Apache always has the strangest of syntax :p</p>
<pre><code>RewriteEngine On

<em># default root uri</em>
RewriteCond %{THE_REQUEST} ^(GET|HEAD)
RewriteCond %{DOCUMENT_ROOT}/cache/index.html -f
RewriteRule ^$ cache/index.html [QSA]

<em># all other pages</em>
RewriteCond %{THE_REQUEST} ^(GET|HEAD)
RewriteCond %{REQUEST_URI} ^([^.]+)/?$
RewriteCond %{DOCUMENT_ROOT}/cache/%1.html -f
RewriteRule ^([^.]+)$ cache/$1.html [QSA]</code></pre>
</p>
<p><h4>NGINX</h4>
<p>NGINX is a bit more straightforward&#8230;</p>
<pre><code><em># Index HTML Files</em>
if (-f $document_root/cache/$uri/index.html) {
    rewrite (.*) /cache/$1/index.html break;
}

<em># HTML Files</em>
if (-f $document_root/cache/$uri.html) {
    rewrite (.*) /cache/$1.html break;
}

<em># Catch all</em>
if (-f $document_root/cache/$uri) {
    rewrite (.*) /cache/$1 break;
}</code></pre>
</p>
<p><h3>Some streed cred&#8217;&#8230;</h3>
<p>Here&#8217;s some lovin&#8217; for some of the websites that helped me put together this article.<br />
<a class="em" href="http://www.fngtps.com/2006/lazy-sweeping-the-rails-page-cache/" target="_blank">http://www.fngtps.com/2006/lazy-sweeping-the-rails-page-cache/</a><br />
<a class="em" href="http://rel.me/2008/01/07/nginx-conf-with-alternate-cache-dir/" target="_blank">http://rel.me/2008/01/07/nginx-conf-with-alternate-cache-dir/</a><br />
<a class="em" target="_blank" href="https://wincent.com/blog/rails-page-caching-vs-nginx">https://wincent.com/blog/rails-page-caching-vs-nginx</a><br />
<a href="http://guides.rubyonrails.org/caching_with_rails.html" target="_blank" class="em">http://guides.rubyonrails.org/caching_with_rails.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://yearofmoo.com/2011/10/rails-page-caching-in-a-separate-directory/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Serve Precompiled Rails Assets from a Static Domain</title>
		<link>http://yearofmoo.com/2011/09/serve-precompiled-rails-assets-from-a-static-domain/</link>
		<comments>http://yearofmoo.com/2011/09/serve-precompiled-rails-assets-from-a-static-domain/#comments</comments>
		<pubDate>Sun, 04 Sep 2011 00:07:51 +0000</pubDate>
		<dc:creator>matsko</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://yearofmoo.com/?p=655</guid>
		<description><![CDATA[Precompile and deliver your Rails assets from a different domain for static content With the release of the Rails 3.1 Asset Pipeline, which uses Sprockets to deliver Sass and Coffeescript rendered assets, the development of properly organized assets in Rails just got much easier. Now there exist many tutorials out on the web that go [...]]]></description>
			<content:encoded><![CDATA[<h3>Precompile and deliver your Rails assets from a different domain for static content</h3>
<p>With the release of the Rails 3.1 Asset Pipeline, which uses Sprockets to deliver Sass and Coffeescript rendered assets, the development of properly organized assets in Rails just got much easier. Now there exist many tutorials out on the web that go into detail about how to play around with this new feature, so I&#8217;m not going to go into detail about how todo that. What I will cover is <em>how to setup a static domain, that does not interfere with your Rails daemon, that serves these assets quickly and smoothly</em>.</p>
<p><span id="more-655"></span></p>
<p><h3>Last Updated</h3>
<p>This blog entry was last updated on <em>September 3rd 2011</em> and was first published on <em>September 3rd 2011</em>
</p>
<p><h3>Why should this be considered</h3>
<p>The major reason why this should be considered is because Rails itself should not have to handle serving asset files regardless if they&#8217;re precompiled or not. Back with Rails 3.1 RC4 and RC5 <em>I noticed a huge performance lag with my stylesheet and javascript files whenever they were being served in production mode</em>. Even when they were precompiled, Rails was serving the assets for me and after trying to disable the <em>serve static assets boolean</em> in my production file, this was still slowing down my server. <em>By offering a different domain (or subdomain) to serve the assets, all of the assets themselves can be managed elsewhere</em>. Note there are also various other benefits to serving asset files from a different domain entirely (this is explained in the next section).
</p>
<p><h3>Domains, Subdomains or just a different port?</h3>
<p>Its better to serve assets from a different domain from your www domain. I don&#8217;t mean a subdomain, but an entirely different domain. This is because when an asset is fetched from the same domain as your website, the user agent that fetches that asset <em>will upload any cookie data that is stored for that domain and that matches that path</em>. Since Rails stores session data in cookies by default, you can imagine how much unnecessary data is being transfered back and forth just to fetch some asset files. With subdomains, this issue is almost gone, but there could be some cookies in your website that may exist for all domains (www, subdomains and no subdomains). If you use a different port to serve assets then this issue is no different from serving it from the same port as the Rails app.
</p>
<p>Modern browsers will download only a certain amount of assets in parallel from a single host. This means that if  use another domain with various other subdomains connected to that domain then you can download almost all your assets in parallel once the page is loaded.</p>
<p><h3>How do we set this up?</h3>
<p>Easy, just configure your NGINX or Apache server to serve assets from a <em>specified directory which never changes</em> and then configure the headers for caching and expiry to their max. Then setup capistrano to rotate the assets each time a deployment is issued.
</p>
<p><h3>Step 1 &#8211; Get the domain(s)</h3>
<p>Whether or not you already have a static domain purchased yet, make sure that you either have a new domain or a subdomain of your primary domain ready so that both domains resolve to the <em>same ip address</em>.
</p>
<p><h3>Step 2 &#8211; Configure your HTTP Daemon</h3>
<p>In this article I will only cover how to setup your NGINX + Passenger HTTP daemon:</p>
<h4>NGINX</h4>
<p>If you installed NGINX via passenger for Rails (which I hope you did) then just hop into your nginx.conf file and add the following:</p>
<pre><code><em># Static Server</em>
server {
    listen 80;
    server_name staticdomain.com;
    root /var/www/staticdomain.com;
    location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
        gzip_static on;
        expires max;
        add_header Cache-Control public;
        break;
    }
}

<em># Rails Server</em>
server {
    listen 80;
    server_name dynamicdomain.com;
    root /var/www/dynamicdomain.com/current/public;
    passenger_enabled on;
}</code></pre>
<p>This way, any static files will be cached properly and no cookie data will be sent to the static files each time an asset is requested.
</p>
<p><h3>Step 3 &#8211; Configure Rails</h3>
<p>Be sure to set the asset_host in Rails to that of the domain that you will be using in production mode. Be sure to set the following configurations in your <em>production.rb</em> file:</p>
<pre><code>#set the asset host
config.action_controller.asset_host = <em>"http://staticdomain.com"</em></code></pre>
</p>
<p><h3>Step 4 &#8211; Configure Capistrano</h3>
<p>Lets now configure capistrano to precompile your assets just after it has updated and symlinked the code. Once the assets are precompiled, then replace the existing assets with your newly updated assets.</p>
<pre><code>task :start, :roles => :app do
    asset_path = <em>"/var/www/staticdomain.com/"</em> #this needs to point to where your assets are saved
    run <em>"cd #{current_release}; rake assets:precompile"</em> #compile the assets
    run <em>"rm -fr #{asset_path}/*"</em> #move the asset files to the asset_path directory
    run <em>"mv #{current_release}/public/assets/* #{assets_link_path}"</em> #move the asset files to the asset_path directory
    run <em>"cd #{current_release}; rake assets:clean"</em> #remove the existing assets
end</code></pre>
<p>Now whenever a cap deploy command is issued, the assets are uploaded to the asset server and updated.
</p>
<p><h3>Room for improvement&#8230;</h3>
<p>If anyone here knows of a better way to hotswap the newly uploaded asset fiels with the existing asset files without having to delete the existing asset files then that would help out a lot. The reason why I use <em>rm -fr + mv</em> is because the asset files <em>within any subdirectories</em> can only be copied over with a move operation (a copy operation will overwrite the entire directory).
</p>
<p>I attempted to solve this by using a <em>symlink</em> operation where the files are linked over to the new assets directory whenever its uploaded. This does not work because NGINX will register the direct path of a symlink whenever it is restarted (this is how passenger works by restarting the mongrel or webrick instance when a capistrano restart call is issued). If anyone knows how to get around this then please let me know in the comments below.</p>
]]></content:encoded>
			<wfw:commentRss>http://yearofmoo.com/2011/09/serve-precompiled-rails-assets-from-a-static-domain/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Last minute fix for Rails 3.1 Stable</title>
		<link>http://yearofmoo.com/2011/09/last-minute-fix-for-rails-3-1-stable/</link>
		<comments>http://yearofmoo.com/2011/09/last-minute-fix-for-rails-3-1-stable/#comments</comments>
		<pubDate>Thu, 01 Sep 2011 16:20:33 +0000</pubDate>
		<dc:creator>matsko</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://yearofmoo.com/?p=646</guid>
		<description><![CDATA[Beware of the config.assets.digest fingerprinting defaults! As you all might be well aware, Rails 3.1 was released yesterday (August 31st 2011) &#8230; and for those of you whom have been developing using the Rails 3.1 beta and RC versions, you may have noticed a small gotcha which occurred in the Rails production configuration that may [...]]]></description>
			<content:encoded><![CDATA[<h3>Beware of the config.assets.digest fingerprinting defaults!</h3>
<p>As you all might be well aware, <em>Rails 3.1 was released yesterday (August 31st 2011)</em> &#8230; and for those of you whom have been developing using the Rails 3.1 beta and RC versions, you may have noticed a small gotcha which occurred in the Rails production configuration that may affect your asset filenames. If you notice that your asset file names <em>are missing a fingerprint hash, md5 token, digest, hash timestamp (or whatever you want to call it) within their filename</em> &#8230; <strong>like application-xxxxxx.js</strong> then the reasoning behind this is because <em>Rails 3.1 disables the config.assets.digest option by default</em> for all environments.</p>
<p><span id="more-646"></span></p>
<p><h3>Last Updated</h3>
<p>This page was last updated on <em>September 1st 2011</em> and was first published on <em>September 1st 2011</em>.</p>
<p><h3>What to do?</h3>
<p>The solution is simple, <em>in your production.rb environment file, just set the following:</em></p>
<pre><code>config.assets.digest = <em>true</em></code></pre>
</p>
<p>Turns out that <a class="em" href="http://guides.rubyonrails.org/asset_pipeline.html#what-is-fingerprinting-and-why-should-i-care" target="_blank" class="ignore-xview">this is documented in the Asset Pipeline Guide</a>, however, since it was enabled by default in previous versions of the Rails 3.1 RC release, I figured that the problem was with my setup and not with Rails.</p>
<p><h3>Why this is important?</h3>
<p>Hopefully this helps. Instead of having to spend a couple of hours testing previous versions of Rails 3.1 stable, RC5 and so on, now you know better <img src='http://yearofmoo.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  My story was that I was using a separate domain to host the precompiled assets folder so that Rails wouldn&#8217;t even get close to serving those asset files on my production server. Then I noticed that, despite the assets being successfully generated and fingerprinted during the rake assets:precompile operation, the javascript, stylesheet and asset helpers were not expanding their fingerprinted filename values. Thankfully, after hopping into the #rubyonrails IRC channel, this issue was quickly extinguished. Many thanks to the rubyonrails dev team.</p>
]]></content:encoded>
			<wfw:commentRss>http://yearofmoo.com/2011/09/last-minute-fix-for-rails-3-1-stable/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MooTools Asset.stylesheet onload event patch</title>
		<link>http://yearofmoo.com/2011/07/mootools-asset-stylesheet-onload-event-patch/</link>
		<comments>http://yearofmoo.com/2011/07/mootools-asset-stylesheet-onload-event-patch/#comments</comments>
		<pubDate>Sun, 31 Jul 2011 02:37:51 +0000</pubDate>
		<dc:creator>matsko</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://yearofmoo.com/?p=611</guid>
		<description><![CDATA[A fully working fix for the onload event on the MooTools Asset.stylesheet library The Asset.stylesheet library itself is a very useful library for preloading stylesheets via JavaScript. The only problem is that it does not support the onload event for all browsers. The reasoning behind this is because the onload event itself is not supported [...]]]></description>
			<content:encoded><![CDATA[<h3>A fully working fix for the onload event on the MooTools Asset.stylesheet library</h3>
<p>
The Asset.stylesheet library itself is a very useful library for preloading stylesheets via JavaScript. The only problem is that it <em>does not support the onload event for all browsers</em>. The reasoning behind this is because the onload event itself is not supported on all browsers; an older <a href="/2011/03/cross-browser-stylesheet-preloading/" class="em">article posted on yearofmoo.com went into full detail about this problem</a>. This blog article, however, provides the code for MooTools 1.2 and 1.3 that <em>patches</em> the Asset.stylesheet library to support the onload event 100% in all browsers.
</p>
<p><span id="more-611"></span></p>
<p><h3>Last Updated</h3>
<p>This page was last updated on <strong class="em">July 30th 2011</strong> and was first published on <strong class="em">July 30th 2011</strong>
</p>
<p>
Using the yearofmoo Asset.stylesheet patch any assets loaded with Asset.stylesheet will fire the onload event normally on success and will fire the <em>onerror</em> event on failure. The syntax is the same, here is an example:
</p>
<p><pre><code>new Asset.stylesheet('/path/to/some/css/file.css', {
    onerror : function() {
        alert('error file not loaded');
    },
    onload : function() {
        alert('success');
    }
});</code></pre>
<div class="jump">
<a href="#top" class="top">To Top</a><a href="#demos" class="demos">Goto Demos</a><a href="#download" class="download">Download</a>
</div>
</p>
<p><h3>What&#8217;s so special about this patch?</h3>
<p>The good thing about this patch is that it uses the existing support of the <em>onload event which is already supported by IE and Opera</em>.It also <em>does not use any other external requests to emulate the event</em>. Finally, Asset.stylesheet comes with an <em>onerror</em> event which works with a timeout event (so once the request is checked enough times without success, it fires the onerror event). Finally, the patch itself works for stylesheet files that are located locally (on the same domain) and externally.</p>
<div class="jump">
<a href="#top" class="top">To Top</a><a href="#demos" class="demos">Goto Demos</a><a href="#download" class="download">Download</a>
</div>
</p>
<p><h3>Demo</h3>
<p><a href="/demos/Asset.stylesheet/" class="ignore-xview" target="_blank">Here is a working demo of the Asset.stylesheet patch for MooTools:</a></p>
<div class="jump">
<a href="#top" class="top">To Top</a><a href="#demos" class="demos">Goto Demos</a><a href="#download" class="download">Download</a>
</div>
</p>
<p><h3 id="download">Download</h3>
<p>The <a target="_blank"  href="https://github.com/matsko/MooTools-Asset.stylesheet-patch">Asset.stylesheet</a> can be found on <a target="_blank"  href="https://github.com/matsko/MooTools-Asset.stylesheet-patch">github</a>.</p>
<div class="jump">
<a href="#top" class="top">To Top</a><a href="#demos" class="demos">Goto Demos</a><a href="#download" class="download">Goto Download</a>
</div>
</p>
<p><h3 id="contact">Contact</h3>
<p>Any issues or questions can be posted on the <a href="https://github.com/matsko/MooTools-Asset.stylesheet-patch/issues">issue tracking page</a> found on <a target="_blank"  href="https://github.com/matsko/MooTools-Asset.stylesheet-patch">github</a>.</p>
<div class="jump">
<a href="#top" class="top">To Top</a><a href="#demos" class="demos">Goto Demos</a><a href="#download" class="download">Download</a>
</div></p>
]]></content:encoded>
			<wfw:commentRss>http://yearofmoo.com/2011/07/mootools-asset-stylesheet-onload-event-patch/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Element rotation using Fx.Rotate</title>
		<link>http://yearofmoo.com/2011/07/element-rotation-using-fx-rotate/</link>
		<comments>http://yearofmoo.com/2011/07/element-rotation-using-fx-rotate/#comments</comments>
		<pubDate>Fri, 29 Jul 2011 03:57:46 +0000</pubDate>
		<dc:creator>matsko</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://yearofmoo.com/?p=597</guid>
		<description><![CDATA[Perform quick and dirty rotations using the Fx.Rotate plugin Yearofmoo.com has now released the Fx.Rotate plugin which allows for cross-browser rotation using CSS3 transforms and IE filter properties. The class itself is a subclass of the super powerful MooTools Fx class which provides tons of functionality for controlled transitions and callbacks. Last Updated This page [...]]]></description>
			<content:encoded><![CDATA[<h3>Perform quick and dirty rotations using the Fx.Rotate plugin</h3>
<p>Yearofmoo.com has now released the Fx.Rotate plugin which allows for cross-browser rotation using CSS3 transforms and IE filter properties. The class itself is a subclass of the super powerful MooTools Fx class which provides tons of functionality for controlled transitions and callbacks.</p>
<p><span id="more-597"></span></p>
<p><h3>Last Updated</h3>
<p>This page was last updated on <strong class="em">July 28th 2011</strong> and was first published on <strong class="em">July 28th 2011</strong>
</p>
<p><h3>Why?</h3>
<p>Most of the plugins for JavaScript rotation are really basic. This plugin allows for a whole bunch of stuff. Plus it also takes care of older browsers and then some :]
</p>
<p><h3>Quick Example</h3>
<p>Here is a quick example to show how simple, yet powerful, the features that Fx.Rotate provides:
</p>
<p><pre><code>var elm = $('element');

<em>//rotate to 180 degrees</em>
elm.rotate(180);

<em>//rotate from 180 to 50</em>
elm.rotate(180,50);

<em>//rotate from where it currently is back to -20</em>
elm.rotate(-20);

<em>//rotate clockwise BY 20 degrees</em>
elm.rotate('+20');

<em>//rotate counter clockwise by 100 degrees</em>
elm.rotate('-100');

<em>//bring it back to 0</em>
elm.normalize();

<em>//do something crazy</em>
var rotator = new Fx.Rotate(elm,{
    transition : 'circ:in',
    link : 'cancel'
});
rotator.start(200).chain(function() {
    rotator.start('-100').chain(function() {
        rotator.normalize();
    });
});</code></pre>
</p>
<p><h3>Full Documentation, Demos &amp; Download</h3>
<p><a href="/Fx.Rotate">Click here to view the documentation for the new Fx.Rotate plugin</a></p>
]]></content:encoded>
			<wfw:commentRss>http://yearofmoo.com/2011/07/element-rotation-using-fx-rotate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

