Acts As Filterable
SVN / Docs
This will implement the acts_as_filterable mixin for an ActiveRecord class. It'll...uhh...you know. Filter stuff. Let's see an example:
class Badger < ActiveRecord::Base
has_many :beers
end
frank = Badger.create(:name => 'Frank')
james = Badger.create(:name => 'James')
drinky = Badger.create(:name => 'Drinky')
Copy/PasteThis is great. We have a couple badgers, everything is going pretty smooth. Frank, James, Drinky have fun adventures damn-near constantly. Though, as we could have guessed, Drinky has a bit of a drinking problem. We'd really rather just not even know he exists if he's had too much. Really, we'd rather not know about any badgers who have been drinking.
class Badger < ActiveRecord::Base
has_many :beers
acts_as_filterable
def too_much_to_drink?
beers.size > 3
end
invisible_if :too_much_to_drink?
end
Copy/PasteCool. So, at the moment, we're kicking:
Badger.find(:all).each do |badger|
puts "#{badger.name} drank #{badger.beers.size} Beers!"
end
> Frank drank 0 Beers!
> James drank 0 Beers!
> Drinky drank 0 Beers!
Copy/PasteNow! On to the drinking!
frank.beers << [Beer.create, Beer.create, Beer.create]
james.beers << [Beer.create, Beer.create]
drinky.beers << [Beer.create, Beer.create, Beer.create, Beer.create]
Christallmighty. Looks like Drinky's had too much to drink again. Let's run the roll call, to see if anyone's going to tell us semi-truthful stories with copious volume and slurring.
Badger.find(:all).each do |badger|
puts "#{badger.name} drank #{badger.beers.size} Beers!"
end
> Frank drank 3 Beers!
> James drank 2 Beers!
Copy/PasteEffing sweet. Drinky's just gone. His antics got a little old. But let's say Frank wants to go find Drinky...he has a awesome story to tell him.
Badger.find_by_name('Drinky')
> nil
Badger.find(3) # Drinky's id
> ActiveRecord::RecordNotFound exception and so-forth!
Copy/PasteNo. Drinky's gone until he sobers up. End of story.
