<?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>Freezzo.com</title>
	<atom:link href="http://www.freezzo.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.freezzo.com</link>
	<description>A blog about programming, technology, rails and anything else.</description>
	<lastBuildDate>Fri, 10 Jun 2011 13:08:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>How to use Delayed Job to handle your Carrierwave processing</title>
		<link>http://www.freezzo.com/2011/01/06/how-to-use-delayed-job-to-handle-your-carrierwave-processing/</link>
		<comments>http://www.freezzo.com/2011/01/06/how-to-use-delayed-job-to-handle-your-carrierwave-processing/#comments</comments>
		<pubDate>Thu, 06 Jan 2011 14:26:49 +0000</pubDate>
		<dc:creator>Randy</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[carrierwave]]></category>
		<category><![CDATA[delayed_job]]></category>
		<category><![CDATA[rails 3]]></category>

		<guid isPermaLink="false">http://www.freezzo.com/?p=153</guid>
		<description><![CDATA[This tutorial builds on my previous post about how to add FFMPEG processing to Carrierwave. Here I will show you my attempt at being able to utilize Delayed::Job to do the heavy lifting of processing when uploading files using Carrierwave. Remember, this could probably use some improvement, but it is a great starting point. So [...]]]></description>
			<content:encoded><![CDATA[<p><script type="text/javascript"><!--
google_ad_client = "ca-pub-5960276596377708";
/* Freezzo below title */
google_ad_slot = "2400676724";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
This tutorial builds on my previous post about how to add <a href="http://www.freezzo.com/2010/12/23/create-ffmpeg-processor-for-carrierwave-in-rails-3/">FFMPEG processing to Carrierwave</a>. Here I will show you my attempt at being able to utilize Delayed::Job to do the heavy lifting of processing when uploading files using Carrierwave. Remember, this could probably use some improvement, but it is a great starting point. So lets begin.</p>
<p>The first thing you will need to do is add Delayed::Job to your application:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Gemfile</span>
gem <span style="color:#996600;">'delayed_job'</span></pre></div></div>

<p>Next you need to create the migration and migrate the database:</p>

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

<p>Now we get to the good part. Lets create a module to include into Carrierwave that will support holding off on doing the processing until Delayed::Job gets around to it:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># lib/carrier_wave/delayed_job.rb</span>
<span style="color:#9966CC; font-weight:bold;">module</span> CarrierWave
  <span style="color:#9966CC; font-weight:bold;">module</span> Delayed
    <span style="color:#9966CC; font-weight:bold;">module</span> Job
      <span style="color:#9966CC; font-weight:bold;">module</span> ActiveRecordInterface
        <span style="color:#9966CC; font-weight:bold;">def</span> delay_carrierwave
          <span style="color:#0066ff; font-weight:bold;">@delay_carrierwave</span> <span style="color:#006600; font-weight:bold;">||</span>= <span style="color:#0000FF; font-weight:bold;">true</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
        <span style="color:#9966CC; font-weight:bold;">def</span> delay_carrierwave=<span style="color:#006600; font-weight:bold;">&#40;</span>delay<span style="color:#006600; font-weight:bold;">&#41;</span>
          <span style="color:#0066ff; font-weight:bold;">@delay_carrierwave</span> = delay
        <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
        <span style="color:#9966CC; font-weight:bold;">def</span> perform
          asset_name = <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9966CC; font-weight:bold;">class</span>.<span style="color:#9900CC;">uploader_options</span>.<span style="color:#9900CC;">keys</span>.<span style="color:#9900CC;">first</span>
          <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>asset_name<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">versions</span>.<span style="color:#9900CC;">each_pair</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>key, value<span style="color:#006600; font-weight:bold;">|</span>
            value.<span style="color:#9900CC;">process_without_delay</span>!
          <span style="color:#9966CC; font-weight:bold;">end</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
        private
&nbsp;
        <span style="color:#9966CC; font-weight:bold;">def</span> enqueue
          ::<span style="color:#6666ff; font-weight:bold;">Delayed::Job</span>.<span style="color:#9900CC;">enqueue</span> <span style="color:#0000FF; font-weight:bold;">self</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
      <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">included</span><span style="color:#006600; font-weight:bold;">&#40;</span>base<span style="color:#006600; font-weight:bold;">&#41;</span>
        base.<span style="color:#9900CC;">extend</span> ClassMethods
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
      <span style="color:#9966CC; font-weight:bold;">module</span> ClassMethods
        <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">extended</span><span style="color:#006600; font-weight:bold;">&#40;</span>base<span style="color:#006600; font-weight:bold;">&#41;</span>
          base.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:include</span>, InstanceMethods<span style="color:#006600; font-weight:bold;">&#41;</span>
          base.<span style="color:#9900CC;">alias_method_chain</span> <span style="color:#ff3333; font-weight:bold;">:process</span>!, <span style="color:#ff3333; font-weight:bold;">:delay</span>
          ::<span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:include</span>, <span style="color:#6666ff; font-weight:bold;">CarrierWave::Delayed::Job::ActiveRecordInterface</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
        <span style="color:#9966CC; font-weight:bold;">module</span> InstanceMethods
          <span style="color:#9966CC; font-weight:bold;">def</span> process_with_delay!<span style="color:#006600; font-weight:bold;">&#40;</span>new_file<span style="color:#006600; font-weight:bold;">&#41;</span>
            process_without_delay!<span style="color:#006600; font-weight:bold;">&#40;</span>new_file<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> model.<span style="color:#9900CC;">delay_carrierwave</span>
          <span style="color:#9966CC; font-weight:bold;">end</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Awesome! Now we need to tie this into our Uploader:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># app/uploaders/asset_uploader.rb</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span>Rails.<span style="color:#9900CC;">root</span>, <span style="color:#996600;">&quot;lib&quot;</span>, <span style="color:#996600;">&quot;carrier_wave&quot;</span>, <span style="color:#996600;">&quot;ffmpeg&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span>Rails.<span style="color:#9900CC;">root</span>, <span style="color:#996600;">&quot;lib&quot;</span>, <span style="color:#996600;">&quot;carrier_wave&quot;</span>, <span style="color:#996600;">&quot;delayed_job&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># New</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> AssetUploader <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">CarrierWave::Uploader::Base</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">CarrierWave::Delayed::Job</span> <span style="color:#008000; font-style:italic;"># New</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">CarrierWave::FFMPEG</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Choose what kind of storage to use for this uploader:</span>
  storage <span style="color:#ff3333; font-weight:bold;">:file</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Override the directory where uploaded files will be stored.</span>
  <span style="color:#008000; font-style:italic;"># This is a sensible default for uploaders that are meant to be mounted:</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> store_dir
    <span style="color:#996600;">&quot;#{Rails.root}/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Add a version, utilizing our processor</span>
  version <span style="color:#ff3333; font-weight:bold;">:bitrate_128k</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    process <span style="color:#ff3333; font-weight:bold;">:resample</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;128k&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The last thing we have to do is update our model to queue up delayed job:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># app/models/asset.rb</span>
<span style="color:#9966CC; font-weight:bold;">class</span> Asset <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  mount_uploader <span style="color:#ff3333; font-weight:bold;">:asset</span>, AssetUploader
&nbsp;
  after_save <span style="color:#ff3333; font-weight:bold;">:enqueue</span> <span style="color:#008000; font-style:italic;"># New</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>There you have it. Now when you create a new Asset, associate a file, and save it, it shouldn&#8217;t run the processes, but instead create a Delayed::Job record. Then Delayed::Job should pick it up and run the processors on it. This may not be perfect, but at least its a start!</p>
<p>Thanks for reading!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.freezzo.com/2011/01/06/how-to-use-delayed-job-to-handle-your-carrierwave-processing/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Create FFMPEG processor for Carrierwave in Rails 3</title>
		<link>http://www.freezzo.com/2010/12/23/create-ffmpeg-processor-for-carrierwave-in-rails-3/</link>
		<comments>http://www.freezzo.com/2010/12/23/create-ffmpeg-processor-for-carrierwave-in-rails-3/#comments</comments>
		<pubDate>Thu, 23 Dec 2010 16:20:13 +0000</pubDate>
		<dc:creator>Randy</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[carrierwave]]></category>
		<category><![CDATA[ffmpeg]]></category>
		<category><![CDATA[rails 3]]></category>

		<guid isPermaLink="false">http://www.freezzo.com/?p=130</guid>
		<description><![CDATA[I have had the pleasure of working with the carrierwave gem recently (as opposed to paperclip), and I must say, I am quite the fan. Once major thing I missed however, was the available list of custom user plugins for it, unlike paperclip. I believe this is mostly due to how new and recent carrierwave [...]]]></description>
			<content:encoded><![CDATA[<p>I have had the pleasure of working with the carrierwave gem recently (as opposed to paperclip), and I must say, I am quite the fan. Once major thing I missed however, was the available list of custom user plugins for it, unlike paperclip. I believe this is mostly due to how new and recent carrierwave is. That being said, I put together a simple example of a FFMPEG process that will allow me to resample the bitrate of a file. This should lay the ground work for other features as well. This example is using Rails 3, but should be easily adaptable for 2. Also, make sure you already have FFMPEG installed and running properly. So lets get started:</p>
<p>First things first&#8230;we need to add the appropriate gems to our Gemfile:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Gemfile</span>
gem <span style="color:#996600;">'carrierwave'</span>
gem <span style="color:#996600;">'streamio-ffmpeg'</span></pre></div></div>

<p>Next is the meat and potatoes of this..the actual FFMPEG process for carrierwave. I choose to keep my plugin files in the directory lib/carrierwave. Make sure you have this path included in your application.rb file if you are using rails 3. Here is the code:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># lib/carrierwave/ffmpeg.rb</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'streamio-ffmpeg'</span>
<span style="color:#9966CC; font-weight:bold;">module</span> CarrierWave
  <span style="color:#9966CC; font-weight:bold;">module</span> FFMPEG
    <span style="color:#9966CC; font-weight:bold;">module</span> ClassMethods
      <span style="color:#9966CC; font-weight:bold;">def</span> resample<span style="color:#006600; font-weight:bold;">&#40;</span> bitrate <span style="color:#006600; font-weight:bold;">&#41;</span>
        process <span style="color:#ff3333; font-weight:bold;">:resample</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> bitrate
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> resample<span style="color:#006600; font-weight:bold;">&#40;</span> bitrate <span style="color:#006600; font-weight:bold;">&#41;</span>
      directory = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">dirname</span><span style="color:#006600; font-weight:bold;">&#40;</span> current_path <span style="color:#006600; font-weight:bold;">&#41;</span>
      tmpfile   = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span> directory, <span style="color:#996600;">&quot;tmpfile&quot;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
      <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">move</span><span style="color:#006600; font-weight:bold;">&#40;</span> current_path, tmpfile <span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
      file = ::<span style="color:#6666ff; font-weight:bold;">FFMPEG::Movie</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>tmpfile<span style="color:#006600; font-weight:bold;">&#41;</span>
      file.<span style="color:#9900CC;">transcode</span><span style="color:#006600; font-weight:bold;">&#40;</span> current_path, <span style="color:#ff3333; font-weight:bold;">:audio_bitrate</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> bitrate<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
      <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span> tmpfile <span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Good. Now that we have the plugin coded up, we need to include it into our uploader. I already have one mounted to my Asset model. Here is what my AssetUploader now looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># app/uploaders/asset_uploader.rb</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span>Rails.<span style="color:#9900CC;">root</span>, <span style="color:#996600;">&quot;lib&quot;</span>, <span style="color:#996600;">&quot;carrier_wave&quot;</span>, <span style="color:#996600;">&quot;ffmpeg&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> AssetUploader <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">CarrierWave::Uploader::Base</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">CarrierWave::FFMPEG</span> <span style="color:#008000; font-style:italic;"># &lt;= include the plugin</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Choose what kind of storage to use for this uploader:</span>
  storage <span style="color:#ff3333; font-weight:bold;">:file</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Override the directory where uploaded files will be stored.</span>
  <span style="color:#008000; font-style:italic;"># This is a sensible default for uploaders that are meant to be mounted:</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> store_dir
    <span style="color:#996600;">&quot;#{Rails.root}/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Add a version, utilizing our processor</span>
  version <span style="color:#ff3333; font-weight:bold;">:bitrate_128k</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    process <span style="color:#ff3333; font-weight:bold;">:resample</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;128k&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>There! Now whenever you add a new file, it should fire off the processor and create a new version. I hope this help anyone still up in the air about how to put together their own plugin/process for carrierwave. Next I will demonstrate how to incorporate Delayed::Job to move these intensive tasks to the background!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.freezzo.com/2010/12/23/create-ffmpeg-processor-for-carrierwave-in-rails-3/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Moved to a new host!</title>
		<link>http://www.freezzo.com/2010/10/09/moved-to-a-new-host/</link>
		<comments>http://www.freezzo.com/2010/10/09/moved-to-a-new-host/#comments</comments>
		<pubDate>Sat, 09 Oct 2010 18:38:38 +0000</pubDate>
		<dc:creator>Randy</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[host]]></category>
		<category><![CDATA[linode]]></category>

		<guid isPermaLink="false">http://www.freezzo.com/?p=128</guid>
		<description><![CDATA[I have spend the last week setting up a new VPS host at Linode. This site and my youtube video playlist site, http://www.jamzee.com, are both currently running on the new host. Everything has been great so far and this will give me more control in the future! If you are interested in joining Linode, check [...]]]></description>
			<content:encoded><![CDATA[<p>I have spend the last week setting up a new VPS host at <a href="http://www.linode.com/?r=9ef51a63e5769c848382a92a4c8b491c1f409975">Linode</a>. This site and my youtube video playlist site, <a href="http://www.jamzee.com">http://www.jamzee.com</a>, are both currently running on the new host. Everything has been great so far and this will give me more control in the future!</p>
<p>If you are interested in joining Linode, check out my <a href="http://www.linode.com/?r=9ef51a63e5769c848382a92a4c8b491c1f409975">referral link</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.freezzo.com/2010/10/09/moved-to-a-new-host/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to create PDF&#8217;s and Images from your website in Rails</title>
		<link>http://www.freezzo.com/2010/07/29/how-to-create-pdfs-and-images-from-your-website-in-rails/</link>
		<comments>http://www.freezzo.com/2010/07/29/how-to-create-pdfs-and-images-from-your-website-in-rails/#comments</comments>
		<pubDate>Thu, 29 Jul 2010 15:03:07 +0000</pubDate>
		<dc:creator>Randy</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[pdf]]></category>
		<category><![CDATA[pdfkit]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[websnap]]></category>
		<category><![CDATA[wkhtmltoimage]]></category>
		<category><![CDATA[wkhtmltopdf]]></category>

		<guid isPermaLink="false">http://www.freezzo.com/?p=118</guid>
		<description><![CDATA[I am going to show you how to generate both a pdf and image from a single action in a controller using the awesome, wkhtmltopdf library. This also uses PDFKit and WebSnap gems available on GitHub. This example assumes the following: wkhtmltopdf and wkhtmltoimage are already installed and accessible on in the PATH. You have [...]]]></description>
			<content:encoded><![CDATA[<p>I am going to show you how to generate both a pdf and image from a single action in a controller using the awesome, wkhtmltopdf library. This also uses PDFKit and WebSnap gems available on GitHub.</p>
<p>This example assumes the following:</p>
<ul>
<li>wkhtmltopdf and wkhtmltoimage are already installed and accessible on in the PATH.
<li>You have an html page setup to display the record.
<li>You have created a pdf CSS file to help display the pdf, if you so choose.
</ul>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#008000; font-style:italic;"># config/initializers/mime_types.rb</span>
  <span style="color:#6666ff; font-weight:bold;">Mime::Type</span>.<span style="color:#9900CC;">register</span> <span style="color:#996600;">&quot;application/pdf&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:pdf</span>
  <span style="color:#6666ff; font-weight:bold;">Mime::Type</span>.<span style="color:#9900CC;">register</span> <span style="color:#996600;">&quot;image/png&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:png</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># app/controllers/items_controller.rb</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> show
    <span style="color:#0066ff; font-weight:bold;">@item</span> = Item.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>format<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">pdf</span> <span style="color:#006600; font-weight:bold;">&#123;</span>
        html = render <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;show.html.erb&quot;</span>
        kit  = PDFKit.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span> html, <span style="color:#ff3333; font-weight:bold;">:zoom</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">0.75</span> <span style="color:#006600; font-weight:bold;">&#41;</span>
        kit.<span style="color:#9900CC;">stylesheets</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span> RAILS_ROOT, <span style="color:#996600;">&quot;public&quot;</span>, <span style="color:#996600;">&quot;stylesheets&quot;</span>, <span style="color:#996600;">&quot;pdf.css&quot;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
        send_data kit.<span style="color:#9900CC;">to_pdf</span>, <span style="color:#ff3333; font-weight:bold;">:filename</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;item.pdf&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:type</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'application/pdf'</span>, <span style="color:#ff3333; font-weight:bold;">:disposition</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'inline'</span>
      <span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">png</span> <span style="color:#006600; font-weight:bold;">&#123;</span>
        html = render <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;show.html.erb&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:layout</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;application.html.erb&quot;</span>
&nbsp;
        <span style="color:#008000; font-style:italic;"># I am nil'ing these options out because my version of wkhtmltoimage does</span>
        <span style="color:#008000; font-style:italic;"># not support the scale options and I do not want to crop the image at all.</span>
        snap = WebSnap.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>html, <span style="color:#ff3333; font-weight:bold;">:format</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'png'</span>, :<span style="color:#996600;">'scale-h'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">nil</span>, :<span style="color:#996600;">'scale-w'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">nil</span>,
          :<span style="color:#996600;">'crop-h'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">nil</span>, :<span style="color:#996600;">'crop-w'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">nil</span>, <span style="color:#ff3333; font-weight:bold;">:quality</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">100</span>, :<span style="color:#996600;">'crop-x'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">nil</span>, :<span style="color:#996600;">'crop-y'</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
        send_data snap.<span style="color:#9900CC;">to_bytes</span>, <span style="color:#ff3333; font-weight:bold;">:filename</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;item.png&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:type</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;image/png&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:disposition</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'inline'</span>
      <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now you should be able to access three distinct views, each producing a different result</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  http:<span style="color:#006600; font-weight:bold;">//</span>example.<span style="color:#9900CC;">com</span><span style="color:#006600; font-weight:bold;">/</span>items<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">1</span> <span style="color:#008000; font-style:italic;"># =&gt; Generates an html page.</span>
  http:<span style="color:#006600; font-weight:bold;">//</span>example.<span style="color:#9900CC;">com</span><span style="color:#006600; font-weight:bold;">/</span>items<span style="color:#006600; font-weight:bold;">/</span>1.<span style="color:#9900CC;">pdf</span> <span style="color:#008000; font-style:italic;"># =&gt; Generates a pdf of the html page.</span>
  http:<span style="color:#006600; font-weight:bold;">//</span>example.<span style="color:#9900CC;">com</span><span style="color:#006600; font-weight:bold;">/</span>items<span style="color:#006600; font-weight:bold;">/</span>1.<span style="color:#9900CC;">png</span> <span style="color:#008000; font-style:italic;"># =&gt; Generates a png of the html page.</span></pre></div></div>

<p>You could easily also add more image types by just created another block for each format, and<br />
changing the :format to whatever one you would like.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.freezzo.com/2010/07/29/how-to-create-pdfs-and-images-from-your-website-in-rails/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Request formats, filters, and functional tests&#8230;</title>
		<link>http://www.freezzo.com/2010/03/04/request-formats-filters-and-functional-tests/</link>
		<comments>http://www.freezzo.com/2010/03/04/request-formats-filters-and-functional-tests/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 19:09:01 +0000</pubDate>
		<dc:creator>Randy</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[format]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[request]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://www.freezzo.com/?p=114</guid>
		<description><![CDATA[I recently had to write some tests against a controller that was filtering based on the requesting format. In this case, I wanted to allow xml requests only, and redirect to login on everything else. This was fine when browsing or using curl by doing a simple: skip_before_filter :login_required, :only =&#62; &#91;:create&#93;, :if =&#62; Proc.new [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had to write some tests against a controller that was filtering based on the requesting format. In this case, I wanted to allow xml requests only, and redirect to login on everything else. This was fine when browsing or using curl by doing a simple:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">skip_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;">:create</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:if</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#CC0066; font-weight:bold;">Proc</span>.<span style="color:#9900CC;">new</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>c<span style="color:#006600; font-weight:bold;">|</span> c.<span style="color:#9900CC;">request</span>.<span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span>?<span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>My problem came when I was trying to create tests to verify that both html and xml requests did in fact produce the correct response. After many hours of messing around, I came up with a simple solution. First I skip the filters for every request, then I have another filter to re-enable them on anything but the xml request:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">skip_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;">:create</span><span style="color:#006600; font-weight:bold;">&#93;</span>
before_fiilter <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;">:create</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>c<span style="color:#006600; font-weight:bold;">|</span>
  c.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:login_required</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> c.<span style="color:#9900CC;">request</span>.<span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span>?
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Voila!!! This allowed me to continue with my rails testing and browser and curl work appropriately. (I use curl to test the xml request).</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> test_not_logged_in_normal_post
  post <span style="color:#ff3333; font-weight:bold;">:create</span>, <span style="color:#ff3333; font-weight:bold;">:login</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;test@test.com&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:password</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;test&quot;</span>
  assert_response <span style="color:#ff3333; font-weight:bold;">:redirect</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> test_not_logged_in_xml_post
  post <span style="color:#ff3333; font-weight:bold;">:create</span>, <span style="color:#ff3333; font-weight:bold;">:format</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'xml'</span>, <span style="color:#ff3333; font-weight:bold;">:login</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;test@test.com&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:password</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;test&quot;</span>
  assert_response <span style="color:#ff3333; font-weight:bold;">:success</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.freezzo.com/2010/03/04/request-formats-filters-and-functional-tests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Including methods and associations in a JSON Data set with Rails</title>
		<link>http://www.freezzo.com/2009/10/02/including-methods-and-associations-in-a-json-data-set-with-rails/</link>
		<comments>http://www.freezzo.com/2009/10/02/including-methods-and-associations-in-a-json-data-set-with-rails/#comments</comments>
		<pubDate>Fri, 02 Oct 2009 13:58:11 +0000</pubDate>
		<dc:creator>Randy</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[association]]></category>
		<category><![CDATA[include]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[methods]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[to_json]]></category>

		<guid isPermaLink="false">http://www.freezzo.com/?p=111</guid>
		<description><![CDATA[I was poking around while working with creating an application specifically for web services. We decided to use JSON as the methods of transportation of data, but the problem came when I wanted to include custom methods, or associations in my data set. The solution was fairly simple, using the to_json method. Suppose you have [...]]]></description>
			<content:encoded><![CDATA[<p>I was poking around while working with creating an application specifically for web services. We decided to use JSON as the methods of transportation of data, but the problem came when I wanted to include custom methods, or associations in my data set. The solution was fairly simple, using the to_json method.</p>
<p>Suppose you have the following classes:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Client <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  has_many <span style="color:#ff3333; font-weight:bold;">:employees</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Employee <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  belongs_to <span style="color:#ff3333; font-weight:bold;">:client</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> full_name
    <span style="color:#996600;">&quot;#{first_name} #{last_name}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>We want the controller to return a client with association employees and the full name in the database. Here is how we would go about doing that:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> show
  <span style="color:#0066ff; font-weight:bold;">@client</span> = Client.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
  respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>format<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">json</span> <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:json</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@client</span>.<span style="color:#9900CC;">to_json</span><span style="color:#006600; font-weight:bold;">&#40;</span>
      <span style="color:#ff3333; font-weight:bold;">:include</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>
        <span style="color:#ff3333; font-weight:bold;">:employee</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>
          <span style="color:#ff3333; font-weight:bold;">:only</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:email</span>,
          <span style="color:#ff3333; font-weight:bold;">:methods</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;">:full_name</span> <span style="color:#006600; font-weight:bold;">&#93;</span>
        <span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>You will end up with the following data set:</p>

<div class="wp_syntax"><div class="code"><pre class="json" style="font-family:monospace;">{ client: { name: &quot;Some client&quot;, employee: { email: &quot;test@test.com&quot;, full_name: &quot;John Doe&quot; } } }</pre></div></div>

<p>Forgive me if I messed up the json output&#8230;doing it from memory <img src='http://www.freezzo.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  There are of course way easier uses for this too, but I just decided to spit out a more complex one.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.freezzo.com/2009/10/02/including-methods-and-associations-in-a-json-data-set-with-rails/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Will paginate &#8211; how to pass custom parameters</title>
		<link>http://www.freezzo.com/2009/07/31/will-paginate-how-to-pass-custom-parameters/</link>
		<comments>http://www.freezzo.com/2009/07/31/will-paginate-how-to-pass-custom-parameters/#comments</comments>
		<pubDate>Fri, 31 Jul 2009 18:06:42 +0000</pubDate>
		<dc:creator>Randy</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[paging]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[will paginate]]></category>

		<guid isPermaLink="false">http://www.freezzo.com/?p=107</guid>
		<description><![CDATA[I came across and issue today that involved my pagination not working correctly. What was happening was a user would do a search with various criteria and the result would show with pagination links. Once they clicked on a new page, it would return them to a list of results, but that wasn&#8217;t using any [...]]]></description>
			<content:encoded><![CDATA[<p>I came across and issue today that involved my pagination not working correctly. What was happening was a user would do a search with various criteria and the result would show with pagination links. Once they clicked on a new page, it would return them to a list of results, but that wasn&#8217;t using any criteria. The solution was fairly simple, but not obviously documented. (Unless you look in the source files)</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span>= will_paginate <span style="color:#0066ff; font-weight:bold;">@object</span>, <span style="color:#ff3333; font-weight:bold;">:params</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:custom_param</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@custom_param_value</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>Here are some additional custom params you can use:</p>
<h3>Display options</h3>
<ul>
<li>:previous_label &#8212; default: &#8220;<< Previous" (this parameter is called :prev_label in versions 2.3.2 and older!)</li>
<li>:next_label &#8212; default: &#8220;Next >>&#8221;</li>
<li>:page_links &#8212; when false, only previous/next links are rendered (default: true)</li>
<li>:inner_window &#8212; how many links are shown around the current page (default: 4)</li>
<li> <img src='http://www.freezzo.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> uter_window &#8212; how many links are around the first and the last page (default: 1)</li>
<li>:separator &#8212; string separator for page HTML elements (default: single space)</li>
</ul>
<h3>HTML options</h3>
<ul>
<li>:class &#8212; CSS class name for the generated DIV (default: &#8220;pagination&#8221;)</li>
<li>:container &#8212; toggles rendering of the DIV container for pagination links, set to false only when you are rendering your own pagination markup (default: true)</li>
<li>:id &#8212; HTML ID for the container (default: nil). Pass +true+ to have the ID automatically generated from the class name of objects in collection: for example, paginating ArticleComment models would yield an ID of &#8220;article_comments_pagination&#8221;.</li>
</ul>
<h3>Advanced options</h3>
<ul>
<li>:param_name &#8212; parameter name for page number in URLs (default: :page)</li>
<li>:params &#8212; additional parameters when generating pagination links (eg. :controller => &#8220;foo&#8221;, :action => nil)</li>
<li>:renderer &#8212; class name, class or instance of a link renderer (default: WillPaginate::LinkRenderer)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.freezzo.com/2009/07/31/will-paginate-how-to-pass-custom-parameters/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to trim a string and fill remaining space in Ruby</title>
		<link>http://www.freezzo.com/2009/06/14/how-to-trim-a-string-and-fill-remaining-space-in-ruby/</link>
		<comments>http://www.freezzo.com/2009/06/14/how-to-trim-a-string-and-fill-remaining-space-in-ruby/#comments</comments>
		<pubDate>Sun, 14 Jun 2009 19:53:36 +0000</pubDate>
		<dc:creator>Randy</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[fill]]></category>
		<category><![CDATA[ltrim]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[trim]]></category>

		<guid isPermaLink="false">http://www.freezzo.com/?p=105</guid>
		<description><![CDATA[So I needed a way to take a string, and depending on its size, shorten it and fill the remaining space with some arbitrary sequence of characters. Ljust would work, except for the fact that it will fill a string up to a given length, but I only needed to do this when over a [...]]]></description>
			<content:encoded><![CDATA[<p>So I needed a way to take a string, and depending on its size, shorten it and fill the remaining space with some arbitrary sequence of characters. Ljust would work, except for the fact that it will fill a string up to a given length, but I only needed to do this when over a certain size. Here is an example:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">str = <span style="color:#996600;">&quot;Somereallylongstring&quot;</span>
<span style="color:#9966CC; font-weight:bold;">if</span> str.<span style="color:#9900CC;">length</span> <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">10</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> str<span style="color:#006600; font-weight:bold;">&#91;</span>0..7<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'...'</span>
<span style="color:#9966CC; font-weight:bold;">else</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> str
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#008000; font-style:italic;"># =&gt; &quot;Somerea...&quot;</span></pre></div></div>

<p>This is the basic idea of what I wanted to do. I decide to make it cleaner and override the String class like so:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC0066; font-weight:bold;">String</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> lfill<span style="color:#006600; font-weight:bold;">&#40;</span>len = <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">length</span>, fill = <span style="color:#996600;">'.'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    tmp = <span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#91;</span>0..<span style="color:#006600; font-weight:bold;">&#40;</span>len <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">'...'</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">length</span> <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#006666;">3</span> <span style="color:#006600; font-weight:bold;">&gt;</span> len
    <span style="color:#0000FF; font-weight:bold;">return</span> tmp <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#0000FF; font-weight:bold;">self</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
str = <span style="color:#996600;">&quot;Somereallylongstring&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> str.<span style="color:#9900CC;">lfill</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">10</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#=&gt; &quot;Somerea...&quot;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.freezzo.com/2009/06/14/how-to-trim-a-string-and-fill-remaining-space-in-ruby/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>acts_as_similar &#8211; A basic similarity activerecord plugin</title>
		<link>http://www.freezzo.com/2009/06/04/acts_as_similar-a-basic-similarity-activerecord-plugin/</link>
		<comments>http://www.freezzo.com/2009/06/04/acts_as_similar-a-basic-similarity-activerecord-plugin/#comments</comments>
		<pubDate>Thu, 04 Jun 2009 17:34:32 +0000</pubDate>
		<dc:creator>Randy</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[recommended]]></category>
		<category><![CDATA[similar]]></category>

		<guid isPermaLink="false">http://www.freezzo.com/?p=100</guid>
		<description><![CDATA[I had a need to find items that were similar to an item. I looked at acts_as_recommendable, and while this was very nice plugin, it was too slow when working with large data sets. I also decided that the results I needed didn&#8217;t need to be scientifically accurate, just a rough match. So I created [...]]]></description>
			<content:encoded><![CDATA[<p>I had a need to find items that were similar to an item. I looked at acts_as_recommendable, and while this was very nice plugin, it was too slow when working with large data sets. I also decided that the results I needed didn&#8217;t need to be scientifically accurate, just a rough match. So I created this plugin to allow for a very elementary way to find  items that are similar to the object you are working with. This works best when used with a has_many :through relationship.</p>
<p>Basically all it is doing is looking for other objects of the same class, that have some related value.</p>
<p>Git it here: <a href="http://github.com/freezzo/acts_as_similar/tree/master">http://github.com/freezzo/acts_as_similar/tree/master</a></p>
<p>Example 1<br />
=========<br />
Look for similar playlists, using the videos as what defines similarity.<br />
This will look for all playlists that have a similar video as the playlist you are looking against.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Playlist
  has_many <span style="color:#ff3333; font-weight:bold;">:playlists_videos</span>
  has_many <span style="color:#ff3333; font-weight:bold;">:videos</span>, <span style="color:#ff3333; font-weight:bold;">:through</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:playlists_videos</span>
&nbsp;
  acts_as_similar <span style="color:#ff3333; font-weight:bold;">:videos</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Example 2<br />
=========<br />
Look for similar playlists, using the title of the playlist as the similarity item.<br />
This will look for all playlists that have a similar title as the playlist you are looking against.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Playlist
  has_many <span style="color:#ff3333; font-weight:bold;">:playlists_videos</span>
  has_many <span style="color:#ff3333; font-weight:bold;">:videos</span>, <span style="color:#ff3333; font-weight:bold;">:through</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:playlists_videos</span>
&nbsp;
  acts_as_similar <span style="color:#ff3333; font-weight:bold;">:field</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:title</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Example 3<br />
=========<br />
Look for similar playlists, using the video_id of the playlists_videos as the similarity item.<br />
This will look for all playlists that have a similar video_id as the playlist you are looking against.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Playlist
  has_many <span style="color:#ff3333; font-weight:bold;">:playlists_videos</span>
  has_many <span style="color:#ff3333; font-weight:bold;">:videos</span>, <span style="color:#ff3333; font-weight:bold;">:through</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:playlists_videos</span>
&nbsp;
  acts_as_similar <span style="color:#ff3333; font-weight:bold;">:playlists_videos</span>, <span style="color:#ff3333; font-weight:bold;">:field</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:video_id</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Execute<br />
=========</p>

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

<p>Note: This is a work in progress.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.freezzo.com/2009/06/04/acts_as_similar-a-basic-similarity-activerecord-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ActiveRecord &#8211; Making :include and :select play nice!</title>
		<link>http://www.freezzo.com/2009/05/20/activerecord-making-include-and-select-play-nice/</link>
		<comments>http://www.freezzo.com/2009/05/20/activerecord-making-include-and-select-play-nice/#comments</comments>
		<pubDate>Wed, 20 May 2009 15:00:45 +0000</pubDate>
		<dc:creator>Randy</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[eager select]]></category>
		<category><![CDATA[include]]></category>

		<guid isPermaLink="false">http://www.freezzo.com/?p=97</guid>
		<description><![CDATA[So I have been using a nice plug-in that will allow me to using :select when using :include, and not have it pull the entire data set. You can add the plug-in to your app like this: script/plugin install git://github.com/blythedunham/eload-select.git Here are some ways to use the plug-in: Employee.find :all, :select =&#62; 'addresses.city, address.state, employees.*', [...]]]></description>
			<content:encoded><![CDATA[<p>So I have been using a nice plug-in that will allow me to using :select when using :include, and not have it pull the entire data set. You can add the plug-in to your app like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">script<span style="color:#006600; font-weight:bold;">/</span>plugin install git:<span style="color:#006600; font-weight:bold;">//</span>github.<span style="color:#9900CC;">com</span><span style="color:#006600; font-weight:bold;">/</span>blythedunham<span style="color:#006600; font-weight:bold;">/</span>eload<span style="color:#006600; font-weight:bold;">-</span><span style="color:#CC0066; font-weight:bold;">select</span>.<span style="color:#9900CC;">git</span></pre></div></div>

<p>Here are some ways to use the plug-in:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Employee.<span style="color:#9900CC;">find</span> <span style="color:#ff3333; font-weight:bold;">:all</span>,
    <span style="color:#ff3333; font-weight:bold;">:select</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'addresses.city, address.state, employees.*'</span>,
    <span style="color:#ff3333; font-weight:bold;">:include</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:address</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Employee.<span style="color:#9900CC;">find</span> <span style="color:#ff3333; font-weight:bold;">:first</span>,
    <span style="color:#ff3333; font-weight:bold;">:select</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'now() as current_time, addresses.city, DATE(addresses.created_at) as addresses.created_at, employee.*'</span>,
    <span style="color:#ff3333; font-weight:bold;">:include</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:address</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Employee.<span style="color:#9900CC;">find</span> <span style="color:#ff3333; font-weight:bold;">:all</span>,
    <span style="color:#ff3333; font-weight:bold;">:select</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'addresses.city, employees.name, employees.start_date'</span>,
    <span style="color:#ff3333; font-weight:bold;">:include</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:address</span></pre></div></div>

<p>Examples taken from:<br />
<a href="http://www.snowgiraffe.com/tech/329/eager-loading-select-plugin-when-select-plays-nice-with-include/">http://www.snowgiraffe.com/tech/329/eager-loading-select-plugin-when-select-plays-nice-with-include/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.freezzo.com/2009/05/20/activerecord-making-include-and-select-play-nice/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

