<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <id>http://jah.pl/</id>
  <title>Jakub Ku&#378;ma - Articles</title>
  <updated>2009-11-14T19:28:00Z</updated>
  <link rel="alternate" href="http://jah.pl"/>
  <link rel="self" href="http://jah.pl/atom.xml"/>
  <author>
    <name>Jakub Ku&#378;ma</name>
    <uri>http://jah.pl/</uri>
  </author>
  <entry>
    <id>tag:jah.pl,2009-11-14:/articles/using-action-mailer-for-internal-messaging.html</id>
    <title type="html">Using ActionMailer for internal messaging</title>
    <published>2009-11-14T19:28:00Z</published>
    <updated>2009-12-02T12:20:45Z</updated>
    <link rel="alternate" href="http://jah.pl/articles/using-action-mailer-for-internal-messaging.html"/>
    <content type="html">&lt;p class="hyphenate" lang="en"&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="caps"&gt;ERB&lt;/span&gt; templates. My solution uses email addresses to figure out the message recipients:&lt;/p&gt;
&lt;pre class="brush: ruby"&gt;
class UserNotifier &amp;lt; ActionMailer::Base
  # sample message
  def some_message(user)
    from %Q{"Internal Messaging System"}
    recipients user.email
    subject "This is internal message"
    body :user =&amp;gt; user
  end

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

  private

  # if the called method has "deliver_internal_"
  # 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="hyphenate" lang="en"&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="brush: ruby"&gt;
class Message &amp;lt; ActiveRecord::Base
  after_create :deliver_notification
  belongs_to :recipient, :class_name =&amp;gt; "User"

  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="hyphenate" lang="en"&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="brush: ruby"&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>
    <id>tag:jah.pl,2009-11-10:/articles/ruby-and-rails-on-ubuntu-9-10-karmic-koala.html</id>
    <title type="html">Ruby and Rails on Ubuntu 9.10 Karmic Koala</title>
    <published>2009-11-10T08:46:00Z</published>
    <updated>2009-12-02T12:20:45Z</updated>
    <link rel="alternate" href="http://jah.pl/articles/ruby-and-rails-on-ubuntu-9-10-karmic-koala.html"/>
    <content type="html">&lt;p class="hyphenate" lang="en"&gt;&lt;a href="http://rvm.beginrescueend.com/"&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="http://rvm.beginrescueend.com/"&gt;&lt;span class="caps"&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="hyphenate" lang="en"&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="http://rvm.beginrescueend.com/implementations/"&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="hyphenate" lang="en"&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="hyphenate" lang="en"&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="caps"&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="hyphenate" lang="en"&gt;For more information about Ruby Version Manager, visit the RVM&amp;#8217;s &lt;a href="http://rvm.beginrescueend.com/"&gt;homepage&lt;/a&gt;.&lt;/p&gt;</content>
  </entry>
  <entry>
    <id>tag:jah.pl,2009-10-21:/articles/patching-rails-and-gems-locally.html</id>
    <title type="html">Patching Rails and gems locally</title>
    <published>2009-10-21T21:08:00Z</published>
    <updated>2009-12-02T12:20:45Z</updated>
    <link rel="alternate" href="http://jah.pl/articles/patching-rails-and-gems-locally.html"/>
    <content type="html">&lt;p class="hyphenate" lang="en"&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="brush: ruby"&gt;
namespace :patch do
  desc "Applies patches to Rails frozen in vendor"
  task :rails do
    rails_dir = Rails.root.join("vendor", "rails")
    Rails.root.join("patch", "rails").children.each do |patch|
      patch(patch, rails_dir)
    end
  end

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

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

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

