-
Notifications
You must be signed in to change notification settings - Fork 0
Joke plugin for Rails. Adds ActiveRecord::Base.there_can_be_only_one method.
License
geeksam/highland_ar
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
HighlandAR
==========
In episode #150 of the Ruby Show podcast[1], 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[2], 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[3]
if you actually use this in a project, because I think that would be hilarious.
Enjoy,
-Sam
--
[1] http://rubyshow.com/episodes/150
[2] alias :there_can_be_only_one :has_one
[3] eval('%w[geeksam gmail].join("@") + ".com"')
--
Copyright (c) 2011 Sam Livingston-Gray, released under the MIT license
About
Joke plugin for Rails. Adds ActiveRecord::Base.there_can_be_only_one method.
Resources
License
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published