PrettyConditions
1 2 class ActiveRecord::Base 3 def self.pretty_conditions(hash) 4 meta_def hash[:name] do 5 array = [] 6 puts "#{hash[:message]}:" 7 puts "------------------" 8 self.find(:all, :conditions => hash[:conditions]).each do |record| 9 module_eval %[puts record.#{hash[:column_name]}] 10 array << record 11 end 12 puts "------------------" 13 puts "Total: #{array.size}" 14 puts "------------------" 15 end 16 end 17 end
1 2 class Youth < ActiveRecord::Base 3 pretty_conditions :name => 'unsponsored', :column_name => 'name', :message => "Unsponsored Youth", :conditions => "sponsor LIKE '%XXX%'" 4 pretty_conditions :name => 'touchgroup_leaders', :column_name => 'name', :message => "Touchgroup Leaders", :conditions => ["touchgroup =?",'t'] 5 pretty_conditions :name => 'vegitarians', :column_name => 'name', :message => "Vegitarians", :conditions => ["vegitarian =?",'t'] 6 end 7 8 Youth.unsponsored 9 Youth.touchgroup_leaders 10 Youth.vegitarians
Output:
1 2 saurasaurusrex:~/software cdcarter$ ruby actions.rb 3 Unsponsored Youth: 4 ------------------ 5 Hannah Gilbert 6 Jesse Lavercombe 7 Zach Adams 8 Danika Zabertini 9 ------------------ 10 Total: 4 11 ------------------ 12 13 Touchgroup Leaders: 14 ------------------ 15 Leland McKeeman 16 Marlee Leebrick-Stryker 17 Meggie Huges-Morrison 18 ------------------ 19 Total: 3 20 ------------------ 21 22 Vegitarians: 23 ------------------ 24 Marlee Leebrick-Stryker 25 Meggie Huges-Morrison 26 Amelia Nybakke 27 Ethan Edl 28 Maggie Heath 29 ------------------ 30 Total: 5 31 ------------------