<?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>2010-07-29T09:34:38+02:00</updated>
  <id>http://jah.pl/</id>
  <author>
    <name>Jakub Kuźma</name>
    <email>kuba@jah.pl</email>
  </author>

  
    <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 class=&quot;hyphenate&quot; lang=&quot;en&quot;&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;pre class=&quot;brush: ruby&quot;&gt;
require &quot;json&quot;

class JavaScriptI18n
  def self.call(env)
    locale = env[&quot;PATH_INFO&quot;][1..-4]
    if translations.key?(locale)
      [200, {&quot;Content-Type&quot; =&amp;gt; &quot;text/javascript&quot;}, new(translations[locale])]
    else
      [404, {}, &quot;Not found&quot;]
    end
  end

  def self.translations
    I18n.backend.send(:init_translations) if I18n.backend.send(:translations).empty?
    I18n.backend.send(:translations).with_indifferent_access
  end

  def initialize(translations)
    @translations = translations
  end

  def each
    yield &quot;var I18n=I18n||{};I18n.translations=&quot;
    yield @translations.to_json
    yield &quot;;&quot;
  end
end
&lt;/pre&gt;
&lt;p class=&quot;hyphenate&quot; lang=&quot;en&quot;&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;pre class=&quot;brush: ruby&quot;&gt;
map &quot;/javascripts/i18n&quot; do
  run JavaScriptI18n
end
map &quot;/&quot; do
  run MyShinyApp::Application
end
&lt;/pre&gt;
&lt;p class=&quot;hyphenate&quot; lang=&quot;en&quot;&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;pre class=&quot;brush: ruby&quot;&gt;
javascript_include_tag &quot;i18n/#{I18n.locale}&quot;
&lt;/pre&gt;
&lt;p class=&quot;hyphenate&quot; lang=&quot;en&quot;&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;pre class=&quot;brush: ruby&quot;&gt;
require &quot;rack/contrib/response_cache&quot;

map &quot;/javascripts/i18n&quot; do
  use(Rack::ResponseCache, &quot;public/javascripts/i18n&quot;) { |env| env[&quot;PATH_INFO&quot;] }
  run JavaScriptI18n
end
map &quot;/&quot; do
  run MyShinyApp::Application
end
&lt;/pre&gt;
&lt;p class=&quot;hyphenate&quot; lang=&quot;en&quot;&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 class=&quot;hyphenate&quot; lang=&quot;en&quot;&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 class=&quot;hyphenate&quot; lang=&quot;en&quot;&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;pre class=&quot;brush: ruby&quot;&gt;
class UserNotifier &amp;lt; ActionMailer::Base
  # sample message
  def some_message(user)
    from %Q{&quot;Internal Messaging System&quot;}
    recipients user.email
    subject &quot;This is internal message&quot;
    body :user =&amp;gt; user
  end

  # sample message notification
  def message_notification(message)
    from %Q{&quot;MyAppName.com mailer&quot;}
    recipients message.recipient.email
    subject &quot;You've new message in your inbox&quot;
    body :user =&amp;gt; user
  end

  private

  # if the called method has &quot;deliver_internal_&quot;
  # prefix, use notify method to deliver message
  def self.method_missing(method_name, *args)
    if method_name.to_s =~ /^deliver_internal_([_a-z]\w*)/
      new($1, *args).send(:deliver_internal)
    else
      super
    end
  end

  # deliver message to user using internal messaging
  def deliver_internal
    users = User.find_all_by_email(@mail.to)
    from = @mail.from.first[1..-2] # remove quotes
    users.each do |user|
      Message.create!(:recipient =&amp;gt; user,
                      :subject =&amp;gt; @mail.subject,
                      :body =&amp;gt; @mail.body,
                      :sender_name =&amp;gt; from)
    end
  end
end
&lt;/pre&gt;
&lt;p class=&quot;hyphenate&quot; lang=&quot;en&quot;&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;pre class=&quot;brush: ruby&quot;&gt;
class Message &amp;lt; ActiveRecord::Base
  after_create :deliver_notification
  belongs_to :recipient, :class_name =&amp;gt; &quot;User&quot;

  private

  def deliver_notification
    # deliver message notification email (external)
    # if the user wants it
    if recipient.wants_message_notifications?
      UserNotifier.deliver_message_notification(self)
    end
  end
end
&lt;/pre&gt;
&lt;p class=&quot;hyphenate&quot; lang=&quot;en&quot;&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;/p&gt;
&lt;pre class=&quot;brush: ruby&quot;&gt;
# send message and notify by email if user wants it
UserNotifier.deliver_internal_some_message(user)
# send email only
UserNotifier.deliver_some_message(user)
&lt;/pre&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 class=&quot;hyphenate&quot; lang=&quot;en&quot;&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;p&gt;&lt;code&gt;sudo apt-get install ruby rubygems libssl-dev libreadline-dev zlib1g-dev libsqlite3-dev curl&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;sudo gem install rvm&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;/var/lib/gems/1.8/bin/rvm-install&lt;/code&gt;&lt;/p&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;p&gt;&lt;code&gt;rvm install 1.9.1&lt;/code&gt;&lt;/p&gt;
&lt;p class=&quot;hyphenate&quot; lang=&quot;en&quot;&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;p&gt;&lt;code&gt;rvm use 1.9.1 --default&lt;/code&gt;&lt;/p&gt;
&lt;p class=&quot;hyphenate&quot; lang=&quot;en&quot;&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;p&gt;&lt;code&gt;gem install rails&lt;/code&gt;&lt;/p&gt;
&lt;p class=&quot;hyphenate&quot; lang=&quot;en&quot;&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 class=&quot;hyphenate&quot; lang=&quot;en&quot;&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;pre class=&quot;brush: ruby&quot;&gt;
namespace :patch do
  desc &quot;Applies patches to Rails frozen in vendor&quot;
  task :rails do
    rails_dir = Rails.root.join(&quot;vendor&quot;, &quot;rails&quot;)
    Rails.root.join(&quot;patch&quot;, &quot;rails&quot;).children.each do |patch|
      patch(patch, rails_dir)
    end
  end

  desc &quot;Applies patches to gems frozen in vendor&quot;
  task :gems do
    Rails.root.join(&quot;patch&quot;, &quot;gems&quot;).children.each do |patch_dir|
      patch_dir.children.each do |patch|
        gem_dir = Rails.root.join(&quot;vendor&quot;, &quot;gems&quot;, patch_dir.basename)
        patch(patch, gem_dir)
      end
    end
  end

  desc &quot;Applies patches to Rails and gems in vendor&quot;
  task :all =&amp;gt; [&quot;gems&quot;, &quot;rails&quot;]
end

def patch(patch, dir)
  puts &quot;Applying patch #{patch.basename} to #{dir.basename}&quot;
  sh &quot;patch -N -p 1 -d #{dir} -i #{patch}&quot;
end
&lt;/pre&gt;
&lt;p class=&quot;hyphenate&quot; lang=&quot;en&quot;&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 class=&quot;hyphenate&quot; lang=&quot;en&quot;&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>
