<?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>eXpand yOur cReativity &#187; ruby</title>
	<atom:link href="http://blog.bhushangahire.net/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.bhushangahire.net</link>
	<description>by Bhushan G Ahire</description>
	<lastBuildDate>Mon, 26 Jul 2010 10:25:25 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Run acts_as_solr in JRuby in background mode</title>
		<link>http://blog.bhushangahire.net/2010/07/12/run-acts_as_solr-in-jruby-in-background-mode/</link>
		<comments>http://blog.bhushangahire.net/2010/07/12/run-acts_as_solr-in-jruby-in-background-mode/#comments</comments>
		<pubDate>Mon, 12 Jul 2010 12:43:07 +0000</pubDate>
		<dc:creator>Bhushan G Ahire</dc:creator>
				<category><![CDATA[JRuby]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[background]]></category>
		<category><![CDATA[fork]]></category>
		<category><![CDATA[solr]]></category>

		<guid isPermaLink="false">http://blog.bhushangahire.net/?p=217</guid>
		<description><![CDATA[<p class="ArticleSummary">When you try and run rake solr:start or rake solr:stop it uses Kernel.fork to spawn of a child process. However in JRuby this is disabled by default due to concurrency issues. And this prevents your solr rake tasks from running under a JRuby environment...</p>

<div class="extended">
<h3>The Problem</h3>
When you try and run rake solr:start or rake solr:stop it uses Kernel.fork to spawn of a child process.
However in JRuby this is disabled by default due to concurrency issues. There is an option to enabling fork (jruby -J-Djruby.fork.enabled=true)
within JRuby but it is experimental and as the warning says, "WARNING: fork is highly unlikely to be safe or stable on the JVM." as it can cause all sorts of weird and wonderful side-effects.
<h3>The Solution</h3>
The solution therefore is to simply alter the solr:start and solr:stop tasks to use Kernel.exec instead.
To do this find the solr rake tasks usually in {RAILS_ROOT}/vendor/acts_as_solr/lib/tasks/solr.rake. In the start task all that is required is to comment out the start of the fork block. so you only have the exec method call,
This will not start the solr process in the background. To do so you just have to add <strong>"&#38;"</strong> at the end of the exec command, which causes the jar file to run in background mode.
]]></description>
			<content:encoded><![CDATA[<div class="fblike_button" style="margin: 10px 0;"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.bhushangahire.net%2F2010%2F07%2F12%2Frun-acts_as_solr-in-jruby-in-background-mode%2F&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=recommend&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:450px; height:25px"></iframe></div>
<p class="ArticleSummary">When you try and run rake solr:start or rake solr:stop it uses Kernel.fork to spawn of a child process. However in JRuby this is disabled by default due to concurrency issues. And this prevents your solr rake tasks from running under a JRuby environment&#8230;</p>
<div class="extended">
<h3>The Problem</h3>
<p>When you try and run rake solr:start or rake solr:stop it uses Kernel.fork to spawn of a child process.<br />
However in JRuby this is disabled by default due to concurrency issues. There is an option to enabling fork (jruby -J-Djruby.fork.enabled=true)<br />
within JRuby but it is experimental and as the warning says, &#8220;WARNING: fork is highly unlikely to be safe or stable on the JVM.&#8221; as it can cause all sorts of weird and wonderful side-effects.</p>
<h3>The Solution</h3>
<p>The solution therefore is to simply alter the solr:start and solr:stop tasks to use Kernel.exec instead.<br />
To do this find the solr rake tasks usually in {RAILS_ROOT}/vendor/acts_as_solr/lib/tasks/solr.rake. In the start task all that is required is to comment out the start of the fork block. so you only have the exec method call,<br />
This will not start the solr process in the background. To do so you just have to add <strong>&#8220;&amp;&#8221;</strong> at the end of the exec command, which causes the jar file to run in background mode.</p>
<pre class="brush: ruby;">
task :start do
      require &quot;#{File.dirname(__FILE__)}/../../config/solr_environment.rb&quot;
      begin
        n = Net::HTTP.new('127.0.0.1', SOLR_PORT)
        n.request_head('/').value

      rescue Net::HTTPServerException #responding
        puts &quot;Port #{SOLR_PORT} in use&quot; and return

      rescue Errno::ECONNREFUSED #not responding
        Dir.chdir(SOLR_PATH) do
            exec &quot;java #{SOLR_JVM_OPTIONS}
-Dsolr.data.dir=#{SOLR_DATA_PATH} -Djetty.logs=#{SOLR_LOGS_PATH}
-Djetty.port=#{SOLR_PORT} -jar start.jar &amp;&quot;
          sleep(5)
          File.open(&quot;#{SOLR_PIDS_PATH}/#{ENV['RAILS_ENV']}_pid&quot;, &quot;w&quot;){ |f| f &amp;lt;&amp;lt; pid}
          puts &quot;#{ENV['RAILS_ENV']} Solr started successfully on #{SOLR_PORT}, pid: #{pid}.&quot;
        end
      end
    end
</pre>
<p>and in the end task just comment out the begining of the fork block so you are left with</p>
<pre class="brush: ruby;">
  task :stop do
    require &quot;#{File.dirname(__FILE__)}/../../config/solr_environment.rb&quot;
      file_path = &quot;#{SOLR_PIDS_PATH}/#{ENV['RAILS_ENV']}_pid&quot;
      if File.exists?(file_path)
        File.open(file_path, &quot;r&quot;) do |f|
          pid = f.readline
          Process.kill('TERM', pid.to_i)
        end
        File.unlink(file_path)
        Rake::Task[&quot;solr:destroy_index&quot;].invoke if ENV['RAILS_ENV'] == 'test'
        puts &quot;Solr shutdown successfully.&quot;
      else
        puts &quot;PID file not found at #{file_path}. Either Solr is not running or no PID file was written.&quot;
      end
  end
</pre>
<p>you should now be able to run your solr rake tasks under jruby with out any problems..</p>
</div>


<!-- Begin SexyBookmarks Menu Code -->
<div class="sexy-bookmarks sexy-bookmarks-expand sexy-bookmarks-center">
<ul class="socials">
		<li class="sexy-delicious">
			<a href="http://del.icio.us/post?url=http://blog.bhushangahire.net/2010/07/12/run-acts_as_solr-in-jruby-in-background-mode/&amp;title=Run+acts_as_solr+in+JRuby+in+background+mode" rel="nofollow" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="sexy-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://blog.bhushangahire.net/2010/07/12/run-acts_as_solr-in-jruby-in-background-mode/&amp;title=Run+acts_as_solr+in+JRuby+in+background+mode" rel="nofollow" title="Digg this!">Digg this!</a>
		</li>
		<li class="sexy-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://blog.bhushangahire.net/2010/07/12/run-acts_as_solr-in-jruby-in-background-mode/&amp;title=Run+acts_as_solr+in+JRuby+in+background+mode" rel="nofollow" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="sexy-technorati">
			<a href="http://technorati.com/faves?add=http://blog.bhushangahire.net/2010/07/12/run-acts_as_solr-in-jruby-in-background-mode/" rel="nofollow" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="sexy-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://blog.bhushangahire.net/2010/07/12/run-acts_as_solr-in-jruby-in-background-mode/&amp;t=Run+acts_as_solr+in+JRuby+in+background+mode" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="sexy-twitter">
			<a href="http://twitter.com/home?status=Run+acts_as_solr+in+JRuby+in+background+mode+-+http://blog.bhushangahire.net/2010/07/12/run-acts_as_solr-in-jruby-in-background-mode/+(via+@bhushangahire)" rel="nofollow" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="sexy-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://blog.bhushangahire.net/2010/07/12/run-acts_as_solr-in-jruby-in-background-mode/&amp;title=Run+acts_as_solr+in+JRuby+in+background+mode&amp;summary=When%20you%20try%20and%20run%20rake%20solr%3Astart%20or%20rake%20solr%3Astop%20it%20uses%20Kernel.fork%20to%20spawn%20of%20a%20child%20process.%20However%20in%20JRuby%20this%20is%20disabled%20by%20default%20due%20to%20concurrency%20issues.%20And%20this%20prevents%20your%20solr%20rake%20tasks%20from%20running%20under%20a%20JRuby%20environment...%0D%0A%0D%0A%0D%0AThe%20Problem%0D%0AWhen%20you%20try%20and%20run%20rake&amp;source=eXpand yOur cReativity" rel="nofollow" title="Share this on Linkedin">Share this on Linkedin</a>
		</li>
		<li class="sexy-tumblr">
			<a href="http://www.tumblr.com/share?v=3&amp;u=http%3A%2F%2Fblog.bhushangahire.net%2F2010%2F07%2F12%2Frun-acts_as_solr-in-jruby-in-background-mode%2F&amp;t=Run+acts_as_solr+in+JRuby+in+background+mode" rel="nofollow" title="Share this on Tumblr">Share this on Tumblr</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>
<!-- End SexyBookmarks Menu Code -->

<div style='clear:both'></div>]]></content:encoded>
			<wfw:commentRss>http://blog.bhushangahire.net/2010/07/12/run-acts_as_solr-in-jruby-in-background-mode/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby Mixin Tutorial</title>
		<link>http://blog.bhushangahire.net/2010/06/03/ruby-mixin-tutorial/</link>
		<comments>http://blog.bhushangahire.net/2010/06/03/ruby-mixin-tutorial/#comments</comments>
		<pubDate>Thu, 03 Jun 2010 05:08:28 +0000</pubDate>
		<dc:creator>Bhushan G Ahire</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[mixin]]></category>

		<guid isPermaLink="false">http://blog.bhushangahire.net/?p=195</guid>
		<description><![CDATA[
In Java you just have classes (both abstract and concrete) and interfaces.  The Ruby language provides classes, modules, and a mix of both.  In this post I want to dive into mixins in Ruby.
In the Ruby language a mixin is a class that is mixed with a module.  In other words the [...]]]></description>
			<content:encoded><![CDATA[<div class="fblike_button" style="margin: 10px 0;"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.bhushangahire.net%2F2010%2F06%2F03%2Fruby-mixin-tutorial%2F&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=recommend&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:450px; height:25px"></iframe></div>
<p>In Java you just have classes (both abstract and concrete) and interfaces.  The Ruby language provides classes, modules, and a mix of both.  In this post I want to dive into mixins in Ruby.</p>
<p>In the Ruby language a mixin is a class that is mixed with a module.  In other words the implementation of the class and module are joined, intertwined, combined, etc.  A mixin is a different mechanism to the extend construct used to add concrete implementation to a class.  With a mixin you can extend from a module instead of a class.  Before we get started with the mixin examples let me first explain what a module is.</p>
<p>I think of a module as a degenerate abstract class.  A module can’t be instantiated and no class can directly extend it but a module can fully implement methods.  A class can leverage the implementation of a module by including the module’s methods.  A module can define methods that can be shared in different and seperate classes either at the class or instance level.</p>
<p>Let me define a module, albeit a trivial one, that would convert a numeric integer value to English.</p>
<pre class="brush: ruby;">
# Convert a integer value to English.
module Stringify
  # Requires an instance variable @value
  def stringify
    if @value == 1
      &amp;amp;quot;One&amp;amp;quot;
    elsif @value == 2
      &amp;amp;quot;Two&amp;amp;quot;
    elsif @value == 3
      &amp;amp;quot;Three&amp;amp;quot;
    end
  end
end
</pre>
<p>Note that the Stringify module makes use of a @value instance variable.  The class that will be mixed with this module needs to define and set a @value instance variable since the Stringify module uses it but does not define it.  In addition to instance variables a module could invoke methods defined not in the module itself but in the class that it will be mixed with.</p>
<p>Now let me construct a self contained module that is not dependent on the implementation of any class that it can be mixed with.</p>
<pre class="brush: ruby;">
# A Math module akin to Java Math class.
module Math
  # Could be called as a class, static, method
  def add(val_one, val_two)
    BigInteger.new(val_one + val_two)
  end
end
</pre>
<p>The methods in the Math module are intended to be invoked like class methods, also known as static methods.  The add method in the Math module accepts two integer values and returns an instance of BigInteger.  Let me now define the mixin BigInteger class.</p>
<pre class="brush: plain;">
# Base Number class
class Number
  def intValue
    @value
  end
end

# BigInteger extends Number
class BigInteger &lt; Number

  # Add instance methods from Stringify
  include Stringify

  # Add class methods from Math
  extend Math

  # Add a constructor with one parameter
  def initialize(value)
    @value = value
  end
end
</pre>
<p>I loosely modeled the BigInteger and Number classes after the Java versions.  The BigInteger class defines one constructor and directly inherits one method from the Number base class.  To mix in the methods implemented in the Stringify and Math modules with the BigInteger class you will note the usage of the include and extend methods, respectively.</p>
<pre class="brush: plain;">
# Create a new object
bigint1 = BigInteger.new(10)
# Call a method inherited from the base class
puts bigint1.intValue   # --&gt; 10
</pre>
<p>The extend method will mix a module’s methods at the class level.  The method defined in the Math module can be used as a class/static method.</p>
<pre class="brush: plain;">
# Call class method extended from Math
bigint2 = BigInteger.add(-2, 4)
puts bigint2.intValue   # --&gt; 2
</pre>
<p>The include method will mix a module’s methods at the instance level, meaning that the methods will become instance methods.  The method defined in the Stringify module can be used as an instance method.</p>
<pre class="brush: plain;">
# Call a method included from Stringify
puts bigint2.stringify   # --&gt; 'Two'
</pre>
<p>There is another use of the extend method.  You can enhance an object instance by mixing it with a module at run time!  This is a powerful  feature.  Let me create a module that will be used to extend an object, changing it’s responsibilities at runtime.</p>
<pre class="brush: plain;">
# Format a numeric value as a currency
module CurrencyFormatter
  def format
    &quot;$#{@value}&quot;
  end
end
</pre>
<p>To mix an object instance with a module you can do the following:</p>
<pre class="brush: plain;">
# Add the module methods to
# this object instance, only!
bigint2.extend CurrencyFormatter
puts bigint2.format   # --&gt; '$2'
</pre>
<p>Calling the extend method on an an instance will only extend that one object, objects of the same class will not be extended with the new functionality.</p>
<pre class="brush: plain;">
puts bigint1.format   # will generate an error
</pre>
<p>Modules that will be mixed with a class via the include or extend method could define something like a contructor or initializer method to the module.  The module initializer method will be invoked at the time the module is mixed with a class.  When a class extends a module the module’s self.extended method will be invoked:</p>
<pre class="brush: plain;">
module Math
  def self.extended(base)
    # Initialize module.
  end
end
</pre>
<p>The self prefix indicates that the method is a static module level method.  The base parameter in the static extended method will be either an instance object or class object of the class that extended the module depending whether you extend a object or class, respectively.</p>
<p>When a class includes a module the module’s self.included method will be invoked.</p>
<pre class="brush: plain;">
module Stringify
  def self.included(base)
    # Initialize module.
  end
end
</pre>
<p>The base parameter will be a class object for the class that includes the module.</p>
<p>It is important to note that inside the included and extended initializer methods you can include and extend other modules, here is an example of that:</p>
<pre class="brush: plain;">
module Stringify
  def self.included(base)
    base.extend SomeOtherModule
  end
end
</pre>


<!-- Begin SexyBookmarks Menu Code -->
<div class="sexy-bookmarks sexy-bookmarks-expand sexy-bookmarks-center">
<ul class="socials">
		<li class="sexy-delicious">
			<a href="http://del.icio.us/post?url=http://blog.bhushangahire.net/2010/06/03/ruby-mixin-tutorial/&amp;title=Ruby+Mixin+Tutorial" rel="nofollow" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="sexy-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://blog.bhushangahire.net/2010/06/03/ruby-mixin-tutorial/&amp;title=Ruby+Mixin+Tutorial" rel="nofollow" title="Digg this!">Digg this!</a>
		</li>
		<li class="sexy-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://blog.bhushangahire.net/2010/06/03/ruby-mixin-tutorial/&amp;title=Ruby+Mixin+Tutorial" rel="nofollow" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="sexy-technorati">
			<a href="http://technorati.com/faves?add=http://blog.bhushangahire.net/2010/06/03/ruby-mixin-tutorial/" rel="nofollow" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="sexy-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://blog.bhushangahire.net/2010/06/03/ruby-mixin-tutorial/&amp;t=Ruby+Mixin+Tutorial" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="sexy-twitter">
			<a href="http://twitter.com/home?status=Ruby+Mixin+Tutorial+-+http://blog.bhushangahire.net/2010/06/03/ruby-mixin-tutorial/+(via+@bhushangahire)" rel="nofollow" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="sexy-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://blog.bhushangahire.net/2010/06/03/ruby-mixin-tutorial/&amp;title=Ruby+Mixin+Tutorial&amp;summary=In%20Java%20you%20just%20have%20classes%20%28both%20abstract%20and%20concrete%29%20and%20interfaces.%20%20The%20Ruby%20language%20provides%20classes%2C%20modules%2C%20and%20a%20mix%20of%20both.%20%20In%20this%20post%20I%20want%20to%20dive%20into%20mixins%20in%20Ruby.%0D%0A%0D%0AIn%20the%20Ruby%20language%20a%20mixin%20is%20a%20class%20that%20is%20mixed%20with%20a%20module.%20%20In%20other%20words%20the%20implementation%20of%20&amp;source=eXpand yOur cReativity" rel="nofollow" title="Share this on Linkedin">Share this on Linkedin</a>
		</li>
		<li class="sexy-tumblr">
			<a href="http://www.tumblr.com/share?v=3&amp;u=http%3A%2F%2Fblog.bhushangahire.net%2F2010%2F06%2F03%2Fruby-mixin-tutorial%2F&amp;t=Ruby+Mixin+Tutorial" rel="nofollow" title="Share this on Tumblr">Share this on Tumblr</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>
<!-- End SexyBookmarks Menu Code -->

<div style='clear:both'></div>]]></content:encoded>
			<wfw:commentRss>http://blog.bhushangahire.net/2010/06/03/ruby-mixin-tutorial/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Paginating multiple models using will_paginate on the same page</title>
		<link>http://blog.bhushangahire.net/2010/04/13/paginating-multiple-models-using-will_paginate-on-the-same-page/</link>
		<comments>http://blog.bhushangahire.net/2010/04/13/paginating-multiple-models-using-will_paginate-on-the-same-page/#comments</comments>
		<pubDate>Tue, 13 Apr 2010 12:22:08 +0000</pubDate>
		<dc:creator>Bhushan G Ahire</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[pagination]]></category>
		<category><![CDATA[will paginate]]></category>

		<guid isPermaLink="false">http://blog.bhushangahire.net/?p=169</guid>
		<description><![CDATA[

The will_paginate plugin makes pagination for your models in Ruby on Rails ridiculously simple. However sometimes you’ll find yourself wanting to paginate over two or more models on a single page. For instance, you might want to display a list of users and administrators on a single page along with a pager for each model [...]]]></description>
			<content:encoded><![CDATA[<div class="fblike_button" style="margin: 10px 0;"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.bhushangahire.net%2F2010%2F04%2F13%2Fpaginating-multiple-models-using-will_paginate-on-the-same-page%2F&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=recommend&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:450px; height:25px"></iframe></div>
<div class="snap_preview">
<p>The <a title="will_paginate plugin" href="http://wiki.github.com/mislav/will_paginate" target="_blank">will_paginate</a> plugin makes pagination for your models in Ruby on Rails ridiculously simple. However sometimes you’ll find yourself wanting to paginate over two or more models on a single page. For instance, you might want to display a list of users and administrators on a single page along with a pager for each model (assuming that users and administrators are stored in separate tables).</p>
<h3>Controller code</h3>
<p>The code is pretty simple, except that I am specifying the page to show to be equal to params[:user_page] and params[:administrator_page] respectively. Since we are allowing the ability to page over two models, we need two separate parameters to determine which page of users or administrators to show.</p>
<pre class="brush: jscript;">
@users = User.paginate(:page =&gt; params[:user_page], :per_page =&gt; 10)&lt;br/&gt;
@administrators = Administrator.paginate(:page =&gt; params[:administrator_page], :per_page =&gt; 10)
</pre>
<h3>View code</h3>
<p>In the view all we need to do is make sure to set the param_value to the correct value to indicate to the plugin that we want to use a different parameter name for the page. The default is simply called ‘page’, but we need to make sure to use ‘user_page’ and ‘administrator_page’ instead for the two different models.</p>
<pre class="brush: jscript;">
&lt;%= will_paginate @users, :param_name =&gt; 'user_page' %&gt;&lt;br/&gt;
&lt;%= will_paginate @administrators, :param_name =&gt; 'administrator_page' %&gt;
</pre>
<p>That’s it, you should now be able to page through your users and administrators on the same page.</p>
</div>


<!-- Begin SexyBookmarks Menu Code -->
<div class="sexy-bookmarks sexy-bookmarks-expand sexy-bookmarks-center">
<ul class="socials">
		<li class="sexy-delicious">
			<a href="http://del.icio.us/post?url=http://blog.bhushangahire.net/2010/04/13/paginating-multiple-models-using-will_paginate-on-the-same-page/&amp;title=Paginating+multiple+models+using+will_paginate+on+the+same+page" rel="nofollow" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="sexy-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://blog.bhushangahire.net/2010/04/13/paginating-multiple-models-using-will_paginate-on-the-same-page/&amp;title=Paginating+multiple+models+using+will_paginate+on+the+same+page" rel="nofollow" title="Digg this!">Digg this!</a>
		</li>
		<li class="sexy-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://blog.bhushangahire.net/2010/04/13/paginating-multiple-models-using-will_paginate-on-the-same-page/&amp;title=Paginating+multiple+models+using+will_paginate+on+the+same+page" rel="nofollow" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="sexy-technorati">
			<a href="http://technorati.com/faves?add=http://blog.bhushangahire.net/2010/04/13/paginating-multiple-models-using-will_paginate-on-the-same-page/" rel="nofollow" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="sexy-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://blog.bhushangahire.net/2010/04/13/paginating-multiple-models-using-will_paginate-on-the-same-page/&amp;t=Paginating+multiple+models+using+will_paginate+on+the+same+page" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="sexy-twitter">
			<a href="http://twitter.com/home?status=Paginating+multiple+models+using+will_paginate+on+the+same+page+-+http://blog.bhushangahire.net/2010/04/13/paginating-multiple-models-using-will_paginate-on-the-same-page/+(via+@bhushangahire)" rel="nofollow" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="sexy-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://blog.bhushangahire.net/2010/04/13/paginating-multiple-models-using-will_paginate-on-the-same-page/&amp;title=Paginating+multiple+models+using+will_paginate+on+the+same+page&amp;summary=The%20will_paginate%20plugin%20makes%20pagination%20for%20your%20models%20in%20Ruby%20on%20Rails%20ridiculously%20simple.%20However%20sometimes%20you%E2%80%99ll%20find%20yourself%20wanting%20to%20paginate%20over%20two%20or%20more%20models%20on%20a%20single%20page.%20For%20instance%2C%20you%20might%20want%20to%20display%20a%20list%20of%20users%20and%20administrators%20on%20a%20single%20page%20along%20wit&amp;source=eXpand yOur cReativity" rel="nofollow" title="Share this on Linkedin">Share this on Linkedin</a>
		</li>
		<li class="sexy-tumblr">
			<a href="http://www.tumblr.com/share?v=3&amp;u=http%3A%2F%2Fblog.bhushangahire.net%2F2010%2F04%2F13%2Fpaginating-multiple-models-using-will_paginate-on-the-same-page%2F&amp;t=Paginating+multiple+models+using+will_paginate+on+the+same+page" rel="nofollow" title="Share this on Tumblr">Share this on Tumblr</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>
<!-- End SexyBookmarks Menu Code -->

<div style='clear:both'></div>]]></content:encoded>
			<wfw:commentRss>http://blog.bhushangahire.net/2010/04/13/paginating-multiple-models-using-will_paginate-on-the-same-page/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>List of useful rake tasks for Rails&#8230;</title>
		<link>http://blog.bhushangahire.net/2010/03/25/list-of-useful-rake-tasks-for-rails/</link>
		<comments>http://blog.bhushangahire.net/2010/03/25/list-of-useful-rake-tasks-for-rails/#comments</comments>
		<pubDate>Thu, 25 Mar 2010 12:12:11 +0000</pubDate>
		<dc:creator>Bhushan G Ahire</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[rake]]></category>

		<guid isPermaLink="false">http://blog.bhushangahire.net/?p=159</guid>
		<description><![CDATA[
rake cache:clear
# Clears all cached pages
rake db:bootstrap
# Loads a schema.rb file into the database and then loads the initial database fixtures.
rake db:bootstrap:copy_default_theme
# Copy default theme to site theme
rake db:migrate
# Migrate the database through scripts in db/migrate. Target specific version with VERSION=x
rake db:schema:dump
# Create a db/schema.rb file that can be portably used against any DB supported [...]]]></description>
			<content:encoded><![CDATA[<div class="fblike_button" style="margin: 10px 0;"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.bhushangahire.net%2F2010%2F03%2F25%2Flist-of-useful-rake-tasks-for-rails%2F&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=recommend&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:450px; height:25px"></iframe></div>
<pre>rake cache<span style="color: #aa6600;">:clear</span>
<span style="color: #888888;"># Clears all cached pages</span>
rake db<span style="color: #aa6600;">:bootstrap</span>
<span style="color: #888888;"># Loads a schema.rb file into the database and then loads the initial database fixtures.</span>
rake db<span style="color: #aa6600;">:bootstrap</span><span style="color: #aa6600;">:copy_default_theme</span>
<span style="color: #888888;"># Copy default theme to site theme</span>
rake db<span style="color: #aa6600;">:migrate</span>
<span style="color: #888888;"># Migrate the database through scripts in db/migrate. Target specific version with VERSION=x</span>
rake db<span style="color: #aa6600;">:schema</span><span style="color: #aa6600;">:dump</span>
<span style="color: #888888;"># Create a db/schema.rb file that can be portably used against any DB supported by AR</span>
rake db<span style="color: #aa6600;">:schema</span><span style="color: #aa6600;">:load</span>
<span style="color: #888888;"># Load a schema.rb file into the database
</span>rake db<span style="color: #aa6600;">:bootstrap</span><span style="color: #aa6600;">:load</span>
<span style="color: #888888;"># Load initial database fixtures (in db/bootstrap/*.yml) into the current environment's database.  Load specific fixtures using FIXTURES=x,y</span>
rake db<span style="color: #aa6600;">:fixtures</span><span style="color: #aa6600;">:load</span>
<span style="color: #888888;"># Load fixtures into the current environment's database.  Load specific fixtures using FIXTURES=x,y</span>
rake db<span style="color: #aa6600;">:sessions</span><span style="color: #aa6600;">:clear</span>
<span style="color: #888888;"># Clear the sessions table</span>
rake db<span style="color: #aa6600;">:sessions</span><span style="color: #aa6600;">:create</span>
<span style="color: #888888;"># Creates a sessions table for use with CGI::Session::ActiveRecordStore</span>
rake db<span style="color: #aa6600;">:structure</span><span style="color: #aa6600;">:dump</span>
<span style="color: #888888;"># Dump the database structure to a SQL file</span>
rake db<span style="color: #aa6600;">:test</span><span style="color: #aa6600;">:clone</span>
<span style="color: #888888;"># Recreate the test database from the current environment's database schema</span>
rake db<span style="color: #aa6600;">:test</span><span style="color: #aa6600;">:clone_structure</span>
<span style="color: #888888;"># Recreate the test databases from the development structure</span>
rake db<span style="color: #aa6600;">:test</span><span style="color: #aa6600;">:prepare</span>
<span style="color: #888888;"># Prepare the test database and load the schema</span>
rake db<span style="color: #aa6600;">:test</span><span style="color: #aa6600;">:purge</span>
<span style="color: #888888;"># Empty the test database</span>
rake deploy
<span style="color: #888888;"># Push the latest revision into production using the release manager</span>
rake diff_from_last_deploy
<span style="color: #888888;"># Describe the differences between HEAD and the last production release</span>
rake doc<span style="color: #aa6600;">:app</span>
<span style="color: #888888;"># Build the app HTML Files</span>
rake doc<span style="color: #aa6600;">:clobber_app
</span><span style="color: #888888;"># Remove rdoc products</span>
rake doc<span style="color: #aa6600;">:clobber_plugins</span>
<span style="color: #888888;"># Remove plugin documentation</span>
rake doc<span style="color: #aa6600;">:clobber_rails</span>
<span style="color: #888888;"># Remove rdoc products</span>
rake doc<span style="color: #aa6600;">:plugins</span>
<span style="color: #888888;"># Generate documation for all installed plugins</span>
rake doc<span style="color: #aa6600;">:rails</span>
<span style="color: #888888;"># Build the rails HTML Files</span>
rake doc<span style="color: #aa6600;">:reapp</span>
<span style="color: #888888;"># Force a rebuild of the RDOC files</span>
rake doc<span style="color: #aa6600;">:rerails</span>
<span style="color: #888888;"># Force a rebuild of the RDOC files</span>
rake edge
<span style="color: #888888;"># freeze rails edge</span>
rake log<span style="color: #aa6600;">:clear</span>
<span style="color: #888888;"># Truncates all *.log files in log/ to zero bytes</span>
rake rails<span style="color: #aa6600;">:freeze</span><span style="color: #aa6600;">:edge
</span><span style="color: #888888;"># Lock to latest Edge Rails or a specific revision with REVISION=X (ex: REVISION=4021) or a tag with TAG=Y (ex: TAG=rel_1-1-0)</span>
rake rails<span style="color: #aa6600;">:freeze</span><span style="color: #aa6600;">:gems</span>
<span style="color: #888888;"># Lock this application to the current gems (by unpacking them into vendor/rails)</span>
rake rails<span style="color: #aa6600;">:unfreeze</span>
<span style="color: #888888;"># Unlock this application from freeze of gems or edge and return to a fluid use of system gems</span>
rake rails<span style="color: #aa6600;">:update</span>
<span style="color: #888888;"># Update both configs, scripts and public/javascripts from Rails</span>
rake rails<span style="color: #aa6600;">:update</span><span style="color: #aa6600;">:configs</span>
<span style="color: #888888;"># Update config/boot.rb from your current rails install</span>
rake rails<span style="color: #aa6600;">:update</span><span style="color: #aa6600;">:javascripts</span>
<span style="color: #888888;"># Update your javascripts from your current rails install</span>
rake rails<span style="color: #aa6600;">:update</span><span style="color: #aa6600;">:scripts
</span><span style="color: #888888;"># Add new scripts to the application script/ directory</span>
rake remote_exec
<span style="color: #888888;"># Execute a specific action using the release manager</span>
rake rollback
<span style="color: #888888;"># Rollback to the release before the current release in production</span>
rake show_deploy_tasks
<span style="color: #888888;"># Enumerate all available deployment tasks</span>
rake stats
<span style="color: #888888;"># Report code statistics (KLOCs, etc) from the application</span>
rake test
<span style="color: #888888;"># Test all units and functionals</span>
rake test<span style="color: #aa6600;">:functionals</span>                 <span style="color: #888888;">
# Run tests for functionalsdb:test:prepare</span>
rake test<span style="color: #aa6600;">:integration</span>
<span style="color: #888888;"># Run tests for integrationdb:test:prepare</span>
rake test<span style="color: #aa6600;">:plugins</span>                     <span style="color: #888888;">
# Run tests for pluginsenvironment</span>
rake test<span style="color: #aa6600;">:recent</span>
<span style="color: #888888;"># Run tests for recentdb:test:prepare</span>
rake test<span style="color: #aa6600;">:uncommitted</span>                 <span style="color: #888888;">
# Run tests for uncommitteddb:test:prepare</span>
rake test<span style="color: #aa6600;">:units</span>
<span style="color: #888888;"># Run tests for unitsdb:test:prepare</span>
rake tmp<span style="color: #aa6600;">:cache</span><span style="color: #aa6600;">:clear</span>
<span style="color: #888888;"># Clears all files and directories in tmp/cache</span>
rake tmp<span style="color: #aa6600;">:clear</span>                        <span style="color: #888888;">
# Clear session, cache, and socket files from tmp/</span>
rake tmp<span style="color: #aa6600;">:create</span>                       <span style="color: #888888;">
# Creates tmp directories for sessions, cache, and sockets</span>
rake tmp<span style="color: #aa6600;">:pids</span><span style="color: #aa6600;">:clear</span>                   <span style="color: #888888;">
# Clears all files in tmp/pids</span>
rake tmp<span style="color: #aa6600;">:sessions</span><span style="color: #aa6600;">:clear</span>               <span style="color: #888888;">
# Clears all files in tmp/sessions</span>
rake tmp<span style="color: #aa6600;">:sockets</span><span style="color: #aa6600;">:clear</span>
<span style="color: #888888;"># Clears all files in tmp/sockets</span>
rake update_dialog_helper
<span style="color: #888888;"># Copies the latest dialog.js to the application's public directory</span></pre>


<!-- Begin SexyBookmarks Menu Code -->
<div class="sexy-bookmarks sexy-bookmarks-expand sexy-bookmarks-center">
<ul class="socials">
		<li class="sexy-delicious">
			<a href="http://del.icio.us/post?url=http://blog.bhushangahire.net/2010/03/25/list-of-useful-rake-tasks-for-rails/&amp;title=List+of+useful+rake+tasks+for+Rails..." rel="nofollow" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="sexy-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://blog.bhushangahire.net/2010/03/25/list-of-useful-rake-tasks-for-rails/&amp;title=List+of+useful+rake+tasks+for+Rails..." rel="nofollow" title="Digg this!">Digg this!</a>
		</li>
		<li class="sexy-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://blog.bhushangahire.net/2010/03/25/list-of-useful-rake-tasks-for-rails/&amp;title=List+of+useful+rake+tasks+for+Rails..." rel="nofollow" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="sexy-technorati">
			<a href="http://technorati.com/faves?add=http://blog.bhushangahire.net/2010/03/25/list-of-useful-rake-tasks-for-rails/" rel="nofollow" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="sexy-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://blog.bhushangahire.net/2010/03/25/list-of-useful-rake-tasks-for-rails/&amp;t=List+of+useful+rake+tasks+for+Rails..." rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="sexy-twitter">
			<a href="http://twitter.com/home?status=List+of+useful+rake+tasks+for+Rails...+-+http://blog.bhushangahire.net/2010/03/25/list-of-useful-rake-tasks-for-rails/+(via+@bhushangahire)" rel="nofollow" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="sexy-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://blog.bhushangahire.net/2010/03/25/list-of-useful-rake-tasks-for-rails/&amp;title=List+of+useful+rake+tasks+for+Rails...&amp;summary=rake%20cache%3Aclear%0D%0A%23%20Clears%20all%20cached%20pages%0D%0Arake%20db%3Abootstrap%20%20%20%20%20%20%0D%0A%23%20Loads%20a%20schema.rb%20file%20into%20the%20database%20and%20then%20loads%20the%20initial%20database%20fixtures.%0D%0Arake%20db%3Abootstrap%3Acopy_default_theme%0D%0A%23%20Copy%20default%20theme%20to%20site%20theme%0D%0Arake%20db%3Amigrate%0D%0A%23%20Migrate%20the%20database%20through%20scripts%20in%20db%2Fmigr&amp;source=eXpand yOur cReativity" rel="nofollow" title="Share this on Linkedin">Share this on Linkedin</a>
		</li>
		<li class="sexy-tumblr">
			<a href="http://www.tumblr.com/share?v=3&amp;u=http%3A%2F%2Fblog.bhushangahire.net%2F2010%2F03%2F25%2Flist-of-useful-rake-tasks-for-rails%2F&amp;t=List+of+useful+rake+tasks+for+Rails..." rel="nofollow" title="Share this on Tumblr">Share this on Tumblr</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>
<!-- End SexyBookmarks Menu Code -->

<div style='clear:both'></div>]]></content:encoded>
			<wfw:commentRss>http://blog.bhushangahire.net/2010/03/25/list-of-useful-rake-tasks-for-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Setup Capistrano to deploy Rails application on Amazon EC2 with Git</title>
		<link>http://blog.bhushangahire.net/2010/02/17/setup-capistrano-to-deploy-rails-application-on-amazon-ec2-with-git/</link>
		<comments>http://blog.bhushangahire.net/2010/02/17/setup-capistrano-to-deploy-rails-application-on-amazon-ec2-with-git/#comments</comments>
		<pubDate>Wed, 17 Feb 2010 06:22:49 +0000</pubDate>
		<dc:creator>Bhushan G Ahire</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Subversion]]></category>
		<category><![CDATA[capistrano]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ec2]]></category>
		<category><![CDATA[server]]></category>

		<guid isPermaLink="false">http://blog.bhushangahire.net/?p=155</guid>
		<description><![CDATA[
1: Create a new Rails app &#8211; we&#8217;ll call is &#8216;deploytest&#8217;
$ rails deploytest
$ cd deploytest
2: Create a local Git repository for it
$ git init
$ git add *
$ git commit -a -m 'initial commit'
$ git status
3: Create a couple of Capistrano files
$ capify .
4: Edit config/deploy.rb
# The name of your app
set :application, "deploytest"
# The directory on [...]]]></description>
			<content:encoded><![CDATA[<div class="fblike_button" style="margin: 10px 0;"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.bhushangahire.net%2F2010%2F02%2F17%2Fsetup-capistrano-to-deploy-rails-application-on-amazon-ec2-with-git%2F&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=recommend&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:450px; height:25px"></iframe></div>
<div class="post-body entry-content">1: Create a new Rails app &#8211; we&#8217;ll call is &#8216;deploytest&#8217;</p>
<pre class="prettyprint"><span class="pln">$ rails deploytest
$ cd deploytest</span></pre>
<p>2: Create a local Git repository for it</p>
<pre class="prettyprint"><span class="pln">$ git init
$ git add </span><span class="pun">*</span><span class="pln">
$ git commit </span><span class="pun">-</span><span class="pln">a </span><span class="pun">-</span><span class="pln">m </span><span class="str">'initial commit'</span><span class="pln">
$ git status</span></pre>
<p>3: Create a couple of Capistrano files</p>
<pre class="prettyprint"><span class="pln">$ capify </span><span class="pun">.</span></pre>
<p>4: Edit config/deploy.rb</p>
<pre class="prettyprint"><span class="com"># The name of your app</span><span class="pln">
</span><span class="kwd">set</span><span class="pln"> </span><span class="pun">:</span><span class="pln">application</span><span class="pun">,</span><span class="pln"> </span><span class="str">"deploytest"</span><span class="pln">
</span><span class="com"># The directory on the EC2 node that will be deployed to</span><span class="pln">
</span><span class="kwd">set</span><span class="pln"> </span><span class="pun">:</span><span class="pln">deploy_to</span><span class="pun">,</span><span class="pln"> </span><span class="str">"/var/www/apps/#{application}"</span><span class="pln">
</span><span class="com"># The type of Source Code Management system you are using</span><span class="pln">
</span><span class="kwd">set</span><span class="pln"> </span><span class="pun">:</span><span class="pln">scm</span><span class="pun">,</span><span class="pln"> </span><span class="pun">:</span><span class="pln">git
</span><span class="com"># The location of the LOCAL repository relative to the current app</span><span class="pln">
</span><span class="kwd">set</span><span class="pln"> </span><span class="pun">:</span><span class="pln">repository</span><span class="pun">,</span><span class="pln">  </span><span class="str">"."</span><span class="pln">
</span><span class="com"># The way in which files will be transferred from repository to remote host</span><span class="pln">
</span><span class="com"># If you were using a hosted github repository this would be slightly different</span><span class="pln">
</span><span class="kwd">set</span><span class="pln"> </span><span class="pun">:</span><span class="pln">deploy_via</span><span class="pun">,</span><span class="pln"> </span><span class="pun">:</span><span class="pln">copy

</span><span class="com"># The address of the remote host on EC2 (the Public DNS address)</span><span class="pln">
</span><span class="kwd">set</span><span class="pln"> </span><span class="pun">:</span><span class="pln">location</span><span class="pun">,</span><span class="pln"> </span><span class="str">"ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com"</span><span class="pln">
</span><span class="com"># setup some Capistrano roles</span><span class="pln">
role </span><span class="pun">:</span><span class="pln">app</span><span class="pun">,</span><span class="pln"> location
role </span><span class="pun">:</span><span class="pln">web</span><span class="pun">,</span><span class="pln"> location
role </span><span class="pun">:</span><span class="pln">db</span><span class="pun">,</span><span class="pln">  location</span><span class="pun">,</span><span class="pln"> </span><span class="pun">:</span><span class="pln">primary </span><span class="pun">=&gt;</span><span class="pln"> </span><span class="kwd">true</span><span class="pln">

</span><span class="com"># Set up SSH so it can connect to the EC2 node - assumes your SSH key is in ~/.ssh/id_rsa</span><span class="pln">
</span><span class="kwd">set</span><span class="pln"> </span><span class="pun">:</span><span class="pln">user</span><span class="pun">,</span><span class="pln"> </span><span class="str">"root"</span><span class="pln">
ssh_options</span><span class="pun">[:</span><span class="pln">keys</span><span class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="pun">[</span><span class="typ">File</span><span class="pun">.</span><span class="pln">join</span><span class="pun">(</span><span class="pln">ENV</span><span class="pun">[</span><span class="str">"HOME"</span><span class="pun">],</span><span class="pln"> </span><span class="str">".ssh"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"id_rsa"</span><span class="pun">)]</span><span class="pln">
</span></pre>
<p>The only account on a default EC2 instance is root. You probably want to create a second user that is responsible for your application.</p>
<p>5: Copy your SSH public key to your EC2 node</p>
<pre class="prettyprint"><span class="pln">$ scp </span><span class="pun">-</span><span class="pln">i </span><span class="pun">~</span><span class="str">/my-ec2-keypair ~/</span><span class="pun">.</span><span class="pln">ssh</span><span class="pun">/</span><span class="pln">id_rsa</span><span class="pun">.</span><span class="pln">pub root@ec2</span><span class="pun">-</span><span class="lit">xxx</span><span class="pun">-</span><span class="lit">xxx</span><span class="pun">-</span><span class="lit">xxx</span><span class="pun">-xxx</span><span class="lit">.compute</span><span class="pun">-</span><span class="lit">1.amazonaws</span><span class="pun">.</span><span class="pln">com</span><span class="pun">:</span><span class="str">/root/</span><span class="pun">.</span><span class="pln">ssh</span><span class="pun">/</span><span class="pln">authorized_keys2</span></pre>
<p>NOTE the filename authorized_keys2 &#8211; not authorized_keys!!</p>
<p>6: Setup the EC2 node for Capistrano deployment.<br />
From your LOCAL machine, not the EC2 node:</p>
<pre class="prettyprint"><span class="pln">$ cap deploy</span><span class="pun">:</span><span class="pln">setup</span></pre>
<p>7: Finally, deploy your application</p>
<pre class="prettyprint"><span class="pln">$ cap deploy</span></pre>
<p>You will see lots of output and with this dummy application some of those will report errors/warnings. Don&#8217;t worry about that for now.</p>
<p>8: Check that the Deployment was successful<br />
Connect to the EC2 node with SSH the regular way, cd to the app directory and check that everything is there. If that is all working then you are ready to deploy a real application and add custom tasks for managing the database, restarting the server etc.</p>
<p>Bear in mind that Capistrano add new &#8216;releases&#8217; of your software in separate directories and symlinks the &#8216;current&#8217; directory to the latest. So the root of your deployed application is the &#8216;current&#8217; subdirectory.</p>
<p>Hope this will help you setting up your ec2 instance with capistrano.</p></div>


<!-- Begin SexyBookmarks Menu Code -->
<div class="sexy-bookmarks sexy-bookmarks-expand sexy-bookmarks-center">
<ul class="socials">
		<li class="sexy-delicious">
			<a href="http://del.icio.us/post?url=http://blog.bhushangahire.net/2010/02/17/setup-capistrano-to-deploy-rails-application-on-amazon-ec2-with-git/&amp;title=Setup+Capistrano+to+deploy+Rails+application+on+Amazon+EC2+with+Git" rel="nofollow" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="sexy-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://blog.bhushangahire.net/2010/02/17/setup-capistrano-to-deploy-rails-application-on-amazon-ec2-with-git/&amp;title=Setup+Capistrano+to+deploy+Rails+application+on+Amazon+EC2+with+Git" rel="nofollow" title="Digg this!">Digg this!</a>
		</li>
		<li class="sexy-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://blog.bhushangahire.net/2010/02/17/setup-capistrano-to-deploy-rails-application-on-amazon-ec2-with-git/&amp;title=Setup+Capistrano+to+deploy+Rails+application+on+Amazon+EC2+with+Git" rel="nofollow" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="sexy-technorati">
			<a href="http://technorati.com/faves?add=http://blog.bhushangahire.net/2010/02/17/setup-capistrano-to-deploy-rails-application-on-amazon-ec2-with-git/" rel="nofollow" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="sexy-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://blog.bhushangahire.net/2010/02/17/setup-capistrano-to-deploy-rails-application-on-amazon-ec2-with-git/&amp;t=Setup+Capistrano+to+deploy+Rails+application+on+Amazon+EC2+with+Git" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="sexy-twitter">
			<a href="http://twitter.com/home?status=Setup+Capistrano+to+deploy+Rails+application+on+Amazon+EC2+with+Git+-+http://tr.im/SU8k+(via+@bhushangahire)" rel="nofollow" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="sexy-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://blog.bhushangahire.net/2010/02/17/setup-capistrano-to-deploy-rails-application-on-amazon-ec2-with-git/&amp;title=Setup+Capistrano+to+deploy+Rails+application+on+Amazon+EC2+with+Git&amp;summary=1%3A%20Create%20a%20new%20Rails%20app%20-%20we%27ll%20call%20is%20%27deploytest%27%0D%0A%24%20rails%20deploytest%0D%0A%24%20cd%20deploytest%0D%0A2%3A%20Create%20a%20local%20Git%20repository%20for%20it%0D%0A%24%20git%20init%0D%0A%24%20git%20add%20%2A%0D%0A%24%20git%20commit%20-a%20-m%20%27initial%20commit%27%0D%0A%24%20git%20status%0D%0A3%3A%20Create%20a%20couple%20of%20Capistrano%20files%0D%0A%24%20capify%20.%0D%0A4%3A%20Edit%20config%2Fdeploy.rb%0D%0A%23%20The%20name%20o&amp;source=eXpand yOur cReativity" rel="nofollow" title="Share this on Linkedin">Share this on Linkedin</a>
		</li>
		<li class="sexy-tumblr">
			<a href="http://www.tumblr.com/share?v=3&amp;u=http%3A%2F%2Fblog.bhushangahire.net%2F2010%2F02%2F17%2Fsetup-capistrano-to-deploy-rails-application-on-amazon-ec2-with-git%2F&amp;t=Setup+Capistrano+to+deploy+Rails+application+on+Amazon+EC2+with+Git" rel="nofollow" title="Share this on Tumblr">Share this on Tumblr</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>
<!-- End SexyBookmarks Menu Code -->

<div style='clear:both'></div>]]></content:encoded>
			<wfw:commentRss>http://blog.bhushangahire.net/2010/02/17/setup-capistrano-to-deploy-rails-application-on-amazon-ec2-with-git/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Send SMS from Ruby On Rails application using web service, SOAP API</title>
		<link>http://blog.bhushangahire.net/2010/01/15/send-sms-from-ruby-on-rails-application-using-web-service-soap-api/</link>
		<comments>http://blog.bhushangahire.net/2010/01/15/send-sms-from-ruby-on-rails-application-using-web-service-soap-api/#comments</comments>
		<pubDate>Fri, 15 Jan 2010 11:09:19 +0000</pubDate>
		<dc:creator>Bhushan G Ahire</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[mailserve]]></category>
		<category><![CDATA[sms]]></category>
		<category><![CDATA[soap]]></category>

		<guid isPermaLink="false">http://blog.bhushangahire.net/2010/01/15/send-sms-from-ruby-on-rails-application-using-web-service-soap-api/</guid>
		<description><![CDATA[
SOAP4R is a Ruby library for accessing Web Services via SOAP. Recently I had a chance to explore SOAP4R. Here&#8217;s how to get started with it.

Installation
Although Ruby 1.8.x comes with SOAP4R in its standard library, it is an old, buggy version. I highly recommend using the latest gem (1.5.8 as of the this update). It [...]]]></description>
			<content:encoded><![CDATA[<div class="fblike_button" style="margin: 10px 0;"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.bhushangahire.net%2F2010%2F01%2F15%2Fsend-sms-from-ruby-on-rails-application-using-web-service-soap-api%2F&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=recommend&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:450px; height:25px"></iframe></div>
<p class="entry">SOAP4R is a Ruby library for accessing Web Services via SOAP. Recently I had a chance to explore SOAP4R. Here&#8217;s how to get started with it.</p>
<p class="entry"><span id="more-3"></span></p>
<h3 class="entry">Installation</h3>
<p class="entry">Although Ruby 1.8.x comes with SOAP4R in its standard library, it is an old, buggy version. I <strong>highly</strong> recommend using the latest gem (1.5.8 as of the this update). It has one dependency, httpclient.</p>
<p class="entry"><code>gem install soap4r --include-dependencies</code></p>
<h3 class="entry">Service</h3>
<p class="entry">There are many services available to send SMS but I prefer to use,</p>
<h4 class="entry">MailServe-SMS</h4>
<p class="entry"><a href="http://qlc.in/ms/overview.htm" target="_blank">MailServe-SMS</a>, text messaging service, is everything you need for fast, no-frills, no-fuss text messaging. A quick and easy way to send SMS.</p>
<p class="entry">Let’s explore these further.</p>
<h3 class="entry">Method 1: Read the WSDL at run-time</h3>
<p class="entry">&#160;</p>
<div class="entry">
<pre class="ruby"><span style="font-weight: bold; color: rgb(204,0,102)">require</span> <span style="color: rgb(153,102,0)">&quot;soap/wsdlDriver&quot;</span>
wsdl = <span style="color: rgb(153,102,0)">&quot;http://sms.qlc.co.in/smsapi.wsdl&quot;</span>
driver = <span style="font-weight: bold; color: rgb(102,102,255)">SOAP::WSDLDriverFactory</span>.<span style="color: rgb(153,0,204)">new</span><span style="font-weight: bold; color: rgb(0,102,0)">(</span>wsdl<span style="font-weight: bold; color: rgb(0,102,0)">)</span>.<span style="color: rgb(153,0,204)">create_rpc_drive</span></pre>
</div>
<p class="entry">&#160;</p>
<p class="entry">A single call to a driver factory reads the WSDL file, and creates a driver class for you to use, complete with the methods defined by the service. What if your service requires authentication? The driver inherits methods from httpclient, so you can specify its options as you would for httpclient:</p>
<p class="entry">Once driver is get initialised you need to call SMS sending API i.e. <b>SendSMSRequest</b>.</p>
<div class="entry">
<pre class="ruby">driver.<span style="color: rgb(153,0,204)">SendSMSRequest</span><span style="font-weight: bold; color: rgb(0,102,0)">(</span><span style="color: rgb(153,102,0)">&quot;username&quot;, <span style="color: rgb(153,102,0)">&quot;password&quot;</span>, <span style="color: rgb(153,102,0)">&quot;sender_no&quot;</span>, <span style="color: rgb(153,102,0)">&quot;from_no&quot;</span>, <span style="color: rgb(153,102,0)">&quot;message&quot;</span></span><span style="font-weight: bold; color: rgb(0,102,0)">)</span></pre>
</div>
<p class="entry">&#160;</p>
<p class="entry">Once This will return you response <strong>200 SMS sent successfully </strong>on success else if the information submitted was wrong then <strong>500 Information submitted was incomplete</strong>.</p>
<p class="entry">&#160;</p>
<h3 class="entry">Method 2: Generate classes from WSDL</h3>
<p class="entry">SOAP4R installs a command-line utility called &#8216;wsdl2ruby&#8217; which can generate a client or server.</p>
<p class="entry"><strong>Coming soon…..</strong></p>


<!-- Begin SexyBookmarks Menu Code -->
<div class="sexy-bookmarks sexy-bookmarks-expand sexy-bookmarks-center">
<ul class="socials">
		<li class="sexy-delicious">
			<a href="http://del.icio.us/post?url=http://blog.bhushangahire.net/2010/01/15/send-sms-from-ruby-on-rails-application-using-web-service-soap-api/&amp;title=Send+SMS+from+Ruby+On+Rails+application+using+web+service%2C+SOAP+API" rel="nofollow" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="sexy-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://blog.bhushangahire.net/2010/01/15/send-sms-from-ruby-on-rails-application-using-web-service-soap-api/&amp;title=Send+SMS+from+Ruby+On+Rails+application+using+web+service%2C+SOAP+API" rel="nofollow" title="Digg this!">Digg this!</a>
		</li>
		<li class="sexy-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://blog.bhushangahire.net/2010/01/15/send-sms-from-ruby-on-rails-application-using-web-service-soap-api/&amp;title=Send+SMS+from+Ruby+On+Rails+application+using+web+service%2C+SOAP+API" rel="nofollow" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="sexy-technorati">
			<a href="http://technorati.com/faves?add=http://blog.bhushangahire.net/2010/01/15/send-sms-from-ruby-on-rails-application-using-web-service-soap-api/" rel="nofollow" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="sexy-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://blog.bhushangahire.net/2010/01/15/send-sms-from-ruby-on-rails-application-using-web-service-soap-api/&amp;t=Send+SMS+from+Ruby+On+Rails+application+using+web+service%2C+SOAP+API" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="sexy-twitter">
			<a href="http://twitter.com/home?status=Send+SMS+from+Ruby+On+Rails+application+using+web+service%2C+SOAP+API+-+http://tr.im/SU8m+(via+@bhushangahire)" rel="nofollow" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="sexy-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://blog.bhushangahire.net/2010/01/15/send-sms-from-ruby-on-rails-application-using-web-service-soap-api/&amp;title=Send+SMS+from+Ruby+On+Rails+application+using+web+service%2C+SOAP+API&amp;summary=SOAP4R%20is%20a%20Ruby%20library%20for%20accessing%20Web%20Services%20via%20SOAP.%20Recently%20I%20had%20a%20chance%20to%20explore%20SOAP4R.%20Here%27s%20how%20to%20get%20started%20with%20it.%20%20%20%20Installation%20%20Although%20Ruby%201.8.x%20comes%20with%20SOAP4R%20in%20its%20standard%20library%2C%20it%20is%20an%20old%2C%20buggy%20version.%20I%20highly%20recommend%20using%20the%20latest%20gem%20%281.5.8%20as%20o&amp;source=eXpand yOur cReativity" rel="nofollow" title="Share this on Linkedin">Share this on Linkedin</a>
		</li>
		<li class="sexy-tumblr">
			<a href="http://www.tumblr.com/share?v=3&amp;u=http%3A%2F%2Fblog.bhushangahire.net%2F2010%2F01%2F15%2Fsend-sms-from-ruby-on-rails-application-using-web-service-soap-api%2F&amp;t=Send+SMS+from+Ruby+On+Rails+application+using+web+service%2C+SOAP+API" rel="nofollow" title="Share this on Tumblr">Share this on Tumblr</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>
<!-- End SexyBookmarks Menu Code -->

<div style='clear:both'></div>]]></content:encoded>
			<wfw:commentRss>http://blog.bhushangahire.net/2010/01/15/send-sms-from-ruby-on-rails-application-using-web-service-soap-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get location from IP address in Ruby On Rails for free&#8230;.</title>
		<link>http://blog.bhushangahire.net/2009/05/20/get-location-from-ip-address-in-ruby-on-rails-for-free/</link>
		<comments>http://blog.bhushangahire.net/2009/05/20/get-location-from-ip-address-in-ruby-on-rails-for-free/#comments</comments>
		<pubDate>Wed, 20 May 2009 09:42:07 +0000</pubDate>
		<dc:creator>Bhushan Ahire</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ip address]]></category>
		<category><![CDATA[location]]></category>

		<guid isPermaLink="false">http://blog.bhushangahire.net/?p=116</guid>
		<description><![CDATA[

Find below the code for finding location from IP address using IP location tools.
require 'net/http'
require 'rexml/document'
include REXML

class MapsController < ApplicationController
	def index
		@location = locateIp()

	end

	def locateIp
		ip = request.remote_ip
		ips = ip.to_s
		url = "http://iplocationtools.com/ip_query.php?ip="+ips

		xml_data = Net::HTTP.get_response(URI.parse(url)).body

                xmldoc = REXML::Document.new(xml_data)

		# Now get the root element
		root [...]]]></description>
			<content:encoded><![CDATA[<div class="fblike_button" style="margin: 10px 0;"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.bhushangahire.net%2F2009%2F05%2F20%2Fget-location-from-ip-address-in-ruby-on-rails-for-free%2F&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=recommend&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:450px; height:25px"></iframe></div>
<div class="snap_preview">
<p>Find below the code for finding location from IP address using <a href="http://www.iplocationtools.com/">IP location tools</a>.</p>
<pre>require 'net/http'
require 'rexml/document'
include REXML

class MapsController < ApplicationController
	def index
		@location = locateIp()

	end

	def locateIp
		ip = request.remote_ip
		ips = ip.to_s
		url = "http://iplocationtools.com/ip_query.php?ip="+ips

		xml_data = Net::HTTP.get_response(URI.parse(url)).body

                xmldoc = REXML::Document.new(xml_data)

		# Now get the root element
		root = xmldoc.root
		city = ""
		regionName = ""
		countryName = ""

		# This will take country name...
		xmldoc.elements.each("Response/CountryName") {
		|e| countryName << e.text
	    }

		# Now get city name...
		xmldoc.elements.each("Response/City") {
   		|e| city << e.text
	    }

		# This will take regionName...
		xmldoc.elements.each("Response/RegionName") {
   		|e| regionName << e.text
	    }

     	ipLocation = city +", "+regionName+", "+countryName

	 return ipLocation
   end #end of method locateIp

end</pre>
</div>


<!-- Begin SexyBookmarks Menu Code -->
<div class="sexy-bookmarks sexy-bookmarks-expand sexy-bookmarks-center">
<ul class="socials">
		<li class="sexy-delicious">
			<a href="http://del.icio.us/post?url=http://blog.bhushangahire.net/2009/05/20/get-location-from-ip-address-in-ruby-on-rails-for-free/&amp;title=Get+location+from+IP+address+in+Ruby+On+Rails+for+free...." rel="nofollow" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="sexy-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://blog.bhushangahire.net/2009/05/20/get-location-from-ip-address-in-ruby-on-rails-for-free/&amp;title=Get+location+from+IP+address+in+Ruby+On+Rails+for+free...." rel="nofollow" title="Digg this!">Digg this!</a>
		</li>
		<li class="sexy-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://blog.bhushangahire.net/2009/05/20/get-location-from-ip-address-in-ruby-on-rails-for-free/&amp;title=Get+location+from+IP+address+in+Ruby+On+Rails+for+free...." rel="nofollow" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="sexy-technorati">
			<a href="http://technorati.com/faves?add=http://blog.bhushangahire.net/2009/05/20/get-location-from-ip-address-in-ruby-on-rails-for-free/" rel="nofollow" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="sexy-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://blog.bhushangahire.net/2009/05/20/get-location-from-ip-address-in-ruby-on-rails-for-free/&amp;t=Get+location+from+IP+address+in+Ruby+On+Rails+for+free...." rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="sexy-twitter">
			<a href="http://twitter.com/home?status=Get+location+from+IP+address+in+Ruby+On+Rails+for+free....+-+http://tr.im/SU8t+(via+@bhushangahire)" rel="nofollow" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="sexy-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://blog.bhushangahire.net/2009/05/20/get-location-from-ip-address-in-ruby-on-rails-for-free/&amp;title=Get+location+from+IP+address+in+Ruby+On+Rails+for+free....&amp;summary=Find%20below%20the%20code%20for%20finding%20location%20from%20IP%20address%20using%20IP%20location%20tools.%0Arequire%20%27net%2Fhttp%27%0Arequire%20%27rexml%2Fdocument%27%0Ainclude%20REXML%0A%0Aclass%20MapsController%20%3C%20ApplicationController%0A%09def%20index%0A%09%09%40location%20%3D%20locateIp%28%29%0A%0A%09end%0A%0A%09def%20locateIp%0A%09%09ip%20%3D%20request.remote_ip%0A%09%09ips%20%3D%20ip.to_s%0A%09%09url%20%3D%20%22http%3A%2F%2F&amp;source=eXpand yOur cReativity" rel="nofollow" title="Share this on Linkedin">Share this on Linkedin</a>
		</li>
		<li class="sexy-tumblr">
			<a href="http://www.tumblr.com/share?v=3&amp;u=http%3A%2F%2Fblog.bhushangahire.net%2F2009%2F05%2F20%2Fget-location-from-ip-address-in-ruby-on-rails-for-free%2F&amp;t=Get+location+from+IP+address+in+Ruby+On+Rails+for+free...." rel="nofollow" title="Share this on Tumblr">Share this on Tumblr</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>
<!-- End SexyBookmarks Menu Code -->

<div style='clear:both'></div>]]></content:encoded>
			<wfw:commentRss>http://blog.bhushangahire.net/2009/05/20/get-location-from-ip-address-in-ruby-on-rails-for-free/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Generating ZIP files via Ruby on Rails using rubyzip</title>
		<link>http://blog.bhushangahire.net/2009/03/03/generating-zip-files-via-ruby-on-rails-using-rubyzip/</link>
		<comments>http://blog.bhushangahire.net/2009/03/03/generating-zip-files-via-ruby-on-rails-using-rubyzip/#comments</comments>
		<pubDate>Tue, 03 Mar 2009 09:33:44 +0000</pubDate>
		<dc:creator>Bhushan Ahire</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[rubyzip]]></category>

		<guid isPermaLink="false">http://blog.bhushangahire.net/?p=65</guid>
		<description><![CDATA[

gem install rubyzip
Then, in the model that I’m using to generate the zip bundles, I add a couple “require” statements:
require 'zip/zip'
require 'zip/zipfilesystem'

class Album < ActiveRecord::Base
  (...)
end
Next, I added a class method called bundle, which when called will use rubygem to generate the zip file. Note: the “permalink” attributes of Album and Artist are populated [...]]]></description>
			<content:encoded><![CDATA[<div class="fblike_button" style="margin: 10px 0;"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.bhushangahire.net%2F2009%2F03%2F03%2Fgenerating-zip-files-via-ruby-on-rails-using-rubyzip%2F&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=recommend&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:450px; height:25px"></iframe></div>
<div class="entry">
<pre>gem install rubyzip</pre>
<p>Then, in the model that I’m using to generate the zip bundles, I add a couple “require” statements:</p>
<pre>require 'zip/zip'
require 'zip/zipfilesystem'

class Album < ActiveRecord::Base
  (...)
end</pre>
<p>Next, I added a class method called bundle, which when called will use rubygem to generate the zip file. Note: the “permalink” attributes of Album and Artist are populated when an object of those models is created. I’m using them because it makes for nice filenames, too.</p>
<pre># create a zipped archive file of all the tracks in an album
def bundle(name = self.permalink, set = self.artist.permalink)
   bundle_filename = "#{RAILS_ROOT}/public/uploads/#{set}-#{name}.zip"

   # check to see if the file exists already, and if it does, delete it.
   if File.file?(bundle_filename)
     File.delete(bundle_filename)
   end

   # set the bundle_filename attribute of this object
   self.bundle_filename = "/uploads/#{set}-#{name}.zip"

   # open or create the zip file
   Zip::ZipFile.open(bundle_filename, Zip::ZipFile::CREATE) {
     |zipfile|
     # collect the album's tracks
     self.tracks.collect {
       |track|
         # add each track to the archive, names using the track's attributes
         zipfile.add( "#{set}/#{track.num}-#{track.filename}", "#{RAILS_ROOT}/public#{track.public_filename}")
       }
   }

   # set read permissions on the file
   File.chmod(0644, bundle_filename)

   # save the object
   self.save
end
</pre>
<p>Next I added a method in my controller:</p>
<pre>def create_bundle
   album = Album.find(params[:id])
   album.bundle
   flash[:notice] = 'Album was successfully zipped.'
   redirect_to album_url(album.artist, album)
end</pre>
<p>And edit my routes.rb accordingly:</p>
<pre>map.create_bundle 'create_bundle/:id', :controller => 'albums', :action => 'create_bundle'</pre>
<p>Now it’s just a matter of creating a link in the view for the admin to click whenever he/she wants to generate the zip file: </p>
<pre><%= link_to('Create Album Zip', create_bundle_path(@album)) %></pre>
<p>…and a link for the user to click to download the zip file if it exists:</p>
<pre><% unless @album.bundle_filename.nil? %>
<div id="grid_right">
    <%= link_to "Download Album Zip", @album.bundle_filename %>
  </div>

<% end %></pre>
<p>That’s it! Refer to the <a href="http://rubyzip.sourceforge.net/">rubyzip documentation</a> for more info.</p>
</p></div>


<!-- Begin SexyBookmarks Menu Code -->
<div class="sexy-bookmarks sexy-bookmarks-expand sexy-bookmarks-center">
<ul class="socials">
		<li class="sexy-delicious">
			<a href="http://del.icio.us/post?url=http://blog.bhushangahire.net/2009/03/03/generating-zip-files-via-ruby-on-rails-using-rubyzip/&amp;title=Generating+ZIP+files+via+Ruby+on+Rails+using+rubyzip" rel="nofollow" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="sexy-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://blog.bhushangahire.net/2009/03/03/generating-zip-files-via-ruby-on-rails-using-rubyzip/&amp;title=Generating+ZIP+files+via+Ruby+on+Rails+using+rubyzip" rel="nofollow" title="Digg this!">Digg this!</a>
		</li>
		<li class="sexy-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://blog.bhushangahire.net/2009/03/03/generating-zip-files-via-ruby-on-rails-using-rubyzip/&amp;title=Generating+ZIP+files+via+Ruby+on+Rails+using+rubyzip" rel="nofollow" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="sexy-technorati">
			<a href="http://technorati.com/faves?add=http://blog.bhushangahire.net/2009/03/03/generating-zip-files-via-ruby-on-rails-using-rubyzip/" rel="nofollow" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="sexy-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://blog.bhushangahire.net/2009/03/03/generating-zip-files-via-ruby-on-rails-using-rubyzip/&amp;t=Generating+ZIP+files+via+Ruby+on+Rails+using+rubyzip" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="sexy-twitter">
			<a href="http://twitter.com/home?status=Generating+ZIP+files+via+Ruby+on+Rails+using+rubyzip+-+http://tr.im/SVHY+(via+@bhushangahire)" rel="nofollow" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="sexy-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://blog.bhushangahire.net/2009/03/03/generating-zip-files-via-ruby-on-rails-using-rubyzip/&amp;title=Generating+ZIP+files+via+Ruby+on+Rails+using+rubyzip&amp;summary=%0Agem%20install%20rubyzip%0AThen%2C%20in%20the%20model%20that%20I%E2%80%99m%20using%20to%20generate%20the%20zip%20bundles%2C%20I%20add%20a%20couple%20%E2%80%9Crequire%E2%80%9D%20statements%3A%0Arequire%20%27zip%2Fzip%27%0Arequire%20%27zip%2Fzipfilesystem%27%0A%0Aclass%20Album%20%3C%20ActiveRecord%3A%3ABase%0A%20%20%28...%29%0Aend%0ANext%2C%20I%20added%20a%20class%20method%20called%20bundle%2C%20which%20when%20called%20will%20use%20rubygem%20to&amp;source=eXpand yOur cReativity" rel="nofollow" title="Share this on Linkedin">Share this on Linkedin</a>
		</li>
		<li class="sexy-tumblr">
			<a href="http://www.tumblr.com/share?v=3&amp;u=http%3A%2F%2Fblog.bhushangahire.net%2F2009%2F03%2F03%2Fgenerating-zip-files-via-ruby-on-rails-using-rubyzip%2F&amp;t=Generating+ZIP+files+via+Ruby+on+Rails+using+rubyzip" rel="nofollow" title="Share this on Tumblr">Share this on Tumblr</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>
<!-- End SexyBookmarks Menu Code -->

<div style='clear:both'></div>]]></content:encoded>
			<wfw:commentRss>http://blog.bhushangahire.net/2009/03/03/generating-zip-files-via-ruby-on-rails-using-rubyzip/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>New features in Rails 2</title>
		<link>http://blog.bhushangahire.net/2008/05/13/new-features-in-rails-2/</link>
		<comments>http://blog.bhushangahire.net/2008/05/13/new-features-in-rails-2/#comments</comments>
		<pubDate>Tue, 13 May 2008 03:32:38 +0000</pubDate>
		<dc:creator>Bhushan Ahire</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[rails 2]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.bhushangahire.net/?p=42</guid>
		<description><![CDATA[

                Today i was reading about the new features of Rails 2, there are a lot of changes, for an overview you can checkout the official rails blog announcement. Here is a little list of major changes and new features:

ActionMailer::Base.server_settings [...]]]></description>
			<content:encoded><![CDATA[<div class="fblike_button" style="margin: 10px 0;"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.bhushangahire.net%2F2008%2F05%2F13%2Fnew-features-in-rails-2%2F&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=recommend&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:450px; height:25px"></iframe></div>
<div class="serendipity_entry_body">
                Today i was reading about the <b>new features</b> of <b>Rails 2</b>, there are a lot of changes, for an overview you can checkout <a href="http://weblog.rubyonrails.org/2007/12/7/rails-2-0-it-s-done">the official rails blog announcement</a>. Here is a little list of major changes and new features:
<ul>
<li><a href="http://ryandaigle.com/articles/2007/1/31/what-s-new-in-edge-rails-actionmailer-base-server_settings-deprecated">ActionMailer::Base.server_settings Deprecated</a></li>
<li><a href="http://ryandaigle.com/articles/2007/1/26/what-s-new-in-edge-rails-1-month-from_now-no_longer-effed">1.month.from_now.no_longer.effed </a></li>
<li><a href="http://ryandaigle.com/articles/2007/2/26/what-s-new-in-edge-rails-source-code-annotations">Source Code Annotations</a></li>
<li><a href="http://ryandaigle.com/articles/2007/2/26/what-s-new-in-edge-rails-quick-way-to-include-all-helpers-in-your-controllers">A Better Way to Access Your Helpers</a></li>
<li><a href="http://ryandaigle.com/articles/2007/2/23/what-s-new-in-edge-rails-stop-littering-your-evnrionment-rb-with-custom-initializations">Stop Littering In Your Environment File</a></li>
<li><a href="http://ryandaigle.com/articles/2007/2/22/what-s-new-in-edge-rails-activerecord-caching-provided-in-actions">ActiveRecord Caching Provided in Actions </a></li>
<li><a href="http://ryandaigle.com/articles/2007/2/21/what-s-new-in-edge-rails-cookie-based-sessions">Cookie Based Sessions are the New Default</a></li>
<li><a href="http://ryandaigle.com/articles/2007/2/21/what-s-new-in-edge-rails-expanded-caching-scope">Expanded Caching Scope</a></li>
<li><a href="http://ryandaigle.com/articles/2007/2/21/what-s-new-in-edge-rails-rhtml-and-rxml-to-die-a-slow-and-painful-death">.rhtml and .rxml to Die a Slow and Painful Death</a></li>
<li><a href="http://ryandaigle.com/articles/2007/2/15/what-s-new-in-edge-rails-mime-type-convenience-methods">Mime::Type Convenience Methods</a></li>
<li><a href="http://ryandaigle.com/articles/2007/2/7/what-s-new-in-edge-rails-activerecord-explicit-caching">ActiveRecord Explicit Caching</a></li>
<li><a href="http://ryandaigle.com/articles/2007/3/29/what-s-new-in-edge-rails-restful-routes-get-a-new-custom-delimiter">RESTful Routes Get a New Custom Delimiter</a></li>
<li><a href="http://ryandaigle.com/articles/2007/3/20/what-s-new-in-edge-rails-object-transactions-are-out">Object Transactions Are Out</a></li>
<li><a href="http://ryandaigle.com/articles/2007/4/26/what-s-new-in-edge-rails-activeresource-gets-custom-methods">ActiveResource Gets Custom Methods</a></li>
<li><a href="http://ryandaigle.com/articles/2007/4/25/what-s-new-in-edge-rails-render-now-70-more-betterer">render Now 70% More Betterer</a></li>
<li><a href="http://ryandaigle.com/articles/2007/4/13/what-s-new-in-edge-rails-a-more-flexible-to_xml">A More Flexible to_xml</a></li>
<li><a href="http://ryandaigle.com/articles/2007/5/29/what-s-new-in-edge-rails-new-database-rake-tasks">New Database Rake Tasks</a></li>
<li><a href="http://ryandaigle.com/articles/2007/5/29/what-s-new-in-edge-rails-validates_numericality_of-gets-pimped">validates_numericality_of Gets Pimped </a></li>
<li><a href="http://ryandaigle.com/articles/2007/5/7/what-s-new-in-edge-rails-activeresource-finder-update">ActiveResource Finder Update and Custom Headers</a></li>
<li><a href="http://ryandaigle.com/articles/2007/5/6/what-s-new-in-edge-rails-restful-routing-updates">RESTful Routing Updates</a></li>
<li><a href="http://ryandaigle.com/articles/2007/5/6/what-s-new-in-edge-rails-bringin-sexy-back">Bringin’ Sexy Back</a></li>
<li><a href="http://ryandaigle.com/articles/2007/6/11/what-s-new-in-edge-rails-no-more-conventional-pagination">No More (conventional) Pagination</a></li>
<li><a href="http://ryandaigle.com/articles/2007/6/5/what-s-new-in-edge-rails-collection-fixtures">Collection Fixtures</a></li>
<li><a href="http://ryandaigle.com/articles/2007/7/2/what-s-new-in-edge-rails-use-rake-to-list-your-routes">Use Rake to List Your Routes</a></li>
<li><a href="http://ryandaigle.com/articles/2007/8/3/what-s-new-in-edge-rails-partials-get-layouts">Partials Get Layouts</a></li>
<li><a href="http://ryandaigle.com/articles/2007/9/30/what-s-new-in-edge-rails-your-db-adapter-may-have-left-the-building">Your DB Adapter May Have Left the Building</a></li>
<li><a href="http://ryandaigle.com/articles/2007/9/25/what-s-new-in-edge-rails-logging-gets-a-speed-bump">Logging Gets a Little Snappier</a></li>
<li><a href="http://ryandaigle.com/articles/2007/9/24/what-s-new-in-edge-rails-better-cross-site-request-forging-prevention">Better Cross-Site Request Forging Prevention </a></li>
<li><a href="http://ryandaigle.com/articles/2007/9/24/what-s-new-in-edge-rails-better-exception-handling">Better Exception Handling</a></li>
<li><a href="http://ryandaigle.com/articles/2007/9/24/what-s-new-in-edge-rails-specify-plugin-load-order">Specify Plugin Load Ordering</a></li>
<li><a href="http://ryandaigle.com/articles/2007/9/5/what-s-new-in-edge-rails-validations-now-allow_blank">Validations Now :allow_blank</a></li>
<li><a href="http://ryandaigle.com/articles/2007/10/26/what-s-new-in-edge-rails-fixtures-just-got-a-whole-lot-easier">Fixtures Just Got a Whole Lot Easier</a></li>
<li><a href="http://ryandaigle.com/articles/2007/10/22/what-s-new-in-edge-rails-filters-get-tweaked">Filters get Tweaked</a></li>
<li><a href="http://ryandaigle.com/articles/2007/11/18/what-s-new-in-edge-rails-pre-environment-load-hook">Pre-Environment Load Hook</a></li>
</ul></div>


<!-- Begin SexyBookmarks Menu Code -->
<div class="sexy-bookmarks sexy-bookmarks-expand sexy-bookmarks-center">
<ul class="socials">
		<li class="sexy-delicious">
			<a href="http://del.icio.us/post?url=http://blog.bhushangahire.net/2008/05/13/new-features-in-rails-2/&amp;title=New+features+in+Rails+2" rel="nofollow" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="sexy-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://blog.bhushangahire.net/2008/05/13/new-features-in-rails-2/&amp;title=New+features+in+Rails+2" rel="nofollow" title="Digg this!">Digg this!</a>
		</li>
		<li class="sexy-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://blog.bhushangahire.net/2008/05/13/new-features-in-rails-2/&amp;title=New+features+in+Rails+2" rel="nofollow" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="sexy-technorati">
			<a href="http://technorati.com/faves?add=http://blog.bhushangahire.net/2008/05/13/new-features-in-rails-2/" rel="nofollow" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="sexy-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://blog.bhushangahire.net/2008/05/13/new-features-in-rails-2/&amp;t=New+features+in+Rails+2" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="sexy-twitter">
			<a href="http://twitter.com/home?status=New+features+in+Rails+2+-+http://blog.bhushangahire.net/2008/05/13/new-features-in-rails-2/+(via+@bhushangahire)" rel="nofollow" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="sexy-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://blog.bhushangahire.net/2008/05/13/new-features-in-rails-2/&amp;title=New+features+in+Rails+2&amp;summary=%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Today%20i%20was%20reading%20about%20the%20new%20features%20of%20Rails%202%2C%20there%20are%20a%20lot%20of%20changes%2C%20for%20an%20overview%20you%20can%20checkout%20the%20official%20rails%20blog%20announcement.%20Here%20is%20a%20little%20list%20of%20major%20changes%20and%20new%20features%3AActionMailer%3A%3ABase.server_settings%20Deprecated1.month.from_now.no_longer.e&amp;source=eXpand yOur cReativity" rel="nofollow" title="Share this on Linkedin">Share this on Linkedin</a>
		</li>
		<li class="sexy-tumblr">
			<a href="http://www.tumblr.com/share?v=3&amp;u=http%3A%2F%2Fblog.bhushangahire.net%2F2008%2F05%2F13%2Fnew-features-in-rails-2%2F&amp;t=New+features+in+Rails+2" rel="nofollow" title="Share this on Tumblr">Share this on Tumblr</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>
<!-- End SexyBookmarks Menu Code -->

<div style='clear:both'></div>]]></content:encoded>
			<wfw:commentRss>http://blog.bhushangahire.net/2008/05/13/new-features-in-rails-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby thumbnail generator</title>
		<link>http://blog.bhushangahire.net/2008/03/18/ruby-thumbnail-generator/</link>
		<comments>http://blog.bhushangahire.net/2008/03/18/ruby-thumbnail-generator/#comments</comments>
		<pubDate>Tue, 18 Mar 2008 10:55:12 +0000</pubDate>
		<dc:creator>Bhushan Ahire</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[images]]></category>

		<guid isPermaLink="false">http://www.bhushangahire.net/2008/03/18/ruby-thumbnail-generator/</guid>
		<description><![CDATA[
Ruby thumbnail generator is simple script which is ideal to use in your Ruby on Rails application to quickly generate thumbnails of any proportions. Just set width and height and get the image.
1. copy following code into /controllers/thumb_controller.rb
2. edit /config/routes.rb and add this line:map.connect &#8220;thumb/*specs&#8221;, :controller =&#62; &#8220;thumb&#8221;, :action =&#62; &#8220;index&#8221;
3. create directory /imagelib/ in [...]]]></description>
			<content:encoded><![CDATA[<div class="fblike_button" style="margin: 10px 0;"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.bhushangahire.net%2F2008%2F03%2F18%2Fruby-thumbnail-generator%2F&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=recommend&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:450px; height:25px"></iframe></div>
<div class="post-body">Ruby thumbnail generator is simple script which is ideal to use in your Ruby on Rails application to quickly generate thumbnails of any proportions. Just set width and height and get the image.</p>
<p>1. copy following code into /controllers/thumb_controller.rb</p>
<p>2. edit /config/routes.rb and add this line:<br />map.connect &#8220;thumb/*specs&#8221;, :controller =&gt; &#8220;thumb&#8221;, :action =&gt; &#8220;index&#8221;</p>
<p>3. create directory /imagelib/ in RoR&#8217;s /public/ directory and<br />/image_cache/ inside /imagelib/ directoory</p>
<p>Now you can call /thumb/photo.jpg?w=400&amp;h=350 and you will<br />see resized picture &#8220;photo.jpg&#8221;. photo.jpg should be stored in<br />/public/imagelib/ directory. Off course you can change directory<br />structure if you wish just don&#8217;t forget to edit thumb_controller.rb<br />than.</p>
<p>visit www.cleverleap.com/ruby-thumbnail-generator/ <br />for more information</p>
<pre><span class="keyword">class </span><span class="class">ThumbController</span> <span class="punct">&lt;</span> <span class="constant">ApplicationController</span>

  <span class="ident">require</span> <span class="punct">'</span><span class="string">gd2</span><span class="punct">'</span>
  <span class="ident">include</span> <span class="constant">GD2</span>

  <span class="keyword">def </span><span class="method">index</span>

    <span class="ident">path</span> <span class="punct">=</span> <span class="punct">"</span><span class="string">imagelib/</span><span class="punct">"</span>    <span class="comment"># default image library directory</span>

    <span class="ident">widthx</span> <span class="punct">=</span> <span class="number">500</span>          <span class="comment"># default width of generated image</span>
    <span class="ident">heightx</span> <span class="punct">=</span> <span class="number">500</span>         <span class="comment"># default height of generated image</span>

    <span class="keyword">if</span> <span class="ident">params</span><span class="punct">[</span><span class="symbol">:w</span><span class="punct">]</span> <span class="keyword">then</span> <span class="ident">widthx</span> <span class="punct">=</span> <span class="ident">params</span><span class="punct">[</span><span class="symbol">:w</span><span class="punct">].</span><span class="ident">to_i</span>

    <span class="keyword">end</span>

    <span class="keyword">if</span> <span class="ident">params</span><span class="punct">[</span><span class="symbol">:h</span><span class="punct">]</span> <span class="keyword">then</span> <span class="ident">heightx</span> <span class="punct">=</span> <span class="ident">params</span><span class="punct">[</span><span class="symbol">:h</span><span class="punct">].</span><span class="ident">to_i</span>

    <span class="keyword">end</span>

    <span class="ident">filepath</span> <span class="punct">=</span> <span class="ident">path</span> <span class="punct">+</span> <span class="ident">params</span><span class="punct">[</span><span class="symbol">:specs</span><span class="punct">].</span><span class="ident">join</span><span class="punct">("</span><span class="string">/</span><span class="punct">")</span>    <span class="comment"># Path to file</span>

    <span class="ident">format</span> <span class="punct">=</span> <span class="ident">filepath</span><span class="punct">.</span><span class="ident">split</span><span class="punct">("</span><span class="string">.</span><span class="punct">").</span><span class="ident">last</span>             <span class="comment"># Format - extension</span>

    <span class="ident">filename</span> <span class="punct">=</span> <span class="ident">params</span><span class="punct">[</span><span class="symbol">:specs</span><span class="punct">].</span><span class="ident">last</span><span class="punct">.</span><span class="ident">split</span><span class="punct">("</span><span class="string">.</span><span class="punct">").</span><span class="ident">first</span> <span class="comment"># just file name without extension</span>

    <span class="comment">#require 'digest/md5'</span>
    <span class="ident">digest</span> <span class="punct">=</span> <span class="constant">Digest</span><span class="punct">::</span><span class="constant">MD5</span><span class="punct">.</span><span class="ident">hexdigest</span><span class="punct">(</span> <span class="ident">filepath</span> <span class="punct">)</span>      <span class="comment"># md5 hash</span>

    <span class="ident">cachefile</span> <span class="punct">=</span> <span class="ident">digest</span> <span class="punct">+</span> <span class="punct">"</span><span class="string">-</span><span class="punct">"</span> <span class="punct">+</span> <span class="ident">widthx</span><span class="punct">.</span><span class="ident">to_s</span> <span class="punct">+</span> <span class="ident">heightx</span><span class="punct">.</span><span class="ident">to_s</span> <span class="punct">+</span> <span class="punct">"</span><span class="string">.</span><span class="punct">"</span> <span class="punct">+</span> <span class="ident">format</span>

    <span class="ident">picfile</span> <span class="punct">=</span> <span class="ident">filepath</span>
    <span class="ident">cachedpicfile</span> <span class="punct">=</span> <span class="ident">path</span> <span class="punct">+</span> <span class="punct">"</span><span class="string">image_cache/</span><span class="punct">"</span> <span class="punct">+</span> <span class="ident">cachefile</span>

    <span class="keyword">if</span> <span class="constant">File</span><span class="punct">.</span><span class="ident">exists?</span><span class="punct">(</span><span class="ident">cachedpicfile</span><span class="punct">)</span> <span class="punct">&amp;&amp;</span> <span class="punct">(</span><span class="constant">File</span><span class="punct">.</span><span class="ident">stat</span><span class="punct">(</span> <span class="ident">cachedpicfile</span> <span class="punct">).</span><span class="ident">mtime</span><span class="punct">.</span><span class="ident">to_i</span> <span class="punct">&gt;</span> <span class="constant">File</span><span class="punct">.</span><span class="ident">stat</span><span class="punct">(</span> <span class="ident">picfile</span> <span class="punct">).</span><span class="ident">mtime</span><span class="punct">.</span><span class="ident">to_i</span><span class="punct">)</span>

      <span class="ident">picsource</span> <span class="punct">=</span> <span class="ident">cachedpicfile</span>
      <span class="ident">cache</span> <span class="punct">=</span> <span class="constant">true</span>
    <span class="keyword">elsif</span> <span class="constant">File</span><span class="punct">.</span><span class="ident">exists?</span> <span class="ident">picfile</span>
      <span class="ident">picsource</span> <span class="punct">=</span> <span class="ident">picfile</span>

    <span class="keyword">end</span>

    <span class="keyword">if</span> <span class="ident">cache</span> <span class="punct">==</span> <span class="constant">true</span>    <span class="comment"># Read from Cache</span>
      <span class="attribute">@pic</span> <span class="punct">=</span> <span class="constant">File</span><span class="punct">.</span><span class="ident">new</span><span class="punct">(</span><span class="ident">picsource</span><span class="punct">).</span><span class="ident">read</span>

    <span class="keyword">else</span>                <span class="comment"># Import an image</span>
      <span class="ident">i</span> <span class="punct">=</span> <span class="constant">Image</span><span class="punct">.</span><span class="ident">import</span><span class="punct">(</span><span class="ident">picsource</span><span class="punct">)</span>

      <span class="keyword">if</span> <span class="ident">i</span><span class="punct">.</span><span class="ident">size</span><span class="punct">[</span><span class="number">0</span><span class="punct">]</span> <span class="punct">&gt;</span> <span class="ident">i</span><span class="punct">.</span><span class="ident">size</span><span class="punct">[</span><span class="number">1</span><span class="punct">]</span>  <span class="comment"># Horizontal proportion. width &gt; height.</span>

        <span class="keyword">if</span> <span class="ident">i</span><span class="punct">.</span><span class="ident">size</span><span class="punct">[</span><span class="number">0</span><span class="punct">]</span> <span class="punct">&lt;</span> <span class="ident">widthx</span> <span class="keyword">then</span> <span class="ident">width</span> <span class="punct">=</span> <span class="ident">i</span><span class="punct">.</span><span class="ident">size</span><span class="punct">[</span><span class="number">0</span><span class="punct">]</span>     <span class="comment"># preffer smaller image width</span>

        <span class="keyword">else</span> <span class="ident">width</span> <span class="punct">=</span> <span class="ident">widthx</span>
        <span class="keyword">end</span>

        <span class="ident">height</span> <span class="punct">=</span> <span class="ident">width</span> <span class="punct">*</span> <span class="ident">i</span><span class="punct">.</span><span class="ident">size</span><span class="punct">[</span><span class="number">1</span><span class="punct">]</span> <span class="punct">/</span> <span class="ident">i</span><span class="punct">.</span><span class="ident">size</span><span class="punct">[</span><span class="number">0</span><span class="punct">]</span>

      <span class="keyword">else</span>                      <span class="comment"># Vertical proportions</span>
        <span class="keyword">if</span> <span class="ident">i</span><span class="punct">.</span><span class="ident">size</span><span class="punct">[</span><span class="number">1</span><span class="punct">]</span> <span class="punct">&lt;</span> <span class="ident">heightx</span> <span class="keyword">then</span> <span class="ident">height</span> <span class="punct">=</span> <span class="ident">i</span><span class="punct">.</span><span class="ident">size</span><span class="punct">[</span><span class="number">1</span><span class="punct">]</span>

        <span class="keyword">else</span> <span class="ident">height</span> <span class="punct">=</span> <span class="ident">heightx</span>
        <span class="keyword">end</span>

        <span class="ident">width</span> <span class="punct">=</span> <span class="ident">i</span><span class="punct">.</span><span class="ident">size</span><span class="punct">[</span><span class="number">0</span><span class="punct">]</span> <span class="punct">/(</span><span class="ident">i</span><span class="punct">.</span><span class="ident">size</span><span class="punct">[</span><span class="number">1</span><span class="punct">]</span> <span class="punct">/</span> <span class="ident">height</span><span class="punct">)</span>
      <span class="keyword">end</span>

      <span class="ident">i</span><span class="punct">.</span><span class="ident">resize!</span> <span class="ident">width</span><span class="punct">,</span> <span class="ident">height</span>

      <span class="keyword">if</span> <span class="ident">format</span> <span class="punct">==</span> <span class="punct">"</span><span class="string">gif</span><span class="punct">"</span> <span class="keyword">then</span> <span class="attribute">@pic</span> <span class="punct">=</span> <span class="ident">i</span><span class="punct">.</span><span class="ident">gif</span>

      <span class="keyword">elsif</span> <span class="ident">format</span> <span class="punct">==</span> <span class="punct">"</span><span class="string">png</span><span class="punct">"</span> <span class="keyword">then</span> <span class="attribute">@pic</span> <span class="punct">=</span> <span class="ident">i</span><span class="punct">.</span><span class="ident">png</span>

      <span class="keyword">else</span> <span class="attribute">@pic</span> <span class="punct">=</span> <span class="ident">i</span><span class="punct">.</span><span class="ident">jpeg</span> <span class="number">80</span>
      <span class="keyword">end</span>

      <span class="ident">i</span><span class="punct">.</span><span class="ident">export</span><span class="punct">(</span><span class="ident">path</span> <span class="punct">+</span> <span class="punct">'</span><span class="string">image_cache/</span><span class="punct">'</span> <span class="punct">+</span> <span class="ident">cachefile</span> <span class="punct">)</span> <span class="comment"># export cache file</span>

    <span class="keyword">end</span>

    <span class="ident">cgi</span> <span class="punct">=</span> <span class="constant">CGI</span><span class="punct">.</span><span class="ident">new</span>
  	<span class="ident">cgi</span><span class="punct">.</span><span class="ident">out</span><span class="punct">("</span><span class="string">type</span><span class="punct">"=&gt;"</span><span class="string">image/jpeg</span><span class="punct">")</span> <span class="punct">{</span> <span class="attribute">@pic</span> <span class="punct">}</span>

  	<span class="ident">render</span> <span class="symbol">:nothing</span> <span class="punct">=&gt;</span> <span class="constant">true</span>

  <span class="keyword">end</span>
<span class="keyword">end</span>
</pre>
</div>


<!-- Begin SexyBookmarks Menu Code -->
<div class="sexy-bookmarks sexy-bookmarks-expand sexy-bookmarks-center">
<ul class="socials">
		<li class="sexy-delicious">
			<a href="http://del.icio.us/post?url=http://blog.bhushangahire.net/2008/03/18/ruby-thumbnail-generator/&amp;title=Ruby+thumbnail+generator" rel="nofollow" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="sexy-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://blog.bhushangahire.net/2008/03/18/ruby-thumbnail-generator/&amp;title=Ruby+thumbnail+generator" rel="nofollow" title="Digg this!">Digg this!</a>
		</li>
		<li class="sexy-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://blog.bhushangahire.net/2008/03/18/ruby-thumbnail-generator/&amp;title=Ruby+thumbnail+generator" rel="nofollow" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="sexy-technorati">
			<a href="http://technorati.com/faves?add=http://blog.bhushangahire.net/2008/03/18/ruby-thumbnail-generator/" rel="nofollow" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="sexy-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://blog.bhushangahire.net/2008/03/18/ruby-thumbnail-generator/&amp;t=Ruby+thumbnail+generator" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="sexy-twitter">
			<a href="http://twitter.com/home?status=Ruby+thumbnail+generator+-+http://blog.bhushangahire.net/2008/03/18/ruby-thumbnail-generator/+(via+@bhushangahire)" rel="nofollow" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="sexy-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://blog.bhushangahire.net/2008/03/18/ruby-thumbnail-generator/&amp;title=Ruby+thumbnail+generator&amp;summary=Ruby%20thumbnail%20generator%20is%20simple%20script%20which%20is%20ideal%20to%20use%20in%20your%20Ruby%20on%20Rails%20application%20to%20quickly%20generate%20thumbnails%20of%20any%20proportions.%20Just%20set%20width%20and%20height%20and%20get%20the%20image.1.%20copy%20following%20code%20into%20%2Fcontrollers%2Fthumb_controller.rb2.%20edit%20%2Fconfig%2Froutes.rb%20and%20add%20this%20line%3Amap&amp;source=eXpand yOur cReativity" rel="nofollow" title="Share this on Linkedin">Share this on Linkedin</a>
		</li>
		<li class="sexy-tumblr">
			<a href="http://www.tumblr.com/share?v=3&amp;u=http%3A%2F%2Fblog.bhushangahire.net%2F2008%2F03%2F18%2Fruby-thumbnail-generator%2F&amp;t=Ruby+thumbnail+generator" rel="nofollow" title="Share this on Tumblr">Share this on Tumblr</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>
<!-- End SexyBookmarks Menu Code -->

<div style='clear:both'></div>]]></content:encoded>
			<wfw:commentRss>http://blog.bhushangahire.net/2008/03/18/ruby-thumbnail-generator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
