Over a million developers have joined DZone.

PrettyConditions

·
// 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
------------------
Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}