Sliced Software

Sliced Software strives to produce high quality, easy to use software.

Software

PGnJ An intuitive Database Development Environment for Mac OS X.

Seymore Content management, made easy.

Argus Issue tracking for the rest of us.

@twitter

I Love Camino!

A better Array#rand

I released a new plugin today, called rand. This plugin expands on the #rand method ActiveSupport adds to Array. As background, the rand method that Rails gives you, returns a single random value back from the array it’s called on.
>> [0, 1, 2, 3, 4].rand
=> 2
This rand plugin overrides the rand method so that you can pass in an integer value that corresponds to how many random values from the array you actually want back.
>> [0, 1, 2, 3, 4].rand(2)
=> [4, 0]

>> [0, 1, 2, 3, 4].rand(4)
=> [3, 0, 2, 1]
Additionally, the existing functionality is still in place so that if you don’t pass in a parameter, it still gives you back a single random value.
Comments (View)

A better Rails.cache.increment

I started working with Rails.cache.increment and Rails.cache.decrement today but quickly found that it didn’t work as I planned. For example, it seemed that when calling these methods they would return the previous value rather than the newly incremented value. Additionally, if you decided to read directly using Rails.cache.read and the same key, it would always return nil despite being able to call Rails.cache.increment again. Another annoying thing is that if you tried to increment a value that wasn’t in memcached, instead of defaulting to 1, it returned nil. Just all sorts of wacky stuff going on. I decided to just write my own.

Update: click through for the source.

# app/models/util/cache.rb
 
class Util::Cache
  def self.increment(key, amount = 1)
    if (value = Rails.cache.read(key)).nil?
      Rails.cache.write(key, (value = amount))
    else
      Rails.cache.write(key, (value = value + amount))
    end
    
    return value
  end
  
  def self.decrement(key, amount = 1)
    if (value = Rails.cache.read(key)).nil?
      value = 0
    else
      Rails.cache.write(key, (value = value - amount))
    end
    
    return value
  end
end
view raw This Gist brought to you by GitHub.
Comments (View)

An update about the LIRR app

My custom, iPhone optimized webapp for interfacing with the LIRR scheduling system has been offline for a few weeks following a complete overhaul of the MTA’s LIRR scheduling software.

Although their new scheduling system looks similar to the old site, it seems to be a complete rewrite. Unfortunately, this totally broke my app. I worked hard rewriting my parsing system to interface with the new site and I actually finished about half of it before I ran into a show stopping problem. I’d rather not say what the exact problem I ran into is, but I will say that I am still attempting to work around the issue.

For now (long overdue, I know), I replaced my app with a message concerning the breaking of the app as well as a link out to the official MTA site.

Comments (View)
My LIRR mobile app is busted because the mta.info site changed. I’ll try and fix it soon.
Comments (View)

Generating RSS Feeds in Rails

It’s very easy to generate RSS feeds in Rails. In fact, if there is any possible benefit to having RSS feeds in your rails application, there is absolutely no reason you shouldn’t take a few minutes and set them up.

These days, you just have to create a file like :action.rss.builder to go along with your :action.html.erb file. You don’t even need a respond_to block in your action. As long as you have a route setup that knows how to handle additional formats, rails will automatically render your .rss.builder file rather than your .html.erb file if an .rss extension is at the end of the url.

For the basics of generating RSS feeds in rails, check out this screencast.

If you’d like to include media in your RSS feeds you should use the Yahoo media namespace xml definitions. To include this namespace in your feed you can use:

xml.rss(:version => "2.0", 
        "xmlns:media" => 'http://search.yahoo.com/mrss/')

You can then generate a namespaced XML element like this:

xml.media(:content, :url => "some-image-url", 
            :type => "image/jpeg")
Comments (View)

I Searched and Searched for searchd

For a while now I’ve been working on a really long and detailed post comparing the different search systems available for rails projects. I just deleted the draft.

I deleted it because I realized there really isn’t anything to compare. If you don’t need live updated search results and you can live with an index that’s updated every 15 minutes or more, just use Sphinx. Sphinx with Thinking Sphinx by Pat Allan is simply the most elegant, full featured and most importantly, stable search system available for rails today. We’ve been using it over at gawkk for months now and haven’t had a single unhappy moment since we switched from Solr.

SQL searches are slow. Ferret is crap in production. Solr is almost as unstable as Ferret in production and will bring down your database with unnecessary connections every time a damn object is instantiated. Sphinx is rock solid and fast as hell.

I will only offer two lines of proof:

total 1133325 docs, 266596694 bytes
total 117.702 sec, 2265011.00 bytes/sec, 9628.75 docs/sec

Over a million documents in 117 seconds. Try and do that with anything other than Sphinx. I dare you.

Comments (View)

Back in Business

I was finally able to get my expired SSL certificate situation resolved. The store is back online. Sorry for any inconvenience!
Comments (View)

A Simple .union Extension For ActiveRecord

I just wrote and released my first rails plugin, called union. It’s absurdly simple and fairly naive. I wrote this plugin so that I didn’t have to look at an ugly UNION I was running in a find_by_sql.

ActiveRecord::Base.union(parts, options = {})

The first parameter, parts, is an array of hashes. Each hash is what you would normally send into a single find and represents each SELECT. All parts will be unioned together.

The second paramter, options, is a hash of remaining options to be applied to the UNION of the parts (ie: order, limit, offset).

A simple (and useless) example would be:
User.union([{:conditions => ['name = ?', 'tom']}, {:conditions => ['name = ?', 'gary']}], {:order => 'created_at'})

This example produces the following SQL:
(SELECT * FROM `users` WHERE (name = 'tom')) UNION (SELECT * FROM `users` WHERE (name = 'gary')) ORDER BY created_at;

Essentially you can do any union, but it’s up to you to make sure you don’t pass the wrong stuff in because it’s a pretty dumb implementation.

Comments (View)

Rake That Data into A Pile of YAML

My friend, @milaniliev, pointed me to his very useful db:fixtures:dump rake task that allows for selectively dumping data from your database directly into YAML fixtures.

Update: It looks like this was also tackled in a different way by topfunky. He seems to have added a .to_fixture method to ActiveRecord::Base. I don’t think it accepts a WHERE clause, but that is probably easily patched.

Comments (View)

Nested Object Forms in Rails 2.3

I’ve just found this great overview of the Nested Object Forms feature that’s new in Rails 2.3. When I first read about nested forms, I didn’t really see the huge benefit, but while working on a new side project, I realized how useful they are. If you don’t already know how this feature works, take a few minutes and read through the blog post above to get an idea.
Comments (View)
Looking for older posts? Until they are here at tumblr, see here!