DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

PrettyConditions

10.04.2006
| 3631 views |
  • submit to reddit
        // PrettyConditions allows you to easily print a report based on a condition for an ActiveRecord subclass

class ActiveRecord::Base
  def self.pretty_conditions(hash)
    meta_def hash[:name] do
      array = []
      puts "#{hash[:message]}:"
      puts "------------------"
      self.find(:all, :conditions => hash[:conditions]).each do |record|
        module_eval %[puts record.#{hash[:column_name]}]
        array << record
      end
      puts "------------------"
      puts "Total: #{array.size}"
      puts "------------------"
    end
  end
end

class Youth < ActiveRecord::Base
  pretty_conditions :name => 'unsponsored', :column_name => 'name', :message => "Unsponsored Youth", :conditions => "sponsor LIKE '%XXX%'"
  pretty_conditions :name => 'touchgroup_leaders', :column_name => 'name', :message => "Touchgroup Leaders", :conditions => ["touchgroup =?",'t']
  pretty_conditions :name => 'vegitarians', :column_name => 'name', :message => "Vegitarians", :conditions => ["vegitarian =?",'t'] 
end

Youth.unsponsored
Youth.touchgroup_leaders
Youth.vegitarians

Output:
saurasaurusrex:~/software cdcarter$ ruby actions.rb
Unsponsored Youth:
------------------
Hannah Gilbert
Jesse Lavercombe
Zach Adams
Danika Zabertini
------------------
Total: 4
------------------

Touchgroup Leaders:
------------------
Leland McKeeman
Marlee Leebrick-Stryker
Meggie Huges-Morrison
------------------
Total: 3
------------------

Vegitarians:
------------------
Marlee Leebrick-Stryker
Meggie Huges-Morrison
Amelia Nybakke
Ethan Edl
Maggie Heath
------------------
Total: 5
------------------
    

Comments

Snippets Manager replied on Tue, 2006/10/03 - 4:48pm

I thought that looked like your code... and then it was!