HowTo: Phusion Passenger aka mod_rails for Apache
August 8th, 2008 — Posted by benedikt — Filed in Articles, Linux, Ruby, Ruby on Rails
Yesterday I decided to give Phusion Passenger aka mod_rails a try and installed it. It was dead simple to set it up and to deploy rails applications with it. I’m now using it for several “small” applications, for which the whole overhead of setting up a cluster of mongrels and a proxy doesn’t seem to be adequate. I’ll give you a short summary on how to install mod_rails for apache2 on Debian Etch.
First, install the passenger gem using RubyGems (if you don’t have Ruby and RubyGems running on your server, install them first – of course):
1 gem install passenger
Afterwards, run the passenger apache2 module installer using this command:
1 passenger-install-apache2-module
It’ll check for the required software to install the module, compile it and copy it to the correct folders. If some software is missing install it using aptitude (ie. aptitude install g++ if you’re missing the GNU C++ Compiler).
Next, create two new files in the /etc/apache2/mods-available directory. One called mod_rails.load:
1 LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.0.2/ext/apache2/mod_passenger.so
... and the other one called mod_rails.conf:
1 PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.0.2 2 PassengerRuby /usr/bin/ruby1.8
Now you can enable the module using a2enmod and restart apache.
1 a2enmod mod_rails 2 apache2ctl restart
That’s it! Now simply deploy your rails application, just make sure apache’s document root is pointing to your applications public folder. Passenger will automatically detect your rails application and start up processes as needed. You can check it’s status and stats using the passenger-status and passenger-memory-stats commands. For more details on mod_rails, take a look at it’s documentation.
My day-to-day resources on Ruby and Rails
August 4th, 2008 — Posted by benedikt — Filed in Articles, Ruby, Ruby on Rails
News
I try to keep up with Ruby and Ruby on Rails, even if I’m not working with one of them at the moment. These are the three feeds helping me to get the latest news:
PlanetRubyOnRails.com, not to be mixed up with PlanetRubyOnRails.org, is a simple feed aggregator with a set of quite informative blogs. Including the official Riding Rails Blog, Ruby Inside, and InfoQ. Unfortunately it doesn’t provide an RSS-Feed anymore, but thanks to Feed43 it’s easy to build one on your own.
Every Wednesday Gregg Pollack and Jason Seifer of Rails Envy publish their Rails Envy Podcast, covering last week’s most important topics in the Ruby and Rails community. They’re giving a short summary for every topic, together with a link in their shownotes and usually are fooling around. The podcast’s length is usually between 10 to 15 minutes.
Just like PlanetRubyOnRails, the planet of the german ruby on rails usergroup is a feed aggregator, except it includes blogs of members of the german Ruby on Rails community. (Yes, mine too …) Its far from being as active as the international one, but usually includes interesting posts.
Documentation
When I’m working on Ruby and Ruby on Rails code, I use there resources to quickly look up documentation:
Ruby-Doc.org provides the documentation for both Ruby’s Core and Stdlib. The documentation is in the default RDoc format, so I usually end up hitting [Strg]+[F] and using my browsers search function to quickly get to the relevant sections.
What Ruby-Doc.org is for ruby, api.rubyonrails.com is for rails. It’s the standard rails documentation in the default RDoc format. As with Ruby-Doc.org I use my browsers search to quickly find what I’m looking for.
Rails-Doc.org is a quite new site providing the full rails documentation. Unlike the default API documentation site (see above) it also includes documentation of older rails versions. Additionally it has a nice search engine, and adds the ability to post notes. There are other sites providing similar functionality for the rails documentation, but somehow Rails-Doc.org just feels right and I’m using more and more.
Gem Server
Did you know the fabulous RubyGem-Tools provide a server including the documentation for all your installed gems? Simple run gem server on the console, fire up your browser and navigate to http://localhost:8808. Okay, it’s just the standard RDoc documentation for each gem, without any fancy search or anything … but who cares if you’re somewhere in the middle of nowhere with no internet connection? :-)
Other
Last but definitely not least, are the RailsCasts by Ryan Bates. Every Monday he publishes a approx. 5 to 10 minute screencast on a variety of topics related to rails development. If you haven’t seen one of them yet, don’t hesitate any longer. Ryan’s explanations are concise and based on practical examples.
What are your resources on Ruby / Rails? Which blogs are you reading to stay up-to-date? Which documentation are you using? I’m interested in your comments (there are way to few anyways … ;-))!
Update (Aug 15.)
Nodeta, creators of Rails-Doc.org, released APIdock yesterday. APIdock extends the Rails-Doc.org concept to multiple projects. Currently Rails, Ruby and RSpec are included.
Using RSpactor with Linux
April 10th, 2008 — Posted by benedikt — Filed in Agile Development, Articles, Linux, Ruby, Ruby on Rails
Andreas Wolff recently released RSpactor, a (up to now) command line tool similar to autotest. Nevertheless it differs from autotest in two points. First it’s focused on RSpec and secondly it’s using Mac OS’ FSEvents to monitor file changes. According to this it only runs on Mac OS. To get it running on Linux you’ll have to change RSpactor’s Listener class to use Linux’ equivalent to FSEvents called inotify. Luckily there’s a gem called RInotify which introduces a simple class to access the inotify events within ruby. I rewrote the Listeners class yesterday to get it running on my Linux notebook:
1 # inotify_listener.rb 2 3 class Listener 4 5 def initialize(&block) 6 require 'rinotify' 7 begin 8 @spec_run_time = Time.now 9 @watching = {} 10 11 notify = RInotify.new 12 Dir.glob(File.join(Dir.pwd, '**')).each do |dir| 13 watch_desc = notify.add_watch(dir, RInotify::MODIFY | RInotify::CREATE | RInotify::DELETE) 14 @watching[watch_desc] = dir 15 end 16 17 while true do 18 changed_files = [] 19 notify.each_event do |event| 20 changed_files << build_path_from_event(event) 21 end 22 changed_files.uniq! 23 unless changed_files.empty? 24 @spec_run_time = Time.now 25 yield changed_files 26 end 27 sleep(5) 28 end 29 rescue Interrupt 30 @watching.each_key { |key| notify.rm_watch(key) } 31 end 32 end 33 34 def build_path_from_event(event) 35 File.join(@watching[event.watch_descriptor], event.name || '') 36 end 37 38 end
To get it running you simply have to install the RInotify gem and change one line in bin/rspactor:
1 # from 2 require File.join(File.dirname(__FILE__), '..', 'lib', 'listener') 3 # to 4 require File.join(File.dirname(__FILE__), '..', 'lib', 'inotify_listener')
That’s it! RSpactor should be running on Linux now and consuming much less CPU than autotest.
(You might also want to change the system()-call in lib/resulting.rb as it’s currently using growl to notify you about the test results.)
Getting a class' subclasses
July 10th, 2007 — Posted by benedikt — Filed in Articles, Ruby
I needed a way to get a list of the subclasses that inherit a specific. Unfortunately there is no method like Class.subclasses (there is Class.superclass, though) so I had to look for another way to achieve this. Let’s say, we want to have an array containing all subclasses as a class variable of our superclass Strategy. In order to fill the array we’ll overwrite the inherited class method of Class. (Already confused by all the classes? ;-))
1 class Strategy 2 @@subclasses = Array.new 3 class << self 4 def inherited(klass) 5 @@subclasses << klass 6 end 7 8 def subclasses 9 @@subclasses.join(', ') 10 end 11 end 12 end
Now, every time a class extends Strategy our new inherited method is called and adds the class to our array.
1 class StrategyA < Strategy; end 2 class StrategyB < Strategy; end 3 class StrategyC < Strategy; end 4 5 # Let's get the current list of subclasses 6 puts Strategy.subclasses # will output StrategyA, StrategyB, StrategyC
What happens if a class inherits any of our subclasses? Well, as long as you don’t overwrite the inherited method again it’s also added to the array.





