<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title>Jakub Kuźma</title>
  <link href="http://jah.pl/atom.xml" rel="self"/>
  <updated>2011-10-20T21:34:56+02:00</updated>
  <id>http://jah.pl/</id>
  <author>
    <name>Jakub Kuźma</name>
    <email>kuba@jah.pl</email>
  </author>

  
    <entry>
      <title>Quick and dirty scanner daemon</title>
      <link href="http://jah.pl/articles/quick-and-dirty-scanner-daemon.html"/>
      <published>2011-10-20T00:00:00+02:00</published>
      <updated>2011-10-20T00:00:00+02:00</updated>
      <id>http://jah.pl/articles/quick-and-dirty-scanner-daemon</id>
      <content type="html">&lt;p&gt;I&amp;#8217;m too lazy to connect my scanner every time I have some papers to scan. It&amp;#8217;s more convenient if the scanner is always connected, and if I just press &lt;a href=&quot;http://www.google.com/search?q=canon+lide+100&amp;amp;tbm=isch&quot;&gt;a button&lt;/a&gt; to perform the scan. I figured out that the states of those buttons can be obtained using &lt;a href=&quot;http://www.sane-project.org/&quot;&gt;&lt;span class=&quot;caps&quot;&gt;SANE&lt;/span&gt;&lt;/a&gt; library. I didn&amp;#8217;t find any simple way to read those states apart from writing some low level C code. Obviously it&amp;#8217;s much more fun to do that in Ruby instead &amp;#8211; that&amp;#8217;s why I decided to write &lt;a href=&quot;https://github.com/qoobaa/sane-ffi&quot;&gt;&lt;span class=&quot;caps&quot;&gt;FFI&lt;/span&gt; bindings to &lt;span class=&quot;caps&quot;&gt;SANE&lt;/span&gt; library&lt;/a&gt;. I didn&amp;#8217;t manage to do everything in Ruby (scanner was hanging up at some point during the scan), but the &lt;code&gt;scanimage&lt;/code&gt; &lt;span class=&quot;caps&quot;&gt;CLI&lt;/span&gt; does the rest for me. I ended up with something like that:&lt;/p&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;br /&gt;
#!/usr/bin/ruby1.9.1&lt;br /&gt;
&lt;br /&gt;
require &amp;#8220;sane&amp;#8221;&lt;br /&gt;
require &amp;#8220;logger&amp;#8221;&lt;br /&gt;
require &amp;#8220;fileutils&amp;#8221;&lt;br /&gt;
require &amp;#8220;tempfile&amp;#8221;&lt;br /&gt;
&lt;br /&gt;
logger = Logger.new(&lt;span class=&quot;caps&quot;&gt;STDERR&lt;/span&gt;)&lt;br /&gt;
&lt;br /&gt;
loop do&lt;br /&gt;
  scan = nil&lt;br /&gt;
&lt;br /&gt;
  begin&lt;br /&gt;
    Sane.open do |sane|&lt;br /&gt;
      device = sane.devices.find { |device| device.vendor == &amp;#8220;Canon&amp;#8221; &amp;amp;&amp;amp; device.model == &amp;#8220;LiDE 100&amp;#8221; }&lt;br /&gt;
      if device&lt;br /&gt;
        logger.info(&amp;#8220;device found at #{device.name}&amp;#8221;)&lt;br /&gt;
        device.open do&lt;br /&gt;
          logger.info(&amp;#8220;device open, polling button states&amp;#8230;&amp;#8221;)&lt;br /&gt;
          while scan.nil?&lt;br /&gt;
            scan = [&amp;#8220;&amp;#8212;device&amp;#8221;, device.name, &amp;#8220;&amp;#8212;mode&amp;#8221;, &amp;#8220;Lineart&amp;#8221;, &amp;#8220;&amp;#8212;resolution&amp;#8221;, &amp;#8220;300&amp;#8221;] if device[:file]&lt;br /&gt;
            sleep(0.5)&lt;br /&gt;
          end&lt;br /&gt;
          logger.info(&amp;#8220;closing device&amp;#8221;)&lt;br /&gt;
        end&lt;br /&gt;
      else&lt;br /&gt;
        logger.error(&amp;#8220;device not found&amp;#8221;)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  rescue Sane::Error =&amp;gt; exception&lt;br /&gt;
    logger.error(&amp;#8220;&lt;span class=&quot;caps&quot;&gt;SANE&lt;/span&gt; exception caught: #{exception}&amp;#8221;)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  if scan&lt;br /&gt;
    tempfile = Tempfile.new(&amp;#8220;scan&amp;#8221;)&lt;br /&gt;
    tempfile.close&lt;br /&gt;
&lt;br /&gt;
    logger.info(&amp;#8220;scanning with #{scan.join(&amp;#8221; &amp;#8220;)} to #{tempfile.path}&amp;#8221;)&lt;br /&gt;
    system(&amp;#8220;scanimage&amp;#8221;, *scan, :out =&amp;gt; tempfile.path)&lt;br /&gt;
&lt;br /&gt;
    output_dir = &amp;#8220;/nfs/media/Scans&amp;#8221;&lt;br /&gt;
    FileUtils.mkdir_p(output_dir, :mode =&amp;gt; 0777)&lt;br /&gt;
&lt;br /&gt;
    output_file = &amp;#8220;#{output_dir}/#{Time.now.strftime(&amp;#8221;%Y%m%d%H%M%S&amp;quot;)}.png&amp;quot;&lt;br /&gt;
    logger.info(&amp;#8220;converting file to #{output_file}&amp;#8221;)&lt;br /&gt;
    system(&amp;#8220;convert&amp;#8221;, tempfile.path, output_file)&lt;br /&gt;
&lt;br /&gt;
    tempfile.unlink&lt;br /&gt;
  else&lt;br /&gt;
    logger.info(&amp;#8220;sleeping&amp;#8230;&amp;#8221;)&lt;br /&gt;
    sleep(60)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;/pre&gt;&lt;/noscript&gt;
&lt;script src=&quot;https://gist.github.com/1301868.js?file=scanner.rb&quot;&gt;&lt;/script&gt;&lt;p&gt;The script saves everything on my local &lt;code&gt;NAS&lt;/code&gt; device, but you can modify it to email the scans somewhere, or to send them to your Google Docs account. You can also make use of other buttons, providing different scan settings, format or destinations.&lt;/p&gt;
&lt;p&gt;You just need an Ubuntu box with &lt;a href=&quot;http://en.wikipedia.org/wiki/Upstart&quot;&gt;Upstart&lt;/a&gt; to run the script as a daemon:&lt;/p&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;br /&gt;
start on startup&lt;br /&gt;
exec /usr/local/bin/scanner.rb&lt;br /&gt;
&lt;/pre&gt;&lt;/noscript&gt;
&lt;script src=&quot;https://gist.github.com/1301868.js?file=scanner.conf&quot;&gt;&lt;/script&gt;&lt;p&gt;Simply save the configuration file in &lt;code&gt;/etc/init/scanner.conf&lt;/code&gt;. To run the daemon manually use &lt;code&gt;start scanner&lt;/code&gt; command.&lt;/p&gt;</content>
    </entry>
  
    <entry>
      <title>Mass assignment in Rails 3.1</title>
      <link href="http://jah.pl/articles/mass-assignment-in-rails-3-1.html"/>
      <published>2011-10-02T00:00:00+02:00</published>
      <updated>2011-10-02T00:00:00+02:00</updated>
      <id>http://jah.pl/articles/mass-assignment-in-rails-3-1</id>
      <content type="html">&lt;p&gt;When I was browsing through the &lt;a href=&quot;http://edgeguides.rubyonrails.org/security.html#mass-assignment&quot;&gt;Ruby on Rails Security Guide&lt;/a&gt; yesterday, I stumbled upon an interesting configuration option:&lt;/p&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;br /&gt;
config.active_record.whitelist_attributes = true&lt;br /&gt;
&lt;/pre&gt;&lt;/noscript&gt;
&lt;script src=&quot;https://gist.github.com/1257215.js?file=gistfile1.rb&quot;&gt;&lt;/script&gt;&lt;blockquote&gt;
&lt;p&gt;This will create an empty whitelist of attributes available for mass assignment for all models in your app. As such, your models will need to explicitly whitelist or blacklist accessible parameters by using an &lt;code&gt;attr_accessible&lt;/code&gt; or &lt;code&gt;attr_protected&lt;/code&gt; declaration.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Shouldn&amp;#8217;t this be enabled by default, just like escaping &lt;code&gt;HTML&lt;/code&gt; output in &lt;code&gt;ERb&lt;/code&gt; templates? The only drawback of this solution is that you&amp;#8217;ll need to turn the protection off explicitly when assigning attributes by hand (i.e. &lt;code&gt;rails console&lt;/code&gt;):&lt;/p&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;br /&gt;
User.new({:name =&amp;gt; &amp;#8220;Bob&amp;#8221;, :role =&amp;gt; &amp;quot;admin&amp;quot;}, :without_protection =&amp;gt; true)&lt;br /&gt;
&lt;/pre&gt;&lt;/noscript&gt;
&lt;script src=&quot;https://gist.github.com/1257215.js?file=gistfile2.rb&quot;&gt;&lt;/script&gt;</content>
    </entry>
  
    <entry>
      <title>Serve local directory using Rack::Static</title>
      <link href="http://jah.pl/articles/serve-local-directory-using-rack-static.html"/>
      <published>2010-09-10T00:00:00+02:00</published>
      <updated>2010-09-10T00:00:00+02:00</updated>
      <id>http://jah.pl/articles/serve-local-directory-using-rack-static</id>
      <content type="html">&lt;p&gt;Recently I needed to test compatibility of a static webpage on Internet Explorer running on VM. It&amp;#8217;s not really handy to sync the files after every single change, so I decided to run a simple webserver to provide access to the directory via &lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt;. After struggling with &lt;a href=&quot;http://tobyho.com/HTTP%20Server%20in%205%20Lines%20With%20Webrick&quot;&gt;5 liners using WEBrick&lt;/a&gt;, my friend came up with the idea to use &lt;code&gt;Rack::Static&lt;/code&gt; to do so. You can simply save the following code in &lt;code&gt;config.ru&lt;/code&gt; file in the directory that you want to publish, and run &lt;code&gt;rackup&lt;/code&gt; command to start the server (don&amp;#8217;t forget to &lt;code&gt;gem install rack&lt;/code&gt; before).&lt;/p&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;br /&gt;
use Rack::Static, :urls =&amp;gt; [&amp;quot;&amp;quot;]; run nil&lt;br /&gt;
&lt;/pre&gt;&lt;/noscript&gt;
&lt;script src=&quot;https://gist.github.com/737443.js?file=gistfile1.rb&quot;&gt;&lt;/script&gt;</content>
    </entry>
  
    <entry>
      <title>Simple Rack application for JavaScript I18n</title>
      <link href="http://jah.pl/articles/simple-rack-application-for-javascript-i18n.html"/>
      <published>2010-06-22T00:00:00+02:00</published>
      <updated>2010-06-22T00:00:00+02:00</updated>
      <id>http://jah.pl/articles/simple-rack-application-for-javascript-i18n</id>
      <content type="html">&lt;p&gt;I tried to improve my old &lt;a href=&quot;http://github.com/qoobaa/javascript_i18n&quot;&gt;&lt;code&gt;javascript_i18n&lt;/code&gt;&lt;/a&gt; gem recently, and I stumbled upon Kelly Redding&amp;#8217;s &lt;a href=&quot;http://github.com/kelredd/rack-sprockets&quot;&gt;&lt;code&gt;rack-sprockets&lt;/code&gt;&lt;/a&gt; gem. I really like the idea of putting such stuff into Rack middleware stack &amp;#8211; it doesn&amp;#8217;t mess up your application, and it becomes framework agnostic. Then I realized that it can be made even simpler using &lt;a href=&quot;http://rack.rubyforge.org/doc/classes/Rack/Builder.html&quot;&gt;&lt;code&gt;Rack::Builder&lt;/code&gt;&lt;/a&gt; and utilities from &lt;a href=&quot;http://github.com/rack/rack-contrib&quot;&gt;&lt;code&gt;rack-contrib&lt;/code&gt;&lt;/a&gt; repository. The following Rack application works with default Rails&amp;#8217; I18n backend:&lt;/p&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;br /&gt;
require &amp;#8220;json&amp;#8221;&lt;br /&gt;
&lt;br /&gt;
class JavaScriptI18n&lt;br /&gt;
  def self.call(env)&lt;br /&gt;
    locale = env[&amp;#8220;PATH_INFO&amp;#8221;][1..-4]&lt;br /&gt;
    if translations.key?(locale)&lt;br /&gt;
      [200, {&amp;quot;Content-Type&amp;quot; =&amp;gt; &amp;#8220;text/javascript&amp;#8221;}, new(translations[locale])]&lt;br /&gt;
    else&lt;br /&gt;
      [404, {}, &amp;#8220;Not found&amp;#8221;]&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def self.translations&lt;br /&gt;
    I18n.backend.send(:init_translations) if I18n.backend.send(:translations).empty?&lt;br /&gt;
    I18n.backend.send(:translations).with_indifferent_access&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def initialize(translations)&lt;br /&gt;
    @translations = translations&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def each&lt;br /&gt;
    yield &amp;#8220;var I18n=I18n||{};I18n.translations=&amp;#8221;&lt;br /&gt;
    yield @translations.to_json&lt;br /&gt;
    yield &amp;#8220;;&amp;#8221;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;/pre&gt;&lt;/noscript&gt;
&lt;script src=&quot;https://gist.github.com/744775.js?file=gistfile1.rb&quot;&gt;&lt;/script&gt;&lt;p&gt;The application serves JavaScripts, containing translations for the requested language, and stores them in the &lt;code&gt;I18n.translations&lt;/code&gt; property. You can easily mount it using &lt;code&gt;Rack::Builder&lt;/code&gt; in your &lt;code&gt;config.ru&lt;/code&gt; file.&lt;/p&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;br /&gt;
map &amp;#8220;/javascripts/i18n&amp;#8221; do&lt;br /&gt;
  run JavaScriptI18n&lt;br /&gt;
end&lt;br /&gt;
map &amp;#8220;/&amp;#8221; do&lt;br /&gt;
  run MyShinyApp::Application&lt;br /&gt;
end&lt;br /&gt;
&lt;/pre&gt;&lt;/noscript&gt;
&lt;script src=&quot;https://gist.github.com/744775.js?file=gistfile2.rb&quot;&gt;&lt;/script&gt;&lt;p&gt;Now you can include the JavaScript in your application using ordinary &lt;code&gt;script&lt;/code&gt; tag or &lt;code&gt;javascript_include_tag&lt;/code&gt; helper:&lt;/p&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;br /&gt;
javascript_include_tag &amp;#8220;i18n/#{I18n.locale}&amp;#8221;&lt;br /&gt;
&lt;/pre&gt;&lt;/noscript&gt;
&lt;script src=&quot;https://gist.github.com/744775.js?file=gistfile3.rb&quot;&gt;&lt;/script&gt;&lt;p&gt;If you need to cache JavaScript translations in the production environment, you can use &lt;a href=&quot;http://github.com/rack/rack-contrib/blob/master/lib/rack/contrib/response_cache.rb&quot;&gt;&lt;code&gt;Rack::ResponseCache&lt;/code&gt;&lt;/a&gt; to do that.&lt;/p&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;br /&gt;
require &amp;#8220;rack/contrib/response_cache&amp;#8221;&lt;br /&gt;
&lt;br /&gt;
map &amp;#8220;/javascripts/i18n&amp;#8221; do&lt;br /&gt;
  use(Rack::ResponseCache, &amp;#8220;public/javascripts/i18n&amp;#8221;) { |env| env[&amp;#8220;PATH_INFO&amp;#8221;] }&lt;br /&gt;
  run JavaScriptI18n&lt;br /&gt;
end&lt;br /&gt;
map &amp;#8220;/&amp;#8221; do&lt;br /&gt;
  run MyShinyApp::Application&lt;br /&gt;
end&lt;br /&gt;
&lt;/pre&gt;&lt;/noscript&gt;
&lt;script src=&quot;https://gist.github.com/744775.js?file=gistfile4.rb&quot;&gt;&lt;/script&gt;&lt;p&gt;&lt;code&gt;Rack::ResponseCache&lt;/code&gt; works very similar to Rails&amp;#8217; page caching &amp;#8211; it stores responses in the given directory (&lt;code&gt;public/javascripts/i18n&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;The solution works great in Rails 3 applications, as well in the other frameworks with &lt;code&gt;Rack::Builder&lt;/code&gt; available. If you need to use it in Rails 2, you should probably check out the &lt;a href=&quot;http://github.com/rack/rack-contrib/blob/master/lib/rack/contrib/simple_endpoint.rb&quot;&gt;&lt;code&gt;Rack::SimpleEndpoint&lt;/code&gt;&lt;/a&gt; middleware. &lt;code&gt;SimpleEndpoint&lt;/code&gt; can easily turn any Rack application into middleware, that you can mount in your Rails 2 app.&lt;/p&gt;</content>
    </entry>
  
    <entry>
      <title>Using ActionMailer for internal messaging</title>
      <link href="http://jah.pl/articles/using-action-mailer-for-internal-messaging.html"/>
      <published>2009-11-14T00:00:00+01:00</published>
      <updated>2009-11-14T00:00:00+01:00</updated>
      <id>http://jah.pl/articles/using-action-mailer-for-internal-messaging</id>
      <content type="html">&lt;p&gt;Rendering internal messages inline can be quite confusing, especially if you want to use some helpers inside (like path helpers). It&amp;#8217;d be more convenient to render them in the same way as other messages &amp;#8211; using ActionMailer and &lt;span class=&quot;caps&quot;&gt;ERB&lt;/span&gt; templates. My solution uses email addresses to figure out the message recipients:&lt;/p&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;br /&gt;
class UserNotifier &amp;lt; ActionMailer::Base&lt;br /&gt;
  # sample message&lt;br /&gt;
  def some_message(user)&lt;br /&gt;
    from %Q{&amp;quot;Internal Messaging System&amp;quot;}&lt;br /&gt;
    recipients user.email&lt;br /&gt;
    subject &amp;#8220;This is internal message&amp;#8221;&lt;br /&gt;
    body :user =&amp;gt; user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # sample message notification&lt;br /&gt;
  def message_notification(message)&lt;br /&gt;
    from %Q{&amp;quot;MyAppName.com mailer&amp;quot;}&lt;br /&gt;
    recipients message.recipient.email&lt;br /&gt;
    subject &amp;#8220;You&amp;#8217;ve new message in your inbox&amp;#8221;&lt;br /&gt;
    body :user =&amp;gt; user&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  private&lt;br /&gt;
&lt;br /&gt;
  # if the called method has &amp;#8220;deliver_internal_&amp;#8221;&lt;br /&gt;
  # prefix, use notify method to deliver message&lt;br /&gt;
  def self.method_missing(method_name, &lt;strong&gt;args)&lt;br /&gt;
    if method_name.to_s =~ /^deliver_internal_([_a-z]\w&lt;/strong&gt;)/&lt;br /&gt;
      new($1, *args).send(:deliver_internal)&lt;br /&gt;
    else&lt;br /&gt;
      super&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  # deliver message to user using internal messaging&lt;br /&gt;
  def deliver_internal&lt;br /&gt;
    users = User.find_all_by_email(@mail.to)&lt;br /&gt;
    from = @mail.from.first[1..-2] # remove quotes&lt;br /&gt;
    users.each do |user|&lt;br /&gt;
      Message.create!(:recipient =&amp;gt; user,&lt;br /&gt;
                      :subject =&amp;gt; @mail.subject,&lt;br /&gt;
                      :body =&amp;gt; @mail.body,&lt;br /&gt;
                      :sender_name =&amp;gt; from)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;/pre&gt;&lt;/noscript&gt;
&lt;script src=&quot;https://gist.github.com/754948.js?file=gistfile1.rb&quot;&gt;&lt;/script&gt;&lt;p&gt;Additionally you can notify the user about a new message, by implementing a simple callback in the &lt;code&gt;Message&lt;/code&gt; model.&lt;/p&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;br /&gt;
class Message &amp;lt; ActiveRecord::Base&lt;br /&gt;
  after_create :deliver_notification&lt;br /&gt;
  belongs_to :recipient, :class_name =&amp;gt; &amp;#8220;User&amp;#8221;&lt;br /&gt;
&lt;br /&gt;
  private&lt;br /&gt;
&lt;br /&gt;
  def deliver_notification&lt;br /&gt;
    # deliver message notification email (external)&lt;br /&gt;
    # if the user wants it&lt;br /&gt;
    if recipient.wants_message_notifications?&lt;br /&gt;
      UserNotifier.deliver_message_notification(self)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
There&amp;#8217;s a really good point of this solution &amp;#8211; you don&amp;#8217;t need to create separate actions for external and internal messaging. The only difference is the method&amp;#8217;s prefix:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush: ruby&quot;&gt;&lt;br /&gt;
# send message and notify by email if user wants it&lt;br /&gt;
UserNotifier.deliver_internal_some_message(user)&lt;br /&gt;
# send email only&lt;br /&gt;
UserNotifier.deliver_some_message(user)&lt;br /&gt;
&lt;/pre&gt;&lt;/noscript&gt;
&lt;script src=&quot;https://gist.github.com/754948.js?file=gistfile2.rb&quot;&gt;&lt;/script&gt;</content>
    </entry>
  
    <entry>
      <title>Ruby and Rails on Ubuntu 9.10 Karmic Koala</title>
      <link href="http://jah.pl/articles/ruby-and-rails-on-ubuntu-9-10-karmic-koala.html"/>
      <published>2009-11-10T00:00:00+01:00</published>
      <updated>2009-11-10T00:00:00+01:00</updated>
      <id>http://jah.pl/articles/ruby-and-rails-on-ubuntu-9-10-karmic-koala</id>
      <content type="html">&lt;p&gt;&lt;a href=&quot;http://rvm.beginrescueend.com/&quot;&gt;Ruby Version Manager&lt;/a&gt; is a really powerful tool which provides an easy, non obtrusive way to install multiple versions of Ruby, Rails and other gems on your Ubuntu 9.10 box. You won&amp;#8217;t have to compile anything manually (&lt;a href=&quot;http://rvm.beginrescueend.com/&quot;&gt;&lt;span class=&quot;caps&quot;&gt;RVM&lt;/span&gt;&lt;/a&gt; does a great job) and everything will be kept in your home directory. All you need to do is:&lt;/p&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;br /&gt;
sudo apt-get install ruby rubygems libssl-dev libreadline-dev zlib1g-dev libsqlite3-dev curl&lt;br /&gt;
&lt;br /&gt;
sudo gem install rvm&lt;br /&gt;
&lt;br /&gt;
/var/lib/gems/1.8/bin/rvm-install&lt;br /&gt;
&lt;/pre&gt;&lt;/noscript&gt;
&lt;script src=&quot;https://gist.github.com/754950.js?file=gistfile1.sh&quot;&gt;&lt;/script&gt;&lt;p class=&quot;hyphenate&quot; lang=&quot;en&quot;&gt;Last step modifies your &lt;code&gt;~/.bashrc&lt;/code&gt; (or &lt;code&gt;~/.zshrc&lt;/code&gt;), so the changes will be visible in all new terminal instances. After restarting your terminal, pick a &lt;a href=&quot;http://rvm.beginrescueend.com/implementations/&quot;&gt;Ruby version&lt;/a&gt; to install:&lt;/p&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;br /&gt;
rvm install 1.9.1&lt;br /&gt;
&lt;/pre&gt;&lt;/noscript&gt;
&lt;script src=&quot;https://gist.github.com/754950.js?file=gistfile2.sh&quot;&gt;&lt;/script&gt;&lt;p&gt;After a couple of minutes you&amp;#8217;ll have a fully functional Ruby 1.9.1 interpreter installed in your &lt;code&gt;~/.rvm&lt;/code&gt; directory. If you want to use it as a default Ruby, type:&lt;/p&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;br /&gt;
rvm use 1.9.1 &amp;#8212;default&lt;br /&gt;
&lt;/pre&gt;&lt;/noscript&gt;
&lt;script src=&quot;https://gist.github.com/754950.js?file=gistfile3.sh&quot;&gt;&lt;/script&gt;&lt;p&gt;Now you can install Rails and other stuff &amp;#8211; remember to not use &lt;code&gt;sudo&lt;/code&gt; to do that (since everything is kept locally in your home directory, &lt;span class=&quot;caps&quot;&gt;RVM&lt;/span&gt; manages your &lt;code&gt;PATH&lt;/code&gt; environment variable as well):&lt;/p&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;br /&gt;
gem install rails&lt;br /&gt;
&lt;/pre&gt;&lt;/noscript&gt;
&lt;script src=&quot;https://gist.github.com/754950.js?file=gistfile4.sh&quot;&gt;&lt;/script&gt;&lt;p&gt;For more information about Ruby Version Manager, visit the RVM&amp;#8217;s &lt;a href=&quot;http://rvm.beginrescueend.com/&quot;&gt;homepage&lt;/a&gt;.&lt;/p&gt;</content>
    </entry>
  
    <entry>
      <title>Patching Rails and gems locally</title>
      <link href="http://jah.pl/articles/patching-rails-and-gems-locally.html"/>
      <published>2009-10-21T00:00:00+02:00</published>
      <updated>2009-10-21T00:00:00+02:00</updated>
      <id>http://jah.pl/articles/patching-rails-and-gems-locally</id>
      <content type="html">&lt;p&gt;How many times did you have to wait ages until a patch that you&amp;#8217;ve submitted gets merged into the Rails or a gem? Sometimes you can&amp;#8217;t wait, it&amp;#8217;s confusing to keep external libraries in your repository. Fortunatelly &amp;#8211; Rails provides a simple way to unpack dependencies into vendor directory, where it&amp;#8217;s possible to make local changes. Instead of tracking whole vendor, you can have small &lt;code&gt;.patch&lt;/code&gt; files in a separate directory and apply them to frozen vendor stuff. You won&amp;#8217;t need to remember which gems are locally modified, repository stays small, patches can be easily applied on your remote production server by invoking rake tasks (you can use Capistrano as well). All you need is a simple Rake task:&lt;/p&gt;
&lt;noscript&gt;&lt;pre&gt;&lt;br /&gt;
namespace :patch do&lt;br /&gt;
  desc &amp;#8220;Applies patches to Rails frozen in vendor&amp;#8221;&lt;br /&gt;
  task :rails do&lt;br /&gt;
    rails_dir = Rails.root.join(&amp;#8220;vendor&amp;#8221;, &amp;#8220;rails&amp;#8221;)&lt;br /&gt;
    Rails.root.join(&amp;#8220;patch&amp;#8221;, &amp;#8220;rails&amp;#8221;).children.each do |patch|&lt;br /&gt;
      patch(patch, rails_dir)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  desc &amp;#8220;Applies patches to gems frozen in vendor&amp;#8221;&lt;br /&gt;
  task :gems do&lt;br /&gt;
    Rails.root.join(&amp;#8220;patch&amp;#8221;, &amp;#8220;gems&amp;#8221;).children.each do |patch_dir|&lt;br /&gt;
      patch_dir.children.each do |patch|&lt;br /&gt;
        gem_dir = Rails.root.join(&amp;#8220;vendor&amp;#8221;, &amp;#8220;gems&amp;#8221;, patch_dir.basename)&lt;br /&gt;
        patch(patch, gem_dir)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  desc &amp;#8220;Applies patches to Rails and gems in vendor&amp;#8221;&lt;br /&gt;
  task :all =&amp;gt; [&amp;#8220;gems&amp;#8221;, &amp;#8220;rails&amp;#8221;]&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
def patch(patch, dir)&lt;br /&gt;
  puts &amp;#8220;Applying patch #{patch.basename} to #{dir.basename}&amp;#8221;&lt;br /&gt;
  sh &amp;#8220;patch -N -p 1 -d #{dir} -i #{patch}&amp;#8221;&lt;br /&gt;
end&lt;br /&gt;
&lt;/pre&gt;&lt;/noscript&gt;
&lt;script src=&quot;https://gist.github.com/754952.js?file=gistfile1.rb&quot;&gt;&lt;/script&gt;&lt;p&gt;Freeze your Rails and/or gems using &lt;code&gt;rake rails:freeze:gems&lt;/code&gt; and &lt;code&gt;rake gems:unpack&lt;/code&gt;. You have to store your patches as shown below. Notice that gem&amp;#8217;s patches directories must include the version number (just like in &lt;code&gt;vendor/gems&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/local-patches.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;To apply the patches run &lt;code&gt;rake patch:gems&lt;/code&gt; or &lt;code&gt;rake patch:rails&lt;/code&gt; task (&lt;code&gt;rake patch:all&lt;/code&gt;). Don&amp;#8217;t forget to add &lt;code&gt;vendor/rails&lt;/code&gt; and &lt;code&gt;vendor/gems&lt;/code&gt; to your &lt;code&gt;.gitignore&lt;/code&gt;.&lt;/p&gt;</content>
    </entry>
  

</feed>

