Join GitHub today
Joke plugin for Rails. Adds ActiveRecord::Base.there_can_be_only_one method.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
|Failed to load latest commit information.|
HighlandAR ========== In episode #150 of the Ruby Show podcast, Jason jokingly suggested that someone should write a plugin that adds a .there_can_be_only_one method to ActiveRecord::Base. I thought that would be funny. So I wrote it. Rather than make this a one liner, I decided to implement a random(ish) selection process. This way, objects with a .there_can_be_only_one association can accept multiple candidates, who will compete for the one available slot until only one remains. Here's a short example: class Fight < ActiveRecord::Base there_can_be_only_one :winner end class Man < ActiveRecord::Base end class String def Man ::Man.new(:name => titlecase + ' Man') end end particle_man = "Particle".Man # Doin' the things a particle can triangle_man = "Triangle".Man # Triangle man hates Particle Man person_man = "Person".Man # Hit on the head with a frying pan Fight.new.tap do |fight| fight.winner = [triangle_man, particle_man] fight.winner # => triangle_man end Fight.new.tap do |fight| fight.winner = [triangle_man, person_man] fight.winner # => triangle_man end See the tests or the EXAMPLE file for more details. Oh, and please email me if you actually use this in a project, because I think that would be hilarious. Enjoy, -Sam --  http://rubyshow.com/episodes/150  alias :there_can_be_only_one :has_one  eval('%w[geeksam gmail].join("@") + ".com"') -- Copyright (c) 2011 Sam Livingston-Gray, released under the MIT license