Can Sphinx and foxy fixtures place nicely together? Due to the way Sphinx indexing works, foxy fixtures will often slow down the indexing process drastically. This article explains how to overcome this limitation.
Category: Rails
-
Printing an array in multiple table columns
<% @categories.in_groups_of(2).each do |group| %> <tr> <% group.each do |category| %> <td><%=h category.name %></td> <% end %> </tr> <% end %>What are you staring at? Move along, move along.
-
Smart asset management for Rails plugins
Many Rails plugins require that certain files like images, CSS, and JavaScript get copied to the public folder during installation. The typical way to do this is add code to install.rb, which then gets executed when you first install the plugin in your Rails project. What happens when the assets in a plugin you’re using change, though? You end up having to manually copy files around anytime the plugin gets updated, which is an extremely error-prone process.
I extracted asset_copier from an existing Terralien project. It fixes this problem beautifully. Once installed, it keeps the assets in your Rails project synchronized with your plugin. It even deletes assets from the Rails project that have been removed in the plugin.
To learn more, check out this post. If instant gratification is more your thing, install the gem directly and apply it to your target plugin:
sudo gem install pelargir-asset_copier --source=http://gems.github.com cd ~/some_rails_project script/generate asset_copier some_plugin
-
Using routes inside Radius tags
I’m in the middle of developing a Radiant extension for Terralien. Radius is the tagging language for Radiant, and today I figured out how to use a route from inside a custom tag class.
A typical Radius tag class might look like this:
module AwesomeTags include Radiant::Taggable tag "session:logout" do |tag| "Logout" end endYou can see that the hard-coded URL isn’t exactly DRY. I’ve already defined a route for it. Why should I have to hard code it here? The solution:
module AwesomeTags include Radiant::Taggable include ActionController::UrlWriter default_url_options[:host] = REGISTRY[:host] tag "session:logout" do |tag| "Logout" end endNote that
UrlWriterneeds to know the host name to base its URLs off of. The host name gets set using the registry pattern. It will be different depending on whether the app is running in development or production mode.The same method can be used to reference routes from inside ActiveRecord models.
-
Textile toolbar plugin for Rails
Have you ever wanted a WYSIWYG-style toolbar that generates Textile markup? textile_toolbar is a Rails plugin that does just that. Extracted from a recent Terralien project, it’s a great way get your users used to the power and simplicity of Textile without frightening them away with long markup guides. Check out the announcement on the Terralien blog for more information.
To install from the command line:
script/plugin install git://github.com/pelargir/textile_toolbar.git
Update: A live example of a Textile-enabled text area can now be found here.
-
rspec_validation_expectations gem released
I just released a new gem on GitHub that provides some common validation expectations to rspec. Instead of writing specs to verify that your models are handling validation correctly, these expectations simply check that the validation is getting declared correctly in your model. For example:
describe User do it_should_validate_presence_of :first_name, :last_name, :email it_should_validate_numericality_of :zip it_should_validate_uniqueness_of :email end
Since the expectations never hit the database, they are also faster than testing the traditional way. It’s dead simple to install on Rails 2.1 or later:
script/plugin install git://github.com/pelargir/rspec_validation_expectations.git
The expectations become available to your specs immediately.
-
RubyCamp is coming to Raleigh
Raleigh’s first RubyCamp is coming to Red Hat on October 18th. This is a similar format to BarCamp in that the presentations are pitched the morning of the conference, and attendees self organize the remainder of the day. Relevance will be running their popular Refactotum workshop in the morning. The conference is free, but attendance is capped at 200 so visit the wiki to grab your spot.
-
finder_filter gem released
I’m at the Ruby Hoedown in Huntsville this weekend. Being around so many brilliant geeks encouraged me to release a gem I’ve had sitting in the hopper for several weeks.
finder_filter encapsulates a pattern I find myself using quite frequently in Rails. Namely, looking up an instance variable before an action. For example:
class UsersController < ActionController::Base before_filter :find_user, :only => [:show, :edit] def show # do something with @user end def edit # do something with @user end def find_user @user = User.find(params[:id) end endSticking the finder in a before filter keeps the code DRY, but it still takes several lines to do this. finder_filter reduces this to a single line of code:
class UsersController < ActionController::Base finder_filter :only => [:show, :edit] def show; end def edit; end end
There are other options to customize the column and param used in the lookup. Check out the README for full details.
To install the gem as a plugin in your Rails project:
sudo gem install pelargir-finder_filter --source=http://gems.github.com
Then open
environment.rbin your Rails app and add the gem as a dependency in your initializer block:Rails::Initializer.run do |config| config.gem "pelargir-finder_filter" ... end
If you have any comments or suggestions, I’d love to hear from you. Contact me through the finder_filter project on GitHub.
-
Sending mail from Rails through Google SMTP
I just ran into a problem configuring a Rails app to deliver email through Google’s SMTP servers. ActionMailer doesn’t support TLS/SSL which is required by Google. Fortunately, the action_mailer_optional_tls plugin provides this functionality.
I wanted to host my SMTP settings in an external YAML file so I wouldn’t end up checking my username and password into the repository. (The YAML file is placed on the production server and a softlink is created during each deploy.) For some reason, I kept getting “connection refused” messages whenever I tried to send email with this configuration:
# smtp.yml production: address: smtp.gmail.com port: 587 tls: true domain: foo.com authentication: :plain user_name: someone@foo.com password: secret # production.rb smtp = YAML::load(File.open("#{RAILS_ROOT}/config/smtp.yml")) ActionMailer::Base.smtp_settings = smtp[Rails.env]The problem was the hash returned by YAML. The keys were strings, whereas ActionMailer was expecting the keys to be symbols. The fix was to make the hash use indifferent access:
... ActionMailer::Base.smtp_settings = smtp[Rails.env].with_indifferent_access
That cleared up the “connection refused” problem. Now my app is sending email like a champ.
-
Tweaked railspdf to support 2.1.0
railspdf is a plugin that adds templating support for the pdf-writer gem to Rails. The project was last updated in January of 2007, but worked fine up until the 2.1.0 release of Rails which broke several things.
I’ve forked the project on GitHub and tweaked it to run under 2.1.0. I also added support for custom helpers. Your PDF templates can now call methods in helpers with the same name as your controller (which is the expected behavior anyway). I’ve also started maintaining a CHANGELOG.