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: Ruby
-
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.
-
Surrounding text with a tag using Ruby’s gsub
Here’s how to identify a keyword in a block of text using a case-insensitive regular expression, and surround that keyword with an HTML tag:
text = "hello world, this is a test" text.gsub!(/(WORLD)/i, "\1")
The following string is produced:
hello world, this is a test
Note that
\1references the group in the regex (surrounded by parens). This results in the original value being placed within the HTML tags, instead of the uppercased WORLD in the regex. -
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.
-
Picking values from Ruby hashes
Want to pick a certain set of key/value pairs from a Ruby hash? You might do this:
hash = {:foo => "bar", :bar => "baz", :baz => "boo"} hash.select { |k,v| [:foo, :bar].include?(k) } # returns [[:foo, "bar"], [:bar, "baz"]]Kind of messy. We can do better by reopening the Hash class this way:
class Hash def pick(*values) select { |k,v| values.include?(k) } end endNow our selection works like this:
hash = {:foo => "bar", :bar => "baz", :baz => "boo"} hash.pick(:foo, :bar) # returns [[:foo, "bar"], [:bar, "baz"]]Ruby is a wonderful language. This is one small example of how having access to existing classes can be incredibly powerful. With this power comes great responsibility. Wield your power wisely.